From 31483fdd2e44dea030cbb2cee6c8b90fd260f7b4 Mon Sep 17 00:00:00 2001 From: Robbe Van Herck Date: Thu, 12 Jul 2018 22:24:06 +0200 Subject: [PATCH] Added endorsing en unendorsing --- achievements.py | 32 ++++++++++++++++++++++++++++++-- 1 file changed, 30 insertions(+), 2 deletions(-) diff --git a/achievements.py b/achievements.py index e455199..86e21ff 100644 --- a/achievements.py +++ b/achievements.py @@ -1,6 +1,8 @@ -from app import app +from app import app, db from models import Achievement +from flask_login import current_user, login_required + import json @app.route("/achievement/") @@ -8,4 +10,30 @@ def get_achievement(id): achievement = Achievement.query.filter_by(id=id).first() if not achievement: return "{}" - return json.dumps(achievement.to_obj()) \ No newline at end of file + return json.dumps(achievement.to_obj()) + +@app.route("/achievement//endorse") +@login_required +def endorse_achievement(id): + achievement = Achievement.query.filter_by(id=id).first() + if not achievement: + return "Invalid id" + if current_user in achievement.endorsed_by: + return "Already endorsed" + achievement.endorsed_by.append(current_user) + db.session.add(achievement) + db.session.commit() + return "OK" + +@app.route("/achievement//unendorse") +@login_required +def unendorse_achievement(id): + achievement = Achievement.query.filter_by(id=id).first() + if not achievement: + return "Invalid id" + if current_user not in achievement.endorsed_by: + return "Can't unendorse if you never endorsed..." + achievement.endorsed_by.remove(current_user) + db.session.add(achievement) + db.session.commit() + return "OK" \ No newline at end of file