forms.py 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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.ext.wtf import Form
  10. from wtforms import TextAreaField, TextField, SelectMultipleField
  11. from wtforms.validators import Required, Optional, Length
  12. from flaskbb.forum.models import Topic, Post, Report, Forum, Category
  13. from flaskbb.user.models import User
  14. class QuickreplyForm(Form):
  15. content = TextAreaField("Quickreply", validators=[
  16. Required(message="You cannot post a reply without content.")])
  17. def save(self, user, topic):
  18. post = Post(**self.data)
  19. return post.save(user=user, topic=topic)
  20. class ReplyForm(Form):
  21. content = TextAreaField("Content", validators=[
  22. Required(message="You cannot post a reply without content.")])
  23. def save(self, user, topic):
  24. post = Post(**self.data)
  25. return post.save(user=user, topic=topic)
  26. class NewTopicForm(ReplyForm):
  27. title = TextField("Topic Title", validators=[
  28. Required(message="A topic title is required")])
  29. content = TextAreaField("Content", validators=[
  30. Required(message="You cannot post a reply without content.")])
  31. def save(self, user, forum):
  32. topic = Topic(title=self.title.data)
  33. post = Post(content=self.content.data)
  34. return topic.save(user=user, forum=forum, post=post)
  35. class ReportForm(Form):
  36. reason = TextAreaField("Reason", validators=[
  37. Required(message="Please insert a reason why you want to report this \
  38. post")
  39. ])
  40. def save(self, user, post):
  41. report = Report(**self.data)
  42. return report.save(user, post)
  43. class UserSearchForm(Form):
  44. search_query = TextField("Search", validators=[Optional(), Length(min=3, max=50)])
  45. def get_results(self):
  46. query = self.search_query.data
  47. return User.query.whoosh_search(query)
  48. class SearchPageForm(Form):
  49. search_query = TextField("Criteria", validators=[Required(), Length(min=3, max=50)])
  50. search_types = SelectMultipleField("Content", validators=[Required()], choices=[
  51. ('post', 'Post'), ('topic', 'Topic'), ('forum', 'Forum'), ('user', 'Users')])
  52. def get_results(self):
  53. # Because the DB is not yet initialized when this form is loaded, the query objects cannot be instantiated
  54. # in the class itself
  55. search_actions = {
  56. 'post': Post.query.whoosh_search,
  57. 'topic': Topic.query.whoosh_search,
  58. 'forum': Forum.query.whoosh_search,
  59. 'user': User.query.whoosh_search
  60. }
  61. query = self.search_query.data
  62. types = self.search_types.data
  63. results = {}
  64. for type in search_actions.keys():
  65. if type in types:
  66. results[type] = search_actions[type](query)
  67. return results