From 7071ac2f5a4018d882a718e35bf18d3c53705094 Mon Sep 17 00:00:00 2001 From: artragis Date: Wed, 21 Oct 2015 13:58:34 +0200 Subject: [PATCH 1/2] =?UTF-8?q?corrige=20le=20probl=C3=A8me=20d'url=20quan?= =?UTF-8?q?d=20on=20n'a=20jamais=20lu=20un=20topic?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- zds/forum/models.py | 34 ++++++++++++++++++++++------------ 1 file changed, 22 insertions(+), 12 deletions(-) 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( From 994a803f3f12c4e39da05a99f260bf754c8fb089 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?fran=C3=A7ois=20dambrine?= Date: Tue, 27 Oct 2015 07:59:10 +0100 Subject: [PATCH 2/2] ajout du test unitaire --- zds/forum/tests/tests_views.py | 35 ++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) 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()