Browse Source

Modifications sur la gestion du compte

- Réorganisation des parties
- Ajout d'un formulaire de suppression

Le formulaire de suppression ne fonctionne pas à cause d'un erreur dans le `session.commit()` (`AssertionError: Failed to add object to the flush context!`). Cela est peut-être du aux relations qui mettent orphelines les contenus dépendants du membre. Cf [delete cascades](https://docs.sqlalchemy.org/en/latest/orm/cascades.html#delete) dans sql alchemy
master
Dark-Storm 8 months ago
parent
commit
62ccaa23d6
Signed by: Dark-Storm <l.gatin@neuf.fr> GPG Key ID: F61F10FA138E797C

+ 11
- 6
app/forms/account.py View File

@@ -5,8 +5,8 @@ from wtforms.validators import DataRequired, Optional, Email, EqualTo
import app.utils.validators as vd

class RegistrationForm(FlaskForm):
username = StringField('Pseudonyme :', validators=[DataRequired(), vd.name])
email = StringField('Adresse Email :', validators=[DataRequired(), Email(), vd.email])
username = StringField('Pseudonyme', validators=[DataRequired(), vd.name])
email = StringField('Adresse Email', validators=[DataRequired(), Email(), vd.email])
password = PasswordField('Mot de passe', validators=[DataRequired(), vd.password])
password2 = PasswordField('Répéter le mot de passe', validators=[DataRequired(), EqualTo('password')])
guidelines = BooleanField('J’accepte les <a href="#">CGU</a>', validators=[DataRequired()])
@@ -14,13 +14,18 @@ class RegistrationForm(FlaskForm):
submit = SubmitField('S\'enregistrer')

class UpdateAccountForm(FlaskForm):
avatar = FileField('Avatar :', validators=[Optional(), vd.avatar])
email = StringField('Adresse Email :', validators=[Optional(), Email(), vd.email, vd.old_password])
avatar = FileField('Avatar', validators=[Optional(), vd.avatar])
email = StringField('Adresse Email', validators=[Optional(), Email(), vd.email, vd.old_password])
password = PasswordField('Mot de passe :', validators=[Optional(), vd.password, vd.old_password])
password2 = PasswordField('Répéter le mot de passe', validators=[Optional(), EqualTo('password')])
old_password = PasswordField('Ancien mot de passe :', validators=[Optional()])
old_password = PasswordField('Mot de passe actuel', validators=[Optional()])
birthday = DateField('Anniversaire', validators=[Optional()])
signature = TextAreaField('Signature', validators=[Optional()])
biography = TextAreaField('Présentation', validators=[Optional()])
newsletter = BooleanField('Inscription à la newsletter', description='Un mail par trimestre environ, pour être prévenu des concours, évènements et nouveautés.')
submit = SubmitField('Mettre à jour')
submit = SubmitField('Mettre à jour')

class DeleteAccountForm(FlaskForm):
delete = BooleanField('Supprimer le compte', validators=[DataRequired()], description='Attention, cette opération est irréversible !')
old_password = PasswordField('Mot de passe', validators=[DataRequired(), vd.old_password])
submit = SubmitField('Supprimer le compte')

+ 18
- 3
app/routes/account.py View File

@@ -1,7 +1,7 @@
from flask import redirect, url_for, request, flash
from flask_login import login_required, current_user
from flask_login import login_required, current_user, logout_user
from app import app, db
from app.forms.account import UpdateAccountForm, RegistrationForm
from app.forms.account import UpdateAccountForm, RegistrationForm, DeleteAccountForm
from app.models.users import Member
from app.utils.render import render

@@ -9,6 +9,7 @@ from app.utils.render import render
@login_required
def account():
form = UpdateAccountForm()
del_form = DeleteAccountForm()
if request.method == "POST":
if form.validate_on_submit():
# Updates to do
@@ -32,7 +33,21 @@ def account():
flash('Modifications effectuées', 'ok')
else:
flash('Erreur lors de la modification', 'error')
return render('account.html', form=form)

return render('account.html', form=form, del_form=del_form)

@app.route('/account/delete', methods=['GET', 'POST'])
def delete_account():
del_form = DeleteAccountForm()
if del_form.validate_on_submit():
db.session.delete(current_user)
logout_user()
db.session.commit()
flash('Compte supprimé', 'ok')
return redirect(url_for('index'))
else:
flash('Erreur lors de la suppression du compte', 'error')
return render('delete_account.html', del_form=del_form)

@app.route('/register', methods=['GET', 'POST'])
def register():

+ 4
- 4
app/static/css/form.css View File

@@ -47,15 +47,15 @@

.form input[type="submit"] {
width: 100%;
background-color: #149641;
/*background-color: #149641;
border-color: #1e7408;
color: #ffffff;
color: #ffffff;*/
}
.form input[type="submit"]:hover,
.form input[type="submit"]:focus,
.form input[type="submit"]:active {
background-color: #0f7331;
border-color: #073617;
/*background-color: #0f7331;
border-color: #073617;*/
}

.form form .msgerror {

+ 13
- 0
app/static/css/global.css View File

@@ -102,3 +102,16 @@ input[type="checkbox"] {
background-color: #0f7331;
border-color: #073617;
}

.bg-red,
.bg-red {
background-color: #c0341d;
border-color: #a62d19;
color: #ffffff;
}
.bg-red:hover,
.bg-red:focus,
.bg-red:active {
background-color: #7b2112;
border-color: #073617;
}

+ 22
- 1
app/templates/account.html View File

@@ -4,7 +4,7 @@
<section class="form" style="width:80%;">
<h1>Gestion du compte</h1>

<form action="" method="post">
<form action="{{ url_for('account') }}" method="post">
{{ form.hidden_tag() }}

<h2>Général</h2>
@@ -78,5 +78,26 @@
</div>
<div>{{ form.submit(class_="bg-green") }}</div>
</form>

<h2 style="margin-top:30px;">Supprimer le compte</h2>
<form action="{{ url_for('delete_account') }}" method="post">
{{ del_form.hidden_tag() }}
<div>
{{ del_form.delete.label }}
{{ del_form.delete }}
<div style="font-size:80%;color:rgba(0,0,0,.5)">{{ del_form.delete.description }}</div>
{% for error in del_form.delete.errors %}
<span class="msgerror">{{ error }}</span>
{% endfor %}
</div>
<div>
{{ del_form.old_password.label }}
{{ del_form.old_password(placeholder='************') }}
{% for error in del_form.old_password.errors %}
<span class="msgerror">{{ error }}</span>
{% endfor %}
</div>
<div>{{ del_form.submit(class_="bg-red") }}</div>
</form>
</section>
{% endblock %}

+ 26
- 0
app/templates/delete_account.html View File

@@ -0,0 +1,26 @@
{% extends "base/container.html" %}

{% block content %}
<section class="form" style="width:80%;">
<h1>Supprimer le compte</h2>
<form action="{{ url_for('delete_account') }}" method="post">
{{ del_form.hidden_tag() }}
<div>
{{ del_form.delete.label }}
{{ del_form.delete }}
<div style="font-size:80%;color:rgba(0,0,0,.5)">{{ del_form.delete.description }}</div>
{% for error in del_form.delete.errors %}
<span class="msgerror">{{ error }}</span>
{% endfor %}
</div>
<div>
{{ del_form.old_password.label }}
{{ del_form.old_password(placeholder='************') }}
{% for error in del_form.old_password.errors %}
<span class="msgerror">{{ error }}</span>
{% endfor %}
</div>
<div>{{ del_form.submit(class_="bg-red") }}</div>
</form>
</section>
{% endblock %}

+ 1
- 1
app/utils/validators.py View File

@@ -1,6 +1,6 @@
from flask_login import current_user
from wtforms.validators import ValidationError
from app.models.users import Member
from app.models.users import User, Member

def name(form, name):
member = Member.query.filter_by(name=name.data).first()

Loading…
Cancel
Save