forms.py 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. # **************************************************************************
  4. # Copyright © 2017 jianglin
  5. # File Name: forms.py
  6. # Author: jianglin
  7. # Email: xiyang0807@gmail.com
  8. # Created: 2017-03-28 12:53:02 (CST)
  9. # Last Update: Monday 2019-05-06 23:36:53 (CST)
  10. # By:
  11. # Description:
  12. # **************************************************************************
  13. from flask import redirect, session, url_for
  14. from flask_babel import lazy_gettext as _
  15. from flask_wtf import FlaskForm as Form
  16. from flask_wtf.file import FileAllowed, FileField, FileRequired
  17. from wtforms import (BooleanField, PasswordField, RadioField, SelectField,
  18. StringField, TextAreaField)
  19. from wtforms.validators import DataRequired, Email, EqualTo, Length
  20. from forums.api.forums.db import Board
  21. from forums.api.topic.db import Topic
  22. from forums.api.user.db import UserSetting
  23. def error_callback():
  24. return redirect(url_for('topic.ask'))
  25. def collect_error_callback():
  26. return redirect(url_for('collect.list'))
  27. def form_board():
  28. form = TopicForm()
  29. results = []
  30. for b in Board.query.all():
  31. if b.parent is None:
  32. results.append((b.id, b.name))
  33. else:
  34. results.append((b.id, b.name + ' --' + b.parent.name))
  35. form.category.choices = results
  36. return form
  37. class BaseForm(Form):
  38. username = StringField(
  39. _('Username:'), [DataRequired(), Length(min=4, max=20)])
  40. password = PasswordField(
  41. _('Password:'), [DataRequired(), Length(min=4, max=20)])
  42. captcha = StringField(
  43. _('Captcha:'), [DataRequired(), Length(min=4, max=4)])
  44. def validate(self):
  45. rv = Form.validate(self)
  46. if not rv:
  47. return False
  48. captcha = session['captcha']
  49. captcha_data = self.captcha.data
  50. if captcha_data.lower() != captcha.lower():
  51. self.captcha.errors.append(_('The captcha is error'))
  52. return False
  53. return True
  54. class RegisterForm(BaseForm):
  55. email = StringField(_('Email:'), [DataRequired(), Email()])
  56. class LoginForm(BaseForm):
  57. remember = BooleanField(_('Remember me'), default=False)
  58. WITHIN = [(0, _('All Topics')), (1, _('One Day')), (2, _('One Week')),
  59. (3, _('One Month')), (4, _('One Year'))]
  60. ORDERBY = [(0, _('Publish')), (1, _('Author'))]
  61. DESC = [(0, _('Desc')), (1, _('Asc'))]
  62. class SortForm(Form):
  63. within = SelectField(_('Choice'), coerce=int, choices=WITHIN)
  64. orderby = SelectField('orderby', coerce=int, choices=ORDERBY)
  65. desc = SelectField('Up and Down', coerce=int, choices=DESC)
  66. class MessageForm(Form):
  67. message = TextAreaField(_('message'), validators=[DataRequired()])
  68. class TopicForm(Form):
  69. title = StringField(_('Title:'), [DataRequired(), Length(min=4, max=36)])
  70. content = TextAreaField(_('Content:'), [DataRequired(), Length(min=6)])
  71. category = SelectField(_('Category:'), coerce=int)
  72. tags = StringField(_('Tags:'), [DataRequired(), Length(min=2, max=36)])
  73. content_type = SelectField(
  74. _('ContentType'), choices=Topic.CONTENT_TYPE, coerce=str)
  75. class ReplyForm(Form):
  76. content = TextAreaField(_('Content:'), [DataRequired()])
  77. class CollectForm(Form):
  78. name = StringField(_('Name:'), [DataRequired()])
  79. description = TextAreaField(_('Description:'))
  80. is_hidden = RadioField(
  81. 'Is_hidden:',
  82. choices=[(0, _('is_hidden')), (1, _('is_public'))],
  83. coerce=int)
  84. choices = UserSetting.STATUS
  85. timezone = UserSetting.TIMEZONE
  86. locale = UserSetting.LOCALE
  87. class AvatarForm(Form):
  88. avatar = FileField(
  89. _('Upload Avatar:'),
  90. validators=[
  91. FileRequired(),
  92. FileAllowed(['jpg', 'png'], '上传文件只能为图片且图片格式为jpg,png')
  93. ])
  94. class PrivacyForm(Form):
  95. online_status = SelectField(
  96. _('Online status:'), coerce=str, choices=choices)
  97. topic_list = SelectField(_('Topic List:'), coerce=str, choices=choices)
  98. rep_list = SelectField(_('Reply List:'), coerce=str, choices=choices)
  99. ntb_list = SelectField(_('Notebook List:'), coerce=str, choices=choices)
  100. collect_list = SelectField(_('Collect List:'), coerce=str, choices=choices)
  101. class ProfileForm(Form):
  102. introduce = TextAreaField(_('Introduce:'), [Length(max=256)])
  103. school = StringField(_('School:'), [Length(max=256)])
  104. word = TextAreaField(_('Signature:'), [Length(max=256)])
  105. class PasswordForm(Form):
  106. old_password = PasswordField(
  107. _('Old Password:'),
  108. [DataRequired(), Length(min=4, max=20)])
  109. new_password = PasswordField(
  110. _('New Password:'),
  111. [DataRequired(), Length(min=4, max=20)])
  112. rnew_password = PasswordField(
  113. _('New Password again:'),
  114. [DataRequired(), EqualTo('new_password')])
  115. class BabelForm(Form):
  116. timezone = SelectField(_('Timezone:'), coerce=str, choices=timezone)
  117. locale = SelectField(_('Locale:'), coerce=str, choices=locale)