def updateUser(uid, json): valid_params = MyHandler.verify_parameters(json, ["user_id"]) if uid and valid_params: try: user_to_update = Users.getUserById(uid) if user_to_update: for key, value in valid_params.items(): if key == "password": if value != user_to_update.password and not \ bcrypt.checkpw(value.encode('utf-8'), user_to_update.password.encode('utf-8')): setattr( user_to_update, key, bcrypt.hashpw( value.encode('utf-8'), bcrypt.gensalt()).decode('utf-8')) else: setattr(user_to_update, key, value) user_to_update.update() result = { "message": "Success!", "user": user_to_update.to_dict(), } return jsonify(result), 200 else: return jsonify(message="Not Found!"), 404 except Exception as err: return jsonify(message="Server Error!", error=err.__str__()), 500 else: return jsonify(message="Bad Request!"), 400
def createQuestion(json): valid_params = MyHandler.verify_parameters(json, ['lesson_id', 'question']) if valid_params: try: new_quest = Questions(**valid_params) created_quest = new_quest.create() result = { "message": "Success!", "question": created_quest.to_dict(), } return jsonify(result), 201 except Exception as err: return jsonify(message="Server error!", error=err.__str__()), 500 else: return jsonify(message="Bad Request!"), 400
def createAnswer(json): valid_params = MyHandler.verify_parameters(json, ["question_id"]) if valid_params: try: new_ans = Answers(**valid_params) created_ans = new_ans.create() result = { "message": "Success!", "answer": created_ans.to_dict(), } return jsonify(result), 201 except Exception as err: return jsonify(message="Server error!", error=err.__str__()), 500 else: return jsonify(message="Bad Request!"), 400
def createProgress(json): valid_params = MyHandler.verify_parameters(json, ['user_id', 'lesson_id', 'type']) check = Progress.checkIfRepeated(json['lesson_id'], json['user_id'], json['type']) if valid_params: if check: try: new_p = Progress(**valid_params) created_p = new_p.create() result = { "message": "Success!", "progress": created_p.to_dict(), } return jsonify(result), 201 except Exception as err: return jsonify(message="Server error!", error=err.__str__()), 500 else: return jsonify(message="Bad Request!"), 400
def updateQuestion(qid,json): valid_params = MyHandler.verify_parameters(json, ['question_id']) if qid and valid_params: try: q_to_update = Questions.getQuestionsById(qid) if q_to_update: for key, value in valid_params.items(): setattr(q_to_update, key, value) q_to_update.update() result = { "message": "Success!", "question": q_to_update.to_dict(), } return jsonify(result), 200 else: return jsonify(message="Not Found!"), 404 except Exception as err: return jsonify(message="Server Error!", error=err.__str__()), 500 else: return jsonify(message="Bad Request!"), 400
def updateProgress(pid,json): valid_params = MyHandler.verify_parameters(json, ['progress_id']) if pid and valid_params: try: p_to_update = Progress.getProgressById(pid) if p_to_update: for key, value in valid_params.items(): setattr(p_to_update, key, value) p_to_update.update() result = { "message": "Success!", "progress": p_to_update.to_dict(), } return jsonify(result), 200 else: return jsonify(message="Not Found!"), 404 except Exception as err: return jsonify(message="Server Error!", error=err.__str__()), 500 else: return jsonify(message="Bad Request!"), 400
def updateScore(sid, json): valid_params = MyHandler.verify_parameters(json, ['score_id']) if sid and valid_params: try: score_to_update = Scores.getScoreById(sid) if score_to_update: for key, value in valid_params.items(): setattr(score_to_update, key, value) score_to_update.update() result = { "message": "Success!", "score": score_to_update.to_dict(), } return jsonify(result), 200 else: return jsonify(message="Not Found!"), 404 except Exception as err: return jsonify(message="Server Error!", error=err.__str__()), 500 else: return jsonify(message="Bad Request!"), 400
def createUser(json): valid_params = MyHandler.verify_parameters( json, ["name", "email", "password"]) if valid_params: try: print(valid_params) emailExists = Users.getUserByEmail(json['email']) if (emailExists): return jsonify(message="email already taken."), 400 new_user = Users(**valid_params) new_user.total_points = 0 created_user = new_user.create() result = { "message": "Success!", "user": created_user.to_dict(), } return jsonify(result), 201 except Exception as err: return jsonify(message="Server error!", error=err.__str__()), 500 else: return jsonify(message="Bad Request!"), 400
def createScore(json): valid_params = MyHandler.verify_parameters(json, ['user_id', 'lesson_id']) if valid_params: try: new_score = Scores(**valid_params) created_score = new_score.create() user_to_update = Users.getUserById(json['user_id']) setattr( user_to_update, "total_points", user_to_update.total_points + int(json['correctPoints'])) user_to_update.update() result = { "message": "Success!", "score": created_score.to_dict(), "user": user_to_update.to_dict() } return jsonify(result), 201 except Exception as err: return jsonify(message="Server error!", error=err.__str__()), 500 else: return jsonify(message="Bad Request!"), 400