Skip to content
Snippets Groups Projects
Commit 8e08619d authored by Vasanth Viswanath S's avatar Vasanth Viswanath S
Browse files

Add challenges module controllers

parent 8754f570
No related branches found
No related tags found
No related merge requests found
...@@ -16,7 +16,7 @@ def add_new_chall(): ...@@ -16,7 +16,7 @@ def add_new_chall():
if request.method == "POST": if request.method == "POST":
try: try:
newchall['chall_id'] = request.data.get('chall_id').strip() newchall['cid'] = request.data.get('cid').strip()
newchall['name'] = str(request.data.get('name').strip()) newchall['name'] = str(request.data.get('name').strip())
newchall['points'] = request.data.get('points').strip() newchall['points'] = request.data.get('points').strip()
newchall['description'] = str(request.data.get('description').strip()) newchall['description'] = str(request.data.get('description').strip())
...@@ -26,23 +26,23 @@ def add_new_chall(): ...@@ -26,23 +26,23 @@ def add_new_chall():
abort(500) abort(500)
chall = Challenges(**newchall) chall = Challenges(**newchall)
chall.save() chall.save()
return make_response(jsonify(status=201, msg="Challenge {} successfully added".format(chall.chall_id) + return make_response(jsonify(status=201, msg="Challenge {} successfully added ".format(chall.cid) +
"to database"), 201) "to database"), 201)
########################### ###########################
# Edit an existing challenge # Edit an existing challenge
########################### ###########################
@app.route('/challenges/edit', methods=['PUT']) @app.route('/challenges/edit/<id_slug>', methods=['PUT'])
def edit_chall(): def edit_chall(id_slug):
""" """
This function enables to edit existing challenges This function enables to edit existing challenges
:return: Response Code :return: Response Code
""" """
chall_id=request.data.get('chall_id').strip() check_id = id_slug.strip()
get_id = Challenges.query.get(chall_id) get_id = Challenges.query.filter_by(cid=check_id)
chall={}
if(get_id): if(get_id):
try: try:
get_id.cid = check_id
get_id.name = str(request.data.get('name').strip()) get_id.name = str(request.data.get('name').strip())
get_id.points = str(request.data.get('points').strip()) get_id.points = str(request.data.get('points').strip())
get_id.description = str(request.data.get('description').strip()) get_id.description = str(request.data.get('description').strip())
...@@ -51,7 +51,8 @@ def edit_chall(): ...@@ -51,7 +51,8 @@ def edit_chall():
print(e) print(e)
abort(500) abort(500)
get_id.commit() get_id.commit()
return make_response(jsonify(status=200, msg="Record succesfully edited"), 200) edited_data = _extract_required_fields(get_id)
return make_response(jsonify(status=200, data=edited_data), 200)
else: else:
return make_response(jsonify(status=404, msg="No such challenge ID found"), 404) return make_response(jsonify(status=404, msg="No such challenge ID found"), 404)
...@@ -65,9 +66,9 @@ def del_chall(id_slug): ...@@ -65,9 +66,9 @@ def del_chall(id_slug):
:return: Response Code :return: Response Code
""" """
check_id = id_slug.strip() check_id = id_slug.strip()
get_id = Challenges.query.filter_by(chall_id=check_id) get_id = Challenges.query.filter_by(cid=check_id)
if get_id: if get_id:
Challenges.query.filter_by(chall_id=id_slug).delete() Challenges.query.filter_by(cid=check_id).delete()
return make_response(jsonify(status=200, msg="Record succesfully deleted"), 200) return make_response(jsonify(status=200, msg="Record succesfully deleted"), 200)
else: else:
return make_response(jsonify(status=404, msg="No such challenge ID found"), 404) return make_response(jsonify(status=404, msg="No such challenge ID found"), 404)
...@@ -85,7 +86,7 @@ def _extract_required_fields(chall): ...@@ -85,7 +86,7 @@ def _extract_required_fields(chall):
filter_chall['description'] = chall.description filter_chall['description'] = chall.description
filter_chall['points'] = chall.points filter_chall['points'] = chall.points
filter_chall['hints'] = chall.hints filter_chall['hints'] = chall.hints
filter_chall['chall_id'] = chall.chall_id filter_chall['cid'] = chall.cid
return filter_chall return filter_chall
......
...@@ -7,7 +7,7 @@ class Challenges(db.Model): ...@@ -7,7 +7,7 @@ class Challenges(db.Model):
__tablename__= 'challenges' __tablename__= 'challenges'
# Data variables for each challenge # Data variables for each challenge
chall_id = db.Column(db.Integer, primary_key=True, unique=True, nullable=False) cid = db.Column(db.Integer, primary_key=True, unique=True, nullable=False)
name = db.Column(db.String(32), nullable=False) name = db.Column(db.String(32), nullable=False)
points = db.Column(db.Integer, nullable=False) points = db.Column(db.Integer, nullable=False)
description = db.Column(db.String(2048), nullable=False) description = db.Column(db.String(2048), nullable=False)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment