def test_topic_notification_comment_handler(self):
     """
     set is_read=False when a comment is posted
     """
     comment = utils.create_comment(topic=self.topic)
     comment_posted.send(sender=self.topic.__class__, comment=comment, mentions=None)
     self.assertFalse(TopicNotification.objects.get(pk=self.topic_notification.pk).is_read)
Beispiel #2
0
def topic_publish(request, category_id=None):
    if category_id:
        Category.objects.get_public_or_404(pk=category_id)

    if request.method == 'POST':
        form = TopicForm(user=request.user, data=request.POST)
        cform = CommentForm(user=request.user, data=request.POST)

        if not request.is_limited and form.is_valid() and cform.is_valid():
            # wrap in transaction.atomic?
            topic = form.save()
            cform.topic = topic
            comment = cform.save()
            comment_posted.send(sender=comment.__class__,
                                comment=comment,
                                mentions=cform.mentions)
            return redirect(topic.get_absolute_url())
    else:
        form = TopicForm(user=request.user,
                         initial={
                             'category': category_id,
                         })
        cform = CommentForm()

    return render(request, 'spirit/topic/topic_publish.html', {
        'form': form,
        'cform': cform
    })
Beispiel #3
0
def comment_publish(request, topic_id, pk=None):
    topic = get_object_or_404(Topic.objects.for_access_open(request.user),
                              pk=topic_id)

    if request.method == 'POST':
        form = CommentForm(user=request.user, topic=topic, data=request.POST)

        if not request.is_limited and form.is_valid():
            comment = form.save()
            comment_posted.send(sender=comment.__class__,
                                comment=comment,
                                mentions=form.mentions)
            return redirect(
                request.POST.get('next', comment.get_absolute_url()))
    else:
        initial = None

        if pk:
            comment = get_object_or_404(Comment.objects.for_all(), pk=pk)
            quote = markdown.quotify(comment.comment, comment.user.username)
            initial = {
                'comment': quote,
            }

        form = CommentForm(initial=initial)

    return render(request, 'spirit/comment/comment_publish.html', {
        'form': form,
        'topic': topic
    })
Beispiel #4
0
def topic_publish(request, category_id=None):
    if category_id:
        Category.objects.get_public_or_404(pk=category_id)

    if request.method == 'POST':
        form = TopicForm(user=request.user, data=request.POST)
        cform = CommentForm(user=request.user, data=request.POST)
        pform = TopicPollForm(data=request.POST)
        pformset = TopicPollChoiceFormSet(can_delete=False, data=request.POST)

        if not request.is_limited and form.is_valid() and cform.is_valid() \
                and pform.is_valid() and pformset.is_valid():
            # wrap in transaction.atomic?
            topic = form.save()

            cform.topic = topic
            comment = cform.save()
            comment_posted.send(sender=comment.__class__, comment=comment, mentions=cform.mentions)

            # Create a poll only if we have choices
            if pformset.is_filled():
                pform.topic = topic
                poll = pform.save()
                pformset.instance = poll
                pformset.save()

            return redirect(topic.get_absolute_url())
    else:
        form = TopicForm(user=request.user, initial={'category': category_id, })
        cform = CommentForm()
        pform = TopicPollForm()
        pformset = TopicPollChoiceFormSet(can_delete=False)

    return render(request, 'spirit/topic/topic_publish.html', {'form': form, 'cform': cform,
                                                               'pform': pform, 'pformset': pformset})
Beispiel #5
0
def private_publish(request, user_id=None):
    if request.method == 'POST':
        tform = TopicForPrivateForm(user=request.user, data=request.POST)
        cform = CommentForm(user=request.user, data=request.POST)
        tpform = TopicPrivateManyForm(user=request.user, data=request.POST)

        if not request.is_limited and tform.is_valid() and cform.is_valid() and tpform.is_valid():
            # wrap in transaction.atomic?
            topic = tform.save()
            cform.topic = topic
            comment = cform.save()
            comment_posted.send(sender=comment.__class__, comment=comment, mentions=None)
            tpform.topic = topic
            topics_private = tpform.save_m2m()
            topic_private_post_create.send(sender=TopicPrivate, topics_private=topics_private, comment=comment)
            return redirect(topic.get_absolute_url())

    else:
        tform = TopicForPrivateForm()
        cform = CommentForm()
        initial = None

        if user_id:
            user = get_object_or_404(User, pk=user_id)
            initial = {'users': [user.username, ]}

        tpform = TopicPrivateManyForm(initial=initial)

    return render(request, 'spirit/topic_private/private_publish.html', {'tform': tform,
                                                                         'cform': cform,
                                                                         'tpform': tpform})
Beispiel #6
0
 def test_topic_comment_posted_handler(self):
     """
     comment_posted_handler signal
     """
     comment = utils.create_comment(topic=self.topic)
     comment_posted.send(sender=comment.__class__, comment=comment, mentions=None)
     self.assertEqual(Topic.objects.get(pk=self.topic.pk).comment_count, 1)
     self.assertGreater(Topic.objects.get(pk=self.topic.pk).last_active, self.topic.last_active)
 def test_topic_notification_comment_handler_unactive(self):
     """
     do nothing if notification is_active=False
     """
     TopicNotification.objects.filter(pk=self.topic_notification.pk).update(is_active=False)
     comment = utils.create_comment(topic=self.topic_notification.topic)
     comment_posted.send(sender=self.topic.__class__, comment=comment, mentions=None)
     self.assertTrue(TopicNotification.objects.get(pk=self.topic_notification.pk).is_read)
Beispiel #8
0
 def test_topic_unread_bulk_handler(self):
     """
     mark as unread when comment posted
     """
     TopicUnread.objects.all().update(is_read=True)
     comment = utils.create_comment(user=self.user, topic=self.topic)
     comment_posted.send(sender=self.topic.__class__, comment=comment, mentions=None)
     self.assertTrue(TopicUnread.objects.get(user=self.user, topic=self.topic).is_read)
     self.assertFalse(TopicUnread.objects.get(user=self.user2, topic=self.topic).is_read)
 def test_topic_notification_mention_handler(self):
     """
     create notification on mention
     """
     topic = utils.create_topic(self.category)
     mentions = {self.user.username: self.user, }
     comment = utils.create_comment(topic=topic)
     comment_posted.send(sender=self.topic.__class__, comment=comment, mentions=mentions)
     self.assertEqual(TopicNotification.objects.get(user=self.user, comment=comment).action, MENTION)
     self.assertFalse(TopicNotification.objects.get(user=self.user, comment=comment).is_read)
Beispiel #10
0
 def test_topic_notification_comment_handler(self):
     """
     set is_read=False when a comment is posted
     """
     comment = utils.create_comment(topic=self.topic)
     comment_posted.send(sender=self.topic.__class__,
                         comment=comment,
                         mentions=None)
     self.assertFalse(
         TopicNotification.objects.get(
             pk=self.topic_notification.pk).is_read)
 def test_topic_notification_mention_handler_unactive(self):
     """
     set is_read=False when user gets mentioned
     even if is_active=False
     """
     TopicNotification.objects.filter(pk=self.topic_notification.pk).update(is_active=False)
     mentions = {self.user.username: self.user, }
     comment = utils.create_comment(topic=self.topic_notification.topic)
     comment_posted.send(sender=self.topic.__class__, comment=comment, mentions=mentions)
     self.assertEqual(TopicNotification.objects.get(pk=self.topic_notification.pk).action, MENTION)
     self.assertFalse(TopicNotification.objects.get(pk=self.topic_notification.pk).is_read)
Beispiel #12
0
 def test_topic_comment_posted_handler(self):
     """
     comment_posted_handler signal
     """
     comment = utils.create_comment(topic=self.topic)
     comment_posted.send(sender=comment.__class__,
                         comment=comment,
                         mentions=None)
     self.assertEqual(Topic.objects.get(pk=self.topic.pk).comment_count, 1)
     self.assertGreater(
         Topic.objects.get(pk=self.topic.pk).last_active,
         self.topic.last_active)
Beispiel #13
0
 def test_topic_notification_comment_handler_unactive(self):
     """
     do nothing if notification is_active=False
     """
     TopicNotification.objects.filter(pk=self.topic_notification.pk).update(
         is_active=False)
     comment = utils.create_comment(topic=self.topic_notification.topic)
     comment_posted.send(sender=self.topic.__class__,
                         comment=comment,
                         mentions=None)
     self.assertTrue(
         TopicNotification.objects.get(
             pk=self.topic_notification.pk).is_read)
Beispiel #14
0
 def test_topic_unread_bulk_handler(self):
     """
     mark as unread when comment posted
     """
     TopicUnread.objects.all().update(is_read=True)
     comment = utils.create_comment(user=self.user, topic=self.topic)
     comment_posted.send(sender=self.topic.__class__,
                         comment=comment,
                         mentions=None)
     self.assertTrue(
         TopicUnread.objects.get(user=self.user, topic=self.topic).is_read)
     self.assertFalse(
         TopicUnread.objects.get(user=self.user2, topic=self.topic).is_read)
Beispiel #15
0
def comment_move(request, topic_id):
    # TODO: comment_move signal (update topic comment_count)
    topic = get_object_or_404(Topic, pk=topic_id)
    form = CommentMoveForm(topic=topic, data=request.POST)

    if form.is_valid():
        comments = form.save()

        for comment in comments:
            comment_posted.send(sender=comment.__class__, comment=comment, mentions=None)
    else:
        messages.error(request, render_form_errors(form))

    return redirect(request.POST.get('next', topic.get_absolute_url()))
Beispiel #16
0
def comment_move(request, topic_id):
    # TODO: comment_move signal (update topic comment_count)
    topic = get_object_or_404(Topic, pk=topic_id)
    form = CommentMoveForm(topic=topic, data=request.POST)

    if form.is_valid():
        comments = form.save()

        for comment in comments:
            comment_posted.send(sender=comment.__class__,
                                comment=comment,
                                mentions=None)
    else:
        messages.error(request, render_form_errors(form))

    return redirect(request.POST.get('next', topic.get_absolute_url()))
Beispiel #17
0
 def test_topic_notification_mention_handler(self):
     """
     create notification on mention
     """
     topic = utils.create_topic(self.category)
     mentions = {
         self.user.username: self.user,
     }
     comment = utils.create_comment(topic=topic)
     comment_posted.send(sender=self.topic.__class__,
                         comment=comment,
                         mentions=mentions)
     self.assertEqual(
         TopicNotification.objects.get(user=self.user,
                                       comment=comment).action, MENTION)
     self.assertFalse(
         TopicNotification.objects.get(user=self.user,
                                       comment=comment).is_read)
Beispiel #18
0
def topic_publish(request, category_id=None):
    if category_id:
        Category.objects.get_public_or_404(pk=category_id)

    if request.method == 'POST':
        form = TopicForm(user=request.user, data=request.POST)
        cform = CommentForm(user=request.user, data=request.POST)
        pform = TopicPollForm(data=request.POST)
        pformset = TopicPollChoiceFormSet(can_delete=False, data=request.POST)

        if not request.is_limited and form.is_valid() and cform.is_valid() \
                and pform.is_valid() and pformset.is_valid():
            # wrap in transaction.atomic?
            topic = form.save()

            cform.topic = topic
            comment = cform.save()
            comment_posted.send(sender=comment.__class__,
                                comment=comment,
                                mentions=cform.mentions)

            # Create a poll only if we have choices
            if pformset.is_filled():
                pform.topic = topic
                poll = pform.save()
                pformset.instance = poll
                pformset.save()

            return redirect(topic.get_absolute_url())
    else:
        form = TopicForm(user=request.user,
                         initial={
                             'category': category_id,
                         })
        cform = CommentForm()
        pform = TopicPollForm()
        pformset = TopicPollChoiceFormSet(can_delete=False)

    return render(request, 'spirit/topic/topic_publish.html', {
        'form': form,
        'cform': cform,
        'pform': pform,
        'pformset': pformset
    })
Beispiel #19
0
 def test_topic_notification_mention_handler_unactive(self):
     """
     set is_read=False when user gets mentioned
     even if is_active=False
     """
     TopicNotification.objects.filter(pk=self.topic_notification.pk).update(
         is_active=False)
     mentions = {
         self.user.username: self.user,
     }
     comment = utils.create_comment(topic=self.topic_notification.topic)
     comment_posted.send(sender=self.topic.__class__,
                         comment=comment,
                         mentions=mentions)
     self.assertEqual(
         TopicNotification.objects.get(
             pk=self.topic_notification.pk).action, MENTION)
     self.assertFalse(
         TopicNotification.objects.get(
             pk=self.topic_notification.pk).is_read)
Beispiel #20
0
def topic_publish(request, category_id=None):
    if category_id:
        Category.objects.get_public_or_404(pk=category_id)

    if request.method == 'POST':
        form = TopicForm(user=request.user, data=request.POST)
        cform = CommentForm(user=request.user, data=request.POST)

        if not request.is_limited and form.is_valid() and cform.is_valid():
            # wrap in transaction.atomic?
            topic = form.save()
            cform.topic = topic
            comment = cform.save()
            comment_posted.send(sender=comment.__class__, comment=comment, mentions=cform.mentions)
            return redirect(topic.get_absolute_url())
    else:
        form = TopicForm(user=request.user, initial={'category': category_id, })
        cform = CommentForm()

    return render(request, 'spirit/topic/topic_publish.html', {'form': form, 'cform': cform})
Beispiel #21
0
def comment_publish(request, topic_id, pk=None):
    topic = Topic.objects.for_access_or_404(pk=topic_id, user=request.user)

    if request.method == 'POST':
        form = CommentForm(user=request.user, topic=topic, data=request.POST)

        if not request.is_limited and form.is_valid():
            comment = form.save()
            comment_posted.send(sender=comment.__class__, comment=comment, mentions=form.mentions)
            return redirect(request.POST.get('next', comment.get_absolute_url()))
    else:
        initial = None

        if pk:
            comment = get_object_or_404(Comment.objects.for_all(), pk=pk)
            quote = markdown.quotify(comment.comment, comment.user.username)
            initial = {'comment': quote, }

        form = CommentForm(initial=initial)

    return render(request, 'spirit/comment/comment_publish.html', {'form': form, 'topic': topic})