details in register

This commit is contained in:
Cayo Puigdefabregas 2022-09-28 17:51:31 +02:00
parent 76c5100fa3
commit d36008934a
3 changed files with 45 additions and 24 deletions

View File

@ -1,9 +1,17 @@
from flask import g from flask import g
from flask_wtf import FlaskForm from flask_wtf import FlaskForm
from werkzeug.security import generate_password_hash from werkzeug.security import generate_password_hash
from wtforms import BooleanField, EmailField, PasswordField, validators from wtforms import (
BooleanField,
EmailField,
PasswordField,
StringField,
TelField,
validators,
)
from ereuse_devicehub.db import db from ereuse_devicehub.db import db
from ereuse_devicehub.resources.agent.models import Person
from ereuse_devicehub.resources.user.models import User from ereuse_devicehub.resources.user.models import User
@ -104,12 +112,15 @@ class PasswordForm(FlaskForm):
class UserNewRegisterForm(FlaskForm): class UserNewRegisterForm(FlaskForm):
email = EmailField('Email Address', [ email = EmailField(
validators.DataRequired(), 'Email Address', [validators.DataRequired(), validators.Length(min=6, max=35)]
validators.Length(min=6, max=35) )
])
password = PasswordField('Password', [validators.DataRequired()]) password = PasswordField('Password', [validators.DataRequired()])
password2 = PasswordField('Password', [validators.DataRequired()]) password2 = PasswordField('Password', [validators.DataRequired()])
name = StringField(
'Name', [validators.DataRequired(), validators.Length(min=3, max=35)]
)
telephone = TelField('Telephone', [validators.DataRequired()])
error_messages = { error_messages = {
'invalid_login': ( 'invalid_login': (
@ -130,21 +141,22 @@ class UserNewRegisterForm(FlaskForm):
password2 = self.password2.data password2 = self.password2.data
if password != password2: if password != password2:
self.form_errors.append('The passwords are not equal.') self.form_errors.append('The passwords are not equal.')
return False
txt = 'This email are in use.' txt = 'This email are in use.'
email = self.email.data email = self.email.data
if User.query.filter_by(email=email).first(): if User.query.filter_by(email=email).first():
self.form_errors.append(txt) self.form_errors.append(txt)
return False
# def authenticate(self, email, password): return True
# if email is None or password is None:
# return
# user = User.query.filter_by(email=email).first()
# if user is None:
# # Run the default password hasher once to reduce the timing
# # difference between an existing and a nonexistent user (#20760).
# generate_password_hash(password)
# else:
# if user.check_password(password):
# return user
def save(self, commit=True):
user = User(email=self.email.data, password=self.password.data)
person = Person(
email=self.email.data, name=self.name.data, telephone=self.telephone.data
)
user.individuals.add(person)
db.session.add(user)
if commit:
db.session.commit()

View File

@ -55,7 +55,17 @@
</div> </div>
<div class="col-12"> <div class="col-12">
<button class="btn btn-primary w-100" type="submit">Login</button> <label for="name" class="form-label">Name</label>
<input name="name" class="form-control" id="name" required>
</div>
<div class="col-12">
<label for="telephone" class="form-label">Telephone</label>
<input type="tel" name="telephone" class="form-control" id="telephone" required>
</div>
<div class="col-12">
<button class="btn btn-primary w-100" type="submit">Register</button>
</div> </div>
<div class="col-12"> <div class="col-12">
<p class="small mb-0">Don't have account? <a href="#TODO" onclick="$('#exampleModal').modal('show')" data-toggle="modal" data-target="#exampleModal">Create an account</a></p> <p class="small mb-0">Don't have account? <a href="#TODO" onclick="$('#exampleModal').modal('show')" data-toggle="modal" data-target="#exampleModal">Create an account</a></p>

View File

@ -115,12 +115,9 @@ class UserRegistrationView(View):
def dispatch_request(self): def dispatch_request(self):
form = UserNewRegisterForm() form = UserNewRegisterForm()
if form.validate_on_submit(): if form.validate_on_submit():
form.save()
next_url = flask.request.args.get('next') messages.success('User created successfully!')
if not is_safe_url(flask.request, next_url): return flask.redirect(flask.url_for('core.login'))
return flask.abort(400)
return flask.redirect(next_url or flask.url_for('core.login'))
context = {'form': form, 'version': __version__} context = {'form': form, 'version': __version__}
return flask.render_template(self.template_name, **context) return flask.render_template(self.template_name, **context)
@ -128,5 +125,7 @@ class UserRegistrationView(View):
core.add_url_rule('/login/', view_func=LoginView.as_view('login')) core.add_url_rule('/login/', view_func=LoginView.as_view('login'))
core.add_url_rule('/logout/', view_func=LogoutView.as_view('logout')) core.add_url_rule('/logout/', view_func=LogoutView.as_view('logout'))
core.add_url_rule('/profile/', view_func=UserProfileView.as_view('user-profile')) core.add_url_rule('/profile/', view_func=UserProfileView.as_view('user-profile'))
core.add_url_rule('/new_register/', view_func=UserRegistrationView.as_view('user-registration')) core.add_url_rule(
'/new_register/', view_func=UserRegistrationView.as_view('user-registration')
)
core.add_url_rule('/set_password/', view_func=UserPasswordView.as_view('set-password')) core.add_url_rule('/set_password/', view_func=UserPasswordView.as_view('set-password'))