def lesson_api(lesson_id): """ Serves the data for a given lesson Use PUT request to update the progression through the lesson. """ lesson = LESSONS_BY_ID.get(lesson_id, None) if not lesson: return error_response(404) if request.method == "PUT": data = request.get_json() if not data: # BAD Request return error_response(400) completed_lesson = data.get("completed_lesson", None) completed_test = data.get("completed_test", None) progression = data.get("progression", None) if progression is not None and not is_valid_progression( progression, lesson.max_progression): return error_response(400, "Invalid value for field 'progression'") if completed_lesson is not None and not is_valid_complete( completed_lesson): return error_response( 400, "Invalid value for field 'completed_lesson'") if completed_test is not None and not is_valid_complete( completed_test): return error_response(400, "Invalid value for field 'completed_test'") update_lesson_completion(lesson_id, g.user.id, progression=progression, completed_lesson=completed_lesson, completed_test=completed_test) return jsonify({"status": "Ok"}) # Convert lesson to JSON lesson_data = { "id": lesson.id, "name": lesson.name, "description": lesson.description, "completed_test": False, "completed_lesson": False, "progression": 0 } for lesson in g.user.lessons: if lesson.lesson_id == lesson_id: lesson_data["completed_lesson"] = lesson.completed_lesson lesson_data["completed_test"] = lesson.completed_test lesson_data["progression"] = lesson.progression break return jsonify({"lesson": lesson_data})
def test_api(test_id): test = Test.query.filter_by(id=test_id).first() if test is not None: # Ensure the request is from the correct user if test.user != g.user.id: return error_response(401) if len(get_unique_puzzle_completions_for_test( test_id, g.user.id)) >= PUZZLES_PER_TEST: test.end_time = datetime.datetime.now() db.session.commit() return jsonify({"status": "Ok"}) return error_response(403) return error_response(404)
def update_user(id): if g.current_user.id != id: abort(403) user = User.query.get_or_404(id) data = request.get_json() or {} if 'username' in data and data['username'] != user.username and \ User.query.filter_by(username=data['username']).first(): return error_response(400, 'please use a different username') if 'email' in data and data['email'] != user.email and \ User.query.filter_by(email=data['email']).first(): return error_response(400, 'please use a different email address') user.from_dict(data, new_user=False) db.session.commit() return jsonify(user.to_dict())
def create_user(): data = request.get_json() or {} if 'username' not in data or 'email' not in data or 'password' not in data: return error_response( 400, 'Wrong registration data or not all data was send') if User.query.filter_by(email=data['email']).first() or \ User.query.filter_by(username=data['username']).first(): return error_response(400, 'Try another registration data') user = User() user.from_dict(data, new_user=True) db.session.add(user) db.session.commit() response = jsonify(user.to_dict()) response.status_code = 201 return response
def get_settings(): """ API route which handles personal user settings """ if request.method == "POST": data = request.get_json() if data is None: return error_response(400, message="Invalid JSON data") settings = {} # Only assign valid keys obtained from DEFAULT_USER_SETTINGS for key in DEFAULT_USER_SETTINGS: if key in data: settings[key] = data[key] g.user.settings = settings db.session.commit() return error_response(200) # Return user settings - return default values if missing key settings = {**DEFAULT_USER_SETTINGS, **g.user.settings} return jsonify({"settings": settings})
def puzzle_api(puzzle_id): """ API route which allows updating the completion of a puzzle """ puzzle = Puzzle.query.filter_by(id=puzzle_id).first() if puzzle is not None: data = request.get_json() if validate_completion_data(data): completion = PuzzleCompletion( user=g.user.id, puzzle_id=puzzle_id, attempts=data["attempts"], start_time=datetime.datetime.fromtimestamp(data["start_time"] / 1000), end_time=datetime.datetime.fromtimestamp(data["end_time"] / 1000), test_number=data["test_id"], ) db.session.add(completion) db.session.commit() return jsonify({"status": "Ok"}) return error_response(400) return error_response(404, message="Puzzle not found or already completed")
def puzzles_post_api(): """ API route used to create a new puzzle from the create_puzzle page """ data = request.get_json() if validate_puzzle_data(data): puzzle = Puzzle( fen=data["fen"], move_tree=data["move_tree"], is_atomic=data["is_atomic"], lesson_id=data["lesson_id"], ) db.session.add(puzzle) db.session.commit() return jsonify({"status": "Ok"}) return error_response(400)
def random_test_puzzle_api(test_id): """ API route which serves a random puzzle that hasn't been completed by the current user """ test = Test.query.get(test_id) # Ensure that the request is from the correct user if test.user != g.user.id: return error_response(401) puzzles = get_incomplete_puzzles_for_test(test_id, g.user.id) if len(puzzles) > 0: return jsonify({ "puzzle": random.choice(puzzles).to_json(), "is_final": len(get_unique_puzzle_completions_for_test(test_id, g.user.id)) >= (PUZZLES_PER_TEST - 1) }) return jsonify({"puzzle": None, "is_final": True})