Пример #1
0
def thread(request, forum, thread=None):
    instance = None
    if thread:
        instance = get_object_or_404(Thread, slug=thread, forum__slug=forum,
                                     forum__site=settings.SITE_ID)
    f_instance = get_object_or_404(Forum, slug=forum, site=settings.SITE_ID)

    def can_post(forum, user):
        if forum.only_staff_posts:
            return user.is_authenticated() and user.is_staff
        if forum.only_upgraders:
            return user.is_authenticated() and (user.is_staff or (hasattr(user, 'userprofile') and
                                                                getattr(user.userprofile, 'is_upgraded', False)))
        return user.is_authenticated()

    if not can_post(f_instance, request.user):
        return HttpResponseForbidden()

    if not Forum.objects.has_access(f_instance, request.user):
        return HttpResponseForbidden()

    if not request.user.is_authenticated():
        if instance and instance.comment and instance.comment.user != request.user:
            return HttpResponseForbidden()

    form = ThreadForm(request.POST or None, instance=instance, user=request.user, forum=f_instance)

    # If previewing, render preview and form.
    if "preview" in request.POST:
        if form.is_valid():
            data = form.cleaned_data
        else:
            data = form.data

        return render(
            request,
            'forum/previewthread.html',
            {
                'form': form,
                'thread': Thread(
                    title=data.get('title') or form.initial.get('title') or '',
                    forum=f_instance),
                'forum': f_instance,
                'instance': instance,
                'comment': data.get('body') or form.initial.get('body') or '',
            })

    if request.method == "POST" and form.is_valid():
        if not thread:
            cache = get_forum_cache()
            key = make_cache_forum_key(request.user, forum, settings.SITE_ID)

            if cache and forum in FORUM_FLOOD_CONTROL:
                if cache.get(key):
                    post_title, post_url, expiry = cache.get(key)
                    expiry = timeuntil(datetime.fromtimestamp(expiry))
                    messages.error(request, "You can't post a thread in the forum %s for %s." %
                                            (f_instance.title, expiry))

                    return HttpResponseRedirect(post_url)
        instance = form.save()
        if not thread:
            Thread.nonrel_objects.push_to_list('%s-latest-comments' % f_instance.slug, instance, trim=30)
            thread_created.send(sender=Thread, instance=instance, author=request.user)
        return HttpResponseRedirect(instance.get_absolute_url())

    if hasattr(form, 'cleaned_data'):
        preview_comment = form.cleaned_data.get('body')
    else:
        preview_comment = form.data.get('body') or form.initial.get('body') or ''

    preview_instance = None
    if not instance:
        if hasattr(form, 'cleaned_data'):
            title = form.cleaned_data.get('title') or ''
        else:
            title = form.data.get('title') or form.initial.get('title') or ''
        preview_instance = Thread(
            title=title,
            forum=f_instance)

    return render(
        request,
        'forum/previewthread.html',
        {
            'form': form,
            'thread': preview_instance or instance,
            'forum': f_instance,
            'comment': preview_comment or '',
            })
Пример #2
0
def previewthread(request, forum):
    """
    Renders a preview of the new post and gives the user
    the option to modify it before posting.

    Only allows a user to post if they're logged in.
    """
    if not request.user.is_authenticated():
        return HttpResponseRedirect('%s?next=%s' % (LOGIN_URL, request.path))

    f = get_object_or_404(Forum, slug=forum, site=settings.SITE_ID)

    if not Forum.objects.has_access(f, request.user):
        return HttpResponseForbidden()

    if request.method == "POST":
        if not can_post(f, request.user):
            return HttpResponseForbidden
        cache = get_forum_cache()
        key = make_cache_forum_key(request.user, forum, settings.SITE_ID)

        if cache and forum in FORUM_FLOOD_CONTROL:
            if cache.get(key):
                post_title, post_url, expiry = cache.get(key)
                expiry = timeuntil(datetime.fromtimestamp(expiry))
                messages.error(request, "You can't post a thread in the forum %s for %s." %
                                        (f.title, expiry))

                return HttpResponseRedirect(post_url)

        form = CreateThreadForm(request.POST)
        if form.is_valid():
            t = Thread(
                forum=f,
                title=form.cleaned_data['title'],
            )
            Post = comments.get_model()
            ct = ContentType.objects.get_for_model(Thread)

            # If previewing, render preview and form.
            if "preview" in request.POST:
                return render_to_response('forum/previewthread.html',
                    RequestContext(request, {
                        'form': form,
                        'forum': f,
                        'thread': t,
                        'comment': form.cleaned_data['body'],
                        'user': request.user,
                    }))

            # No preview means we're ready to save the post.
            else:
                t.save()
                p = Post(
                    content_type=ct,
                    object_pk=t.pk,
                    user=request.user,
                    comment=form.cleaned_data['body'],
                    submit_date=datetime.now(),
                    site=Site.objects.get_current(),
                )
                p.save()
                t.latest_post = p
                t.comment = p
                t.save()
                Thread.nonrel_objects.push_to_list('%s-latest-comments' % t.forum.slug, t, trim=30)

                thread_created.send(sender=Thread, instance=t, author=request.user)
                if cache and forum in FORUM_FLOOD_CONTROL:
                    cache.set(key,
                              (t.title, t.get_absolute_url(), get_forum_expire_datetime(forum)),
                              FORUM_FLOOD_CONTROL.get(forum, FORUM_POST_EXPIRE_IN))

                return HttpResponseRedirect(t.get_absolute_url())

    else:
        form = CreateThreadForm()

    return render_to_response('forum/newthread.html',
        RequestContext(request, {
            'form': form,
            'forum': f,
        }))
Пример #3
0
def previewthread(request, forum):
    """
    Renders a preview of the new post and gives the user
    the option to modify it before posting. If called without
    a POST, redirects to newthread.

    Only allows a user to post if they're logged in.
    """
    if not request.user.is_authenticated():
        return HttpResponseRedirect('%s?next=%s' % (LOGIN_URL, request.path))

    f = get_object_or_404(Forum, slug=forum)

    if not Forum.objects.has_access(f, request.user):
        return HttpResponseForbidden()

    if request.method == "POST":
        form = CreateThreadForm(request.POST)
        if form.is_valid():
            t = Thread(
                forum=f,
                title=form.cleaned_data['title'],
            )
            Post = comments.get_model()
            ct = ContentType.objects.get_for_model(Thread)

            # If previewing, render preview and form.
            if "preview" in request.POST:
                return render_to_response('forum/previewthread.html',
                    RequestContext(request, {
                        'form': form,
                        'forum': f,
                        'thread': t,
                        'comment': form.cleaned_data['body'],
                        'user': request.user,
                    }))

            # No preview means we're ready to save the post.
            else:
                t.save()
                p = Post(
                    content_type=ct,
                    object_pk=t.pk,
                    user=request.user,
                    comment=form.cleaned_data['body'],
                    submit_date=datetime.now(),
                    site=Site.objects.get_current(),
                )
                p.save()
                t.latest_post = p
                t.comment = p
                t.save()

                thread_created.send(sender=Thread, instance=t, author=request.user)

                return HttpResponseRedirect(t.get_absolute_url())

    else:
        form = CreateThreadForm()

    return render_to_response('forum/newthread.html',
        RequestContext(request, {
            'form': form,
            'forum': f, 
        }))