Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
Hodor Web Backend
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Container registry
Model registry
Operate
Environments
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Hodor Security Project
Hodor Web Backend
Merge requests
!4
Add challenges module controllers
Code
Review changes
Check out branch
Download
Patches
Plain diff
Open
Add challenges module controllers
Vasanth/hodor-webapp:add-challenge
into
develop
Overview
7
Commits
2
Pipelines
0
Changes
2
All threads resolved!
Hide all comments
Open
Vasanth Viswanath S
requested to merge
Vasanth/hodor-webapp:add-challenge
into
develop
7 years ago
Overview
7
Commits
2
Pipelines
0
Changes
2
All threads resolved!
Hide all comments
Expand
0
0
Merge request reports
Compare
develop
version 1
8754f570
7 years ago
develop (base)
and
latest version
latest version
8e08619d
2 commits,
7 years ago
version 1
8754f570
1 commit,
7 years ago
2 files
+
142
−
2
Inline
Compare changes
Side-by-side
Inline
Show whitespace changes
Show one file at a time
Files
2
Search (e.g. *.vue) (Ctrl+P)
hodor/controllers/challenges.py
0 → 100644
+
135
−
0
Options
from
hodor
import
app
from
flask
import
request
,
jsonify
,
abort
,
make_response
from
hodor.models.challenges
import
Challenges
from
sqlalchemy.exc
import
IntegrityError
###########################
# Add a new challenge to the platform #
###########################
@app.route
(
'
/challenges/new
'
,
methods
=
[
'
POST
'
])
def
add_new_chall
():
"""
This function is used to add new challenges to the platform
:return: Response Code
"""
newchall
=
dict
()
if
request
.
method
==
"
POST
"
:
try
:
newchall
[
'
cid
'
]
=
request
.
data
.
get
(
'
cid
'
).
strip
()
newchall
[
'
name
'
]
=
str
(
request
.
data
.
get
(
'
name
'
).
strip
())
newchall
[
'
points
'
]
=
request
.
data
.
get
(
'
points
'
).
strip
()
newchall
[
'
description
'
]
=
str
(
request
.
data
.
get
(
'
description
'
).
strip
())
newchall
[
'
hints
'
]
=
str
(
request
.
data
.
get
(
'
hints
'
).
strip
())
except
Exception
as
e
:
print
(
e
)
abort
(
500
)
chall
=
Challenges
(
**
newchall
)
chall
.
save
()
return
make_response
(
jsonify
(
status
=
201
,
msg
=
"
Challenge {} successfully added
"
.
format
(
chall
.
cid
)
+
"
to database
"
),
201
)
###########################
# Edit an existing challenge
###########################
@app.route
(
'
/challenges/edit/<id_slug>
'
,
methods
=
[
'
PUT
'
])
def
edit_chall
(
id_slug
):
"""
This function enables to edit existing challenges
:return: Response Code
"""
check_id
=
id_slug
.
strip
()
get_id
=
Challenges
.
query
.
filter_by
(
cid
=
check_id
)
if
(
get_id
):
try
:
get_id
.
cid
=
check_id
get_id
.
name
=
str
(
request
.
data
.
get
(
'
name
'
).
strip
())
get_id
.
points
=
str
(
request
.
data
.
get
(
'
points
'
).
strip
())
get_id
.
description
=
str
(
request
.
data
.
get
(
'
description
'
).
strip
())
get_id
.
hints
=
str
(
request
.
data
.
get
(
'
hints
'
).
strip
())
except
Exception
as
e
:
print
(
e
)
abort
(
500
)
get_id
.
commit
()
edited_data
=
_extract_required_fields
(
get_id
)
return
make_response
(
jsonify
(
status
=
200
,
data
=
edited_data
),
200
)
else
:
return
make_response
(
jsonify
(
status
=
404
,
msg
=
"
No such challenge ID found
"
),
404
)
###########################
# Deleting an existing challenge
###########################
@app.route
(
'
/challenges/<id_slug>
'
,
methods
=
[
'
DELETE
'
])
def
del_chall
(
id_slug
):
"""
This function deletes existing challenge
:return: Response Code
"""
check_id
=
id_slug
.
strip
()
get_id
=
Challenges
.
query
.
filter_by
(
cid
=
check_id
)
if
get_id
:
Challenges
.
query
.
filter_by
(
cid
=
check_id
).
delete
()
return
make_response
(
jsonify
(
status
=
200
,
msg
=
"
Record succesfully deleted
"
),
200
)
else
:
return
make_response
(
jsonify
(
status
=
404
,
msg
=
"
No such challenge ID found
"
),
404
)
###########################
# Extracting required fields
###########################
def
_extract_required_fields
(
chall
):
filter_chall
=
dict
()
'''
This can be done by directly dumping the dictionary but there
'
s always
a risk of data leak.So, we pick what we need to give out out of the API
'''
filter_chall
[
'
name
'
]
=
chall
.
name
filter_chall
[
'
description
'
]
=
chall
.
description
filter_chall
[
'
points
'
]
=
chall
.
points
filter_chall
[
'
hints
'
]
=
chall
.
hints
filter_chall
[
'
cid
'
]
=
chall
.
cid
return
filter_chall
###########################
#Get all challenge IDs
###########################
@app.route
(
'
/challenges
'
,
methods
=
[
'
GET
'
,
'
POST
'
])
def
get_all_challids
():
"""
Returns all challenge IDs and names
:return: Response Code
"""
response
=
dict
()
response
[
'
status
'
]
=
200
response
[
'
data
'
]
=
[]
for
challenge
in
Challenges
.
get_all
():
'''
All records for each challenge is retrieved and name, id are only returned
'''
response
[
'
data
'
].
append
(
_extract_required_fields
(
challenge
))
return
response
################################################
# Handle Integrity Exceptions in API #
################################################
@app.errorhandler
(
IntegrityError
)
def
handle_sql_assertion_error
(
err
):
try
:
'''
err.orig.args is from the DBAPIError class of SQLAlchemy. It usually
contains the original error message.
The below is an attempt to clean up the message and only return the
relevant part to API
'''
try
:
errmsg
=
err
.
orig
.
args
[
0
].
split
(
'
\n
'
)[
1
][
9
:]
except
IndexError
:
errmsg
=
err
.
orig
.
args
[
0
].
split
(
'
\n
'
)
except
IndexError
:
errmsg
=
err
.
orig
.
args
[
0
]
return
make_response
(
jsonify
(
status
=
400
,
msg
=
errmsg
),
400
)
Loading