|
@@ -11,27 +11,32 @@
|
|
|
import logging
|
|
|
|
|
|
from flask import current_app
|
|
|
-
|
|
|
from flask_babelplus import lazy_gettext as _
|
|
|
from flask_wtf import FlaskForm
|
|
|
-from flaskbb.forum.models import Forum, Post, Report, Topic
|
|
|
-from flaskbb.user.models import User
|
|
|
from wtforms import (BooleanField, SelectMultipleField, StringField,
|
|
|
SubmitField, TextAreaField)
|
|
|
from wtforms.validators import DataRequired, Length, Optional
|
|
|
|
|
|
+from flaskbb.forum.models import Forum, Post, Report, Topic
|
|
|
+from flaskbb.user.models import User
|
|
|
+from flaskbb.utils.helpers import time_utcnow
|
|
|
+
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
class PostForm(FlaskForm):
|
|
|
- content = TextAreaField(_("Content"), validators=[
|
|
|
- DataRequired(message=_("You cannot post a reply without content."))])
|
|
|
+ content = TextAreaField(
|
|
|
+ _("Content"),
|
|
|
+ validators=[
|
|
|
+ DataRequired(message=_("You cannot post a reply without content."))
|
|
|
+ ],
|
|
|
+ )
|
|
|
|
|
|
submit = SubmitField(_("Reply"))
|
|
|
|
|
|
def save(self, user, topic):
|
|
|
post = Post(content=self.content.data)
|
|
|
- current_app.pluggy.hook.flaskbb_form_new_post_save(form=self)
|
|
|
+ current_app.pluggy.hook.flaskbb_form_post_save(form=self)
|
|
|
return post.save(user=user, topic=topic)
|
|
|
|
|
|
|
|
@@ -40,27 +45,49 @@ class QuickreplyForm(PostForm):
|
|
|
|
|
|
|
|
|
class ReplyForm(PostForm):
|
|
|
- track_topic = BooleanField(_("Track this topic"), default=False,
|
|
|
- validators=[Optional()])
|
|
|
+ track_topic = BooleanField(
|
|
|
+ _("Track this topic"), default=False, validators=[Optional()]
|
|
|
+ )
|
|
|
+
|
|
|
+ def __init__(self, *args, **kwargs):
|
|
|
+ self.post = kwargs.get("obj", None)
|
|
|
+ PostForm.__init__(self, *args, **kwargs)
|
|
|
|
|
|
def save(self, user, topic):
|
|
|
+ # new post
|
|
|
+ if self.post is None:
|
|
|
+ self.post = Post(content=self.content.data)
|
|
|
+ else:
|
|
|
+ self.post.date_modified = time_utcnow()
|
|
|
+ self.post.modified_by = user.username
|
|
|
+
|
|
|
if self.track_topic.data:
|
|
|
user.track_topic(topic)
|
|
|
else:
|
|
|
user.untrack_topic(topic)
|
|
|
|
|
|
- return super(ReplyForm, self).save(user, topic)
|
|
|
+ current_app.pluggy.hook.flaskbb_form_post_save(form=self)
|
|
|
+ return self.post.save(user=user, topic=topic)
|
|
|
|
|
|
|
|
|
class TopicForm(FlaskForm):
|
|
|
- title = StringField(_("Topic title"), validators=[
|
|
|
- DataRequired(message=_("Please choose a title for your topic."))])
|
|
|
-
|
|
|
- content = TextAreaField(_("Content"), validators=[
|
|
|
- DataRequired(message=_("You cannot post a reply without content."))])
|
|
|
-
|
|
|
- track_topic = BooleanField(_("Track this topic"), default=False,
|
|
|
- validators=[Optional()])
|
|
|
+ title = StringField(
|
|
|
+ _("Topic title"),
|
|
|
+ validators=[
|
|
|
+ DataRequired(message=_("Please choose a title for your topic."))
|
|
|
+ ],
|
|
|
+ )
|
|
|
+
|
|
|
+ content = TextAreaField(
|
|
|
+ _("Content"),
|
|
|
+ validators=[
|
|
|
+ DataRequired(message=_("You cannot post a reply without content."))
|
|
|
+ ],
|
|
|
+ )
|
|
|
+
|
|
|
+ track_topic = BooleanField(
|
|
|
+ _("Track this topic"), default=False, validators=[Optional()]
|
|
|
+ )
|
|
|
|
|
|
submit = SubmitField(_("Post topic"))
|
|
|
|
|
@@ -72,8 +99,7 @@ class TopicForm(FlaskForm):
|
|
|
else:
|
|
|
user.untrack_topic(topic)
|
|
|
|
|
|
- current_app.pluggy.hook.flaskbb_form_new_topic_save(form=self,
|
|
|
- topic=topic)
|
|
|
+ current_app.pluggy.hook.flaskbb_form_topic_save(form=self, topic=topic)
|
|
|
return topic.save(user=user, forum=forum)
|
|
|
|
|
|
|
|
@@ -85,6 +111,10 @@ class EditTopicForm(TopicForm):
|
|
|
|
|
|
submit = SubmitField(_("Save topic"))
|
|
|
|
|
|
+ def __init__(self, *args, **kwargs):
|
|
|
+ self.topic = kwargs.get("obj").topic
|
|
|
+ TopicForm.__init__(self, *args, **kwargs)
|
|
|
+
|
|
|
def populate_obj(self, *objs):
|
|
|
"""
|
|
|
Populates the attributes of the passed `obj`s with data from the
|
|
@@ -94,11 +124,36 @@ class EditTopicForm(TopicForm):
|
|
|
for obj in objs:
|
|
|
super(EditTopicForm, self).populate_obj(obj)
|
|
|
|
|
|
+ def save(self, user, forum):
|
|
|
+ if self.track_topic.data:
|
|
|
+ user.track_topic(self.topic)
|
|
|
+ else:
|
|
|
+ user.untrack_topic(self.topic)
|
|
|
+
|
|
|
+ if (
|
|
|
+ self.topic.last_post_id == forum.last_post_id
|
|
|
+ and self.title.data != forum.last_post_title
|
|
|
+ ):
|
|
|
+ forum.last_post_title = self.title.data
|
|
|
+
|
|
|
+ self.topic.first_post.date_modified = time_utcnow()
|
|
|
+ self.topic.first_post.modified_by = user.username
|
|
|
+
|
|
|
+ current_app.pluggy.hook.flaskbb_form_topic_save(
|
|
|
+ form=self, topic=self.topic
|
|
|
+ )
|
|
|
+ return self.topic.save(user=user, forum=forum)
|
|
|
+
|
|
|
|
|
|
class ReportForm(FlaskForm):
|
|
|
- reason = TextAreaField(_("Reason"), validators=[
|
|
|
- DataRequired(message=_("What is the reason for reporting this post?"))
|
|
|
- ])
|
|
|
+ reason = TextAreaField(
|
|
|
+ _("Reason"),
|
|
|
+ validators=[
|
|
|
+ DataRequired(
|
|
|
+ message=_("What is the reason for reporting this post?")
|
|
|
+ )
|
|
|
+ ],
|
|
|
+ )
|
|
|
|
|
|
submit = SubmitField(_("Report post"))
|
|
|
|
|
@@ -108,9 +163,9 @@ class ReportForm(FlaskForm):
|
|
|
|
|
|
|
|
|
class UserSearchForm(FlaskForm):
|
|
|
- search_query = StringField(_("Search"), validators=[
|
|
|
- DataRequired(), Length(min=3, max=50)
|
|
|
- ])
|
|
|
+ search_query = StringField(
|
|
|
+ _("Search"), validators=[DataRequired(), Length(min=3, max=50)]
|
|
|
+ )
|
|
|
|
|
|
submit = SubmitField(_("Search"))
|
|
|
|
|
@@ -120,12 +175,20 @@ class UserSearchForm(FlaskForm):
|
|
|
|
|
|
|
|
|
class SearchPageForm(FlaskForm):
|
|
|
- search_query = StringField(_("Criteria"), validators=[
|
|
|
- DataRequired(), Length(min=3, max=50)])
|
|
|
-
|
|
|
- search_types = SelectMultipleField(_("Content"), validators=[
|
|
|
- DataRequired()], choices=[('post', _('Post')), ('topic', _('Topic')),
|
|
|
- ('forum', _('Forum')), ('user', _('Users'))])
|
|
|
+ search_query = StringField(
|
|
|
+ _("Criteria"), validators=[DataRequired(), Length(min=3, max=50)]
|
|
|
+ )
|
|
|
+
|
|
|
+ search_types = SelectMultipleField(
|
|
|
+ _("Content"),
|
|
|
+ validators=[DataRequired()],
|
|
|
+ choices=[
|
|
|
+ ("post", _("Post")),
|
|
|
+ ("topic", _("Topic")),
|
|
|
+ ("forum", _("Forum")),
|
|
|
+ ("user", _("Users")),
|
|
|
+ ],
|
|
|
+ )
|
|
|
|
|
|
submit = SubmitField(_("Search"))
|
|
|
|
|
@@ -133,10 +196,10 @@ class SearchPageForm(FlaskForm):
|
|
|
# Because the DB is not yet initialized when this form is loaded,
|
|
|
# the query objects cannot be instantiated in the class itself
|
|
|
search_actions = {
|
|
|
- 'post': Post.query.whooshee_search,
|
|
|
- 'topic': Topic.query.whooshee_search,
|
|
|
- 'forum': Forum.query.whooshee_search,
|
|
|
- 'user': User.query.whooshee_search
|
|
|
+ "post": Post.query.whooshee_search,
|
|
|
+ "topic": Topic.query.whooshee_search,
|
|
|
+ "forum": Forum.query.whooshee_search,
|
|
|
+ "user": User.query.whooshee_search,
|
|
|
}
|
|
|
|
|
|
query = self.search_query.data
|