Added endorsing en unendorsing
This commit is contained in:
parent
8e9e1b6e8f
commit
31483fdd2e
1 changed files with 30 additions and 2 deletions
|
@ -1,6 +1,8 @@
|
||||||
from app import app
|
from app import app, db
|
||||||
from models import Achievement
|
from models import Achievement
|
||||||
|
|
||||||
|
from flask_login import current_user, login_required
|
||||||
|
|
||||||
import json
|
import json
|
||||||
|
|
||||||
@app.route("/achievement/<id>")
|
@app.route("/achievement/<id>")
|
||||||
|
@ -8,4 +10,30 @@ def get_achievement(id):
|
||||||
achievement = Achievement.query.filter_by(id=id).first()
|
achievement = Achievement.query.filter_by(id=id).first()
|
||||||
if not achievement:
|
if not achievement:
|
||||||
return "{}"
|
return "{}"
|
||||||
return json.dumps(achievement.to_obj())
|
return json.dumps(achievement.to_obj())
|
||||||
|
|
||||||
|
@app.route("/achievement/<id>/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/<id>/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"
|
Loading…
Reference in a new issue