forms.py 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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:星期三 2017-3-29 22:51:17 (CST)
  10. # By:
  11. # Description:
  12. # **************************************************************************
  13. from flask import redirect, session, url_for
  14. from flask_babelex 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.models import Board
  21. from forums.api.topic.models import Topic
  22. from forums.api.user.models 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(
  40. min=4, max=20)])
  41. password = PasswordField(
  42. _('Password:'), [DataRequired(), Length(
  43. min=4, max=20)])
  44. captcha = StringField(
  45. _('Captcha:'), [DataRequired(), Length(
  46. min=4, max=4)])
  47. def validate(self):
  48. rv = Form.validate(self)
  49. if not rv:
  50. return False
  51. captcha = session['captcha']
  52. captcha_data = self.captcha.data
  53. if captcha_data.lower() != captcha.lower():
  54. self.captcha.errors.append(_('The captcha is error'))
  55. return False
  56. return True
  57. class RegisterForm(BaseForm):
  58. email = StringField(_('Email:'), [DataRequired(), Email()])
  59. class LoginForm(BaseForm):
  60. remember = BooleanField(_('Remember me'), default=False)
  61. WITHIN = [(0, _('All Topics')), (1, _('One Day')), (2, _('One Week')),
  62. (3, _('One Month'))]
  63. ORDERBY = [(0, _('Publish')), (1, _('Author'))]
  64. DESC = [(0, _('Desc')), (1, _('Asc'))]
  65. class SortForm(Form):
  66. within = SelectField(_('Choice'), coerce=int, choices=WITHIN)
  67. orderby = SelectField('orderby', coerce=int, choices=ORDERBY)
  68. desc = SelectField('Up and Down', coerce=int, choices=DESC)
  69. class SearchForm(Form):
  70. search = StringField(_('search'), validators=[DataRequired()])
  71. class MessageForm(Form):
  72. message = TextAreaField(_('message'), validators=[DataRequired()])
  73. class TopicForm(Form):
  74. title = StringField(_('Title:'), [DataRequired(), Length(min=4, max=36)])
  75. content = TextAreaField(_('Content:'), [DataRequired(), Length(min=6)])
  76. category = SelectField(_('Category:'), coerce=int)
  77. tags = StringField(_('Tags:'), [DataRequired(), Length(min=2, max=36)])
  78. content_type = SelectField(
  79. _('ContentType'), choices=Topic.CONTENT_TYPE, coerce=str)
  80. class ReplyForm(Form):
  81. content = TextAreaField(_('Content:'), [DataRequired()])
  82. class CollectForm(Form):
  83. name = StringField(_('Name:'), [DataRequired()])
  84. description = TextAreaField(_('Description:'))
  85. is_hidden = RadioField(
  86. 'Is_hidden:', choices=[(0, 'is_hidden'), (1, 'is_public')], coerce=int)
  87. choices = UserSetting.STATUS
  88. timezone = UserSetting.TIMEZONE
  89. locale = UserSetting.LOCALE
  90. class AvatarForm(Form):
  91. avatar = FileField(
  92. _('Upload Avatar:'),
  93. validators=[FileRequired(), FileAllowed(['jpg', 'png'],
  94. '上传文件只能为图片且图片格式为jpg,png')])
  95. class PrivacyForm(Form):
  96. online_status = SelectField(
  97. _('Online status:'), coerce=str, choices=choices)
  98. topic_list = SelectField(_('Topic List:'), coerce=str, choices=choices)
  99. rep_list = SelectField(_('Reply List:'), coerce=str, choices=choices)
  100. ntb_list = SelectField(_('Notebook List:'), coerce=str, choices=choices)
  101. collect_list = SelectField(_('Collect List:'), coerce=str, choices=choices)
  102. class ProfileForm(Form):
  103. introduce = TextAreaField(_('Introduce:'), [Length(max=256)])
  104. school = StringField(_('School:'), [Length(max=256)])
  105. word = TextAreaField(_('Signature:'), [Length(max=256)])
  106. class PasswordForm(Form):
  107. old_password = PasswordField(
  108. _('Old Password:'), [DataRequired(), Length(
  109. min=4, max=20)])
  110. new_password = PasswordField(
  111. _('New Password:'), [DataRequired(), Length(
  112. min=4, max=20)])
  113. rnew_password = PasswordField(
  114. _('New Password again:'), [DataRequired(), EqualTo('new_password')])
  115. class BabelForm(Form):
  116. timezone = SelectField(_('Timezone:'), coerce=str, choices=timezone)
  117. locale = SelectField(_('Locale:'), coerce=str, choices=locale)