Skip to content

Commit

Permalink
Merge pull request #3116 from artragis/patch-1
Browse files Browse the repository at this point in the history
[15.9 rc4]corrige le problème d'url quand on n'a jamais lu un topic
  • Loading branch information
pierre-24 committed Oct 27, 2015
2 parents 98b7c22 + 994a803 commit b09d8f2
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 12 deletions.
34 changes: 22 additions & 12 deletions zds/forum/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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(
Expand Down
35 changes: 35 additions & 0 deletions zds/forum/tests/tests_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down

0 comments on commit b09d8f2

Please sign in to comment.