Browse Source

Ajout de la page de gestion du compte, correction de bugs & autres

pull/12/head
Dark-Storm 2 years ago
parent
commit
52aee800f9
Signed by: Darks GPG Key ID: F61F10FA138E797C
6 changed files with 126 additions and 51 deletions
  1. +32
    -17
      app/forms.py
  2. +31
    -20
      app/routes.py
  3. +1
    -1
      app/static/css/navbar.css
  4. +6
    -5
      app/static/scripts/pc-utils.js
  5. +50
    -0
      app/templates/account.html
  6. +6
    -8
      app/templates/base/navbar/account.html

+ 32
- 17
app/forms.py View File

@ -1,9 +1,31 @@
from flask_login import current_user
from flask_wtf import FlaskForm
from wtforms import StringField, PasswordField, BooleanField, SubmitField
from wtforms.validators import ValidationError, DataRequired, Email, EqualTo
from app.models.users import Member
# TODO: Put those validators into a specific file
def validate_username(self, username):
member = Member.query.filter_by(username=username.data).first()
if member is not None:
raise ValidationError('Pseudo indisponible.')
def validate_email(form, email):
member = Member.query.filter_by(email=email.data).first()
if member is not None:
raise ValidationError('Adresse email déjà utilisée.')
def validate_password(form, password):
if len(password.data) != 0 and len(password.data) < 10:
raise ValidationError('Mot de passe est trop court (10 caractères minimum)')
# TODO: add more rules >:]
def verify_authentication(form, old_password):
if not current_user.check_password(old_password.data):
raise ValidationError('Mot de passe erroné')
class LoginForm(FlaskForm):
username = StringField('Pseudonyme :', validators=[DataRequired()])
password = PasswordField('Mot de passe :', validators=[DataRequired()])
@ -12,25 +34,18 @@ class LoginForm(FlaskForm):
class RegistrationForm(FlaskForm):
username = StringField('Pseudonyme :', validators=[DataRequired()])
email = StringField('Adresse Email :', validators=[DataRequired(), Email()])
password = PasswordField('Mot de passe :', validators=[DataRequired()])
username = StringField('Pseudonyme :', validators=[DataRequired()], validate_username)
email = StringField('Adresse Email :', validators=[DataRequired(), Email(), validate_email])
password = PasswordField('Mot de passe', validators=[DataRequired(), validate_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()])
newsletter = BooleanField('Inscription à la newsletter', description='Un mail par trimestre environ, pour être prévenu des concours, évènements et nouveautés.')
submit = SubmitField('S\'enregistrer')
def validate_username(self, username):
member = Member.query.filter_by(username=username.data).first()
if member is not None:
raise ValidationError('Pseudo indisponible.')
def validate_email(self, email):
member = Member.query.filter_by(email=email.data).first()
if member is not None:
raise ValidationError('Adresse email déjà utilisée.')
def validate_password(self, password):
if len(password.data) < 10:
raise ValidationError('Mot de passe est trop court (10 caractères minimum)')
# TODO: add more rules >:]
class UpdateAccountForm(FlaskForm):
email = StringField('Adresse Email :', validators=[Email(), validate_email])
password = PasswordField('Mot de passe :', validators=[validate_password])
password2 = PasswordField('Répéter le mot de passe', validators=[EqualTo('password')])
newsletter = BooleanField('Inscription à la newsletter', description='Un mail par trimestre environ, pour être prévenu des concours, évènements et nouveautés.')
old_password = PasswordField('Ancien mot de passe :', validators=[DataRequired(), verify_authentication])
submit = SubmitField('Mettre à jour')

+ 31
- 20
app/routes.py View File

@ -2,35 +2,49 @@ from flask import render_template, flash, redirect, url_for, request
from flask_login import login_user, logout_user, current_user, login_required
from werkzeug.urls import url_parse
from app import app, db
from app.forms import LoginForm, RegistrationForm
from app.forms import LoginForm, RegistrationForm, UpdateAccountForm
from app.models.users import Member
@app.route('/', methods=['GET', 'POST'])
@app.route('/')
def index():
form = LoginForm()
flash('pseudo ou mot de passe invalide', 'error')
flash('tout ça c\'est ok !', 'ok')
flash('mais ça bof', 'warning')
flash('et une info', 'info')
return render_template('index.html', form=form)
@app.route('/login', methods=['GET', 'POST'])
def login():
if request.method == 'GET':
return redirect(url_for('index'))
form = LoginForm()
if form.validate_on_submit():
flash('test', 'ok')
member = Member.query.filter_by(username=form.username.data).first()
if member is None or not member.check_password(form.password.data):
flash('pseudo ou mot de passe invalide')
return redirect(url_for('index'))
flash('Connexion réussie')
flash('Pseudo ou mot de passe invalide', 'error')
return redirect(request.referrer)
login_user(member, remember=form.remember_me.data)
flash('Connexion réussie', 'info')
return redirect(request.referrer)
return render_template('index.html', form=form)
@app.route('/logout/')
@app.route('/logout')
def logout():
logout_user()
flash('Déconnexion réussie', 'info')
return redirect(url_for('index'))
@app.route('/account', methods=['GET', 'POST'])
def account():
if not current_user.is_authenticated:
flash('Veuillez vous authentifier', 'warning')
return redirect(url_for('index'))
form = LoginForm()
form2 = UpdateAccountForm()
if form2.validate_on_submit():
flash('Modifications pas encore effectuées', 'ok')
else:
flash('Erreur quelque part', 'error')
return render_template('account.html', form=form, form2=form2)
@app.route('/register', methods=['GET', 'POST'])
def register():
if current_user.is_authenticated:
@ -41,16 +55,13 @@ def register():
member = Member(form2.username.data, form2.email.data, form2.password.data)
db.session.add(member)
db.session.commit()
flash('Congratulations, you are now a registered member!')
flash('Inscription réussie', 'ok')
return redirect(url_for('validation'))
return render_template('register.html', title='Register', form=form, form2 = form2)
return render_template('register.html', title='Register', form=form, form2=form2)
@app.route('/register/validation/')
def validation():
if current_user.is_authenticated :
return redirect(url_for('index'))
form = LoginForm()
return render_template('validation.html', form = form)
return render_template('validation.html', form=form)

+ 1
- 1
app/static/css/navbar.css View File

@ -180,7 +180,7 @@ nav a:focus {
margin-right: 10px;
}
#menu form div {
#menu form {
padding: 0 5%;
}
#menu form input[type="text"],


+ 6
- 5
app/static/scripts/pc-utils.js View File

@ -14,6 +14,7 @@ function getCookie(name) {
/*
Flash messages
TODO: Find a way to have good flash messages in a KISS & DRY way
*/
function flash_add(type, message) {
template = `<div class="flash {{ category }}" style="top: {{ top }}px;" onclick="flash_close(this)">
@ -47,13 +48,14 @@ function flash_close(element) {
for(var i = 0; i < childs.length; i++) {
childs[i].style.top = ((i + 1) * 70 - 45) + 'px';
}
}, 200);
}, 0);
}
/*
Send post ajax request to url defined in action.
Callback the function defined in the callback attribute from the submit type.
*/
/* We don't need Ajax at that time. Maybe later
function ajaxWrapper(evt){
evt.preventDefault();
var elems = evt.target;
@ -78,9 +80,7 @@ function ajaxWrapper(evt){
req.send(params);
}
/*
Add event listener on submit for all form with class with-ajax.
*/
// Add event listener on submit for all form with class with-ajax.
window.onload = function(){
@ -104,4 +104,5 @@ window.onload = function(){
function login(response){
alert(response);
}
}
//*/

+ 50
- 0
app/templates/account.html View File

@ -0,0 +1,50 @@
{% extends "base/container.html" %}
{% block content %}
<section class="home-pinned-content flex">
<div id="register">
<h1>Gestion du compte :</h1>
<form action="" method="post">
{{ form2.hidden_tag() }}
<div>
{{ form2.email.label }}
{{ form2.email() }}
{% for error in form2.email.errors %}
<span class="msgerror">{{ error }}</span>
{% endfor %}
</div>
<div>
{{ form2.password.label }}
{{ form2.password() }}
{% for error in form2.password.errors %}
<span class="msgerror">{{ error }}</span>
{% endfor %}
</div>
<div>
{{ form2.password2.label }}
{{ form2.password2() }}
{% for error in form2.password2.errors %}
<span class="msgerror">{{ error }}</span>
{% endfor %}
</div>
<div>
{{ form2.newsletter.label }}
{{ form2.newsletter() }}
<div style="font-size:80%;color:rgba(0,0,0,.5)">{{ form2.newsletter.description }}</div>
{% for error in form2.newsletter.errors %}
<span class="msgerror">{{ error }}</span>
{% endfor %}
</div>
<div>
{{ form2.old_password.label }}
{{ form2.old_password() }}
{% for error in form2.old_password.errors %}
<span class="msgerror">{{ error }}</span>
{% endfor %}
</div>
<div>{{ form2.submit(class_="bg-green") }}</div>
</form>
</div>
</section>
{% endblock %}

+ 6
- 8
app/templates/base/navbar/account.html View File

@ -31,7 +31,7 @@
<hr />
<a href="account.html">
<a href="{{ url_for('account') }}">
<svg viewBox="0 0 24 24">
<path fill="#ffffff" d="M12,15.5A3.5,3.5 0 0,1 8.5,12A3.5,3.5 0 0,1 12,8.5A3.5,3.5 0 0,1 15.5,12A3.5,3.5 0 0,1 12,15.5M19.43,12.97C19.47,12.65 19.5,12.33 19.5,12C19.5,11.67 19.47,11.34 19.43,11L21.54,9.37C21.73,9.22 21.78,8.95 21.66,8.73L19.66,5.27C19.54,5.05 19.27,4.96 19.05,5.05L16.56,6.05C16.04,5.66 15.5,5.32 14.87,5.07L14.5,2.42C14.46,2.18 14.25,2 14,2H10C9.75,2 9.54,2.18 9.5,2.42L9.13,5.07C8.5,5.32 7.96,5.66 7.44,6.05L4.95,5.05C4.73,4.96 4.46,5.05 4.34,5.27L2.34,8.73C2.21,8.95 2.27,9.22 2.46,9.37L4.57,11C4.53,11.34 4.5,11.67 4.5,12C4.5,12.33 4.53,12.65 4.57,12.97L2.46,14.63C2.27,14.78 2.21,15.05 2.34,15.27L4.34,18.73C4.46,18.95 4.73,19.03 4.95,18.95L7.44,17.94C7.96,18.34 8.5,18.68 9.13,18.93L9.5,21.58C9.54,21.82 9.75,22 10,22H14C14.25,22 14.46,21.82 14.5,21.58L14.87,18.93C15.5,18.67 16.04,18.34 16.56,17.94L19.05,18.95C19.27,19.03 19.54,18.95 19.66,18.73L21.66,15.27C21.78,15.05 21.73,14.78 21.54,14.63L19.43,12.97Z"></path>
</svg>
@ -49,14 +49,12 @@
<h2>
Invité
</h2>
<form method="post" action="/" class="login with-ajax">
<form method="post" action="/login" class="login">
{{ form.hidden_tag() }}
<div>
{{ form.username(size=32, placeholder="Identifiant") }}
{{ form.password(size=32, placeholder="Mot de passe") }}
</div>
<div>{{ form.submit(class_="bg-green") }}</div>
<div>{{ form.remember_me.label }} {{ form.remember_me() }}</div>
{{ form.username(size=32, placeholder="Identifiant") }}
{{ form.password(size=32, placeholder="Mot de passe") }}
{{ form.submit(class_="bg-green") }}
{{ form.remember_me.label }} {{ form.remember_me() }}
</form>
<hr />
<a href="{{ url_for('register') }}">Mot de passe oublié ?</a>


Loading…
Cancel
Save