details in register
This commit is contained in:
parent
76c5100fa3
commit
d36008934a
|
@ -1,9 +1,17 @@
|
|||
from flask import g
|
||||
from flask_wtf import FlaskForm
|
||||
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.resources.agent.models import Person
|
||||
from ereuse_devicehub.resources.user.models import User
|
||||
|
||||
|
||||
|
@ -104,12 +112,15 @@ class PasswordForm(FlaskForm):
|
|||
|
||||
|
||||
class UserNewRegisterForm(FlaskForm):
|
||||
email = EmailField('Email Address', [
|
||||
validators.DataRequired(),
|
||||
validators.Length(min=6, max=35)
|
||||
])
|
||||
email = EmailField(
|
||||
'Email Address', [validators.DataRequired(), validators.Length(min=6, max=35)]
|
||||
)
|
||||
password = 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 = {
|
||||
'invalid_login': (
|
||||
|
@ -130,21 +141,22 @@ class UserNewRegisterForm(FlaskForm):
|
|||
password2 = self.password2.data
|
||||
if password != password2:
|
||||
self.form_errors.append('The passwords are not equal.')
|
||||
return False
|
||||
|
||||
txt = 'This email are in use.'
|
||||
email = self.email.data
|
||||
if User.query.filter_by(email=email).first():
|
||||
self.form_errors.append(txt)
|
||||
return False
|
||||
|
||||
# def authenticate(self, email, password):
|
||||
# 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
|
||||
return True
|
||||
|
||||
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()
|
||||
|
|
|
@ -55,7 +55,17 @@
|
|||
</div>
|
||||
|
||||
<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 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>
|
||||
|
|
|
@ -115,12 +115,9 @@ class UserRegistrationView(View):
|
|||
def dispatch_request(self):
|
||||
form = UserNewRegisterForm()
|
||||
if form.validate_on_submit():
|
||||
|
||||
next_url = flask.request.args.get('next')
|
||||
if not is_safe_url(flask.request, next_url):
|
||||
return flask.abort(400)
|
||||
|
||||
return flask.redirect(next_url or flask.url_for('core.login'))
|
||||
form.save()
|
||||
messages.success('User created successfully!')
|
||||
return flask.redirect(flask.url_for('core.login'))
|
||||
context = {'form': form, 'version': __version__}
|
||||
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('/logout/', view_func=LogoutView.as_view('logout'))
|
||||
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'))
|
||||
|
|
Reference in New Issue