user.py 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. from flask.ext.wtf import Form
  2. from wtforms import TextField, PasswordField
  3. from wtforms.validators import (Required, Length, Email, ValidationError,
  4. EqualTo)
  5. from app.models import User
  6. class Unique(object):
  7. '''
  8. Custom validator to check an object's attribute
  9. is unique. For example users should not be able
  10. to create an account if the account's email
  11. address is already in the database. This class
  12. supposes you are using SQLAlchemy to query the
  13. database.
  14. '''
  15. def __init__(self, model, field, message):
  16. self.model = model
  17. self.field = field
  18. self.message = message
  19. def __call__(self, form, field):
  20. check = self.model.query.filter(self.field == field.data).first()
  21. if check:
  22. raise ValidationError(self.message)
  23. class Forgot(Form):
  24. ''' User forgot password form. '''
  25. email = TextField(validators=[Required(), Email()],
  26. description='Email address')
  27. class Reset(Form):
  28. ''' User reset password form. '''
  29. password = PasswordField(validators=[
  30. Required(), Length(min=6),
  31. EqualTo('confirm', message='Passwords must match.')
  32. ], description='Password')
  33. confirm = PasswordField(description='Confirm password')
  34. class Login(Form):
  35. ''' User login form. '''
  36. email = TextField(validators=[Required(), Email()],
  37. description='Email address')
  38. password = PasswordField(validators=[Required()],
  39. description='Password')
  40. class SignUp(Form):
  41. ''' User sign up form. '''
  42. first_name = TextField(validators=[Required(), Length(min=2)],
  43. description='Name')
  44. last_name = TextField(validators=[Required(), Length(min=2)],
  45. description='Surname')
  46. phone = TextField(validators=[Required(), Length(min=6)],
  47. description='Phone number')
  48. email = TextField(validators=[Required(), Email(),
  49. Unique(User, User.email,
  50. 'This email address is ' +
  51. 'already linked to an account.')],
  52. description='Email address')
  53. password = PasswordField(validators=[
  54. Required(), Length(min=6),
  55. EqualTo('confirm', message='Passwords must match.')
  56. ], description='Password')
  57. confirm = PasswordField(description='Confirm password')