2019-06-07 01:44:04 +02:00
|
|
|
from flask import request, flash, redirect, url_for
|
2019-06-06 23:24:14 +02:00
|
|
|
from app.utils.priv_required import priv_required
|
|
|
|
from app.models.trophies import Trophy, Title
|
2019-06-07 01:44:04 +02:00
|
|
|
from app.forms.trophies import TrophyForm, DeleteTrophyForm
|
2019-06-06 23:24:14 +02:00
|
|
|
from app.utils.render import render
|
|
|
|
from app import app, db
|
|
|
|
|
|
|
|
|
|
|
|
@app.route('/admin/trophies', methods=['GET', 'POST'])
|
2019-06-07 01:44:04 +02:00
|
|
|
@priv_required('access-admin-panel', 'edit-trophies')
|
2019-06-06 23:24:14 +02:00
|
|
|
def adm_trophies():
|
2019-06-07 01:44:04 +02:00
|
|
|
form = TrophyForm()
|
2019-06-06 23:24:14 +02:00
|
|
|
if request.method == "POST":
|
|
|
|
if form.validate_on_submit():
|
2019-06-11 00:15:23 +02:00
|
|
|
is_title = form.title.data
|
2019-06-07 01:44:04 +02:00
|
|
|
if is_title:
|
2019-06-11 00:15:23 +02:00
|
|
|
trophy = Title(form.name.data, form.css.data)
|
2019-06-06 23:24:14 +02:00
|
|
|
else:
|
|
|
|
trophy = Trophy(form.name.data)
|
|
|
|
db.session.add(trophy)
|
|
|
|
db.session.commit()
|
2019-06-07 01:44:04 +02:00
|
|
|
flash(f'Nouveau {["trophée", "titre"][is_title]} ajouté', 'ok')
|
2019-06-06 23:24:14 +02:00
|
|
|
else:
|
|
|
|
flash('Erreur lors de la création du trophée', 'error')
|
|
|
|
|
|
|
|
trophies = Trophy.query.all()
|
|
|
|
return render('admin/trophies.html', trophies=trophies,
|
|
|
|
form=form)
|
2019-06-07 01:44:04 +02:00
|
|
|
|
|
|
|
|
|
|
|
@app.route('/admin/trophies/<trophy_id>/edit', methods=['GET', 'POST'])
|
|
|
|
@priv_required('access-admin-panel', 'edit-trophies')
|
|
|
|
def adm_edit_trophy(trophy_id):
|
|
|
|
trophy = Trophy.query.filter_by(id=trophy_id).first_or_404()
|
|
|
|
|
|
|
|
form = TrophyForm()
|
|
|
|
if request.method == "POST":
|
|
|
|
if form.validate_on_submit():
|
|
|
|
is_title = form.title.data != ""
|
|
|
|
if is_title:
|
|
|
|
trophy.name = form.name.data
|
|
|
|
trophy.title = form.title.data
|
|
|
|
trophy.css = form.css.data
|
|
|
|
else:
|
|
|
|
trophy.name = form.name.data
|
|
|
|
db.session.merge(trophy)
|
|
|
|
db.session.commit()
|
|
|
|
flash(f'{["Trophée", "Titre"][is_title]} modifié', 'ok')
|
|
|
|
return redirect(url_for('adm_trophies'))
|
|
|
|
else:
|
|
|
|
flash('Erreur lors de la création du trophée', 'error')
|
|
|
|
return render('admin/edit_trophy.html', trophy=trophy, form=form)
|
|
|
|
|
|
|
|
|
|
|
|
@app.route('/admin/trophies/<trophy_id>/delete', methods=['GET', 'POST'])
|
|
|
|
@priv_required('access-admin-panel', 'edit-trophies')
|
|
|
|
def adm_delete_trophy(trophy_id):
|
|
|
|
trophy = Trophy.query.filter_by(id=trophy_id).first_or_404()
|
|
|
|
|
|
|
|
# TODO: Add an overview of what will be deleted.
|
|
|
|
del_form = DeleteTrophyForm()
|
|
|
|
if request.method == "POST":
|
|
|
|
if del_form.validate_on_submit():
|
2019-06-07 19:48:12 +02:00
|
|
|
# TODO: Remove relationship with users that have the trophy
|
2019-06-07 01:44:04 +02:00
|
|
|
db.session.delete(trophy)
|
|
|
|
db.session.commit()
|
|
|
|
flash('Trophée supprimé', 'ok')
|
|
|
|
return redirect(url_for('adm_trophies'))
|
|
|
|
else:
|
|
|
|
flash('Erreur lors de la suppression du trophée', 'error')
|
2019-06-07 19:48:12 +02:00
|
|
|
del_form.delete.data = False # Force to tick to delete the trophy
|
2019-06-07 01:44:04 +02:00
|
|
|
return render('admin/delete_trophy.html', trophy=trophy, del_form=del_form)
|