Exemplo n.º 1
0
def post_delete(request, post_id):
    """Delete a post"""

    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 post.can_delete(user):
       raise Forbidden()

    form = DeletePostForm(post)

    if request.method == 'POST':
        if form.validate(request.form):
            if 'confirm' in request.form:
                forum = topic.forum
            try:
                form.delete_post()
                return redirect_to('board/topic_detail', topic_id=topic.id)
            except TopicEmpty:
                return redirect_to('board/topics', forum_id=forum.id)
                
    return render_response('board_delete_post.html', topic=topic,
                          post=post, form=form.as_widget())
Exemplo n.º 2
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)
Exemplo n.º 3
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)
Exemplo n.º 4
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)
Exemplo n.º 5
0
def board_index(request):
    """Render the board index page

    Available template variables:

        `categories`:
            Ordered Dictionary with 'catname': 'forums'

    :Template name: ``board_index.html``
    :URL endpoint: ``board/index``
    """
    renderdict = OrderedDict()
    categories = Category.query.order_by(db.asc('ordering')).all()
    for category in categories:
        forums = category.forums.order_by(db.asc('ordering')).all()
        if forums:
            user = request.user
            forumlist = [forum for forum in \
                         forums if forum.can_see(request.user)]
            if forumlist:
                renderdict[category] = forumlist

    return render_response('board_index.html', categories=renderdict)