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/forms/account.py b/app/forms/account.py index 33c35c7..ee522cc 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): + # Boolean inputs are generated on-the-fly from trophies list + submit = SubmitField('Modifier') class AdminDeleteAccountForm(FlaskForm): diff --git a/app/models/users.py b/app/models/users.py index aa2964c..50d056d 100644 --- a/app/models/users.py +++ b/app/models/users.py @@ -206,23 +206,27 @@ 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.query.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) == int: + t = Trophy.query.get(t) if type(t) == str: t = Trophy.query.filter_by(name=name).first() if t in self.trophies: @@ -271,7 +275,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') diff --git a/app/routes/admin/account.py b/app/routes/admin/account.py index c1c71fb..f1b0dda 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,13 @@ 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] + class TrophyForm(AdminAccountEditTrophyForm): + pass + + 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(): @@ -47,26 +51,31 @@ 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(): + 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') - 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) + 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 13e8616..808067d 100644 --- a/app/templates/admin/edit_account.html +++ b/app/templates/admin/edit_account.html @@ -92,29 +92,20 @@
- {{ 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 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 %}
-
{{ addtrophy_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") }}
+
{{ trophy_form.submit(class_="bg-green") }}

Supprimer le compte