<commit_before># -*- coding: utf-8 -*-
"""
    flaskbb.forum.forms
    ~~~~~~~~~~~~~~~~~~~~

    It provides the forms that are needed for the forum views.

    :copyright: (c) 2014 by the FlaskBB Team.
    :license: BSD, see LICENSE for more details.
"""
from flask.ext.wtf import Form
from wtforms import TextAreaField, TextField, SelectMultipleField
from wtforms.validators import Required, Optional, Length

from flaskbb.forum.models import Topic, Post, Report, Forum
from flaskbb.user.models import User


class QuickreplyForm(Form):
    content = TextAreaField("Quickreply", validators=[
        Required(message="You cannot post a reply without content.")])

    def save(self, user, topic):
        post = Post(**self.data)
        return post.save(user=user, topic=topic)


class ReplyForm(Form):
    content = TextAreaField("Content", validators=[
        Required(message="You cannot post a reply without content.")])

    def save(self, user, topic):
        post = Post(**self.data)
        return post.save(user=user, topic=topic)


class NewTopicForm(ReplyForm):
    title = TextField("Topic Title", validators=[
        Required(message="A topic title is required")])
    content = TextAreaField("Content", validators=[
        Required(message="You cannot post a reply without content.")])

    def save(self, user, forum):
        topic = Topic(title=self.title.data)
        post = Post(content=self.content.data)
        return topic.save(user=user, forum=forum, post=post)


class ReportForm(Form):
    reason = TextAreaField("Reason", validators=[
        Required(message="Please insert a reason why you want to report this \
                          post")
    ])

    def save(self, user, post):
        report = Report(**self.data)
        return report.save(user, post)


class UserSearchForm(Form):
    search_query = TextField("Search", validators=[
        Optional(), Length(min=3, max=50)
    ])

    def get_results(self):
        query = self.search_query.data
        return User.query.whoosh_search(query)


class SearchPageForm(Form):
    search_query = TextField("Criteria", validators=[
        Required(), Length(min=3, max=50)])

    search_types = SelectMultipleField("Content", validators=[
        Required()], choices=[('post', 'Post'), ('topic', 'Topic'),
                              ('forum', 'Forum'), ('user', 'Users')])

    def get_results(self):
        # 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.whoosh_search,
            'topic': Topic.query.whoosh_search,
            'forum': Forum.query.whoosh_search,
            'user': User.query.whoosh_search
        }

        query = self.search_query.data
        types = self.search_types.data
        results = {}

        for search_type in search_actions.keys():
            if search_type in types:
                results[search_type] = search_actions[search_type](query)

        return results
<commit_msg>Added option to track the topic upon creation<commit_after># -*- coding: utf-8 -*-
"""
    flaskbb.forum.forms
    ~~~~~~~~~~~~~~~~~~~~

    It provides the forms that are needed for the forum views.

    :copyright: (c) 2014 by the FlaskBB Team.
    :license: BSD, see LICENSE for more details.
"""
from flask.ext.wtf import Form
from wtforms import TextAreaField, TextField, SelectMultipleField, BooleanField
from wtforms.validators import Required, Optional, Length

from flaskbb.forum.models import Topic, Post, Report, Forum
from flaskbb.user.models import User


class QuickreplyForm(Form):
    content = TextAreaField("Quickreply", validators=[
        Required(message="You cannot post a reply without content.")])

    def save(self, user, topic):
        post = Post(**self.data)
        return post.save(user=user, topic=topic)


class ReplyForm(Form):
    content = TextAreaField("Content", validators=[
        Required(message="You cannot post a reply without content.")])

    def save(self, user, topic):
        post = Post(**self.data)
        return post.save(user=user, topic=topic)


class NewTopicForm(ReplyForm):
    title = TextField("Topic Title", validators=[
        Required(message="A topic title is required")])

    content = TextAreaField("Content", validators=[
        Required(message="You cannot post a reply without content.")])

    track_topic = BooleanField("Track this topic", default=False, validators=[
        Optional()])

    def save(self, user, forum):
        topic = Topic(title=self.title.data)
        post = Post(content=self.content.data)

        if self.track_topic.data:
            user.track_topic(topic)
        return topic.save(user=user, forum=forum, post=post)


class ReportForm(Form):
    reason = TextAreaField("Reason", validators=[
        Required(message="Please insert a reason why you want to report this \
                          post")
    ])

    def save(self, user, post):
        report = Report(**self.data)
        return report.save(user, post)


class UserSearchForm(Form):
    search_query = TextField("Search", validators=[
        Optional(), Length(min=3, max=50)
    ])

    def get_results(self):
        query = self.search_query.data
        return User.query.whoosh_search(query)


class SearchPageForm(Form):
    search_query = TextField("Criteria", validators=[
        Required(), Length(min=3, max=50)])

    search_types = SelectMultipleField("Content", validators=[
        Required()], choices=[('post', 'Post'), ('topic', 'Topic'),
                              ('forum', 'Forum'), ('user', 'Users')])

    def get_results(self):
        # 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.whoosh_search,
            'topic': Topic.query.whoosh_search,
            'forum': Forum.query.whoosh_search,
            'user': User.query.whoosh_search
        }

        query = self.search_query.data
        types = self.search_types.data
        results = {}

        for search_type in search_actions.keys():
            if search_type in types:
                results[search_type] = search_actions[search_type](query)

        return results

{'fraction_non_alphanumeric': 0.12053501180173092, 'fraction_numerical': 0.0034618410700236035, 'mean_word_length': 2.899386503067485, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1839676', 'n_tokens_mistral': 1802, 'n_tokens_neox': 1716, 'n_words': 506}