Code source de Planète Casio https://planet-casio.com
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 2.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. from flask import redirect, url_for, request, flash
  2. from flask_login import login_required, current_user, logout_user
  3. from app import app, db
  4. from app.forms.account import UpdateAccountForm, RegistrationForm, DeleteAccountForm
  5. from app.models.users import Member
  6. from app.utils.render import render
  7. @app.route('/account', methods=['GET', 'POST'])
  8. @login_required
  9. def account():
  10. form = UpdateAccountForm()
  11. if request.method == "POST":
  12. if form.validate_on_submit():
  13. if form.avatar.data:
  14. f = form.avatar.data
  15. f.save("./app/static/"+current_user.avatar)
  16. current_user.update(
  17. email = form.email.data or None,
  18. password = form.password.data or None,
  19. birthday = form.birthday.data,
  20. signature = form.signature.data,
  21. biography = form.biography.data,
  22. newsletter = form.newsletter.data
  23. )
  24. db.session.merge(current_user)
  25. db.session.commit()
  26. flash('Modifications effectuées', 'ok')
  27. else:
  28. flash('Erreur lors de la modification', 'error')
  29. return render('account.html', form=form)
  30. @app.route('/account/delete', methods=['GET', 'POST'])
  31. def delete_account():
  32. del_form = DeleteAccountForm()
  33. if request.method == "POST":
  34. if del_form.validate_on_submit():
  35. db.session.delete(current_user)
  36. logout_user()
  37. db.session.commit()
  38. flash('Compte supprimé', 'ok')
  39. return redirect(url_for('index'))
  40. else:
  41. flash('Erreur lors de la suppression du compte', 'error')
  42. del_form.delete.data = False # Force to check the case to delete the account
  43. return render('delete_account.html', del_form=del_form)
  44. @app.route('/register', methods=['GET', 'POST'])
  45. def register():
  46. if current_user.is_authenticated:
  47. return redirect(url_for('index'))
  48. form = RegistrationForm()
  49. if form.validate_on_submit():
  50. member = Member(form.username.data, form.email.data, form.password.data)
  51. db.session.add(member)
  52. db.session.commit()
  53. flash('Inscription réussie', 'ok')
  54. return redirect(url_for('validation'))
  55. return render('register.html', title='Register', form=form)
  56. @app.route('/register/validation/')
  57. def validation():
  58. if current_user.is_authenticated :
  59. return redirect(url_for('index'))
  60. return render('validation.html')