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

View file

@ -3,9 +3,19 @@
<h2>{{data.name}}</h2> <h2>{{data.name}}</h2>
<ul> <ul>
{% for endorser in data.endorsed_by -%} {% 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 -%} {% 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> </ul>
</body> </body>
</html> </html>

View file

@ -3,7 +3,9 @@
<h2>{{data.id}}</h2> <h2>{{data.id}}</h2>
<ul> <ul>
{% for achievement in data.achievements -%} {% 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 -%} {% endfor -%}
<li> <a href="{{url_for('add_achievement', id=data.id)}}">Add achievement</a></li> <li> <a href="{{url_for('add_achievement', id=data.id)}}">Add achievement</a></li>
</ul> </ul>