Browse Source

Redefinition des formulaires

master
Dark-Storm 8 months ago
parent
commit
46ac8fcd1f
Signed by: Dark-Storm <l.gatin@neuf.fr> GPG Key ID: F61F10FA138E797C

+ 1
- 1
app/__init__.py View File

@@ -14,4 +14,4 @@ login.login_view = 'login'
login.login_message = "Veuillez vous authentifier avant de continuer."

from app import models
from app.routes import index
from app.routes import index, login, search, account

+ 0
- 71
app/forms.py View File

@@ -1,71 +0,0 @@
from flask_login import current_user
from flask_wtf import FlaskForm
from wtforms import StringField, PasswordField, BooleanField, TextAreaField, SubmitField, FileField
from wtforms.fields.html5 import DateField
from wtforms.validators import ValidationError, DataRequired, Optional, Email, EqualTo
from app.models.users import Member

from wtforms.meta import DefaultMeta

# TODO: Put those validators into a specific file
def validate_name(form, name):
member = Member.query.filter_by(name=name.data).first()
if member is not None:
raise ValidationError('Pseudo indisponible.')
if not Member.valid_name(name.data):
raise ValidationError("Nom d'utilisateur invalide.")

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é')

def login_form(f):
pass

class LoginForm(FlaskForm):
username = StringField('Pseudonyme :', validators=[DataRequired()])
password = PasswordField('Mot de passe :', validators=[DataRequired()])
remember_me = BooleanField('Se souvenir de moi :')
submit = SubmitField('Connexion')


class RegistrationForm(FlaskForm):
username = StringField('Pseudonyme :', validators=[DataRequired(), validate_name])
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')

class UpdateAccountForm(FlaskForm):
avatar = FileField('Avatar :', validators=[])
email = StringField('Adresse Email :', validators=[Optional(), Email(), validate_email])
password = PasswordField('Mot de passe :', validators=[Optional(), validate_password])
password2 = PasswordField('Répéter le mot de passe', validators=[Optional(), EqualTo('password')])
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.')
old_password = PasswordField('Ancien mot de passe :', validators=[DataRequired(), verify_authentication])
submit = SubmitField('Mettre à jour')

# TODO: compléter le formulaire de recherche avancée
class AdvancedSearchForm(FlaskForm):
q = StringField('Rechercher :', validators=[DataRequired()])
date = DateField('Date', validators=[Optional()])
submit = SubmitField('Affiner la recherche')

class SearchForm(FlaskForm):
q = StringField('Rechercher', validators=[DataRequired()])

+ 26
- 0
app/forms/account.py View File

@@ -0,0 +1,26 @@
from flask_wtf import FlaskForm
from wtforms import StringField, PasswordField, BooleanField, TextAreaField, SubmitField, FileField
from wtforms.fields.html5 import DateField
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])
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()])
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')

class UpdateAccountForm(FlaskForm):
avatar = FileField('Avatar :', validators=[])
email = StringField('Adresse Email :', validators=[Optional(), Email(), vd.email])
password = PasswordField('Mot de passe :', validators=[Optional(), vd.password])
password2 = PasswordField('Répéter le mot de passe', validators=[Optional(), EqualTo('password')])
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.')
old_password = PasswordField('Ancien mot de passe :', validators=[DataRequired(), vd.authentication])
submit = SubmitField('Mettre à jour')

+ 9
- 0
app/forms/login.py View File

@@ -0,0 +1,9 @@
from flask_wtf import FlaskForm
from wtforms import StringField, PasswordField, BooleanField, SubmitField
from wtforms.validators import DataRequired

class LoginForm(FlaskForm):
username = StringField('Pseudonyme :', validators=[DataRequired()])
password = PasswordField('Mot de passe :', validators=[DataRequired()])
remember_me = BooleanField('Se souvenir de moi :')
submit = SubmitField('Connexion')

+ 14
- 0
app/forms/search.py View File

@@ -0,0 +1,14 @@
from flask_wtf import FlaskForm
from wtforms import StringField, SubmitField
from wtforms.fields.html5 import DateField
from wtforms.validators import DataRequired, Optional

# TODO: compléter le formulaire de recherche avancée
class AdvancedSearchForm(FlaskForm):
q = StringField('Rechercher :', validators=[DataRequired()])
date = DateField('Date', validators=[Optional()])
submit = SubmitField('Affiner la recherche')

class SearchForm(FlaskForm):
q = StringField('Rechercher', validators=[DataRequired()])

+ 1
- 1
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 app import app, db
from app.forms import UpdateAccountForm, RegistrationForm
from app.forms.account import UpdateAccountForm, RegistrationForm
from app.models.users import Member
from app.utils.render import render


+ 1
- 9
app/routes/index.py View File

@@ -1,15 +1,7 @@
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, UpdateAccountForm, SearchForm, AdvancedSearchForm
from app.models.users import Member

from app.routes import login, account, search
from app import app

from app.utils.render import render

@app.route('/')
def index():
login_form = LoginForm()
return render('index.html')

+ 2
- 2
app/routes/login.py View File

@@ -1,11 +1,11 @@
from flask import redirect, url_for, request, flash
from flask_login import login_user, logout_user, login_required, current_user
from app import app
from app.forms import LoginForm
from app.forms.login import LoginForm
from app.models.users import Member
from app.utils.render import render

from app.routes import * # For open redirections
# from app.routes.index import index

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

+ 1
- 1
app/routes/search.py View File

@@ -1,5 +1,5 @@
from app import app
from app.forms import AdvancedSearchForm
from app.forms.search import AdvancedSearchForm
from app.utils.render import render

@app.route('/search')

+ 2
- 1
app/utils/render.py View File

@@ -1,5 +1,6 @@
from flask import render_template
from app.forms import LoginForm, SearchForm
from app.forms.login import LoginForm
from app.forms.search import SearchForm

def render(*args, **kwargs):
# TODO: debugguer cette merde : au logout, ça foire

+ 22
- 0
app/utils/validators.py View File

@@ -0,0 +1,22 @@
from wtforms.validators import ValidationError

def name(form, name):
member = Member.query.filter_by(name=name.data).first()
if member is not None:
raise ValidationError('Pseudo indisponible.')
if not Member.valid_name(name.data):
raise ValidationError("Nom d'utilisateur invalide.")

def 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 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 authentication(form, old_password):
if not current_user.check_password(old_password.data):
raise ValidationError('Mot de passe erroné')

Loading…
Cancel
Save