46 lines
1.5 KiB
Python
46 lines
1.5 KiB
Python
from app import app, db
|
|
from models import Achievement
|
|
|
|
from flask import render_template, redirect, url_for
|
|
from flask_login import current_user, login_required
|
|
|
|
import json
|
|
|
|
@app.route("/achievement/<id>")
|
|
def get_achievement(id):
|
|
achievement = Achievement.query.filter_by(id=id).first()
|
|
if not achievement:
|
|
return "Unknown achievement"
|
|
return render_template("achievement.html", data=achievement, endorsed=(current_user in achievement.endorsed_by))
|
|
|
|
@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 redirect(url_for("get_achievement", id=id))
|
|
|
|
@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)
|
|
if len(achievement.endorsed_by) == 0:
|
|
db.session.delete(achievement)
|
|
db.session.commit()
|
|
return redirect(url_for("all_users"))
|
|
else:
|
|
db.session.commit()
|
|
return redirect(url_for("get_achievement", id=id))
|
|
|