Created
December 20, 2015 23:28
-
-
Save pcote/3f2662b80dacb5fc3cdf to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| from functools import wraps | |
| def validate_json(*expected_args): | |
| def decorator(func): | |
| @wraps(func) | |
| def wrapper(*args, **kwargs): | |
| json_ob = request.get_json() | |
| for expected_arg in expected_args: | |
| if expected_arg not in json_ob or json_ob.get(expected_arg) is None: | |
| abort(400) | |
| return func(*args, **kwargs) | |
| return wrapper | |
| return decorator | |
| @app.route("/addscore", methods=["POST"]) | |
| @validate_json("exercise_id", "score") | |
| def add_score(): | |
| json_data = request.get_json() | |
| exercise_id = json_data.get("exercise_id") | |
| score = json_data.get("score") | |
| model.add_attempt(exercise_id, score) | |
| return jsonify(dict(result="success")) | |
| @app.route("/addtopic", methods=["POST"]) | |
| @validate_json("topic", "tags") | |
| def add_topic(): | |
| json_data = request.get_json() | |
| topic = json_data.get("topic") | |
| tags = json_data.get("tags") | |
| email = session.get("email") | |
| topic = topic.strip() | |
| tags = [tag.strip() for tag in tags if re.search(r"\w+", tag)] | |
| model.add_topic(topic, email, tags) | |
| return jsonify({"result": "task completed"}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment