Esempio n. 1
0
def new_thread(request, document_slug):
    """Start a new thread."""
    doc = get_document(document_slug, request)

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

    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, creator=request.user, content=form.cleaned_data["content"])
        elif not _is_ratelimited(request):
            thread = doc.thread_set.create(creator=request.user, title=form.cleaned_data["title"])
            thread.save()
            statsd.incr("kbforums.thread")
            post = thread.new_post(creator=request.user, content=form.cleaned_data["content"])
            post.save()

            # Send notifications to forum watchers.
            NewThreadEvent(post).fire(exclude=post.creator)

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

            return HttpResponseRedirect(reverse("wiki.discuss.posts", args=[document_slug, thread.id]))

    return render(request, "kbforums/new_thread.html", {"form": form, "document": doc, "post_preview": post_preview})
Esempio n. 2
0
def reply(request, document_slug, thread_id):
    """Reply to a thread."""
    doc = get_document(document_slug, request)

    form = ReplyForm(request.POST)
    post_preview = None
    if form.is_valid():
        thread = get_object_or_404(Thread, pk=thread_id, document=doc)

        if not thread.is_locked:
            reply_ = form.save(commit=False)
            reply_.thread = thread
            reply_.creator = request.user
            if 'preview' in request.POST:
                post_preview = reply_
            elif not _is_ratelimited(request):
                reply_.save()
                statsd.incr('kbforums.reply')

                # Subscribe the user to the thread.
                if Setting.get_for_user(request.user,
                                        'kbforums_watch_after_reply'):
                    NewPostEvent.notify(request.user, thread)

                # Send notifications to thread/forum watchers.
                NewPostEvent(reply_).fire(exclude=reply_.creator)

                return HttpResponseRedirect(reply_.get_absolute_url())

    return posts(request, document_slug, thread_id, form, post_preview)
Esempio n. 3
0
def watch_thread(request, document_slug, thread_id):
    """Watch/unwatch a thread (based on 'watch' POST param)."""
    doc = get_document(document_slug, request)
    thread = get_object_or_404(Thread, pk=thread_id, document=doc)

    if request.POST.get("watch") == "yes":
        NewPostEvent.notify(request.user, thread)
        statsd.incr("kbforums.watches.thread")
    else:
        NewPostEvent.stop_notifying(request.user, thread)

    return HttpResponseRedirect(reverse("wiki.discuss.posts", args=[document_slug, thread_id]))
Esempio n. 4
0
def watch_thread(request, document_slug, thread_id):
    """Watch/unwatch a thread (based on 'watch' POST param)."""
    doc = get_document(document_slug, request)
    thread = get_object_or_404(Thread, pk=thread_id, document=doc)

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

    return HttpResponseRedirect(
        reverse("wiki.discuss.posts", args=[document_slug, thread_id]))
 def _toggle_watch_thread_as(self, username, thread, turn_on=True):
     """Watch a thread and return it."""
     self.client.login(username=username, password="******")
     user = User.objects.get(username=username)
     watch = "yes" if turn_on else "no"
     post(self.client, "wiki.discuss.watch_thread", {"watch": watch}, args=[thread.document.slug, thread.id])
     # Watch exists or not, depending on watch.
     if turn_on:
         assert NewPostEvent.is_notifying(user, thread), "NewPostEvent should be notifying."
     else:
         assert not NewPostEvent.is_notifying(user, thread), "NewPostEvent should not be notifying."
     return thread
Esempio n. 6
0
def watch_thread(request, document_slug, thread_id):
    """Watch/unwatch a thread (based on 'watch' POST param)."""
    doc = get_document(document_slug, request)
    thread = get_object_or_404(Thread, pk=thread_id, document=doc)

    if request.POST.get('watch') == 'yes':
        NewPostEvent.notify(request.user, thread)
        statsd.incr('kbforums.watches.thread')
    else:
        NewPostEvent.stop_notifying(request.user, thread)

    return HttpResponseRedirect(
        reverse('wiki.discuss.posts', args=[document_slug, thread_id]))
Esempio n. 7
0
 def _toggle_watch_thread_as(self, username, thread, turn_on=True):
     """Watch a thread and return it."""
     self.client.login(username=username, password='******')
     user = User.objects.get(username=username)
     watch = 'yes' if turn_on else 'no'
     post(self.client, 'wiki.discuss.watch_thread', {'watch': watch},
          args=[thread.document.slug, thread.id])
     # Watch exists or not, depending on watch.
     if turn_on:
         assert NewPostEvent.is_notifying(user, thread), (
             'NewPostEvent should be notifying.')
     else:
         assert not NewPostEvent.is_notifying(user, thread), (
             'NewPostEvent should not be notifying.')
     return thread
Esempio n. 8
0
    def test_watch_thread(self):
        """Watch then unwatch a thread."""
        u = user(save=True)
        self.client.login(username=u.username, password='******')

        t = thread(save=True)
        post(self.client, 'wiki.discuss.watch_thread', {'watch': 'yes'},
             args=[t.document.slug, t.id])
        assert NewPostEvent.is_notifying(u, t)
        # NewThreadEvent is not notifying.
        assert not NewThreadEvent.is_notifying(u, t.document)

        post(self.client, 'wiki.discuss.watch_thread', {'watch': 'no'},
             args=[t.document.slug, t.id])
        assert not NewPostEvent.is_notifying(u, t)
Esempio n. 9
0
def posts(request, document_slug, thread_id, form=None, post_preview=None):
    """View all the posts in a thread."""
    doc = get_document(document_slug, request)

    thread = get_object_or_404(Thread, pk=thread_id, document=doc)

    posts_ = thread.post_set.all()
    count = posts_.count()
    if count:
        last_post = posts_[count - 1]
    else:
        last_post = None
    posts_ = paginate(request, posts_, kbforums.POSTS_PER_PAGE)

    if not form:
        form = ReplyForm()

    feed_urls = ((reverse('wiki.discuss.posts.feed',
                          kwargs={'document_slug': document_slug,
                                  'thread_id': thread_id}),
                  PostsFeed().title(thread)),)

    is_watching_thread = (request.user.is_authenticated() and
                          NewPostEvent.is_notifying(request.user, thread))
    return render(request, 'kbforums/posts.html', {
        'document': doc, 'thread': thread,
        'posts': posts_, 'form': form,
        'count': count,
        'last_post': last_post,
        'post_preview': post_preview,
        'is_watching_thread': is_watching_thread,
        'feeds': feed_urls})
Esempio n. 10
0
def posts(request, document_slug, thread_id, form=None, post_preview=None):
    """View all the posts in a thread."""
    doc = get_document(document_slug, request)

    thread = get_object_or_404(Thread, pk=thread_id, document=doc)

    posts_ = thread.post_set.all()
    count = posts_.count()
    if count:
        last_post = posts_[count - 1]
    else:
        last_post = None
    posts_ = paginate(request, posts_, kbforums.POSTS_PER_PAGE)

    if not form:
        form = ReplyForm()

    feed_urls = ((reverse('wiki.discuss.posts.feed',
                          kwargs={'document_slug': document_slug,
                                  'thread_id': thread_id}),
                  PostsFeed().title(thread)),)

    is_watching_thread = (request.user.is_authenticated() and
                          NewPostEvent.is_notifying(request.user, thread))
    return render(request, 'kbforums/posts.html', {
        'document': doc, 'thread': thread,
        'posts': posts_, 'form': form,
        'count': count,
        'last_post': last_post,
        'post_preview': post_preview,
        'is_watching_thread': is_watching_thread,
        'feeds': feed_urls})
Esempio n. 11
0
def new_thread(request, document_slug):
    """Start a new thread."""
    doc = get_document(document_slug, request)

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

    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,
                                creator=request.user,
                                content=form.cleaned_data["content"])
        elif not _is_ratelimited(request):
            thread = doc.thread_set.create(creator=request.user,
                                           title=form.cleaned_data["title"])
            thread.save()
            post = thread.new_post(creator=request.user,
                                   content=form.cleaned_data["content"])
            post.save()

            # Send notifications to forum watchers.
            NewThreadEvent(post).fire(exclude=post.creator)

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

            return HttpResponseRedirect(
                reverse("wiki.discuss.posts", args=[document_slug, thread.id]))

    return render(
        request,
        "kbforums/new_thread.html",
        {
            "form": form,
            "document": doc,
            "post_preview": post_preview
        },
    )
Esempio n. 12
0
    def test_watch_thread(self):
        """Watch then unwatch a thread."""
        u = user(save=True)
        self.client.login(username=u.username, password='******')

        t = thread(save=True)
        post(self.client,
             'wiki.discuss.watch_thread', {'watch': 'yes'},
             args=[t.document.slug, t.id])
        assert NewPostEvent.is_notifying(u, t)
        # NewThreadEvent is not notifying.
        assert not NewThreadEvent.is_notifying(u, t.document)

        post(self.client,
             'wiki.discuss.watch_thread', {'watch': 'no'},
             args=[t.document.slug, t.id])
        assert not NewPostEvent.is_notifying(u, t)
Esempio n. 13
0
def new_thread(request, document_slug):
    """Start a new thread."""
    doc = get_document(document_slug, request)

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

    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,
                                creator=request.user,
                                content=form.cleaned_data['content'])
        elif not _is_ratelimited(request):
            thread = doc.thread_set.create(creator=request.user,
                                           title=form.cleaned_data['title'])
            thread.save()
            statsd.incr('kbforums.thread')
            post = thread.new_post(creator=request.user,
                                   content=form.cleaned_data['content'])
            post.save()

            # Send notifications to forum watchers.
            NewThreadEvent(post).fire(exclude=post.creator)

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

            return HttpResponseRedirect(
                reverse('wiki.discuss.posts', args=[document_slug, thread.id]))

    return render(request, 'kbforums/new_thread.html', {
        'form': form,
        'document': doc,
        'post_preview': post_preview
    })
Esempio n. 14
0
    def test_autowatch_reply(self, get_current):
        get_current.return_value.domain = "testserver"

        u = user(save=True)
        t1 = thread(is_locked=False, save=True)
        t2 = thread(is_locked=False, save=True)
        assert not NewPostEvent.is_notifying(u, t1)
        assert not NewPostEvent.is_notifying(u, t2)

        self.client.login(username=u.username, password="******")
        s = Setting.objects.create(user=u, name="kbforums_watch_after_reply", value="True")
        data = {"content": "some content"}
        post(self.client, "wiki.discuss.reply", data, args=[t1.document.slug, t1.pk])
        assert NewPostEvent.is_notifying(u, t1)

        s.value = "False"
        s.save()
        post(self.client, "wiki.discuss.reply", data, args=[t2.document.slug, t2.pk])
        assert not NewPostEvent.is_notifying(u, t2)
Esempio n. 15
0
 def _toggle_watch_thread_as(self, username, thread, turn_on=True):
     """Watch a thread and return it."""
     self.client.login(username=username, password="******")
     user = User.objects.get(username=username)
     watch = "yes" if turn_on else "no"
     post(
         self.client,
         "wiki.discuss.watch_thread",
         {"watch": watch},
         args=[thread.document.slug, thread.id],
     )
     # Watch exists or not, depending on watch.
     if turn_on:
         assert NewPostEvent.is_notifying(
             user, thread), "NewPostEvent should be notifying."
     else:
         assert not NewPostEvent.is_notifying(
             user, thread), "NewPostEvent should not be notifying."
     return thread
Esempio n. 16
0
    def test_autowatch_new_thread(self, get_current):
        """Creating a new thread should email responses"""
        get_current.return_value.domain = "testserver"

        d = document(save=True)
        u = user(save=True)
        self.client.login(username=u.username, password="******")
        s = Setting.objects.create(user=u, name="kbforums_watch_new_thread", value="False")
        data = {"title": "a title", "content": "a post"}
        post(self.client, "wiki.discuss.new_thread", data, args=[d.slug])

        t1 = thread(document=d, save=True)
        assert not NewPostEvent.is_notifying(u, t1), "NewPostEvent should not be notifying."

        s.value = "True"
        s.save()
        post(self.client, "wiki.discuss.new_thread", data, args=[d.slug])
        t2 = Thread.uncached.all().order_by("-id")[0]
        assert NewPostEvent.is_notifying(u, t2), "NewPostEvent should be notifying"
Esempio n. 17
0
    def test_autowatch_new_thread(self, get_current):
        """Creating a new thread should email responses"""
        get_current.return_value.domain = 'testserver'

        d = document(save=True)
        u = user(save=True)
        self.client.login(username=u.username, password='******')
        s = Setting.objects.create(user=u, name='kbforums_watch_new_thread',
                                   value='False')
        data = {'title': 'a title', 'content': 'a post'}
        post(self.client, 'wiki.discuss.new_thread', data, args=[d.slug])

        t1 = thread(document=d, save=True)
        assert not NewPostEvent.is_notifying(u, t1), (
            'NewPostEvent should not be notifying.')

        s.value = 'True'
        s.save()
        post(self.client, 'wiki.discuss.new_thread', data, args=[d.slug])
        t2 = Thread.uncached.all().order_by('-id')[0]
        assert NewPostEvent.is_notifying(u, t2), (
            'NewPostEvent should be notifying')
Esempio n. 18
0
    def test_autowatch_reply(self, get_current):
        get_current.return_value.domain = 'testserver'

        u = user(save=True)
        t1 = thread(is_locked=False, save=True)
        t2 = thread(is_locked=False, save=True)
        assert not NewPostEvent.is_notifying(u, t1)
        assert not NewPostEvent.is_notifying(u, t2)

        self.client.login(username=u.username, password='******')
        s = Setting.objects.create(user=u,
                                   name='kbforums_watch_after_reply',
                                   value='True')
        data = {'content': 'some content'}
        post(self.client, 'wiki.discuss.reply', data,
             args=[t1.document.slug, t1.pk])
        assert NewPostEvent.is_notifying(u, t1)

        s.value = 'False'
        s.save()
        post(self.client, 'wiki.discuss.reply', data,
             args=[t2.document.slug, t2.pk])
        assert not NewPostEvent.is_notifying(u, t2)
Esempio n. 19
0
    def test_autowatch_new_thread(self, get_current):
        """Creating a new thread should email responses"""
        get_current.return_value.domain = "testserver"

        d = DocumentFactory()
        u = UserFactory()
        self.client.login(username=u.username, password="******")
        s = Setting.objects.create(user=u,
                                   name="kbforums_watch_new_thread",
                                   value="False")
        data = {"title": "a title", "content": "a post"}
        post(self.client, "wiki.discuss.new_thread", data, args=[d.slug])

        t1 = ThreadFactory(document=d)
        assert not NewPostEvent.is_notifying(
            u, t1), "NewPostEvent should not be notifying."

        s.value = "True"
        s.save()
        post(self.client, "wiki.discuss.new_thread", data, args=[d.slug])
        t2 = Thread.objects.all().order_by("-id")[0]
        assert NewPostEvent.is_notifying(
            u, t2), "NewPostEvent should be notifying"
Esempio n. 20
0
    def test_watch_forum(self):
        """Watch then unwatch a forum."""
        u = UserFactory()
        self.client.login(username=u.username, password='******')

        d = DocumentFactory()
        post(self.client, 'wiki.discuss.watch_forum', {'watch': 'yes'},
             args=[d.slug])
        assert NewThreadEvent.is_notifying(u, d)
        # NewPostEvent is not notifying.
        t = ThreadFactory(document=d)
        p = t.new_post(creator=t.creator, content='test')
        assert not NewPostEvent.is_notifying(u, p)

        post(self.client, 'wiki.discuss.watch_forum', {'watch': 'no'}, args=[d.slug])
        assert not NewThreadEvent.is_notifying(u, d)
Esempio n. 21
0
def posts(request, document_slug, thread_id, form=None, post_preview=None):
    """View all the posts in a thread."""
    doc = get_document(document_slug, request)

    thread = get_object_or_404(Thread, pk=thread_id, document=doc)

    posts_ = thread.post_set.all()
    count = posts_.count()
    if count:
        last_post = posts_[count - 1]
    else:
        last_post = None
    posts_ = paginate(request, posts_, kbforums.POSTS_PER_PAGE)

    if not form:
        form = ReplyForm()

    feed_urls = ((
        reverse(
            "wiki.discuss.posts.feed",
            kwargs={
                "document_slug": document_slug,
                "thread_id": thread_id
            },
        ),
        PostsFeed().title(thread),
    ), )

    is_watching_thread = request.user.is_authenticated and NewPostEvent.is_notifying(
        request.user, thread)
    return render(
        request,
        "kbforums/posts.html",
        {
            "document": doc,
            "thread": thread,
            "posts": posts_,
            "form": form,
            "count": count,
            "last_post": last_post,
            "post_preview": post_preview,
            "is_watching_thread": is_watching_thread,
            "feeds": feed_urls,
        },
    )
Esempio n. 22
0
    def test_watch_forum(self):
        """Watch then unwatch a forum."""
        u = user(save=True)
        self.client.login(username=u.username, password='******')

        d = document(save=True)
        post(self.client, 'wiki.discuss.watch_forum', {'watch': 'yes'},
             args=[d.slug])
        assert NewThreadEvent.is_notifying(u, d)
        # NewPostEvent is not notifying.
        t = thread(document=d, save=True)
        p = t.new_post(creator=t.creator, content='test')
        #p = d.thread_set.all()[0].post_set.all()[0]
        assert not NewPostEvent.is_notifying(u, p)

        post(self.client, 'wiki.discuss.watch_forum', {'watch': 'no'},
             args=[d.slug])
        assert not NewThreadEvent.is_notifying(u, d)
Esempio n. 23
0
    def test_watch_forum(self):
        """Watch then unwatch a forum."""
        u = user(save=True)
        self.client.login(username=u.username, password='******')

        d = document(save=True)
        post(self.client,
             'wiki.discuss.watch_forum', {'watch': 'yes'},
             args=[d.slug])
        assert NewThreadEvent.is_notifying(u, d)
        # NewPostEvent is not notifying.
        t = thread(document=d, save=True)
        p = t.new_post(creator=t.creator, content='test')
        assert not NewPostEvent.is_notifying(u, p)

        post(self.client,
             'wiki.discuss.watch_forum', {'watch': 'no'},
             args=[d.slug])
        assert not NewThreadEvent.is_notifying(u, d)
Esempio n. 24
0
def posts(request, document_slug, thread_id, form=None, post_preview=None):
    """View all the posts in a thread."""
    doc = get_document(document_slug, request)

    thread = get_object_or_404(Thread, pk=thread_id, document=doc)

    posts_ = thread.post_set.all()
    count = posts_.count()
    if count:
        last_post = posts_[count - 1]
    else:
        last_post = None
    posts_ = paginate(request, posts_, kbforums.POSTS_PER_PAGE)

    if not form:
        form = ReplyForm()

    feed_urls = (
        (
            reverse("wiki.discuss.posts.feed", kwargs={"document_slug": document_slug, "thread_id": thread_id}),
            PostsFeed().title(thread),
        ),
    )

    is_watching_thread = request.user.is_authenticated() and NewPostEvent.is_notifying(request.user, thread)
    return render(
        request,
        "kbforums/posts.html",
        {
            "document": doc,
            "thread": thread,
            "posts": posts_,
            "form": form,
            "count": count,
            "last_post": last_post,
            "post_preview": post_preview,
            "is_watching_thread": is_watching_thread,
            "feeds": feed_urls,
        },
    )