forms.py 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. # -*- coding: utf-8 -*-
  2. """
  3. flaskbb.forum.forms
  4. ~~~~~~~~~~~~~~~~~~~
  5. It provides the forms that are needed for the forum views.
  6. :copyright: (c) 2014 by the FlaskBB Team.
  7. :license: BSD, see LICENSE for more details.
  8. """
  9. from flask_wtf import FlaskForm
  10. from wtforms import (TextAreaField, StringField, SelectMultipleField,
  11. BooleanField, SubmitField)
  12. from wtforms.validators import DataRequired, Optional, Length
  13. from flask_babelplus import lazy_gettext as _
  14. from flaskbb.forum.models import Topic, Post, Report, Forum
  15. from flaskbb.user.models import User
  16. class QuickreplyForm(FlaskForm):
  17. content = TextAreaField(_("Quick reply"), validators=[
  18. DataRequired(message=_("You cannot post a reply without content."))])
  19. submit = SubmitField(_("Reply"))
  20. def save(self, user, topic):
  21. post = Post(content=self.content.data)
  22. return post.save(user=user, topic=topic)
  23. class ReplyForm(FlaskForm):
  24. content = TextAreaField(_("Content"), validators=[
  25. DataRequired(message=_("You cannot post a reply without content."))])
  26. track_topic = BooleanField(_("Track this topic"), default=False,
  27. validators=[Optional()])
  28. submit = SubmitField(_("Reply"))
  29. preview = SubmitField(_("Preview"))
  30. def save(self, user, topic):
  31. post = Post(content=self.content.data)
  32. if self.track_topic.data:
  33. user.track_topic(topic)
  34. return post.save(user=user, topic=topic)
  35. class NewTopicForm(ReplyForm):
  36. title = StringField(_("Topic title"), validators=[
  37. DataRequired(message=_("Please choose a title for your topic."))])
  38. content = TextAreaField(_("Content"), validators=[
  39. DataRequired(message=_("You cannot post a reply without content."))])
  40. track_topic = BooleanField(_("Track this topic"), default=False,
  41. validators=[Optional()])
  42. submit = SubmitField(_("Post Topic"))
  43. preview = SubmitField(_("Preview"))
  44. def save(self, user, forum):
  45. topic = Topic(title=self.title.data)
  46. post = Post(content=self.content.data)
  47. if self.track_topic.data:
  48. user.track_topic(topic)
  49. return topic.save(user=user, forum=forum, post=post)
  50. class ReportForm(FlaskForm):
  51. reason = TextAreaField(_("Reason"), validators=[
  52. DataRequired(message=_("What is the reason for reporting this post?"))
  53. ])
  54. submit = SubmitField(_("Report post"))
  55. def save(self, user, post):
  56. report = Report(reason=self.reason.data)
  57. return report.save(post=post, user=user)
  58. class UserSearchForm(FlaskForm):
  59. search_query = StringField(_("Search"), validators=[
  60. DataRequired(), Length(min=3, max=50)
  61. ])
  62. submit = SubmitField(_("Search"))
  63. def get_results(self):
  64. query = self.search_query.data
  65. return User.query.whooshee_search(query)
  66. class SearchPageForm(FlaskForm):
  67. search_query = StringField(_("Criteria"), validators=[
  68. DataRequired(), Length(min=3, max=50)])
  69. search_types = SelectMultipleField(_("Content"), validators=[
  70. DataRequired()], choices=[('post', _('Post')), ('topic', _('Topic')),
  71. ('forum', _('Forum')), ('user', _('Users'))])
  72. submit = SubmitField(_("Search"))
  73. def get_results(self):
  74. # Because the DB is not yet initialized when this form is loaded,
  75. # the query objects cannot be instantiated in the class itself
  76. search_actions = {
  77. 'post': Post.query.whooshee_search,
  78. 'topic': Topic.query.whooshee_search,
  79. 'forum': Forum.query.whooshee_search,
  80. 'user': User.query.whooshee_search
  81. }
  82. query = self.search_query.data
  83. types = self.search_types.data
  84. results = {}
  85. for search_type in search_actions.keys():
  86. if search_type in types:
  87. results[search_type] = search_actions[search_type](query)
  88. return results