forms.py 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. # **************************************************************************
  4. # Copyright © 2016 jianglin
  5. # File Name: forms.py
  6. # Author: jianglin
  7. # Email: xiyang0807@gmail.com
  8. # Created: 2016-12-18 18:49:00 (CST)
  9. # Last Update:星期六 2017-3-25 21:47:25 (CST)
  10. # By:
  11. # Description:
  12. # **************************************************************************
  13. from flask import request, render_template, redirect, url_for
  14. from flask_wtf import Form
  15. from wtforms import (StringField, TextAreaField, SelectField, RadioField)
  16. from wtforms.validators import DataRequired, Length
  17. from flask_babelex import lazy_gettext as _
  18. from forums.api.forums.models import Board
  19. from .models import Topic
  20. def error_callback():
  21. return redirect(url_for('topic.ask'))
  22. def collect_error_callback():
  23. return redirect(url_for('topic.collectlist'))
  24. def form_board():
  25. form = TopicForm()
  26. results = []
  27. for b in Board.query.filter_by(parent_id=None):
  28. if b.parent is None:
  29. results.append((b.id, b.name))
  30. else:
  31. results.append((b.id, b.name + ' --' + b.parent.name))
  32. form.category.choices = results
  33. return form
  34. class TopicForm(Form):
  35. title = StringField(_('Title:'), [DataRequired(), Length(min=4, max=36)])
  36. content = TextAreaField(_('Content:'), [DataRequired(), Length(min=6)])
  37. category = SelectField(_('Category:'), coerce=int)
  38. tags = StringField(_('Tags:'), [DataRequired(), Length(min=2, max=36)])
  39. content_type = SelectField(
  40. _('ContentType'), choices=Topic.CONTENT_TYPE, coerce=str)
  41. class ReplyForm(Form):
  42. content = TextAreaField(_('Content:'), [DataRequired()])
  43. class CollectForm(Form):
  44. name = StringField(_('Name:'), [DataRequired()])
  45. description = TextAreaField(_('Description:'))
  46. private = RadioField(
  47. 'Private:', choices=[(0, 'privacy'), (1, 'public')], coerce=int)