2019-06-11 00:15:23 +02:00
|
|
|
from flask import flash, redirect, url_for
|
2019-02-06 12:44:44 +01:00
|
|
|
from app.utils.priv_required import priv_required
|
2019-06-06 01:45:39 +02:00
|
|
|
from app.models.users import Member
|
2019-06-11 00:15:23 +02:00
|
|
|
from app.models.trophies import Trophy
|
|
|
|
from app.forms.account import AdminUpdateAccountForm, AdminDeleteAccountForm, \
|
|
|
|
AdminAccountAddTrophyForm, AdminAccountDelTrophyForm
|
2019-02-04 16:41:29 +01:00
|
|
|
from app.utils.render import render
|
|
|
|
from app import app, db
|
2019-02-06 12:44:44 +01:00
|
|
|
|
2019-06-05 11:35:54 +02:00
|
|
|
|
2019-06-07 01:44:04 +02:00
|
|
|
@app.route('/admin/account/<user_id>/edit', methods=['GET', 'POST'])
|
2019-06-06 23:24:14 +02:00
|
|
|
@priv_required('access-admin-panel', 'edit-account')
|
2019-02-06 12:44:44 +01:00
|
|
|
def adm_edit_account(user_id):
|
2019-03-30 22:37:57 +01:00
|
|
|
user = Member.query.filter_by(id=user_id).first_or_404()
|
|
|
|
|
2019-06-11 00:15:23 +02:00
|
|
|
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]
|
|
|
|
|
|
|
|
if form.submit.data:
|
2019-02-06 12:44:44 +01:00
|
|
|
if form.validate_on_submit():
|
|
|
|
if form.avatar.data:
|
|
|
|
f = form.avatar.data
|
2019-06-05 11:35:54 +02:00
|
|
|
f.save("./app/static/" + user.avatar)
|
2019-03-30 22:37:57 +01:00
|
|
|
|
|
|
|
newname = form.username.data
|
|
|
|
names = list(Member.query.filter(Member.id != user.id).values(Member.name))
|
|
|
|
if newname in names:
|
2019-06-05 11:35:54 +02:00
|
|
|
raise Exception(f'{newname} is not available')
|
2019-02-06 12:44:44 +01:00
|
|
|
user.update(
|
2019-06-05 11:35:54 +02:00
|
|
|
name=form.username.data or None,
|
|
|
|
email=form.email.data or None,
|
|
|
|
password=form.password.data or None,
|
|
|
|
birthday=form.birthday.data,
|
|
|
|
signature=form.signature.data,
|
|
|
|
bio=form.biography.data,
|
|
|
|
newsletter=form.newsletter.data,
|
|
|
|
xp=form.xp.data or None,
|
2019-02-06 12:44:44 +01:00
|
|
|
)
|
|
|
|
db.session.merge(user)
|
|
|
|
db.session.commit()
|
2019-04-17 12:25:24 +02:00
|
|
|
# TODO: send an email to member saying his account has been modified
|
2019-02-06 12:44:44 +01:00
|
|
|
flash('Modifications effectuées', 'ok')
|
|
|
|
else:
|
|
|
|
flash('Erreur lors de la modification', 'error')
|
|
|
|
|
2019-06-11 00:15:23 +02:00
|
|
|
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')
|
|
|
|
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')
|
|
|
|
|
|
|
|
return render('admin/edit_account.html', user=user, form=form,
|
|
|
|
addtrophy_form=addtrophy_form, deltrophy_form=deltrophy_form)
|
2019-02-06 12:44:44 +01:00
|
|
|
|
2019-06-05 11:35:54 +02:00
|
|
|
|
2019-06-07 01:44:04 +02:00
|
|
|
@app.route('/admin/account/<user_id>/delete', methods=['GET', 'POST'])
|
2019-06-06 23:24:14 +02:00
|
|
|
@priv_required('access-admin-panel', 'delete-account')
|
2019-02-06 12:44:44 +01:00
|
|
|
def adm_delete_account(user_id):
|
2019-02-10 15:46:53 +01:00
|
|
|
user = Member.query.filter_by(id=user_id).first_or_404()
|
2019-02-09 21:18:12 +01:00
|
|
|
|
|
|
|
# Note: A user deleting their own account will be disconnected.
|
|
|
|
|
|
|
|
# TODO: Add an overview of what will be deleted.
|
|
|
|
# * How many posts will be turned into guest posts
|
|
|
|
# * Option: purely delete the posts in question
|
|
|
|
# * How many PMs will be deleted (can't unassign PMs)
|
|
|
|
# * etc.
|
2019-02-06 12:44:44 +01:00
|
|
|
del_form = AdminDeleteAccountForm()
|
2019-06-11 00:15:23 +02:00
|
|
|
if del_form.submit.data:
|
2019-02-06 12:44:44 +01:00
|
|
|
if del_form.validate_on_submit():
|
2019-02-10 18:51:53 +01:00
|
|
|
user.delete()
|
2019-02-06 12:44:44 +01:00
|
|
|
flash('Compte supprimé', 'ok')
|
2019-02-10 15:46:53 +01:00
|
|
|
return redirect(url_for('adm'))
|
2019-02-06 12:44:44 +01:00
|
|
|
else:
|
|
|
|
flash('Erreur lors de la suppression du compte', 'error')
|
2019-06-05 11:35:54 +02:00
|
|
|
del_form.delete.data = False # Force to tick to delete the account
|
2019-02-06 12:44:44 +01:00
|
|
|
return render('admin/delete_account.html', user=user, del_form=del_form)
|