Example #1
0
def topic_detail(request, topic_id, page=1):
    """Render posts for a given topic

    Available template variables:

        `topic`:
            Topic instance we give details about

        `pagination`:
            a pagination object to render a pagination

        `posts`:
            Sorted list of posts for given topic

        `form`:
            Form for new post creation or None if user is not allowed
            to post here

    :Template name: ``board_topic_detail.html``
    :URL endpoint: ``board/topic_detail``
    """

    topic = Topic.query.get(topic_id)
    if topic is None:
        raise NotFound()
    if not topic.can_see(request.user):
       raise Forbidden()

    form = None
    if topic.can_post(request.user):
        form = PostForm(topic)
        del form.title

    if request.method == 'POST':
        if form.validate(request.form):
            form.create_post()
            form.reset()

    data = Post.query.filter(Post.topic==topic)\
               .get_list('board/topic_detail', page,
                         request.per_page, {'topic_id': topic_id})

    # Mark read up to last shown post date
    check_unread(request.user, topic, data['posts'][-1].date)

    return render_response('board_topic_detail.html', topic=topic,
                          posts=data['posts'], pagination=data['pagination'],
                          form=form.as_widget() if form else None)
Example #2
0
def topic_list(request, forum_id, page=1):
    """Render topics for a given forum

    Available template variables:

        `forum`:
            Forum instance we give details about

        `pagination`:
            a pagination object to render a pagination

        `stickies`:
            Sorted list of sticky topics for given `forum_id`

        `topics`:
            Sorted list of non-sticky topics for given `forum_id`

        `form`:
            Form for topic creation or None if user is not allowed
            to create topics

    :Template name: ``board_topic_index.html``
    :URL endpoint: ``board/topics``
    """

    forum = Forum.query.get(forum_id)
    if forum is None:
        raise NotFound()
    if not forum.can_see(request.user):
       raise Forbidden()

    form = None
    if forum.can_create_topic(request.user):
        form = PostForm(forum)

    if request.method == 'POST':
        if form.validate(request.form):
            newpost = form.create_post()
            return redirect_to('board/topic_detail', topic_id=newpost.topic.id)

    data = Topic.query.filter(Topic.forum==forum) \
                .get_list('board/topics', page,
                          request.per_page, {'forum_id': forum_id})
    data['forum'] = forum
    data['form'] = form.as_widget() if form else None

    return render_response('board_topic_list.html', **data)
Example #3
0
def post_edit(request, post_id):
    """Render post for editing, re-uses topic detail template

    Available template variables:

        `topic`:
            Topic instance we give details about

        `pagination`:
            a pagination object to render a pagination

        `posts`:
            Sorted list of posts for given topic

        `form`:
            Form for post editing

    :Template name: ``board_topic_detail.html``
    :URL endpoint: ``board/post_edit``
    """

    post = Post.query.get(post_id)
    if post is None:
        raise NotFound()
    (postnr, page, topic_id) = locate_post(post)

    user = request.user
    topic = post.topic
    if not topic.can_see(user) or not post.can_edit(user):
       raise Forbidden()

    form = PostForm(topic,post)
    del form.title

    if request.method == 'POST':
        if form.validate(request.form):
            form.save_changes(post)
            return redirect_to('board/post_find', post_id=post.id)

    data = Post.query.filter(Post.topic==topic)\
               .get_list('board/topic_detail', page,
                         request.per_page, {'topic_id': topic_id})

    return render_response('board_topic_detail.html', topic=topic,
                          posts=data['posts'], pagination=data['pagination'],
                          form=form.as_widget() if form else None)