La version 5 de Planète Casio. Regroupe le forum, les programmes, les tutoriel, les sprites et tous les autres outils développés par nos soins.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

account.py 3.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. from flask import flash, redirect, url_for
  2. from wtforms import BooleanField
  3. from app.utils.priv_required import priv_required
  4. from app.models.users import Member
  5. from app.models.trophies import Trophy
  6. from app.forms.account import AdminUpdateAccountForm, AdminDeleteAccountForm, \
  7. AdminAccountEditTrophyForm
  8. from app.utils.render import render
  9. from app import app, db
  10. @app.route('/admin/account/<user_id>/edit', methods=['GET', 'POST'])
  11. @priv_required('access-admin-panel', 'edit-account')
  12. def adm_edit_account(user_id):
  13. user = Member.query.filter_by(id=user_id).first_or_404()
  14. form = AdminUpdateAccountForm(prefix="user")
  15. class TrophyForm(AdminAccountEditTrophyForm):
  16. pass
  17. for t in Trophy.query.all():
  18. setattr(TrophyForm, f't{t.id}', BooleanField(t.name))
  19. setattr(TrophyForm, "user_trophies", [f't{t.id}' for t in user.trophies])
  20. trophy_form = TrophyForm(prefix="trophies")
  21. if form.submit.data:
  22. if form.validate_on_submit():
  23. if form.avatar.data:
  24. f = form.avatar.data
  25. f.save("./app/static/" + user.avatar)
  26. newname = form.username.data
  27. names = list(Member.query.filter(Member.id != user.id).values(Member.name))
  28. if newname in names:
  29. raise Exception(f'{newname} is not available')
  30. user.update(
  31. name=form.username.data or None,
  32. email=form.email.data or None,
  33. password=form.password.data or None,
  34. birthday=form.birthday.data,
  35. signature=form.signature.data,
  36. bio=form.biography.data,
  37. newsletter=form.newsletter.data,
  38. xp=form.xp.data or None,
  39. )
  40. db.session.merge(user)
  41. db.session.commit()
  42. # TODO: send an email to member saying his account has been modified
  43. flash('Modifications effectuées', 'ok')
  44. else:
  45. flash('Erreur lors de la modification', 'error')
  46. if trophy_form.submit.data:
  47. if trophy_form.validate_on_submit():
  48. for id, field in trophy_form.__dict__.items():
  49. if id[0] == "t":
  50. print(f"id: {id[1:]}, name: {field.label}, checked={field.data}", end=" ")
  51. if field.data:
  52. print(f"Add trophy {id[1:]}")
  53. user.add_trophy(int(id[1:]))
  54. else:
  55. print(f"Del trophy {id[1:]}")
  56. user.del_trophy(int(id[1:]))
  57. db.session.merge(user)
  58. db.session.commit()
  59. else:
  60. flash("Erreur lors de l'ajout du trophée", 'error')
  61. user_owned = set()
  62. for t in user.trophies:
  63. user_owned.add(f"t{t.id}")
  64. return render('admin/edit_account.html', user=user,
  65. form=form, trophy_form=trophy_form, user_owned=user_owned)
  66. @app.route('/admin/account/<user_id>/delete', methods=['GET', 'POST'])
  67. @priv_required('access-admin-panel', 'delete-account')
  68. def adm_delete_account(user_id):
  69. user = Member.query.filter_by(id=user_id).first_or_404()
  70. # Note: A user deleting their own account will be disconnected.
  71. # TODO: Add an overview of what will be deleted.
  72. # * How many posts will be turned into guest posts
  73. # * Option: purely delete the posts in question
  74. # * How many PMs will be deleted (can't unassign PMs)
  75. # * etc.
  76. del_form = AdminDeleteAccountForm()
  77. if del_form.submit.data:
  78. if del_form.validate_on_submit():
  79. user.delete()
  80. flash('Compte supprimé', 'ok')
  81. return redirect(url_for('adm'))
  82. else:
  83. flash('Erreur lors de la suppression du compte', 'error')
  84. del_form.delete.data = False # Force to tick to delete the account
  85. return render('admin/delete_account.html', user=user, del_form=del_form)