Added endorse/unendorse switching

This commit is contained in:
Robbe Van Herck 2018-07-17 09:56:02 +02:00
parent a65e2c48cb
commit bfe2a86d39
No known key found for this signature in database
GPG key ID: 4D1C8E8FBD1C7188
3 changed files with 19 additions and 7 deletions

View file

@ -1,7 +1,7 @@
from app import app, db
from models import Achievement
from flask import render_template
from flask import render_template, redirect, url_for
from flask_login import current_user, login_required
import json
@ -11,7 +11,7 @@ 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)
return render_template("achievement.html", data=achievement, endorsed=(current_user in achievement.endorsed_by))
@app.route("/achievement/<id>/endorse")
@login_required
@ -24,7 +24,7 @@ def endorse_achievement(id):
achievement.endorsed_by.append(current_user)
db.session.add(achievement)
db.session.commit()
return "OK"
return redirect(url_for("get_achievement", id=id))
@app.route("/achievement/<id>/unendorse")
@login_required
@ -37,4 +37,4 @@ def unendorse_achievement(id):
achievement.endorsed_by.remove(current_user)
db.session.add(achievement)
db.session.commit()
return "OK"
return redirect(url_for("get_achievement", id=id))

View file

@ -3,9 +3,19 @@
<h2>{{data.name}}</h2>
<ul>
{% for endorser in data.endorsed_by -%}
<li> <a href="{{url_for('get_user', id=endorser.id)}}">{{ endorser.id }} </a></li>
<li>
<a href="{{url_for('get_user', id=endorser.id)}}">{{ endorser.id }} </a>
</li>
{% endfor -%}
<li> <a href="{{url_for('endorse_achievement', id=data.id)}}">Endorse</a></li>
{% if endorsed -%}
<li>
<a href="{{url_for('unendorse_achievement', id=data.id)}}">Unendorse</a>
</li>
{% else -%}
<li>
<a href="{{url_for('endorse_achievement', id=data.id)}}">Endorse</a>
</li>
{% endif -%}
</ul>
</body>
</html>

View file

@ -3,7 +3,9 @@
<h2>{{data.id}}</h2>
<ul>
{% for achievement in data.achievements -%}
<li> <a href="{{url_for('get_achievement', id=achievement.id)}}">{{ achievement.name }}</a>: {{ achievement.endorsed_by|length }} endorsement{{ "s" if (achievement.endorsed_by|length != 1) else "" }}</li>
<li>
<a href="{{url_for('get_achievement', id=achievement.id)}}">{{ achievement.name }}</a>: {{ achievement.endorsed_by|length }} endorsement{{ "s" if (achievement.endorsed_by|length != 1) else "" }}
</li>
{% endfor -%}
<li> <a href="{{url_for('add_achievement', id=data.id)}}">Add achievement</a></li>
</ul>