Пример #1
0
def watch_forum(request, forum_slug):
    """Watch/unwatch a forum (based on 'watch' POST param)."""
    forum = get_object_or_404(Forum, slug=forum_slug)
    if not forum.allows_viewing_by(request.user):
        raise Http404

    if request.POST.get("watch") == "yes":
        NewThreadEvent.notify(request.user, forum)
    else:
        NewThreadEvent.stop_notifying(request.user, forum)

    return HttpResponseRedirect(reverse("forums.threads", args=[forum_slug]))
Пример #2
0
def watch_forum(request, forum_slug):
    """Watch/unwatch a forum (based on 'watch' POST param)."""
    forum = get_object_or_404(Forum, slug=forum_slug)
    if not forum.allows_viewing_by(request.user):
        raise Http404

    if request.POST.get('watch') == 'yes':
        NewThreadEvent.notify(request.user, forum)
        statsd.incr('forums.watches.forum')
    else:
        NewThreadEvent.stop_notifying(request.user, forum)

    return HttpResponseRedirect(reverse('forums.threads', args=[forum_slug]))
Пример #3
0
def threads(request, forum_slug):
    """View all the threads in a forum."""
    forum = get_object_or_404(Forum, slug=forum_slug)
    if not forum.allows_viewing_by(request.user):
        raise Http404  # Pretend there's nothing there.

    try:
        sort = int(request.GET.get('sort', 0))
    except ValueError:
        sort = 0

    try:
        desc = int(request.GET.get('desc', 0))
    except ValueError:
        desc = 0
    desc_toggle = 0 if desc else 1

    threads_ = sort_threads(forum.thread_set, sort, desc)
    count = threads_.count()
    threads_ = threads_.select_related('creator', 'last_post',
                                       'last_post__author')
    threads_ = paginate(request, threads_,
                        per_page=constants.THREADS_PER_PAGE, count=count)

    feed_urls = ((reverse('forums.threads.feed', args=[forum_slug]),
                  ThreadsFeed().title(forum)),)

    is_watching_forum = (request.user.is_authenticated() and
                         NewThreadEvent.is_notifying(request.user, forum))
    return render(request, 'forums/threads.html', {
        'forum': forum, 'threads': threads_,
        'is_watching_forum': is_watching_forum,
        'sort': sort, 'desc_toggle': desc_toggle,
        'feeds': feed_urls})
Пример #4
0
def threads(request, forum_slug):
    """View all the threads in a forum."""
    forum = get_object_or_404(Forum, slug=forum_slug)
    if not forum.allows_viewing_by(request.user):
        raise Http404  # Pretend there's nothing there.

    try:
        sort = int(request.GET.get('sort', 0))
    except ValueError:
        sort = 0

    try:
        desc = int(request.GET.get('desc', 0))
    except ValueError:
        desc = 0
    desc_toggle = 0 if desc else 1

    threads_ = sort_threads(forum.thread_set, sort, desc)
    count = threads_.count()
    threads_ = threads_.select_related('creator', 'last_post',
                                       'last_post__author')
    threads_ = paginate(request, threads_,
                        per_page=constants.THREADS_PER_PAGE, count=count)

    feed_urls = ((reverse('forums.threads.feed', args=[forum_slug]),
                  ThreadsFeed().title(forum)),)

    is_watching_forum = (request.user.is_authenticated() and
                         NewThreadEvent.is_notifying(request.user, forum))
    return render(request, 'forums/threads.html', {
        'forum': forum, 'threads': threads_,
        'is_watching_forum': is_watching_forum,
        'sort': sort, 'desc_toggle': desc_toggle,
        'feeds': feed_urls})
Пример #5
0
    def test_watch_forum(self):
        """Watch then unwatch a forum."""
        f = forum(save=True)
        forum_post(thread=thread(forum=f, save=True), save=True)
        u = user(save=True)

        self.client.login(username=u.username, password='******')

        post(self.client,
             'forums.watch_forum', {'watch': 'yes'},
             args=[f.slug])
        assert NewThreadEvent.is_notifying(u, f)
        # NewPostEvent is not notifying.
        assert not NewPostEvent.is_notifying(u, f.last_post)

        post(self.client, 'forums.watch_forum', {'watch': 'no'}, args=[f.slug])
        assert not NewThreadEvent.is_notifying(u, f)
Пример #6
0
    def test_watch_forum(self):
        """Watch then unwatch a forum."""
        f = ForumFactory()
        PostFactory(thread__forum=f)
        u = UserFactory()

        self.client.login(username=u.username, password='******')

        post(self.client, 'forums.watch_forum', {'watch': 'yes'},
             args=[f.slug])
        assert NewThreadEvent.is_notifying(u, f)
        # NewPostEvent is not notifying.
        assert not NewPostEvent.is_notifying(u, f.last_post)

        post(self.client, 'forums.watch_forum', {'watch': 'no'},
             args=[f.slug])
        assert not NewThreadEvent.is_notifying(u, f)
Пример #7
0
    def test_watch_forum(self):
        """Watch then unwatch a forum."""
        f = forum(save=True)
        forum_post(thread=thread(forum=f, save=True), save=True)
        u = user(save=True)

        self.client.login(username=u.username, password='******')

        post(self.client, 'forums.watch_forum', {'watch': 'yes'},
             args=[f.slug])
        assert NewThreadEvent.is_notifying(u, f)
        # NewPostEvent is not notifying.
        assert not NewPostEvent.is_notifying(u, f.last_post)

        post(self.client, 'forums.watch_forum', {'watch': 'no'},
             args=[f.slug])
        assert not NewThreadEvent.is_notifying(u, f)
Пример #8
0
    def test_watch_forum(self):
        """Watch then unwatch a forum."""
        f = ForumFactory()
        PostFactory(thread__forum=f)
        u = UserFactory()

        self.client.login(username=u.username, password="******")

        post(self.client,
             "forums.watch_forum", {"watch": "yes"},
             args=[f.slug])
        assert NewThreadEvent.is_notifying(u, f)
        # NewPostEvent is not notifying.
        assert not NewPostEvent.is_notifying(u, f.last_post)

        post(self.client, "forums.watch_forum", {"watch": "no"}, args=[f.slug])
        assert not NewThreadEvent.is_notifying(u, f)
Пример #9
0
    def test_watch_forum(self):
        """Watch then unwatch a forum."""
        f = ForumFactory()
        PostFactory(thread__forum=f)
        u = UserFactory()

        self.client.login(username=u.username, password='******')

        post(self.client, 'forums.watch_forum', {'watch': 'yes'},
             args=[f.slug])
        assert NewThreadEvent.is_notifying(u, f)
        # NewPostEvent is not notifying.
        assert not NewPostEvent.is_notifying(u, f.last_post)

        post(self.client, 'forums.watch_forum', {'watch': 'no'},
             args=[f.slug])
        assert not NewThreadEvent.is_notifying(u, f)
Пример #10
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
    })
Пример #11
0
 def _toggle_watch_forum_as(self, forum, user, turn_on=True):
     """Watch a forum and return it."""
     self.client.login(username=user.username, password="******")
     watch = "yes" if turn_on else "no"
     post(self.client, "forums.watch_forum", {"watch": watch}, args=[forum.slug])
     # Watch exists or not, depending on watch.
     if turn_on:
         assert NewThreadEvent.is_notifying(user, forum), "NewThreadEvent should be notifying."
     else:
         assert not NewPostEvent.is_notifying(user, forum), "NewThreadEvent should not be notifying."
Пример #12
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
        },
    )
Пример #13
0
 def _toggle_watch_forum_as(self, forum, user, turn_on=True):
     """Watch a forum and return it."""
     self.client.login(username=user.username, password='******')
     watch = 'yes' if turn_on else 'no'
     post(self.client, 'forums.watch_forum', {'watch': watch},
          args=[forum.slug])
     # Watch exists or not, depending on watch.
     if turn_on:
         assert NewThreadEvent.is_notifying(user, forum), (
             'NewThreadEvent should be notifying.')
     else:
         assert not NewPostEvent.is_notifying(user, forum), (
             'NewThreadEvent should not be notifying.')
Пример #14
0
 def _toggle_watch_forum_as(self, forum, user, turn_on=True):
     """Watch a forum and return it."""
     self.client.login(username=user.username, password='******')
     watch = 'yes' if turn_on else 'no'
     post(self.client, 'forums.watch_forum', {'watch': watch},
          args=[forum.slug])
     # Watch exists or not, depending on watch.
     if turn_on:
         assert NewThreadEvent.is_notifying(user, forum), (
             'NewThreadEvent should be notifying.')
     else:
         assert not NewPostEvent.is_notifying(user, forum), (
             'NewThreadEvent should not be notifying.')
Пример #15
0
 def _toggle_watch_forum_as(self, forum, user, turn_on=True):
     """Watch a forum and return it."""
     self.client.login(username=user.username, password="******")
     watch = "yes" if turn_on else "no"
     post(self.client,
          "forums.watch_forum", {"watch": watch},
          args=[forum.slug])
     # Watch exists or not, depending on watch.
     if turn_on:
         assert NewThreadEvent.is_notifying(
             user, forum), "NewThreadEvent should be notifying."
     else:
         assert not NewPostEvent.is_notifying(
             user, forum), "NewThreadEvent should not be notifying."
Пример #16
0
    def test_watch_thread(self):
        """Watch then unwatch a thread."""
        t = ThreadFactory()
        u = UserFactory()

        self.client.login(username=u.username, password='******')

        post(self.client, 'forums.watch_thread', {'watch': 'yes'}, args=[t.forum.slug, t.id])
        assert NewPostEvent.is_notifying(u, t)
        # NewThreadEvent is not notifying.
        assert not NewThreadEvent.is_notifying(u, t.forum)

        post(self.client, 'forums.watch_thread', {'watch': 'no'},
             args=[t.forum.slug, t.id])
        assert not NewPostEvent.is_notifying(u, t)
Пример #17
0
    def test_watch_thread(self):
        """Watch then unwatch a thread."""
        t = ThreadFactory()
        u = UserFactory()

        self.client.login(username=u.username, password='******')

        post(self.client, 'forums.watch_thread', {'watch': 'yes'}, args=[t.forum.slug, t.id])
        assert NewPostEvent.is_notifying(u, t)
        # NewThreadEvent is not notifying.
        assert not NewThreadEvent.is_notifying(u, t.forum)

        post(self.client, 'forums.watch_thread', {'watch': 'no'},
             args=[t.forum.slug, t.id])
        assert not NewPostEvent.is_notifying(u, t)
Пример #18
0
    def test_watch_thread(self):
        """Watch then unwatch a thread."""
        t = ThreadFactory()
        u = UserFactory()

        self.client.login(username=u.username, password="******")

        post(self.client,
             "forums.watch_thread", {"watch": "yes"},
             args=[t.forum.slug, t.id])
        assert NewPostEvent.is_notifying(u, t)
        # NewThreadEvent is not notifying.
        assert not NewThreadEvent.is_notifying(u, t.forum)

        post(self.client,
             "forums.watch_thread", {"watch": "no"},
             args=[t.forum.slug, t.id])
        assert not NewPostEvent.is_notifying(u, t)
Пример #19
0
def threads(request, forum_slug):
    """View all the threads in a forum."""
    forum = get_object_or_404(Forum, slug=forum_slug)
    if not forum.allows_viewing_by(request.user):
        raise Http404  # Pretend there's nothing there.

    try:
        sort = int(request.GET.get("sort", 0))
    except ValueError:
        sort = 0

    try:
        desc = int(request.GET.get("desc", 0))
    except ValueError:
        desc = 0
    desc_toggle = 0 if desc else 1

    threads_ = sort_threads(forum.thread_set, sort, desc)
    count = threads_.count()
    threads_ = threads_.select_related("creator", "last_post",
                                       "last_post__author")
    threads_ = paginate(request,
                        threads_,
                        per_page=constants.THREADS_PER_PAGE,
                        count=count)

    feed_urls = ((reverse("forums.threads.feed",
                          args=[forum_slug]), ThreadsFeed().title(forum)), )

    is_watching_forum = request.user.is_authenticated and NewThreadEvent.is_notifying(
        request.user, forum)
    return render(
        request,
        "forums/threads.html",
        {
            "forum": forum,
            "threads": threads_,
            "is_watching_forum": is_watching_forum,
            "sort": sort,
            "desc_toggle": desc_toggle,
            "feeds": feed_urls,
        },
    )
Пример #20
0
 def test_delete_removes_watches(self):
     f = ForumFactory()
     NewThreadEvent.notify('*****@*****.**', f)
     assert NewThreadEvent.is_notifying('*****@*****.**', f)
     f.delete()
     assert not NewThreadEvent.is_notifying('*****@*****.**', f)
Пример #21
0
 def test_delete_removes_watches(self):
     f = forum(save=True)
     NewThreadEvent.notify('*****@*****.**', f)
     assert NewThreadEvent.is_notifying('*****@*****.**', f)
     f.delete()
     assert not NewThreadEvent.is_notifying('*****@*****.**', f)
Пример #22
0
 def test_delete_removes_watches(self):
     f = ForumFactory()
     NewThreadEvent.notify('*****@*****.**', f)
     assert NewThreadEvent.is_notifying('*****@*****.**', f)
     f.delete()
     assert not NewThreadEvent.is_notifying('*****@*****.**', f)
Пример #23
0
 def test_delete_removes_watches(self):
     f = forum(save=True)
     NewThreadEvent.notify('*****@*****.**', f)
     assert NewThreadEvent.is_notifying('*****@*****.**', f)
     f.delete()
     assert not NewThreadEvent.is_notifying('*****@*****.**', f)