Browse Source

Tentative (foireuse) de générer une liste de BooleanInput

master
Darks 3 months ago
parent
commit
95efa36228
Signed by: Darks <l.gatin@neuf.fr> GPG Key ID: F61F10FA138E797C
3 changed files with 36 additions and 38 deletions
  1. 3
    8
      app/forms/account.py
  2. 24
    20
      app/routes/admin/account.py
  3. 9
    10
      app/templates/admin/edit_account.html

+ 3
- 8
app/forms/account.py View File

@@ -50,14 +50,9 @@ class AdminUpdateAccountForm(FlaskForm):
submit = SubmitField('Mettre à jour')


class AdminAccountAddTrophyForm(FlaskForm):
trophy = SelectField('Trophée', validators=[InputRequired()], coerce=int)
#trophy = SelectField('Trophée', validators=[DataRequired()])
submit = SubmitField('Ajouter')


class AdminAccountDelTrophyForm(AdminAccountAddTrophyForm):
submit = SubmitField('Supprimer')
class AdminAccountEditTrophyForm(FlaskForm):
trophies = [BooleanField(t.name, validators=[Optional()]) for t in Trophy.query.all()]
submit = SubmitField('Modifier')


class AdminDeleteAccountForm(FlaskForm):

+ 24
- 20
app/routes/admin/account.py View File

@@ -1,9 +1,10 @@
from flask import flash, redirect, url_for
from wtforms import BooleanField
from app.utils.priv_required import priv_required
from app.models.users import Member
from app.models.trophies import Trophy
from app.forms.account import AdminUpdateAccountForm, AdminDeleteAccountForm, \
AdminAccountAddTrophyForm, AdminAccountDelTrophyForm
AdminAccountEditTrophyForm
from app.utils.render import render
from app import app, db

@@ -15,10 +16,11 @@ def adm_edit_account(user_id):

form = AdminUpdateAccountForm(prefix="user")

addtrophy_form = AdminAccountAddTrophyForm(prefix="addtrophy")
addtrophy_form.trophy.choices = [(t.id, t.name) for t in Trophy.query.all()]
deltrophy_form = AdminAccountDelTrophyForm(prefix="deltrophy")
deltrophy_form.trophy.choices = [(t.id, t.name) for t in user.trophies]
for t in Trophy.query.all():
setattr(AdminAccountEditTrophyForm, "t" + str(t.id), BooleanField(t.name))
trophy_form = AdminAccountEditTrophyForm(prefix="trophy")

print(trophy_form.t22)

if form.submit.data:
if form.validate_on_submit():
@@ -47,26 +49,28 @@ def adm_edit_account(user_id):
else:
flash('Erreur lors de la modification', 'error')

if addtrophy_form.submit.data:
if addtrophy_form.validate_on_submit():
trophy = Trophy.query.get(addtrophy_form.trophy.data)
if trophy is not None:
user.add_trophy(trophy)
flash('Trophée ajouté', 'ok')
if trophy_form.submit.data:
if trophy_form.validate_on_submit():
trophies = [(t.label, t.data) for t in trophy_form.trophies]
for t, set in trophies:
if set:
user.add_trophy(t)
else:
user.del_trophy(t)
else:
flash("Erreur lors de l'ajout du trophée", 'error')

if deltrophy_form.submit.data:
if deltrophy_form.validate_on_submit():
trophy = Trophy.query.get(deltrophy_form.trophy.data)
if trophy is not None:
user.del_trophy(trophy)
flash('Trophée retiré', 'ok')
else:
flash("Erreur lors du retrait du trophée", 'error')
# if deltrophy_form.submit.data:
# if deltrophy_form.validate_on_submit():
# trophy = Trophy.query.get(deltrophy_form.trophy.data)
# if trophy is not None:
# user.del_trophy(trophy)
# flash('Trophée retiré', 'ok')
# else:
# flash("Erreur lors du retrait du trophée", 'error')

return render('admin/edit_account.html', user=user, form=form,
addtrophy_form=addtrophy_form, deltrophy_form=deltrophy_form)
trophy_form=trophy_form)


@app.route('/admin/account/<user_id>/delete', methods=['GET', 'POST'])

+ 9
- 10
app/templates/admin/edit_account.html View File

@@ -92,19 +92,18 @@
</form>

<form action="{{ url_for('adm_edit_account', user_id=user.id) }}" method="post">
{{ addtrophy_form.hidden_tag() }}
<h2>Accorder un trophée</h2>
<div>
{{ addtrophy_form.trophy.label }}
{{ addtrophy_form.trophy }}
{% for error in addtrophy_form.trophy.errors %}
<span class="msgerror">{{ error }}</span>
{{ trophy_form.hidden_tag() }}
<h2>Trophées</h2>
<div class="trophies-panel">
{% for trophy in trophy_form.trophies %}
{{ trophy.label }}
{{ trophy }}
{% endfor %}
</div>
<div>{{ addtrophy_form.submit(class_="bg-green") }}</div>
<div>{{ trophy_form.submit(class_="bg-green") }}</div>
</form>

<form action="{{ url_for('adm_edit_account', user_id=user.id) }}" method="post">
{# <form action="{{ url_for('adm_edit_account', user_id=user.id) }}" method="post">
{{ deltrophy_form.hidden_tag() }}
<h2>Retirer un trophée</h2>
<div>
@@ -115,7 +114,7 @@
{% endfor %}
</div>
<div>{{ deltrophy_form.submit(class_="bg-red") }}</div>
</form>
</form> #}

<h2 style="margin-top:30px;">Supprimer le compte</h2>
<a href="{{ url_for('adm_delete_account', user_id=user.id) }}" class="button bg-red">Supprimer le compte</a>

Loading…
Cancel
Save