Пример #1
0
def test_topic_update_read(database, user, topic):
    """Tests the update read method if the topic is unread/read."""
    forumsread = ForumsRead.query.\
        filter(ForumsRead.user_id == user.id,
               ForumsRead.forum_id == topic.forum_id).first()

    with current_app.test_request_context():
        # Test with logged in user
        login_user(user)
        assert current_user.is_authenticated

        # Update the tracker
        assert topic.update_read(current_user, topic.forum, forumsread)
        # Because the tracker is already up-to-date, it shouldn't update it
        # again.
        assert not topic.update_read(current_user, topic.forum, forumsread)

        # Adding a new post - now the tracker shouldn't be up-to-date anymore.
        post = Post(content="Test Content")
        post.save(topic=topic, user=user)

        forumsread = ForumsRead.query.\
            filter(ForumsRead.user_id == user.id,
                   ForumsRead.forum_id == topic.forum_id).first()

        # Test tracker length
        flaskbb_config["TRACKER_LENGTH"] = 0
        assert not topic.update_read(current_user, topic.forum, forumsread)
        flaskbb_config["TRACKER_LENGTH"] = 1
        assert topic.update_read(current_user, topic.forum, forumsread)

        # Test with logged out user
        logout_user()
        assert not current_user.is_authenticated
        assert not topic.update_read(current_user, topic.forum, forumsread)
Пример #2
0
def test_topic_update_read(database, user, topic):
    """Tests the update read method if the topic is unread/read."""
    forumsread = ForumsRead.query.\
        filter(ForumsRead.user_id == user.id,
               ForumsRead.forum_id == topic.forum_id).first()

    with current_app.test_request_context():
        # Test with logged in user
        login_user(user)
        assert current_user.is_authenticated

        # Update the tracker
        assert topic.update_read(current_user, topic.forum, forumsread)
        # Because the tracker is already up-to-date, it shouldn't update it
        # again.
        assert not topic.update_read(current_user, topic.forum, forumsread)

        # Adding a new post - now the tracker shouldn't be up-to-date anymore.
        post = Post(content="Test Content")
        post.save(topic=topic, user=user)

        forumsread = ForumsRead.query.\
            filter(ForumsRead.user_id == user.id,
                   ForumsRead.forum_id == topic.forum_id).first()

        # Test tracker length
        flaskbb_config["TRACKER_LENGTH"] = 0
        assert not topic.update_read(current_user, topic.forum, forumsread)
        flaskbb_config["TRACKER_LENGTH"] = 1
        assert topic.update_read(current_user, topic.forum, forumsread)

        # Test with logged out user
        logout_user()
        assert not current_user.is_authenticated
        assert not topic.update_read(current_user, topic.forum, forumsread)
Пример #3
0
def test_post_delete(topic):
    """Tests the delete post method with three different post types.
    The three types are:
        * First Post
        * A post between the first and last post (middle)
        * Last Post
    """
    post_middle = Post(content="Test Content Middle")
    post_middle.save(topic=topic, user=topic.user)

    post_last = Post(content="Test Content Last")
    post_last.save(topic=topic, user=topic.user)

    assert topic.post_count == 3
    assert topic.forum.post_count == 3
    assert topic.user.post_count == 3

    post_middle.delete()

    # Check the last posts
    assert topic.last_post == post_last
    assert topic.forum.last_post == post_last

    post_last.delete()

    # That was a bit trickier..
    assert topic.post_count == 1
    assert topic.forum.post_count == 1
    assert topic.user.post_count == 1
    assert topic.first_post_id == topic.last_post_id

    assert topic.forum.last_post_id == topic.last_post_id
Пример #4
0
def save_post_with_image_and_text(user, topic, image_url, text):
    post_content = ""
    post_content += text
    post_content += "\n"
    post_content += "\n"
    post_content += "![]({})".format(image_url)
    post = Post(content=post_content)
    post.save(user=user, topic=topic)
Пример #5
0
    def save(self, user, topic):
        post = Post(content=self.content.data)

        if self.track_topic.data:
            user.track_topic(topic)

        current_app.pluggy.hook.flaskbb_form_new_post_save(form=self)
        return post.save(user=user, topic=topic)
Пример #6
0
    def save(self, user, topic):
        post = Post(content=self.content.data)

        if self.track_topic.data:
            user.track_topic(topic)

        current_app.pluggy.hook.flaskbb_form_new_post_save(form=self)
        return post.save(user=user, topic=topic)
Пример #7
0
def test_forum_update_last_post(topic_normal, normal_user):
    post = Post(content="Test Content 2")
    post.save(topic=topic_normal, user=normal_user)

    assert topic_normal.forum.last_post == post

    post.delete()

    topic_normal.forum.update_last_post()

    assert topic_normal.forum.last_post == topic_normal.first_post
Пример #8
0
def test_forum_update_last_post(topic, user):
    """Test the update last post method."""
    post = Post(content="Test Content 2")
    post.save(topic=topic, user=user)

    assert topic.forum.last_post == post

    post.delete()

    topic.forum.update_last_post()

    assert topic.forum.last_post == topic.first_post
Пример #9
0
def insert_bulk_data(topic_count=10, post_count=100):
    """Creates a specified number of topics in the first forum with
    each topic containing a specified amount of posts.
    Returns the number of created topics and posts.

    :param topics: The amount of topics in the forum.
    :param posts: The number of posts in each topic.
    """
    user1 = User.query.filter_by(id=1).first()
    user2 = User.query.filter_by(id=2).first()
    forum = Forum.query.filter_by(id=1).first()

    last_post = Post.query.order_by(Post.id.desc()).first()
    last_post_id = 1 if last_post is None else last_post.id

    created_posts = 0
    created_topics = 0
    posts = []

    if not (user1 or user2 or forum):
        return False

    with db.session.no_autoflush:
        for i in range(1, topic_count + 1):
            last_post_id += 1

            # create a topic
            topic = Topic(title="Test Title %s" % i)
            post = Post(content="First Post")
            topic.save(post=post, user=user1, forum=forum)
            created_topics += 1

            # create some posts in the topic
            for _ in range(1, post_count + 1):
                last_post_id += 1
                post = Post(content="Some other Post",
                            user=user2,
                            topic=topic.id)
                topic.last_updated = post.date_created
                topic.post_count += 1

                created_posts += 1
                posts.append(post)

        db.session.bulk_save_objects(posts)

    # and finally, lets update some stats
    forum.recalculate(last_post=True)
    user1.recalculate()
    user2.recalculate()

    return created_topics, created_posts
Пример #10
0
def test_forum_update_read(database, user, topic):
    """Test the update read method."""
    forumsread = ForumsRead.query.\
        filter(ForumsRead.user_id == user.id,
               ForumsRead.forum_id == topic.forum_id).first()

    topicsread = TopicsRead.query.\
        filter(TopicsRead.user_id == user.id,
               TopicsRead.topic_id == topic.id).first()

    forum = topic.forum

    with current_app.test_request_context():
        # Test with logged in user
        login_user(user)

        # Should return False because topicsread is None
        assert not forum.update_read(current_user, forumsread, topicsread)

        # This is the first time the user visits the topic
        topicsread = TopicsRead()
        topicsread.user_id = user.id
        topicsread.topic_id = topic.id
        topicsread.forum_id = topic.forum_id
        topicsread.last_read = datetime.utcnow()
        topicsread.save()

        # hence, we also need to create a new forumsread entry
        assert forum.update_read(current_user, forumsread, topicsread)

        forumsread = ForumsRead.query.\
            filter(ForumsRead.user_id == user.id,
                   ForumsRead.forum_id == topic.forum_id).first()

        # everything should be up-to-date now
        assert not forum.update_read(current_user, forumsread, topicsread)

        post = Post(content="Test Content")
        post.save(user=user, topic=topic)

        # Updating the topicsread tracker
        topicsread.last_read = datetime.utcnow()
        topicsread.save()

        # now the forumsread tracker should also need a update
        assert forum.update_read(current_user, forumsread, topicsread)

        logout_user()
        # should fail because the user is logged out
        assert not forum.update_read(current_user, forumsread, topicsread)
Пример #11
0
def test_forum_update_read(database, user, topic):
    """Test the update read method."""
    forumsread = ForumsRead.query.\
        filter(ForumsRead.user_id == user.id,
               ForumsRead.forum_id == topic.forum_id).first()

    topicsread = TopicsRead.query.\
        filter(TopicsRead.user_id == user.id,
               TopicsRead.topic_id == topic.id).first()

    forum = topic.forum

    with current_app.test_request_context():
        # Test with logged in user
        login_user(user)

        # Should return False because topicsread is None
        assert not forum.update_read(current_user, forumsread, topicsread)

        # This is the first time the user visits the topic
        topicsread = TopicsRead()
        topicsread.user_id = user.id
        topicsread.topic_id = topic.id
        topicsread.forum_id = topic.forum_id
        topicsread.last_read = datetime.utcnow()
        topicsread.save()

        # hence, we also need to create a new forumsread entry
        assert forum.update_read(current_user, forumsread, topicsread)

        forumsread = ForumsRead.query.\
            filter(ForumsRead.user_id == user.id,
                   ForumsRead.forum_id == topic.forum_id).first()

        # everything should be up-to-date now
        assert not forum.update_read(current_user, forumsread, topicsread)

        post = Post(content="Test Content")
        post.save(user=user, topic=topic)

        # Updating the topicsread tracker
        topicsread.last_read = datetime.utcnow()
        topicsread.save()

        # now the forumsread tracker should also need a update
        assert forum.update_read(current_user, forumsread, topicsread)

        logout_user()
        # should fail because the user is logged out
        assert not forum.update_read(current_user, forumsread, topicsread)
Пример #12
0
def insert_mass_data(topics=100, posts=100):
    """Creates 100 topics in the first forum and each topic has 100 posts.
    Returns ``True`` if the topics and posts were successfully created.

    :param topics: The amount of topics in the forum.
    :param posts: The number of posts in each topic.
    """
    user1 = User.query.filter_by(id=1).first()
    user2 = User.query.filter_by(id=2).first()
    forum = Forum.query.filter_by(id=1).first()

    if not user1 or user2 or forum:
        raise "Please make sure that there are at least 2 users and 1 forum."

    # create 1000 topics
    for i in range(1, topics + 1):

        # create a topic
        topic = Topic()
        post = Post()

        topic.title = "Test Title %s" % i
        post.content = "Test Content"
        topic.save(post=post, user=user1, forum=forum)

        # create 100 posts in each topic
        for j in range(1, posts):
            post = Post()
            post.content = "Test Post"
            post.save(user=user2, topic=topic)
Пример #13
0
    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)

        current_app.pluggy.hook.flaskbb_form_post_save(form=self,
                                                       post=self.post)
        return self.post.save(user=user, topic=topic)
Пример #14
0
class ReplyForm(PostForm):
    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)

        current_app.pluggy.hook.flaskbb_form_post_save(
            form=self, post=self.post
        )
        return self.post.save(user=user, topic=topic)
Пример #15
0
class ReplyForm(PostForm):
    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)

        current_app.pluggy.hook.flaskbb_form_post_save(form=self,
                                                       post=self.post)
        return self.post.save(user=user, topic=topic)
Пример #16
0
def test_topic_save(forum, user):
    """Test the save topic method with creating and editing a topic."""
    post = Post(content="Test Content")
    topic = Topic(title="Test Title")

    assert forum.last_post_id is None
    assert forum.post_count == 0
    assert forum.topic_count == 0

    topic.save(forum=forum, post=post, user=user)

    assert topic.title == "Test Title"

    topic.title = "Test Edit Title"
    topic.save()

    assert topic.title == "Test Edit Title"

    # The first post in the topic is also the last post
    assert topic.first_post_id == post.id
    assert topic.last_post_id == post.id

    assert forum.last_post_id == post.id
    assert forum.post_count == 1
    assert forum.topic_count == 1
Пример #17
0
    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)
Пример #18
0
def save_thread(user, forum):

    thread_name, thread_id = generate_thread_name(forum)
    text_model = get_thread_model(forum, thread_id)

    post_content = text_model.make_sentence()
    post = Post(content=post_content)
    topic = Topic(title=thread_name)
    topic.save(user=user, forum=forum, post=post, thread_id=thread_id)
Пример #19
0
    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)

        current_app.pluggy.hook.flaskbb_form_new_topic_save(form=self)
        return topic.save(user=user, forum=forum, post=post)
Пример #20
0
def save_thread(user, forum, subreddit):

    thread_name = generate_title(subreddit)

    text_model = utils.load_model(model_fname(subreddit, 'post'))
    post_content = utils.generate_body(text_model)

    post = Post(content=post_content)
    thread = Topic(title=thread_name)
    thread.save(user=user, forum=forum, post=post)
Пример #21
0
def test_topic_merge_other_forum(topic):
    """You cannot merge a topic with a topic from another forum."""
    forum_other = Forum(title="Test Forum 2", category_id=1)
    forum_other.save()

    topic_other = Topic(title="Test Topic 2")
    post_other = Post(content="Test Content 2")
    topic_other.save(user=topic.user, forum=forum_other, post=post_other)

    assert not topic.merge(topic_other)
Пример #22
0
def create_test_data():

    create_default_groups()

    # create 5 users
    for u in range(1, 6):
        username = "******" % u
        email = "*****@*****.**" % u
        user = User(username=username, password="******", email=email)
        user.primary_group_id = u
        user.save()

    # create a category
    category = Forum(is_category=True, title="Test Category",
                     description="Test Description")
    category.save()

    # create 2 forums in the category
    for i in range(1, 3):
        forum_title = "Test Forum %s " % i
        forum = Forum(title=forum_title, description="Test Description",
                      parent_id=category.id)
        forum.save()

        # Create a subforum
        subforum_title = "Test Subforum %s " % i
        subforum = Forum(title=subforum_title,
                         description="Test Description", parent_id=forum.id)
        subforum.save()

    user = User.query.filter_by(id=1).first()

    # create 1 topic in each forum
    for i in range(2, 6):  # Forum ids are not sequential because categories.
        forum = Forum.query.filter_by(id=i).first()

        topic = Topic()
        post = Post()

        topic.title = "Test Title %s" % (i-1)
        post.content = "Test Content"
        topic.save(user=user, forum=forum, post=post)
Пример #23
0
def create_test_data():
    """Creates 5 users, 2 categories and 2 forums in each category.
    It also creates a new topic topic in each forum with a post.
    """
    create_default_groups()
    create_default_settings()

    # create 5 users
    for u in range(1, 6):
        username = "******" % u
        email = "*****@*****.**" % u
        user = User(username=username, password="******", email=email)
        user.primary_group_id = u
        user.save()

    user1 = User.query.filter_by(id=1).first()
    user2 = User.query.filter_by(id=2).first()

    # create 2 categories
    for i in range(1, 3):
        category_title = "Test Category %s" % i
        category = Category(title=category_title,
                            description="Test Description")
        category.save()

        # create 2 forums in each category
        for j in range(1, 3):
            if i == 2:
                j += 2

            forum_title = "Test Forum %s %s" % (j, i)
            forum = Forum(title=forum_title,
                          description="Test Description",
                          category_id=i)
            forum.save()

            # create a topic
            topic = Topic()
            post = Post()

            topic.title = "Test Title %s" % j
            post.content = "Test Content"
            topic.save(post=post, user=user1, forum=forum)

            # create a second post in the forum
            post = Post()
            post.content = "Test Post"
            post.save(user=user2, topic=topic)
Пример #24
0
def test_topic_tracker_needs_update(database, user, topic):
    """Tests if the topicsread tracker needs an update if a new post has been
    submitted.
    """
    forumsread = ForumsRead.query.\
        filter(ForumsRead.user_id == user.id,
               ForumsRead.forum_id == topic.forum_id).first()

    topicsread = TopicsRead.query.\
        filter(TopicsRead.user_id == user.id,
               TopicsRead.topic_id == topic.id).first()

    with current_app.test_request_context():
        assert topic.tracker_needs_update(forumsread, topicsread)

        # Update the tracker
        topicsread = TopicsRead()
        topicsread.user_id = user.id
        topicsread.topic_id = topic.id
        topicsread.forum_id = topic.forum_id
        topicsread.last_read = datetime.utcnow()
        topicsread.save()

        forumsread = ForumsRead()
        forumsread.user_id = user.id
        forumsread.forum_id = topic.forum_id
        forumsread.last_read = datetime.utcnow()
        forumsread.save()

        # Now the topic should be read
        assert not topic.tracker_needs_update(forumsread, topicsread)

        post = Post(content="Test Content")
        post.save(topic=topic, user=user)

        assert topic.tracker_needs_update(forumsread, topicsread)
Пример #25
0
def test_hiding_post_updates_counts(forum, topic, user):
    new_post = Post(content='spam')
    new_post.save(user=user, topic=topic)
    new_post.hide(user)
    assert user.post_count == 1
    assert topic.post_count == 1
    assert forum.post_count == 1
    assert topic.last_post != new_post
    assert forum.last_post != new_post
    assert new_post.hidden_by == user
    new_post.unhide()
    assert topic.post_count == 2
    assert user.post_count == 2
    assert forum.post_count == 2
    assert topic.last_post == new_post
    assert forum.last_post == new_post
    assert new_post.hidden_by is None
Пример #26
0
def test_topic_tracker_needs_update(database, user, topic):
    """Tests if the topicsread tracker needs an update if a new post has been
    submitted.
    """
    forumsread = ForumsRead.query.\
        filter(ForumsRead.user_id == user.id,
               ForumsRead.forum_id == topic.forum_id).first()

    topicsread = TopicsRead.query.\
        filter(TopicsRead.user_id == user.id,
               TopicsRead.topic_id == topic.id).first()

    with current_app.test_request_context():
        assert topic.tracker_needs_update(forumsread, topicsread)

        # Update the tracker
        topicsread = TopicsRead()
        topicsread.user_id = user.id
        topicsread.topic_id = topic.id
        topicsread.forum_id = topic.forum_id
        topicsread.last_read = datetime.utcnow()
        topicsread.save()

        forumsread = ForumsRead()
        forumsread.user_id = user.id
        forumsread.forum_id = topic.forum_id
        forumsread.last_read = datetime.utcnow()
        forumsread.save()

        # Now the topic should be read
        assert not topic.tracker_needs_update(forumsread, topicsread)

        post = Post(content="Test Content")
        post.save(topic=topic, user=user)

        assert topic.tracker_needs_update(forumsread, topicsread)
Пример #27
0
def test_retrieving_hidden_posts(topic, user):
    new_post = Post(content='stuff')
    new_post.save(user, topic)
    new_post.hide(user)

    assert Post.query.get(new_post.id) is None
    assert Post.query.get(new_post.id, include_hidden=True) == new_post
    assert Post.query.filter(Post.id == new_post.id).first() is None
    assert Post.query.with_hidden().filter(
        Post.id == new_post.id).first() == new_post
Пример #28
0
    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)

        current_app.pluggy.hook.flaskbb_form_post_save(
            form=self, post=self.post
        )
        return self.post.save(user=user, topic=topic)
Пример #29
0
def test_forum_update_last_post(topic, user):
    """Test the update last post method."""
    post = Post(content="Test Content 2")
    post.save(topic=topic, user=user)

    assert topic.forum.last_post == post

    post.delete()

    topic.forum.update_last_post()

    assert topic.forum.last_post == topic.first_post
Пример #30
0
def test_post_save(topic, user):
    """Tests the save post method."""
    post = Post(content="Test Content")
    post.save(topic=topic, user=user)

    assert post.content == "Test Content"

    post.content = "Test Edit Content"
    post.save()

    assert post.content == "Test Edit Content"

    assert topic.user.post_count == 2
    assert topic.post_count == 2
    assert topic.last_post == post
    assert topic.forum.post_count == 2
Пример #31
0
def test_topic_merge(topic):
    """Tests the topic merge method."""
    topic_other = Topic(title="Test Topic Merge")
    post = Post(content="Test Content Merge")
    topic_other.save(post=post, user=topic.user, forum=topic.forum)

    # Save the last_post_id in another variable because topic_other will be
    # overwritten later
    last_post_other = topic_other.last_post_id

    assert topic_other.merge(topic)

    # I just want to be sure that the topic is deleted
    topic_other = Topic.query.filter_by(id=topic_other.id).first()
    assert topic_other is None

    assert topic.post_count == 2
    assert topic.last_post_id == last_post_other
Пример #32
0
def create_welcome_forum():
    """This will create the `welcome forum` with a welcome topic."""

    if User.query.count() < 1:
        raise "You need to create the admin user first!"

    user = User.query.filter_by(id=1).first()

    category = Category(title="My Category", position=1)
    category.save()

    forum = Forum(title="Welcome",
                  description="Your first forum",
                  category_id=category.id)
    forum.save()

    topic = Topic(title="Welcome!")
    post = Post(content="Have fun with your new FlaskBB Forum!")

    topic.save(user=user, forum=forum, post=post)
Пример #33
0
def create_test_data():
    """
    Creates 5 users, 2 categories and 2 forums in each category. It also opens
    a new topic topic in each forum with a post.
    """
    create_default_groups()
    create_default_settings()

    # create 5 users
    for u in range(1, 6):
        username = "******" % u
        email = "*****@*****.**" % u
        user = User(username=username, password="******", email=email)
        user.primary_group_id = u
        user.save()

    user1 = User.query.filter_by(id=1).first()
    user2 = User.query.filter_by(id=2).first()

    # create 2 categories
    for i in range(1, 3):
        category_title = "Test Category %s" % i
        category = Category(title=category_title,
                            description="Test Description")
        category.save()

        # create 2 forums in each category
        for j in range(1, 3):
            if i == 2:
                j += 2

            forum_title = "Test Forum %s %s" % (j, i)
            forum = Forum(title=forum_title, description="Test Description",
                          category_id=i)
            forum.save()

            # create a topic
            topic = Topic()
            post = Post()

            topic.title = "Test Title %s" % j
            post.content = "Test Content"
            topic.save(post=post, user=user1, forum=forum)

            # create a second post in the forum
            post = Post()
            post.content = "Test Post"
            post.save(user=user2, topic=topic)
Пример #34
0
def create_welcome_forum():
    """This will create the `welcome forum` with a welcome topic.
    Returns True if it's created successfully.
    """
    if User.query.count() < 1:
        return False

    user = User.query.filter_by(id=1).first()

    category = Category(title="My Category", position=1)
    category.save()

    forum = Forum(title="Welcome", description="Your first forum",
                  category_id=category.id)
    forum.save()

    topic = Topic(title="Welcome!")
    post = Post(content="Have fun with your new FlaskBB Forum!")

    topic.save(user=user, forum=forum, post=post)
    return True
Пример #35
0
def insert_mass_data(topics=100, posts=100):
    """Creates a few topics in the first forum and each topic has
    a few posts. WARNING: This might take very long!
    Returns the count of created topics and posts.

    :param topics: The amount of topics in the forum.
    :param posts: The number of posts in each topic.
    """
    user1 = User.query.filter_by(id=1).first()
    user2 = User.query.filter_by(id=2).first()
    forum = Forum.query.filter_by(id=1).first()

    created_posts = 0
    created_topics = 0

    if not (user1 or user2 or forum):
        return False

    # create 1000 topics
    for i in range(1, topics + 1):

        # create a topic
        topic = Topic()
        post = Post()

        topic.title = "Test Title %s" % i
        post.content = "Test Content"
        topic.save(post=post, user=user1, forum=forum)
        created_topics += 1

        # create 100 posts in each topic
        for j in range(1, posts + 1):
            post = Post()
            post.content = "Test Post"
            post.save(user=user2, topic=topic)
            created_posts += 1

    return created_topics, created_posts
Пример #36
0
def test_post_save(topic, user):
    """Tests the save post method."""
    post = Post(content="Test Content")
    post.save(topic=topic, user=user)

    assert post.content == "Test Content"

    post.content = "Test Edit Content"
    post.save()

    assert post.content == "Test Edit Content"

    assert topic.user.post_count == 2
    assert topic.post_count == 2
    assert topic.last_post == post
    assert topic.forum.post_count == 2
Пример #37
0
def insert_mass_data():
    """
    Creates 100 topics in the first forum and each topic has 100 posts.
    """
    user1 = User.query.filter_by(id=1).first()
    user2 = User.query.filter_by(id=2).first()
    forum = Forum.query.filter_by(id=1).first()

    # create 1000 topics
    for i in range(1, 101):

        # create a topic
        topic = Topic()
        post = Post()

        topic.title = "Test Title %s" % i
        post.content = "Test Content"
        topic.save(post=post, user=user1, forum=forum)

        # create 100 posts in each topic
        for j in range(1, 100):
            post = Post()
            post.content = "Test Post"
            post.save(user=user2, topic=topic)
Пример #38
0
def seed_topics():
    f = open("{}/{}/meme_threads.txt".format(PROJECT_DIR, DATA_DIR), 'r')
    memes = f.read().split('\n')

    forum = Forum.query.filter(Forum.id == MEME_FORUM_ID).all()[0]
    text_model = utils.load_model(model_fname())
    users = User.query.all()

    for meme in memes[48:]:
        user = random.choice(users)
        if len(meme) > 1:
            title = meme
            url = google_scraper.run(100, title + " meme")
            post_content = "![]({})".format(url)
            post_content += "\n"
            post_content += "\n"
            post_content += text_model.make_sentence()
            rand_val = random.random()
            if rand_val > 0.6:
                title += " meme"

            post = Post(content=post_content)
            topic = Topic(title=title)
            topic.save(user=user, forum=forum, post=post)
Пример #39
0
def insert_mass_data(topics=100, posts=100):
    """Creates a few topics in the first forum and each topic has
    a few posts. WARNING: This might take very long!
    Returns the count of created topics and posts.

    :param topics: The amount of topics in the forum.
    :param posts: The number of posts in each topic.
    """
    user1 = User.query.filter_by(id=1).first()
    user2 = User.query.filter_by(id=2).first()
    forum = Forum.query.filter_by(id=1).first()

    created_posts = 0
    created_topics = 0

    if not (user1 or user2 or forum):
        return False

    # create 1000 topics
    for i in range(1, topics + 1):

        # create a topic
        topic = Topic()
        post = Post()

        topic.title = "Test Title %s" % i
        post.content = "Test Content"
        topic.save(post=post, user=user1, forum=forum)
        created_topics += 1

        # create 100 posts in each topic
        for j in range(1, posts + 1):
            post = Post()
            post.content = "Test Post"
            post.save(user=user2, topic=topic)
            created_posts += 1

    return created_topics, created_posts
Пример #40
0
    def save(self, user, topic):
        post = Post(content=self.content.data)

        if self.track_topic.data:
            user.track_topic(topic)
        return post.save(user=user, topic=topic)
Пример #41
0
def createall():
    """
    Creates the database with some example content.
    """

    # Just for testing purposes
    dbfile = os.path.join(Config._basedir, "flaskbb.sqlite")
    if os.path.exists(dbfile):
        os.remove(dbfile)

    db.create_all()

    groups = OrderedDict((
        ('Administrator', {
            'description': 'The Administrator Group',
            'admin': True,
            'super_mod': False,
            'mod': False,
            'banned': False,
            'guest': False,
            'editpost': True,
            'deletepost': True,
            'deletetopic': True,
            'posttopic': True,
            'postreply': True,
            'viewtopic': True,
            'viewprofile': True
        }),
        ('Super Moderator', {
            'description': 'The Super Moderator Group',
            'admin': False,
            'super_mod': True,
            'mod': False,
            'banned': False,
            'guest': False,
            'editpost': True,
            'deletepost': True,
            'deletetopic': True,
            'posttopic': True,
            'postreply': True,
            'viewtopic': True,
            'viewprofiles': True
        }),
        ('Moderator', {
            'description': 'The Moderator Group',
            'admin': False,
            'super_mod': False,
            'mod': True,
            'banned': False,
            'guest': False,
            'editpost': True,
            'deletepost': True,
            'deletetopic': True,
            'posttopic': True,
            'postreply': True,
            'viewtopic': True,
            'viewprofile': True
        }),
        ('Member', {
            'description': 'The Member Group',
            'admin': False,
            'super_mod': False,
            'mod': False,
            'banned': False,
            'guest': False,
            'editpost': True,
            'deletepost': False,
            'deletetopic': False,
            'posttopic': True,
            'postreply': True,
            'viewtopic': True,
            'viewprofile': True
        }),
        ('Banned', {
            'description': 'The Banned Group',
            'admin': False,
            'super_mod': False,
            'mod': False,
            'banned': True,
            'guest': False,
            'editpost': False,
            'deletepost': False,
            'deletetopic': False,
            'posttopic': False,
            'postreply': False,
            'viewtopic': False,
            'viewprofile': False
        }),
        ('Guest', {
            'description': 'The Guest Group',
            'admin': False,
            'super_mod': False,
            'mod': False,
            'banned': False,
            'guest': True,
            'editpost': False,
            'deletepost': False,
            'deletetopic': False,
            'posttopic': False,
            'postreply': False,
            'viewtopic': False,
            'viewprofile': False
        })
    ))

    # create 5 groups
    for key, value in groups.items():
        group = Group(name=key)

        for k, v in value.items():
            setattr(group, k, v)

        db.session.add(group)
        db.session.commit()

    # create 5 users
    groups = Group.query.all()
    for u in range(1, 6):
        username = "******" % u
        email = "*****@*****.**" % u
        user = User(username=username, password="******", email=email)
        user.secondary_groups.append(groups[u-1])
        user.primary_group_id = u
        db.session.add(user)
        db.session.commit()

    # create 2 categories
    for i in range(1, 3):
        category_title = "Test Category %s" % i
        category = Forum(is_category=True, title=category_title,
                         description="Test Description")
        db.session.add(category)

        # create 2 forums in each category
        for j in range(1, 3):
            if i == 2:
                j += 2

            forum_title = "Test Forum %s %s" % (j, i)
            forum = Forum(title=forum_title, description="Test Description",
                          parent_id=i)
            db.session.add(forum)
        db.session.commit()

    # create 1 topic in each forum
    for k in [2, 3, 5, 6]:  # Forum ids are not sequential because categories.
        topic = Topic()
        first_post = Post()

        topic.title = "Test Title %s" % k
        topic.user_id = 1
        topic.forum_id = k
        db.session.add(topic)
        db.session.commit()

        first_post.content = "Test Content"
        first_post.user_id = 1
        first_post.topic_id = topic.id
        db.session.add(first_post)
        db.session.commit()

        # Invalidate relevant caches
        topic.invalidate_cache()
        topic.forum.invalidate_cache()

        # create 2 additional posts for each topic
        for m in range(1, 3):
            post = Post(content="Test Post", user_id=2, topic_id=k)

            db.session.add(post)
            db.session.commit()

            # Update the post count
            post.user.invalidate_cache()
            topic.invalidate_cache()
            topic.forum.invalidate_cache()

            db.session.commit()

    db.session.commit()
Пример #42
0
    def save(self, user, topic):
        post = Post(content=self.content.data)

        if self.track_topic.data:
            user.track_topic(topic)
        return post.save(user=user, topic=topic)
Пример #43
0
 def save(self, user, topic):
     post = Post(content=self.content.data)
     return post.save(user=user, topic=topic)
Пример #44
0
def insert_bulk_data(topic_count=10, post_count=100):
    """Creates a specified number of topics in the first forum with
    each topic containing a specified amount of posts.
    Returns the number of created topics and posts.

    :param topics: The amount of topics in the forum.
    :param posts: The number of posts in each topic.
    """
    user1 = User.query.filter_by(id=1).first()
    user2 = User.query.filter_by(id=2).first()
    forum = Forum.query.filter_by(id=1).first()

    last_post = Post.query.order_by(Post.id.desc()).first()
    last_post_id = 1 if last_post is None else last_post.id

    created_posts = 0
    created_topics = 0
    posts = []

    if not (user1 or user2 or forum):
        return False

    db.session.begin(subtransactions=True)

    for i in range(1, topic_count + 1):
        last_post_id += 1

        # create a topic
        topic = Topic(title="Test Title %s" % i)
        post = Post(content="First Post")
        topic.save(post=post, user=user1, forum=forum)
        created_topics += 1

        # create some posts in the topic
        for j in range(1, post_count + 1):
            last_post_id += 1
            post = Post(content="Some other Post", user=user2, topic=topic.id)
            topic.last_updated = post.date_created
            topic.post_count += 1

            # FIXME: Is there a way to ignore IntegrityErrors?
            # At the moment, the first_post_id is also the last_post_id.
            # This does no harm, except that in the forums view, you see
            # the information for the first post instead of the last one.
            # I run a little benchmark:
            # 5.3643078804 seconds to create 100 topics and 10000 posts
            # Using another method (where data integrity is ok) I benchmarked
            # these stats:
            # 49.7832770348 seconds to create 100 topics and 10000 posts

            # Uncomment the line underneath and the other line to reduce
            # performance but fixes the above mentioned problem.
            #topic.last_post_id = last_post_id

            created_posts += 1
            posts.append(post)

        # uncomment this and delete the one below, also uncomment the
        # topic.last_post_id line above. This will greatly reduce the
        # performance.
        #db.session.bulk_save_objects(posts)
    db.session.bulk_save_objects(posts)

    # and finally, lets update some stats
    forum.recalculate(last_post=True)
    user1.recalculate()
    user2.recalculate()

    return created_topics, created_posts
Пример #45
0
def create_test_data(users=5, categories=2, forums=2, topics=1, posts=1):
    """Creates 5 users, 2 categories and 2 forums in each category.
    It also creates a new topic topic in each forum with a post.
    Returns the amount of created users, categories, forums, topics and posts
    as a dict.

    :param users: The number of users.
    :param categories: The number of categories.
    :param forums: The number of forums which are created in each category.
    :param topics: The number of topics which are created in each forum.
    :param posts: The number of posts which are created in each topic.
    """
    create_default_groups()
    create_default_settings()

    data_created = {'users': 0, 'categories': 0, 'forums': 0,
                    'topics': 0, 'posts': 0}

    # create 5 users
    for u in range(1, users + 1):
        username = "******" % u
        email = "*****@*****.**" % u
        user = User(username=username, password="******", email=email)
        user.primary_group_id = u
        user.activated = True
        user.save()
        data_created['users'] += 1

    user1 = User.query.filter_by(id=1).first()
    user2 = User.query.filter_by(id=2).first()

    # lets send them a few private messages
    for i in range(1, 3):
        # TODO
        pass

    # create 2 categories
    for i in range(1, categories + 1):
        category_title = "Test Category %s" % i
        category = Category(title=category_title,
                            description="Test Description")
        category.save()
        data_created['categories'] += 1

        # create 2 forums in each category
        for j in range(1, forums + 1):
            if i == 2:
                j += 2

            forum_title = "Test Forum %s %s" % (j, i)
            forum = Forum(title=forum_title, description="Test Description",
                          category_id=i)
            forum.save()
            data_created['forums'] += 1

            for t in range(1, topics + 1):
                # create a topic
                topic = Topic(title="Test Title %s" % j)
                post = Post(content="Test Content")

                topic.save(post=post, user=user1, forum=forum)
                data_created['topics'] += 1

                for p in range(1, posts + 1):
                    # create a second post in the forum
                    post = Post(content="Test Post")
                    post.save(user=user2, topic=topic)
                    data_created['posts'] += 1

    return data_created
Пример #46
0
 def save(self, user, topic):
     post = Post(content=self.content.data)
     current_app.pluggy.hook.flaskbb_form_post_save(form=self, post=post)
     return post.save(user=user, topic=topic)
Пример #47
0
 def save(self, user, topic):
     post = Post(**self.data)
     return post.save(user=user, topic=topic)
Пример #48
0
 def save(self, user, topic):
     post = Post(**self.data)
     return post.save(user=user, topic=topic)