Example #1
0
def post_preview_async(request):
    """Ajax preview of posts."""
    post = Post(author=request.user, content=request.POST.get('content', ''))
    post.author_post_count = 1

    return render(request, 'forums/includes/post_preview.html',
                  {'post_preview': post})
Example #2
0
def post_preview_async(request):
    """Ajax preview of posts."""
    post = Post(author=request.user, content=request.POST.get("content", ""))
    post.author_post_count = 1

    return render(request, "forums/includes/post_preview.html",
                  {"post_preview": post})
Example #3
0
 def test_save_new_post_timestamps(self):
     # Saving a new post should allow you to override auto_add_now-
     # and auto_now-like functionality.
     created_ = datetime(1992, 1, 12, 10, 12, 32)
     p = Post(thread=self.thread, content="bar", author=self.user, created=created_, updated=created_)
     p.save()
     eq_(created_, p.created)
     eq_(created_, p.updated)
Example #4
0
 def test_save_new_post_timestamps(self):
     # Saving a new post should allow you to override auto_add_now-
     # and auto_now-like functionality.
     created_ = datetime(1992, 1, 12, 10, 12, 32)
     p = Post(thread=self.thread, content='bar', author=self.user,
              created=created_, updated=created_)
     p.save()
     eq_(created_, p.created)
     eq_(created_, p.updated)
Example #5
0
def new_thread(request, forum_slug):
    """Start a new thread."""
    forum = get_object_or_404(Forum, slug=forum_slug)
    user = request.user
    if not forum.allows_posting_by(user):
        if forum.allows_viewing_by(user):
            raise PermissionDenied
        else:
            raise Http404

    if request.method == 'GET':
        form = NewThreadForm()
        return render(request, 'forums/new_thread.html', {
            'form': form,
            'forum': forum
        })

    form = NewThreadForm(request.POST)
    post_preview = None
    if form.is_valid():
        if 'preview' in request.POST:
            thread = Thread(creator=request.user,
                            title=form.cleaned_data['title'])
            post_preview = Post(thread=thread,
                                author=request.user,
                                content=form.cleaned_data['content'])
            post_preview.author_post_count = \
                post_preview.author.post_set.count()
        elif (_skip_post_ratelimit(request)
              or not is_ratelimited(request,
                                    increment=True,
                                    rate='5/d',
                                    ip=False,
                                    keys=user_or_ip('forum-post'))):
            thread = forum.thread_set.create(creator=request.user,
                                             title=form.cleaned_data['title'])
            thread.save()
            statsd.incr('forums.thread')
            post = thread.new_post(author=request.user,
                                   content=form.cleaned_data['content'])
            post.save()

            NewThreadEvent(post).fire(exclude=post.author)

            # Add notification automatically if needed.
            if Setting.get_for_user(request.user, 'forums_watch_new_thread'):
                NewPostEvent.notify(request.user, thread)

            url = reverse('forums.posts', args=[forum_slug, thread.id])
            return HttpResponseRedirect(urlparams(url, last=post.id))

    return render(request, 'forums/new_thread.html', {
        'form': form,
        'forum': forum,
        'post_preview': post_preview
    })
Example #6
0
def new_thread(request, forum_slug):
    """Start a new thread."""
    forum = get_object_or_404(Forum, slug=forum_slug)
    user = request.user
    if not forum.allows_posting_by(user):
        if forum.allows_viewing_by(user):
            raise PermissionDenied
        else:
            raise Http404

    if request.method == "GET":
        form = NewThreadForm()
        return render(request, "forums/new_thread.html", {
            "form": form,
            "forum": forum
        })

    form = NewThreadForm(request.POST)
    post_preview = None
    if form.is_valid():
        if "preview" in request.POST:
            thread = Thread(creator=request.user,
                            title=form.cleaned_data["title"])
            post_preview = Post(thread=thread,
                                author=request.user,
                                content=form.cleaned_data["content"])
            post_preview.author_post_count = post_preview.author.post_set.count(
            )
        elif not is_ratelimited(request, "forum-post", "5/d"):
            thread = forum.thread_set.create(creator=request.user,
                                             title=form.cleaned_data["title"])
            thread.save()
            post = thread.new_post(author=request.user,
                                   content=form.cleaned_data["content"])
            post.save()

            NewThreadEvent(post).fire(exclude=post.author)

            # Add notification automatically if needed.
            if Setting.get_for_user(request.user, "forums_watch_new_thread"):
                NewPostEvent.notify(request.user, thread)

            url = reverse("forums.posts", args=[forum_slug, thread.id])
            return HttpResponseRedirect(urlparams(url, last=post.id))

    return render(
        request,
        "forums/new_thread.html",
        {
            "form": form,
            "forum": forum,
            "post_preview": post_preview
        },
    )
Example #7
0
def new_thread(request, forum_slug):
    """Start a new thread."""
    forum = get_object_or_404(Forum, slug=forum_slug)
    user = request.user
    if not forum.allows_posting_by(user):
        if forum.allows_viewing_by(user):
            raise PermissionDenied
        else:
            raise Http404

    if request.method == 'GET':
        form = NewThreadForm()
        return render(request, 'forums/new_thread.html', {
            'form': form, 'forum': forum})

    form = NewThreadForm(request.POST)
    post_preview = None
    if form.is_valid():
        if 'preview' in request.POST:
            thread = Thread(creator=request.user,
                            title=form.cleaned_data['title'])
            post_preview = Post(thread=thread, author=request.user,
                                content=form.cleaned_data['content'])
            post_preview.author_post_count = \
                post_preview.author.post_set.count()
        elif (_skip_post_ratelimit(request) or
              not is_ratelimited(request, increment=True, rate='5/d', ip=False,
                                 keys=user_or_ip('forum-post'))):
            thread = forum.thread_set.create(creator=request.user,
                                             title=form.cleaned_data['title'])
            thread.save()
            statsd.incr('forums.thread')
            post = thread.new_post(author=request.user,
                                   content=form.cleaned_data['content'])
            post.save()

            NewThreadEvent(post).fire(exclude=post.author)

            # Add notification automatically if needed.
            if Setting.get_for_user(request.user, 'forums_watch_new_thread'):
                NewPostEvent.notify(request.user, thread)

            url = reverse('forums.posts', args=[forum_slug, thread.id])
            return HttpResponseRedirect(urlparams(url, last=post.id))

    return render(request, 'forums/new_thread.html', {
        'form': form, 'forum': forum,
        'post_preview': post_preview})
Example #8
0
def post(**kwargs):
    defaults = dict()
    defaults.update(kwargs)
    if 'author' not in kwargs and 'author_id' not in kwargs:
        defaults['author'] = user(save=True)
    if 'thread' not in kwargs and 'thread_id' not in kwargs:
        # The thread creator should match the post author if the
        # thread doesn't exist yet.
        kw = {}
        if 'author' in defaults:
            kw['creator'] = defaults['author']
        else:
            kw['creator_id'] = defaults['author_id']
        defaults['thread'] = thread(save=True, **kw)
    return Post(**defaults)
Example #9
-1
def post_preview_async(request):
    """Ajax preview of posts."""
    statsd.incr('forums.preview')
    post = Post(author=request.user, content=request.POST.get('content', ''))
    post.author_post_count = 1

    return render(request, 'forums/includes/post_preview.html', {
        'post_preview': post})