forms.py 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. # -*- coding: utf-8 -*-
  2. """
  3. flaskbb.user.forms
  4. ~~~~~~~~~~~~~~~~~~
  5. It provides the forms that are needed for the user views.
  6. :copyright: (c) 2014 by the FlaskBB Team.
  7. :license: BSD, see LICENSE for more details.
  8. """
  9. import logging
  10. from flask_babelplus import lazy_gettext as _
  11. from wtforms import PasswordField, SelectField, StringField, SubmitField, TextAreaField
  12. from wtforms.validators import (
  13. URL,
  14. DataRequired,
  15. Email,
  16. EqualTo,
  17. InputRequired,
  18. Length,
  19. Optional,
  20. )
  21. from flaskbb.utils.fields import BirthdayField
  22. from flaskbb.utils.forms import FlaskBBForm
  23. from ..core.user.update import (
  24. EmailUpdate,
  25. PasswordUpdate,
  26. SettingsUpdate,
  27. UserDetailsChange,
  28. )
  29. logger = logging.getLogger(__name__)
  30. class GeneralSettingsForm(FlaskBBForm):
  31. # The choices for those fields will be generated in the user view
  32. # because we cannot access the current_app outside of the context
  33. language = SelectField(_("Language"))
  34. theme = SelectField(_("Theme"))
  35. submit = SubmitField(_("Save"))
  36. def as_change(self):
  37. return SettingsUpdate(language=self.language.data, theme=self.theme.data)
  38. class ChangeEmailForm(FlaskBBForm):
  39. old_email = StringField(
  40. _("Old email address"),
  41. validators=[
  42. DataRequired(message=_("A valid email address is required.")),
  43. Email(message=_("Invalid email address.")),
  44. ],
  45. )
  46. new_email = StringField(
  47. _("New email address"),
  48. validators=[
  49. InputRequired(),
  50. EqualTo("confirm_new_email", message=_("Email addresses must match.")),
  51. Email(message=_("Invalid email address.")),
  52. ],
  53. )
  54. confirm_new_email = StringField(
  55. _("Confirm email address"),
  56. validators=[Email(message=_("Invalid email address."))],
  57. )
  58. submit = SubmitField(_("Save"))
  59. def __init__(self, user, *args, **kwargs):
  60. self.user = user
  61. kwargs["obj"] = self.user
  62. super(ChangeEmailForm, self).__init__(*args, **kwargs)
  63. def as_change(self):
  64. return EmailUpdate(old_email=self.old_email.data, new_email=self.new_email.data)
  65. class ChangePasswordForm(FlaskBBForm):
  66. old_password = PasswordField(
  67. _("Password"),
  68. validators=[DataRequired(message=_("Please enter your password."))],
  69. )
  70. new_password = PasswordField(
  71. _("New password"),
  72. validators=[
  73. InputRequired(),
  74. EqualTo("confirm_new_password", message=_("New passwords must match.")),
  75. ],
  76. )
  77. confirm_new_password = PasswordField(_("Confirm new password"))
  78. submit = SubmitField(_("Save"))
  79. def as_change(self):
  80. return PasswordUpdate(
  81. new_password=self.new_password.data, old_password=self.old_password.data
  82. )
  83. class ChangeUserDetailsForm(FlaskBBForm):
  84. birthday = BirthdayField(_("Birthday"), format="%d %m %Y", validators=[Optional()])
  85. gender = StringField(_("Gender"), validators=[Optional()])
  86. location = StringField(_("Location"), validators=[Optional()])
  87. website = StringField(_("Website"), validators=[Optional(), URL()])
  88. avatar = StringField(_("Avatar"), validators=[Optional(), URL()])
  89. signature = TextAreaField(_("Forum Signature"), validators=[Optional()])
  90. notes = TextAreaField(_("Notes"), validators=[Optional(), Length(min=0, max=5000)])
  91. submit = SubmitField(_("Save"))
  92. def validate_birthday(self, field):
  93. if field.data is None:
  94. return True
  95. def as_change(self):
  96. return UserDetailsChange(
  97. birthday=self.birthday.data,
  98. gender=self.gender.data,
  99. location=self.location.data,
  100. website=self.website.data,
  101. avatar=self.avatar.data,
  102. signature=self.signature.data,
  103. notes=self.notes.data,
  104. )