From 7f640a13e93ae4a973d0c266741f9eda7e66b3b2 Mon Sep 17 00:00:00 2001 From: Darks Date: Sat, 10 Aug 2019 20:06:07 +0200 Subject: [PATCH] =?UTF-8?q?Modification=20de=20la=20zone=20d'admin=20des?= =?UTF-8?q?=20comptes=20Retrait=20de=20la=20liste=20d=C3=A9roulante=20au?= =?UTF-8?q?=20profit=20d'une=20liste=20de=20checkbox.=20Il=20faudra=20ajou?= =?UTF-8?q?ter=20les=20icones.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/forms/account.py | 2 +- app/models/users.py | 4 +++- app/routes/admin/account.py | 29 ++++++++++++++++----------- app/static/css/form.css | 12 +++++++++++ app/templates/admin/edit_account.html | 24 ++++++++-------------- 5 files changed, 41 insertions(+), 30 deletions(-) diff --git a/app/forms/account.py b/app/forms/account.py index b72a5d1..ee522cc 100644 --- a/app/forms/account.py +++ b/app/forms/account.py @@ -51,7 +51,7 @@ class AdminUpdateAccountForm(FlaskForm): class AdminAccountEditTrophyForm(FlaskForm): - trophies = [BooleanField(t.name, validators=[Optional()]) for t in Trophy.query.all()] + # Boolean inputs are generated on-the-fly from trophies list submit = SubmitField('Modifier') diff --git a/app/models/users.py b/app/models/users.py index 7ba0248..50d056d 100644 --- a/app/models/users.py +++ b/app/models/users.py @@ -210,7 +210,7 @@ class Member(User, db.Model): the right to do this! """ if type(t) == int: - t = Trophy.get(t) + t = Trophy.query.get(t) if type(t) == str: t = Trophy.query.filter_by(name=t).first() if t not in self.trophies: @@ -225,6 +225,8 @@ class Member(User, db.Model): Add a trophy to the current user. Check whether the request sender has the right to do this! """ + if type(t) == int: + t = Trophy.query.get(t) if type(t) == str: t = Trophy.query.filter_by(name=name).first() if t in self.trophies: diff --git a/app/routes/admin/account.py b/app/routes/admin/account.py index df82fac..f1b0dda 100644 --- a/app/routes/admin/account.py +++ b/app/routes/admin/account.py @@ -16,11 +16,13 @@ def adm_edit_account(user_id): form = AdminUpdateAccountForm(prefix="user") - for t in Trophy.query.all(): - setattr(AdminAccountEditTrophyForm, "t" + str(t.id), BooleanField(t.name)) - trophy_form = AdminAccountEditTrophyForm(prefix="trophy") + class TrophyForm(AdminAccountEditTrophyForm): + pass - print(trophy_form.t22) + for t in Trophy.query.all(): + setattr(TrophyForm, f't{t.id}', BooleanField(t.name)) + setattr(TrophyForm, "user_trophies", [f't{t.id}' for t in user.trophies]) + trophy_form = TrophyForm(prefix="trophies") if form.submit.data: if form.validate_on_submit(): @@ -51,12 +53,15 @@ def adm_edit_account(user_id): 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) + for id, field in trophy_form.__dict__.items(): + if id[0] == "t": + print(f"id: {id[1:]}, name: {field.label}, checked={field.data}", end=" ") + if field.data: + print(f"Add trophy {id[1:]}") + user.add_trophy(int(id[1:])) + else: + print(f"Del trophy {id[1:]}") + user.del_trophy(int(id[1:])) else: flash("Erreur lors de l'ajout du trophée", 'error') @@ -69,8 +74,8 @@ def adm_edit_account(user_id): # else: # flash("Erreur lors du retrait du trophée", 'error') - return render('admin/edit_account.html', user=user, form=form, - trophy_form=trophy_form) + return render('admin/edit_account.html', user=user, + form=form, trophy_form=trophy_form) @app.route('/admin/account//delete', methods=['GET', 'POST']) diff --git a/app/static/css/form.css b/app/static/css/form.css index b28c973..1ea1957 100644 --- a/app/static/css/form.css +++ b/app/static/css/form.css @@ -65,3 +65,15 @@ font-size: 80%; color: gray; } + +.trophies-panel { + display: flex; flex-wrap: wrap; +} +.trophies-panel > div { + margin: 3px 5px; padding: 3px; + border: 1px solid #969696; + border-radius: 3px; +} +.trophies-panel label { + margin-right: 5px; +} diff --git a/app/templates/admin/edit_account.html b/app/templates/admin/edit_account.html index e171cc0..808067d 100644 --- a/app/templates/admin/edit_account.html +++ b/app/templates/admin/edit_account.html @@ -95,27 +95,19 @@ {{ trophy_form.hidden_tag() }}

Trophées

- {% for trophy in trophy_form.trophies %} - {{ trophy.label }} - {{ trophy }} + {% for id, input in trophy_form.__dict__.items() %} + {% if id[0] == "t" %} +
+ {# TODO: add trophies icons #} + {{ input(checked=id in trophy_form.user_trophies) }} + {{ input.label }} +
+ {% endif %} {% endfor %}
{{ trophy_form.submit(class_="bg-green") }}
- {#
- {{ deltrophy_form.hidden_tag() }} -

Retirer un trophée

-
- {{ deltrophy_form.trophy.label }} - {{ deltrophy_form.trophy }} - {% for error in deltrophy_form.trophy.errors %} - {{ error }} - {% endfor %} -
-
{{ deltrophy_form.submit(class_="bg-red") }}
-
#} -

Supprimer le compte

Supprimer le compte