Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
Hodor Web Applicaton
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
GitLab community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Vasanth Viswanath S
Hodor Web Applicaton
Commits
8754f570
Commit
8754f570
authored
7 years ago
by
Vasanth Viswanath S
Browse files
Options
Downloads
Patches
Plain Diff
Add challenges module controllers
parent
da46cf11
Branches
Branches containing commit
No related tags found
No related merge requests found
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
hodor/controllers/challenges.py
+134
-0
134 additions, 0 deletions
hodor/controllers/challenges.py
hodor/models/challenges.py
+7
-2
7 additions, 2 deletions
hodor/models/challenges.py
with
141 additions
and
2 deletions
hodor/controllers/challenges.py
0 → 100644
+
134
−
0
View file @
8754f570
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
[
'
chall_id
'
]
=
request
.
data
.
get
(
'
chall_id
'
).
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
.
chall_id
)
+
"
to database
"
),
201
)
###########################
# Edit an existing challenge
###########################
@app.route
(
'
/challenges/edit
'
,
methods
=
[
'
PUT
'
])
def
edit_chall
():
"""
This function enables to edit existing challenges
:return: Response Code
"""
chall_id
=
request
.
data
.
get
(
'
chall_id
'
).
strip
()
get_id
=
Challenges
.
query
.
get
(
chall_id
)
chall
=
{}
if
(
get_id
):
try
:
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
()
return
make_response
(
jsonify
(
status
=
200
,
msg
=
"
Record succesfully edited
"
),
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
(
chall_id
=
check_id
)
if
get_id
:
Challenges
.
query
.
filter_by
(
chall_id
=
id_slug
).
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
[
'
chall_id
'
]
=
chall
.
chall_id
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
)
This diff is collapsed.
Click to expand it.
hodor/models/challenges.py
+
7
−
2
View file @
8754f570
...
...
@@ -7,13 +7,13 @@ class Challenges(db.Model):
__tablename__
=
'
challenges
'
# Data variables for each challenge
id
=
db
.
Column
(
db
.
String
(
32
)
,
primary_key
=
True
,
unique
=
True
,
nullable
=
False
)
chall_
id
=
db
.
Column
(
db
.
Integer
,
primary_key
=
True
,
unique
=
True
,
nullable
=
False
)
name
=
db
.
Column
(
db
.
String
(
32
),
nullable
=
False
)
points
=
db
.
Column
(
db
.
Integer
,
nullable
=
False
)
description
=
db
.
Column
(
db
.
String
(
2048
),
nullable
=
False
)
hints
=
db
.
Column
(
db
.
String
(
512
),
nullable
=
False
)
@staticmethod
def
save
(
self
):
db
.
session
.
add
(
self
)
db
.
session
.
commit
()
...
...
@@ -22,5 +22,10 @@ class Challenges(db.Model):
db
.
session
.
delete
(
self
)
db
.
session
.
commit
()
@staticmethod
def
get_all
():
return
Challenges
.
query
.
all
()
@staticmethod
def
commit
():
return
db
.
session
.
commit
()
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment