From 8e08619da9c5f21f07faeeed7d18f9e12edc3ba5 Mon Sep 17 00:00:00 2001 From: Vasanth Viswanath S <cb.en.u4cse14254@cb.students.amrita.edu> Date: Thu, 2 Nov 2017 15:54:40 +0530 Subject: [PATCH] Add challenges module controllers --- hodor/controllers/challenges.py | 23 ++++++++++++----------- hodor/models/challenges.py | 2 +- 2 files changed, 13 insertions(+), 12 deletions(-) diff --git a/hodor/controllers/challenges.py b/hodor/controllers/challenges.py index eb109c3..b51ed65 100644 --- a/hodor/controllers/challenges.py +++ b/hodor/controllers/challenges.py @@ -16,7 +16,7 @@ def add_new_chall(): if request.method == "POST": 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['points'] = request.data.get('points').strip() newchall['description'] = str(request.data.get('description').strip()) @@ -26,23 +26,23 @@ def add_new_chall(): abort(500) chall = Challenges(**newchall) 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) ########################### # Edit an existing challenge ########################### -@app.route('/challenges/edit', methods=['PUT']) -def edit_chall(): +@app.route('/challenges/edit/<id_slug>', methods=['PUT']) +def edit_chall(id_slug): """ 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={} + 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()) @@ -51,7 +51,8 @@ def edit_chall(): print(e) abort(500) 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: return make_response(jsonify(status=404, msg="No such challenge ID found"), 404) @@ -65,9 +66,9 @@ def del_chall(id_slug): :return: Response Code """ 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: - 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) else: return make_response(jsonify(status=404, msg="No such challenge ID found"), 404) @@ -85,7 +86,7 @@ def _extract_required_fields(chall): filter_chall['description'] = chall.description filter_chall['points'] = chall.points filter_chall['hints'] = chall.hints - filter_chall['chall_id'] = chall.chall_id + filter_chall['cid'] = chall.cid return filter_chall diff --git a/hodor/models/challenges.py b/hodor/models/challenges.py index 5c1e639..3f0c78f 100644 --- a/hodor/models/challenges.py +++ b/hodor/models/challenges.py @@ -7,7 +7,7 @@ class Challenges(db.Model): __tablename__= 'challenges' # 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) points = db.Column(db.Integer, nullable=False) description = db.Column(db.String(2048), nullable=False) -- GitLab