Example #1
0
def post_reply(topic_id, topic_name):
    topic = Topic.objects(topic_url_id=topic_id).first()
    if topic is None:
        abort(404)
    if not topic_name == topic.get_url_name():
        return redirect(topic.get_url())
    board = topic.board
    forum = board.forum

    form = TopicReplyForm(request.form)

    if request.method == 'GET':
        return render_template('forum_post_reply.html', forum=forum, board=board, topic=topic, form=form)

    elif request.method == 'POST':
        if not form.validate():
            return render_template('forum_post_reply.html', forum=forum, board=board, topic=topic, form=form)

        post = Post(author=current_user.to_dbref(), content=form.content.data, topic=topic, forum=forum)
        post_edit = PostEdit(author=post.author, content=post.content, date=post.date)
        post.edits.append(post_edit)
        post.save()

        topic.update(set__users_read_topic=[], set__last_post_date=post.date)

        return redirect(topic.get_url())
Example #2
0
def post_topic(board_id, board_name):
    board = Board.objects(board_id=board_id).first()
    if board is None:
        abort(404)
    forum = board.forum

    form = PostTopicForm(request.form)

    if request.method == 'GET':
        return render_template('forum_post_topic.html', board=board, forum=forum, form=form)

    elif request.method == 'POST':
        if not form.validate():
            return render_template('forum_post_topic.html', board=board, forum=forum, form=form)

        topic = Topic(title=form.title.data, author=current_user.to_dbref(), forum=forum, board=board)
        topic_edit = TopicEdit(title=topic.title, date=topic.date,
                               announcement=topic.announcement, author=topic.author)
        topic.edits.append(topic_edit)
        topic.save()

        post = Post(author=current_user.to_dbref(), content=form.content.data, topic=topic,
                    forum=forum, date=topic.date, is_op=True)
        post_edit = PostEdit(author=post.author, content=post.content, date=post.date)
        post.edits.append(post_edit)
        post.save()

        topic.op = post
        topic.date = post.date
        topic.save()

        return redirect(topic.get_url())
Example #3
0
def post_topic(board_id, board_name):
    board = Board.objects(board_id=board_id).first()
    if board is None:
        abort(404)
    forum = board.forum

    form = PostTopicForm(request.form)

    if request.method == 'GET':
        return render_template('forum_post_topic.html',
                               board=board,
                               forum=forum,
                               form=form)

    elif request.method == 'POST':
        if not form.validate():
            return render_template('forum_post_topic.html',
                                   board=board,
                                   forum=forum,
                                   form=form)

        topic = Topic(title=form.title.data,
                      author=current_user.to_dbref(),
                      forum=forum,
                      board=board)
        topic_edit = TopicEdit(title=topic.title,
                               date=topic.date,
                               announcement=topic.announcement,
                               author=topic.author)
        topic.edits.append(topic_edit)
        topic.save()

        post = Post(author=current_user.to_dbref(),
                    content=form.content.data,
                    topic=topic,
                    forum=forum,
                    date=topic.date,
                    is_op=True)
        post_edit = PostEdit(author=post.author,
                             content=post.content,
                             date=post.date)
        post.edits.append(post_edit)
        post.save()

        topic.op = post
        topic.date = post.date
        topic.save()

        return redirect(topic.get_url())
Example #4
0
def permalink_post_redirect(post_id, dummy_name):

    post = Post.objects(id=post_id).first()
    if post is None:
        abort(404)

    return redirect(post.get_post_url())
Example #5
0
def edit_post(post_id):
    form = PostEditForm(request.form)

    if not ObjectId.is_valid(post_id):
        abort(404)

    post = Post.objects(id=post_id).first()
    if post is None:
        abort(404)

    if not post.can_edit(current_user):
        abort(403)

    topic = post.topic
    board = topic.board
    forum = post.forum

    if request.method == "POST" and form.validate():
        post_edit = PostEdit(author=current_user.to_dbref(), content=form.content.data, date=datetime.utcnow())
        post.edits.append(post_edit)
        post.content = form.content.data
        post.save()

        return redirect(topic.get_url())

    form.content.data = post.content
    return render_template("forum_post_edit.html", form=form, topic=topic, board=board, forum=forum)
Example #6
0
 def __init__(self, user):
     posts_raw = Post.objects(author=user.to_dbref()).order_by('-date')
     self.post_num = len(posts_raw)
     self.forum_activity = posts_raw.limit(6)
     self.topic_num = len(Topic.objects(author=user))
Example #7
0
def view_topic(topic_id, topic_name, page):
    if page == 0:
        abort(404)

    topic_reply_form = TopicReplyForm()

    topic = Topic.objects(topic_url_id=topic_id).exclude().first()
    if topic is None:
        abort(404)
    if not topic_name == topic.get_url_name():
        return redirect(topic.get_url())

    if current_user.is_authenticated():
        topic.update(add_to_set__users_read_topic=current_user.to_dbref())

    board = topic.board
    forum = board.forum

    # Get our sorted posts and the number of posts.
    posts = Post.objects(topic=topic).order_by('+date')
    num_posts = len(posts)

    # Calculate the total number of pages and make sure the request is a valid page.
    num_pages = int(math.ceil(num_posts / float(POSTS_PER_PAGE)))
    if num_pages < page:
        if page == 1:
            return render_template('forum_topic_view.html',
                                   topic=topic,
                                   board=board,
                                   forum=forum,
                                   posts=posts,
                                   topic_reply_form=topic_reply_form,
                                   total_pages=num_pages,
                                   current_page=page,
                                   next=None,
                                   prev=None,
                                   links=[])
        abort(404)

    # Compile the list of topics we want displayed.
    display_posts = posts.skip(
        (page - 1) * POSTS_PER_PAGE).limit(POSTS_PER_PAGE)

    # Find the links we want for the next/prev buttons if applicable.
    next_page = url_for(
        'forum.view_topic', page=page +
        1, **topic.get_url_info()) if page < num_pages else None
    prev_page = url_for(
        'forum.view_topic', page=page -
        1, **topic.get_url_info()) if page > 1 and not num_pages == 1 else None

    # Mash together a list of what pages we want linked to in the pagination bar.
    links = []
    for page_mod in range(-min(PAGINATION_VALUE_RANGE, page - 1),
                          min(PAGINATION_VALUE_RANGE, num_pages - page) + 1):
        num = page + page_mod
        links.append({
            'num':
            num,
            'url':
            url_for('forum.view_topic', page=num, **topic.get_url_info()),
            'active': (num == page)
        })

    return render_template('forum_topic_view.html',
                           topic=topic,
                           board=board,
                           forum=forum,
                           posts=display_posts,
                           topic_reply_form=topic_reply_form,
                           total_pages=num_pages,
                           current_page=page,
                           next=next_page,
                           prev=prev_page,
                           links=links,
                           markdown_escape=markdown_escape,
                           post_edit=PostEditForm(),
                           forum_menu_current=board.id,
                           **forum_template_data(forum))
Example #8
0
 def __init__(self, user):
     posts_raw = Post.objects(author=user.to_dbref()).order_by('-date')
     self.post_num = len(posts_raw)
     self.forum_activity = posts_raw.limit(6)
     self.topic_num = len(Topic.objects(author=user))
Example #9
0
def view_topic(topic_id, topic_name, page):
    if page == 0:
        abort(404)

    topic_reply_form = TopicReplyForm()

    topic = Topic.objects(topic_url_id=topic_id).exclude().first()
    if topic is None:
        abort(404)
    if not topic_name == topic.get_url_name():
        return redirect(topic.get_url())

    if current_user.is_authenticated():
        topic.update(add_to_set__users_read_topic=current_user.to_dbref())

    board = topic.board
    forum = board.forum

    # Get our sorted posts and the number of posts.
    posts = Post.objects(topic=topic).order_by("+date")
    num_posts = len(posts)

    # Calculate the total number of pages and make sure the request is a valid page.
    num_pages = int(math.ceil(num_posts / float(POSTS_PER_PAGE)))
    if num_pages < page:
        if page == 1:
            return render_template(
                "forum_topic_view.html",
                topic=topic,
                board=board,
                forum=forum,
                posts=posts,
                topic_reply_form=topic_reply_form,
                total_pages=num_pages,
                current_page=page,
                next=None,
                prev=None,
                links=[],
            )
        abort(404)

    # Compile the list of topics we want displayed.
    display_posts = posts.skip((page - 1) * POSTS_PER_PAGE).limit(POSTS_PER_PAGE)

    # Find the links we want for the next/prev buttons if applicable.
    next_page = url_for("forum.view_topic", page=page + 1, **topic.get_url_info()) if page < num_pages else None
    prev_page = (
        url_for("forum.view_topic", page=page - 1, **topic.get_url_info()) if page > 1 and not num_pages == 1 else None
    )

    # Mash together a list of what pages we want linked to in the pagination bar.
    links = []
    for page_mod in range(-min(PAGINATION_VALUE_RANGE, page - 1), min(PAGINATION_VALUE_RANGE, num_pages - page) + 1):
        num = page + page_mod
        links.append(
            {"num": num, "url": url_for("forum.view_topic", page=num, **topic.get_url_info()), "active": (num == page)}
        )

    return render_template(
        "forum_topic_view.html",
        topic=topic,
        board=board,
        forum=forum,
        posts=display_posts,
        topic_reply_form=topic_reply_form,
        total_pages=num_pages,
        current_page=page,
        next=next_page,
        prev=prev_page,
        links=links,
        markdown_escape=markdown_escape,
        post_edit=PostEditForm(),
        forum_menu_current=board.id,
        **forum_template_data(forum)
    )