123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150 |
- # -*- coding: utf-8 -*-
- """
- flaskbb.auth.forms
- ~~~~~~~~~~~~~~~~~~~~
- It provides the forms that are needed for the auth views.
- :copyright: (c) 2014 by the FlaskBB Team.
- :license: BSD, see LICENSE for more details.
- """
- from datetime import datetime
- from flask_wtf import Form
- from wtforms import (StringField, PasswordField, BooleanField, HiddenField,
- SubmitField, SelectField)
- from wtforms.validators import (DataRequired, InputRequired, Email, EqualTo,
- regexp, ValidationError)
- from flask_babelplus import lazy_gettext as _
- from flaskbb.user.models import User
- from flaskbb.utils.recaptcha import RecaptchaField
- USERNAME_RE = r'^[\w.+-]+$'
- is_username = regexp(USERNAME_RE,
- message=_("You can only use letters, numbers or dashes."))
- class LoginForm(Form):
- login = StringField(_("Username or E-Mail Address"), validators=[
- DataRequired(message=_("A Username or E-Mail Address is required."))]
- )
- password = PasswordField(_("Password"), validators=[
- DataRequired(message=_("A Password is required."))])
- recaptcha = RecaptchaField(_("Captcha"))
- remember_me = BooleanField(_("Remember Me"), default=False)
- submit = SubmitField(_("Login"))
- class RegisterForm(Form):
- username = StringField(_("Username"), validators=[
- DataRequired(message=_("A Username is required.")),
- is_username])
- email = StringField(_("E-Mail Address"), validators=[
- DataRequired(message=_("A E-Mail Address is required.")),
- Email(message=_("Invalid E-Mail Address."))])
- password = PasswordField(_('Password'), validators=[
- InputRequired(),
- EqualTo('confirm_password', message=_('Passwords must match.'))])
- confirm_password = PasswordField(_('Confirm Password'))
- recaptcha = RecaptchaField(_("Captcha"))
- language = SelectField(_('Language'))
- accept_tos = BooleanField(_("I accept the Terms of Service"), default=True)
- submit = SubmitField(_("Register"))
- def validate_username(self, field):
- user = User.query.filter_by(username=field.data).first()
- if user:
- raise ValidationError(_("This Username is already taken."))
- def validate_email(self, field):
- email = User.query.filter_by(email=field.data).first()
- if email:
- raise ValidationError(_("This E-Mail Address is already taken."))
- def save(self):
- user = User(username=self.username.data,
- email=self.email.data,
- password=self.password.data,
- date_joined=datetime.utcnow(),
- primary_group_id=4,
- language=self.language.data)
- return user.save()
- class ReauthForm(Form):
- password = PasswordField(_('Password'), validators=[
- DataRequired(message=_("A Password is required."))])
- submit = SubmitField(_("Refresh Login"))
- class ForgotPasswordForm(Form):
- email = StringField(_('E-Mail Address'), validators=[
- DataRequired(message=_("A E-Mail Address is reguired.")),
- Email()])
- recaptcha = RecaptchaField(_("Captcha"))
- submit = SubmitField(_("Request Password"))
- class ResetPasswordForm(Form):
- token = HiddenField('Token')
- email = StringField(_('E-Mail Address'), validators=[
- DataRequired(message=_("A E-Mail Address is required.")),
- Email()])
- password = PasswordField(_('Password'), validators=[
- InputRequired(),
- EqualTo('confirm_password', message=_('Passwords must match.'))])
- confirm_password = PasswordField(_('Confirm Password'))
- submit = SubmitField(_("Reset Password"))
- def validate_email(self, field):
- email = User.query.filter_by(email=field.data).first()
- if not email:
- raise ValidationError(_("Wrong E-Mail Address."))
- class RequestActivationForm(Form):
- username = StringField(_("Username"), validators=[
- DataRequired(message=_("A Username is required.")),
- is_username])
- email = StringField(_("E-Mail Address"), validators=[
- DataRequired(message=_("A E-Mail Address is required.")),
- Email(message=_("Invalid E-Mail Address."))])
- submit = SubmitField(_("Send Confirmation Mail"))
- def validate_email(self, field):
- self.user = User.query.filter_by(email=field.data).first()
- # check if the username matches the one found in the database
- if not self.user.username == self.username.data:
- raise ValidationError(_("Account does not exist."))
- if self.user.activated is not None:
- raise ValidationError(_("Account is already active."))
- class AccountActivationForm(Form):
- token = StringField(_("E-Mail Confirmation Token"), validators=[
- DataRequired(message=_("Please enter the token that we have sent to "
- "you."))
- ])
- submit = SubmitField(_("Confirm E-Mail"))
|