diff --git a/app/forms/account.py b/app/forms/account.py index f162ffe..20473d6 100644 --- a/app/forms/account.py +++ b/app/forms/account.py @@ -213,6 +213,13 @@ class AdminAccountEditTrophyForm(FlaskForm): ) +class AdminAccountEditGroupForm(FlaskForm): + # Boolean inputs are generated on-the-fly from group list + submit = SubmitField( + 'Modifier', + ) + + class AdminDeleteAccountForm(FlaskForm): delete = BooleanField( 'Confirmer la suppression', diff --git a/app/models/users.py b/app/models/users.py index a9ee066..14aeb26 100644 --- a/app/models/users.py +++ b/app/models/users.py @@ -199,7 +199,7 @@ class Member(User): im.save(V5Config.AVATARS_FOLDER + self.avatar, 'PNG') def get_public_data(self): - """Returns the public information of the member.""" + """ Returns the public information of the member.""" return { "name": self.name, "xp": self.xp, @@ -228,7 +228,7 @@ class Member(User): password, method='pbkdf2:sha512', salt_length=10) def check_password(self, password): - """Compares password against member hash.""" + """ Compares password against member hash or LDAP record """ if V5Config.USE_LDAP: return ldap.check_password(self, password) else: @@ -236,17 +236,44 @@ class Member(User): password) def notify(self, message, href=None): - """ Notify a user with a message. - An hyperlink can be added to redirect to the notification source """ + """ + Notify a user with a message. + An hyperlink can be added to redirect to the notification source + """ return n = Notification(self.id, message, href=href) db.session.add(n) db.session.commit() + def add_group(self, g): + """ + Add a group to the user. + Check wheter or not the request sender has the right to do this! + """ + if type(g) == int: + g = Group.query.get(g) + if type(g) == str: + g = Group.query.filter_by(name=g).first() + if g not in self.groups: + self.groups.append(g) + self.notify(f"Vous avez été ajouté au groupe '{g.name}'") + + def del_group(self, g): + """ + Remove a group to the user. + Check wheter or not the request sender has the right to do this! + """ + if type(g) == int: + g = Group.query.get(g) + if type(g) == str: + g = Group.query.filter_by(name=g).first() + if g in self.groups: + self.groups.remove(g) + def add_trophy(self, t): """ - Add a trophy to the current user. Check 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) @@ -258,8 +285,8 @@ class Member(User): def del_trophy(self, t): """ - Add a trophy to the current user. Check whether the request sender has - the right to do this! + Delete 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) diff --git a/app/routes/admin/account.py b/app/routes/admin/account.py index 3ef3689..a068f2e 100644 --- a/app/routes/admin/account.py +++ b/app/routes/admin/account.py @@ -4,8 +4,9 @@ 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.models.privs import Group from app.forms.account import AdminUpdateAccountForm, AdminDeleteAccountForm, \ - AdminAccountEditTrophyForm + AdminAccountEditTrophyForm, AdminAccountEditGroupForm from app.utils.render import render from app.utils.notify import notify from app import app, db @@ -21,12 +22,21 @@ def adm_edit_account(user_id): class TrophyForm(AdminAccountEditTrophyForm): pass + class GroupForm(AdminAccountEditGroupForm): + 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") + for g in Group.query.all(): + setattr(GroupForm, f'g{g.id}', BooleanField(g.name)) + setattr(GroupForm, "user_groups", [f'g{g.id}' for g in user.groups]) + group_form = GroupForm(prefix="group") + + print(group_form.__dict__.items()) + if form.submit.data: if form.validate_on_submit(): newname = form.username.data @@ -56,29 +66,51 @@ def adm_edit_account(user_id): else: flash('Erreur lors de la modification', 'error') + # Trophies 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:])) db.session.merge(user) db.session.commit() + flash('Modifications effectuées', 'ok') + return redirect(request.url) else: - flash("Erreur lors de l'ajout du trophée", 'error') + flash("Erreur lors de la modification des trophées", 'error') - user_owned = set() + # Groups + if group_form.submit.data: + if group_form.validate_on_submit(): + for id, field in group_form.__dict__.items(): + if id[0] == "g": + if field.data: + user.add_group(int(id[1:])) + else: + user.del_group(int(id[1:])) + + db.session.merge(user) + db.session.commit() + flash('Modifications effectuées', 'ok') + return redirect(request.url) + else: + flash("Erreur lors de la modification des groupes", 'error') + + trophies_owned = set() for t in user.trophies: - user_owned.add(f"t{t.id}") + trophies_owned.add(f"t{t.id}") - return render('admin/edit_account.html', user=user, - form=form, trophy_form=trophy_form, user_owned=user_owned) + groups_owned = set() + for g in user.groups: + groups_owned.add(f"g{g.id}") + + return render('admin/edit_account.html', user=user, form=form, + trophy_form=trophy_form, trophies_owned=trophies_owned, + group_form=group_form, groups_owned=groups_owned) @app.route('/admin/account//delete', methods=['GET', 'POST']) diff --git a/app/static/css/container.css b/app/static/css/container.css index 79aab07..1e03e6e 100644 --- a/app/static/css/container.css +++ b/app/static/css/container.css @@ -26,3 +26,26 @@ section .avatar { display: block; width: 128px; height: 128px; } + + +/* Some grid */ +.flex-grid { + display: flex; + flex-flow: row wrap; +} +.flex-grid > * { + min-width: 250px; + flex: auto; +} +/* Two columns */ +.flex-grid.fg2 > * { + width: 50%; +} +/* Three columns */ +.flex-grid.fg3 > * { + width: 33%; +} +/* Four columns */ +.flex-grid.fg4 > * { + width: 25%; +} diff --git a/app/templates/admin/edit_account.html b/app/templates/admin/edit_account.html index d5b8a01..783857c 100644 --- a/app/templates/admin/edit_account.html +++ b/app/templates/admin/edit_account.html @@ -99,12 +99,12 @@
{{ 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 user_owned) }} + {{ input(checked=id in trophies_owned) }} {{ input.label }}
{% endif %} @@ -115,6 +115,25 @@
+ + {{ group_form.hidden_tag() }} +

Groupes

+
+ {% for id, input in group_form.__dict__.items() %} + {% if id[0] == "g" %} +
+ {# TODO: add trophies icons #} + {{ input(checked=id in groups_owned) }} + {{ input.label }} +
+ {% endif %} + {% endfor %} +
+
{{ group_form.submit(class_="bg-green") }}
+ + +
+

Supprimer le compte

Supprimer le compte