Merge branch 'trophies'
This commit is contained in:
commit
e6544a68c7
2
Pipfile
2
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"
|
||||
|
|
|
@ -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):
|
||||
|
|
|
@ -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'<Member: {self.name}>'
|
||||
|
|
|
@ -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')
|
||||
|
|
|
@ -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/<user_id>/delete', methods=['GET', 'POST'])
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
|
|
|
@ -92,29 +92,20 @@
|
|||
</form>
|
||||
|
||||
<form action="{{ url_for('adm_edit_account', user_id=user.id) }}" method="post">
|
||||
{{ addtrophy_form.hidden_tag() }}
|
||||
<h2>Accorder un trophée</h2>
|
||||
<div>
|
||||
{{ addtrophy_form.trophy.label }}
|
||||
{{ addtrophy_form.trophy }}
|
||||
{% for error in addtrophy_form.trophy.errors %}
|
||||
<span class="msgerror">{{ error }}</span>
|
||||
{{ trophy_form.hidden_tag() }}
|
||||
<h2>Trophées</h2>
|
||||
<div class="trophies-panel">
|
||||
{% for id, input in trophy_form.__dict__.items() %}
|
||||
{% if id[0] == "t" %}
|
||||
<div>
|
||||
{# TODO: add trophies icons #}
|
||||
{{ input(checked=id in trophy_form.user_trophies) }}
|
||||
{{ input.label }}
|
||||
</div>
|
||||
{% endif %}
|
||||
{% endfor %}
|
||||
</div>
|
||||
<div>{{ addtrophy_form.submit(class_="bg-green") }}</div>
|
||||
</form>
|
||||
|
||||
<form action="{{ url_for('adm_edit_account', user_id=user.id) }}" method="post">
|
||||
{{ deltrophy_form.hidden_tag() }}
|
||||
<h2>Retirer un trophée</h2>
|
||||
<div>
|
||||
{{ deltrophy_form.trophy.label }}
|
||||
{{ deltrophy_form.trophy }}
|
||||
{% for error in deltrophy_form.trophy.errors %}
|
||||
<span class="msgerror">{{ error }}</span>
|
||||
{% endfor %}
|
||||
</div>
|
||||
<div>{{ deltrophy_form.submit(class_="bg-red") }}</div>
|
||||
<div>{{ trophy_form.submit(class_="bg-green") }}</div>
|
||||
</form>
|
||||
|
||||
<h2 style="margin-top:30px;">Supprimer le compte</h2>
|
||||
|
|
Loading…
Reference in New Issue