def perform_unread_message(post, user): """ Marks a post unread so we create a notification between the user and the topic host of the post. But, if there is only one post in the topic, we mark the topic unread but we don't create a notification. """ if TopicFollowed.objects.filter(user=user, topic=post.topic).count() == 0: TopicFollowed(user=user, topic=post.topic).save() topic_read = TopicRead.objects.filter(topic=post.topic, user=user).first() if topic_read is None and post.position > 1: unread = Post.objects.filter(topic=post.topic, position=(post.position - 1)).first() topic_read = TopicRead(post=unread, topic=unread.topic, user=user) topic_read.save() else: if post.position > 1: unread = Post.objects.filter(topic=post.topic, position=(post.position - 1)).first() topic_read.post = unread topic_read.save() else: topic_read.delete()
def perform_unread_message(post, user): """ Marks a post unread so we create a notification between the user and the topic host of the post. But, if there is only one post in the topic, we mark the topic unread but we don't create a notification. """ topic_read = TopicRead.objects.filter(topic=post.topic, user=user).first() # issue 3227 proves that you can have post.position==1 AND topic_read to None # it can happen whether on double click (the event "mark as not read" is therefore sent twice) # or if you have two tabs in your browser. if topic_read is None and post.position > 1: unread = Post.objects.filter(topic=post.topic, position=(post.position - 1)).first() topic_read = TopicRead(post=unread, topic=unread.topic, user=user) topic_read.save() else: if post.position > 1: unread = Post.objects.filter(topic=post.topic, position=(post.position - 1)).first() topic_read.post = unread topic_read.save() elif topic_read: topic_read.delete() signals.answer_unread.send(sender=post.topic.__class__, instance=post, user=user)
def unread_post(request): """Marks a message as unread """ try: post_pk = request.GET["message"] except: # problem in variable format raise Http404 post = get_object_or_404(Post, pk=post_pk) # check that author can access the forum if not post.topic.forum.can_read(request.user): raise PermissionDenied t = TopicRead.objects.filter(topic=post.topic, user=request.user).first() if t is None: if post.position > 1: unread = Post.objects.filter(topic=post.topic, position=(post.position - 1)).first() t = TopicRead(post=unread, topic=unread.topic, user=request.user) t.save() else: if post.position > 1: unread = Post.objects.filter(topic=post.topic, position=(post.position - 1)).first() t.post = unread t.save() else: t.delete() return redirect(reverse("zds.forum.views.details", args=[post.topic.forum.category.slug, post.topic.forum.slug]))
def perform_unread_message(post, user): """ Marks a post unread so we create a notification between the user and the topic host of the post. But, if there is only one post in the topic, we mark the topic unread but we don't create a notification. """ topic_read = TopicRead.objects.filter(topic=post.topic, user=user).first() if topic_read is None and post.position > 1: unread = Post.objects.filter(topic=post.topic, position=(post.position - 1)).first() topic_read = TopicRead(post=unread, topic=unread.topic, user=user) topic_read.save() else: if post.position > 1: unread = Post.objects.filter(topic=post.topic, position=(post.position - 1)).first() topic_read.post = unread topic_read.save() else: topic_read.delete() signals.answer_unread.send(sender=post.topic.__class__, instance=post, user=user)
def unread_post(request): """Marks a message as unread """ try: post_pk = request.GET["message"] except KeyError: raise Http404 post = get_object_or_404(Post, pk=post_pk) # check that author can access the forum if not post.topic.forum.can_read(request.user): raise PermissionDenied t = TopicRead.objects.filter(topic=post.topic, user=request.user).first() if t is None: if post.position > 1: unread = Post.objects.filter(topic=post.topic, position=(post.position - 1)).first() t = TopicRead(post=unread, topic=unread.topic, user=request.user) t.save() else: if post.position > 1: unread = Post.objects.filter(topic=post.topic, position=(post.position - 1)).first() t.post = unread t.save() else: t.delete() return redirect(reverse("zds.forum.views.details", args=[post.topic.forum.category.slug, post.topic.forum.slug]))
def test_answer(self): """To test all aspects of answer.""" user1 = ProfileFactory().user user2 = ProfileFactory().user topic1 = TopicFactory(forum=self.forum11, author=self.user) post1 = PostFactory(topic=topic1, author=self.user, position=1) post2 = PostFactory(topic=topic1, author=self.user, position=2) post3 = PostFactory(topic=topic1, author=user1, position=3) TopicRead(topic=topic1, user=user1, post=post3).save() TopicRead(topic=topic1, user=user2, post=post3).save() TopicRead(topic=topic1, user=self.user, post=post3).save() TopicFollowed(topic=topic1, user=user1, email=True).save() TopicFollowed(topic=topic1, user=user2, email=True).save() TopicFollowed(topic=topic1, user=self.user, email=True).save() # check if we send ane empty text result = self.client.post(reverse('zds.forum.views.answer') + '?sujet={0}'.format(topic1.pk), { 'last_post': topic1.last_message.pk, 'text': u'' }, follow=False) self.assertEqual(result.status_code, 200) # check topic's number self.assertEqual(Topic.objects.all().count(), 1) # check post's number (should be 3 for the moment) self.assertEqual(Post.objects.all().count(), 3) # now check what happen if everything is fine result = self.client.post(reverse( 'zds.forum.views.answer' ) + '?sujet={0}'.format(topic1.pk), { 'last_post': topic1.last_message.pk, 'text': u'C\'est tout simplement l\'histoire de la ville de Paris que je voudrais vous conter ' }, follow=False) self.assertEqual(result.status_code, 302) self.assertEquals(len(mail.outbox), 2) # check topic's number self.assertEqual(Topic.objects.all().count(), 1) # check post's number self.assertEqual(Post.objects.all().count(), 4) # check topic and post self.assertEqual(post1.topic, topic1) self.assertEqual(post2.topic, topic1) self.assertEqual(post3.topic, topic1) # check values post_final = Post.objects.last() self.assertEqual(post_final.topic, topic1) self.assertEqual(post_final.position, 4) self.assertEqual(post_final.editor, None) self.assertEqual( post_final.text, u'C\'est tout simplement l\'histoire de la ville de Paris que je voudrais vous conter ' ) # test antispam return 403 result = self.client.post(reverse('zds.forum.views.answer') + '?sujet={0}'.format(topic1.pk), { 'last_post': topic1.last_message.pk, 'text': u'Testons l\'antispam' }, follow=False) self.assertEqual(result.status_code, 403)