Ajout des groupes dans le panel de modification d'un compte

This commit is contained in:
Darks 2019-12-10 22:27:39 +01:00
parent d63173b48d
commit bf8f766131
Signed by: Darks
GPG Key ID: F61F10FA138E797C
5 changed files with 127 additions and 19 deletions

View File

@ -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',

View File

@ -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)

View File

@ -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/<user_id>/delete', methods=['GET', 'POST'])

View File

@ -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%;
}

View File

@ -99,12 +99,12 @@
<form action="{{ url_for('adm_edit_account', user_id=user.id) }}" method="post">
{{ trophy_form.hidden_tag() }}
<h2>Trophées</h2>
<div class="trophies-panel">
<div class="trophies-panel flex-grid fg3">
{% for id, input in trophy_form.__dict__.items() %}
{% if id[0] == "t" %}
<div>
{# TODO: add trophies icons #}
{{ input(checked=id in user_owned) }}
{{ input(checked=id in trophies_owned) }}
{{ input.label }}
</div>
{% endif %}
@ -115,6 +115,25 @@
<hr>
<form action="{{ url_for('adm_edit_account', user_id=user.id) }}" method="post">
{{ group_form.hidden_tag() }}
<h2>Groupes</h2>
<div class="groups-panel flex-grid fg3">
{% for id, input in group_form.__dict__.items() %}
{% if id[0] == "g" %}
<div>
{# TODO: add trophies icons #}
{{ input(checked=id in groups_owned) }}
{{ input.label }}
</div>
{% endif %}
{% endfor %}
</div>
<div>{{ group_form.submit(class_="bg-green") }}</div>
</form>
<hr>
<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>