Пример #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)