diff --git a/zds/forum/models.py b/zds/forum/models.py index 717dc3edc5..2a6d61da5a 100644 --- a/zds/forum/models.py +++ b/zds/forum/models.py @@ -2,6 +2,7 @@ from django.conf import settings from django.db import models +from zds.settings import ZDS_APP from zds.utils import slugify from math import ceil import os @@ -307,24 +308,33 @@ def resolve_last_read_post_absolute_url(self): if user is None or not user.is_authenticated(): return self.resolve_first_post_url() else: - return '{0}?page=1#p{1}'.format( - self.get_absolute_url(), - self.resolve_last_post_pk_read_by_user(user)) + try: + pk, pos = self.resolve_last_post_pk_and_pos_read_by_user(user) + return '{}?page={}#p{}'.format( + self.get_absolute_url(), + pos / ZDS_APP["forum"]["posts_per_page"] + 1, pk) + except TopicRead.DoesNotExist: + return self.resolve_first_post_url() - def resolve_last_post_pk_read_by_user(self, user): - """get the primary key of the last post the user read + def resolve_last_post_pk_and_pos_read_by_user(self, user): + """get the primary key and position of the last post the user read :param user: the current (authenticated) user. Please do not try with unauthenticated user, il would lead to a \ useless request. :return: the primary key :rtype: int """ - return TopicRead.objects\ - .select_related('post')\ - .filter(topic__pk=self.pk, - user__pk=user.pk) \ - .latest('post__position')\ - .pk + t_read = TopicRead.objects\ + .select_related('post')\ + .filter(topic__pk=self.pk, + user__pk=user.pk) \ + .latest('post__position') + if t_read: + return t_read.post.pk, t_read.post.position + return Post.objects\ + .filter(topic__pk=self.pk)\ + .order_by('position')\ + .values('pk', "position").first().values() def resolve_first_post_url(self): """resolve the url that leads to this topic first post @@ -333,7 +343,7 @@ def resolve_first_post_url(self): """ pk = Post.objects\ .filter(topic__pk=self.pk)\ - .order_by('-position')\ + .order_by('position')\ .values('pk').first() return '{0}?page=1#p{1}'.format( diff --git a/zds/forum/tests/tests_views.py b/zds/forum/tests/tests_views.py index 250ef04be2..72345974a0 100644 --- a/zds/forum/tests/tests_views.py +++ b/zds/forum/tests/tests_views.py @@ -252,6 +252,41 @@ def test_success_create_topic_with_a_post_in_get_method(self): self.assertEqual(forum, response.context['forum']) self.assertIsNotNone(response.context['form']) + def test_last_read_topic_url(self): + profile = ProfileFactory() + profile2 = ProfileFactory() + notvisited = ProfileFactory() + category, forum = create_category() + self.assertTrue(self.client.login(username=profile.user.username, password='hostel77')) + data = { + 'title': 'Title of the topic', + 'subtitle': 'Subtitle of the topic', + 'text': 'A new post!' + } + self.client.post(reverse('topic-new') + '?forum={}'.format(forum.pk), data, follow=False) + self.client.logout() + self.assertTrue(self.client.login(username=profile2.user.username, password='hostel77')) + data = { + 'title': 'Title of the topic', + 'subtitle': 'Subtitle of the topic', + 'text': 'A new post!' + } + self.client.post(reverse('topic-new') + '?forum={}'.format(forum.pk), data, follow=False) + self.client.logout() + self.assertTrue(self.client.login(username=profile.user.username, password='hostel77')) + topic = Topic.objects.last() + post = Post.objects.filter(topic__pk=topic.pk).first() + # for user + url = topic.resolve_last_read_post_absolute_url() + self.assertEquals(url, topic.get_absolute_url() + "?page=1#p" + str(post.pk)) + + # for anonymous + self.client.logout() + self.assertEquals(url, topic.get_absolute_url() + "?page=1#p" + str(post.pk)) + # for no visit + self.assertTrue(self.client.login(username=notvisited.user.username, password='hostel77')) + self.assertEquals(url, topic.get_absolute_url() + "?page=1#p" + str(post.pk)) + def test_success_create_topic_with_post_in_preview_in_ajax(self): profile = ProfileFactory() category, forum = create_category()