From ff21d89c2312aac2c828fc5483c05107c1529bf5 Mon Sep 17 00:00:00 2001 From: Darks Date: Fri, 9 Aug 2019 23:20:53 +0200 Subject: [PATCH 1/3] Small modifications --- Pipfile | 2 +- app/models/users.py | 17 ++++++++++------- app/routes/account/account.py | 1 + 3 files changed, 12 insertions(+), 8 deletions(-) diff --git a/Pipfile b/Pipfile index 9bee1b5..50b3487 100644 --- a/Pipfile +++ b/Pipfile @@ -7,7 +7,7 @@ name = "pypi" flask = ">=1.0" flask-wtf = ">=0.14" flask-login = ">=0.4" -flask-migrate = ">=2.3 " +flask-migrate = ">=2.3" flask-sqlalchemy = ">=2.3" flask-script = ">=2.0" uwsgi = ">=2.0" diff --git a/app/models/users.py b/app/models/users.py index aa2964c..7ba0248 100644 --- a/app/models/users.py +++ b/app/models/users.py @@ -206,22 +206,24 @@ class Member(User, db.Model): def add_trophy(self, t): """ - Add a trophy to the current user. heck whether the request sender has the right - to do this! + Add a trophy to the current user. Check whether the request sender has + the right to do this! """ + if type(t) == int: + t = Trophy.get(t) if type(t) == str: - t = Trophy.query.filter_by(name=name).first() + t = Trophy.query.filter_by(name=t).first() if t not in self.trophies: self.trophies.append(t) db.session.merge(self) db.session.commit() # TODO: implement the notification system - # self.notify(f"Vous venez de débloquer le trophée '{name}'") + # self.notify(f"Vous venez de débloquer le trophée '{t.name}'") def del_trophy(self, t): """ - Add a trophy to the current user. heck whether the request sender has the right - to do this! + Add a trophy to the current user. Check whether the request sender has + the right to do this! """ if type(t) == str: t = Trophy.query.filter_by(name=name).first() @@ -271,7 +273,8 @@ class Member(User, db.Model): if age.days > 365.25 * 5: self.add_trophy("Papy Casio") if context == "on-profile-update" or context is None: - pass + # TODO: add a better condition (this is for test) + self.add_trophy("Artiste") def __repr__(self): return f'' diff --git a/app/routes/account/account.py b/app/routes/account/account.py index 601f5af..e24786d 100644 --- a/app/routes/account/account.py +++ b/app/routes/account/account.py @@ -25,6 +25,7 @@ def edit_account(): ) db.session.merge(current_user) db.session.commit() + current_user.update_trophies("on-profile-update") flash('Modifications effectuées', 'ok') else: flash('Erreur lors de la modification', 'error') From 95efa3622831211a825ebe1cbe1e04f735c5e1eb Mon Sep 17 00:00:00 2001 From: Darks Date: Sat, 10 Aug 2019 00:07:50 +0200 Subject: [PATCH 2/3] =?UTF-8?q?Tentative=20(foireuse)=20de=20g=C3=A9n?= =?UTF-8?q?=C3=A9rer=20une=20liste=20de=20BooleanInput?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/forms/account.py | 11 ++----- app/routes/admin/account.py | 44 +++++++++++++++------------ app/templates/admin/edit_account.html | 19 ++++++------ 3 files changed, 36 insertions(+), 38 deletions(-) diff --git a/app/forms/account.py b/app/forms/account.py index 33c35c7..b72a5d1 100644 --- a/app/forms/account.py +++ b/app/forms/account.py @@ -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): diff --git a/app/routes/admin/account.py b/app/routes/admin/account.py index c1c71fb..df82fac 100644 --- a/app/routes/admin/account.py +++ b/app/routes/admin/account.py @@ -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//delete', methods=['GET', 'POST']) diff --git a/app/templates/admin/edit_account.html b/app/templates/admin/edit_account.html index 13e8616..e171cc0 100644 --- a/app/templates/admin/edit_account.html +++ b/app/templates/admin/edit_account.html @@ -92,19 +92,18 @@
- {{ addtrophy_form.hidden_tag() }} -

Accorder un trophée

-
- {{ addtrophy_form.trophy.label }} - {{ addtrophy_form.trophy }} - {% for error in addtrophy_form.trophy.errors %} - {{ error }} + {{ trophy_form.hidden_tag() }} +

Trophées

+
+ {% for trophy in trophy_form.trophies %} + {{ trophy.label }} + {{ trophy }} {% endfor %}
-
{{ addtrophy_form.submit(class_="bg-green") }}
+
{{ trophy_form.submit(class_="bg-green") }}
-
+ {# {{ deltrophy_form.hidden_tag() }}

Retirer un trophée

@@ -115,7 +114,7 @@ {% endfor %}
{{ deltrophy_form.submit(class_="bg-red") }}
-
+ #}

Supprimer le compte

Supprimer le compte From 7f640a13e93ae4a973d0c266741f9eda7e66b3b2 Mon Sep 17 00:00:00 2001 From: Darks Date: Sat, 10 Aug 2019 20:06:07 +0200 Subject: [PATCH 3/3] =?UTF-8?q?Modification=20de=20la=20zone=20d'admin=20d?= =?UTF-8?q?es=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