Skip to content

Commit

Permalink
Merge pull request #2907 from artragis/patch-10
Browse files Browse the repository at this point in the history
[hotfix v15.6] fix #2873 avec mysql
  • Loading branch information
SpaceFox committed Jul 13, 2015
2 parents 4462e6f + 3ffbe31 commit 4c0ab5c
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 3 deletions.
15 changes: 15 additions & 0 deletions zds/forum/managers.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,14 @@ def get_all_messages_of_a_user(self, current, target):
class TopicReadManager(models.Manager):

def topic_read_by_user(self, user, topic_sub_list=None):
""" get all the topic that the user has already read.
:param user: an authenticated user
:param topic_sub_list: optional list of topics. If not ``None`` no subject out of this list will be selected
:type topic_sub_list: list
:return: the queryset over the already read topics
:rtype: QuerySet
"""
base_query_set = self.filter(user__pk=user.pk)
if topic_sub_list is not None:
base_query_set = base_query_set.filter(topic__in=topic_sub_list)
Expand All @@ -107,4 +114,12 @@ def topic_read_by_user(self, user, topic_sub_list=None):
return base_query_set

def list_read_topic_pk(self, user, topic_sub_list=None):
""" get all the topic that the user has already read in a flat list.
:param user: an authenticated user
:param topic_sub_list: optional list of topics. If not ``None`` no subject out of this list will be selected
:type topic_sub_list: list
:return: the flat list of all topics primary key
:rtype: list
"""
return self.topic_read_by_user(user, topic_sub_list).values_list('topic__pk', flat=True)
14 changes: 13 additions & 1 deletion zds/forum/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,9 @@ def get(self, request, *args, **kwargs):

def get_context_data(self, **kwargs):
context = super(ForumTopicsListView, self).get_context_data(**kwargs)
context['topics'] = list(context['topics'].all())
# we need to load it in memory because later we will get the
# "already read topic" set out of this list and MySQL does not support that type of subquery
context.update({
'forum': self.object,
'sticky_topics': self.filter_queryset(
Expand All @@ -84,7 +87,13 @@ def get_context_data(self, **kwargs):
return context

def get_object(self, queryset=None):
return get_object_or_404(Forum, slug=self.kwargs.get('forum_slug'))
forum = Forum.objects\
.select_related('category')\
.filter(slug=self.kwargs.get('forum_slug'))\
.first()
if forum is None:
raise Http404("Forum with slug {} was not found".format(self.kwargs.get('forum_slug')))
return forum

def get_queryset(self):
self.queryset = Topic.objects.get_all_topics_of_a_forum(self.object.pk)
Expand Down Expand Up @@ -354,6 +363,9 @@ def get(self, request, *args, **kwargs):

def get_context_data(self, *args, **kwargs):
context = super(FindTopicByTag, self).get_context_data(*args, **kwargs)
context['topics'] = list(context['topics'].all())
# we need to load it in memory because later we will get the
# "already read topic" set out of this list and MySQL does not support that type of subquery
context.update({
'tag': self.object,
'topic_read': TopicRead.objects.list_read_topic_pk(self.request.user, context['topics'])
Expand Down
4 changes: 2 additions & 2 deletions zds/member/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,14 +74,14 @@ def get_context_data(self, **kwargs):
usr = context['usr']
profile = usr.profile
context['profile'] = profile
context['topics'] = Topic.objects.last_topics_of_a_member(usr, self.request.user)
context['topics'] = list(Topic.objects.last_topics_of_a_member(usr, self.request.user))
context['articles'] = Article.objects.last_articles_of_a_member_loaded(usr)
context['tutorials'] = Tutorial.objects.last_tutorials_of_a_member_loaded(usr)
context['old_tutos'] = Profile.objects.all_old_tutos_from_site_du_zero(profile)
context['karmanotes'] = KarmaNote.objects.filter(user=usr).order_by('-create_at')
context['karmaform'] = KarmaForm(profile)
context['form'] = OldTutoForm(profile)
context['topic_read'] = TopicRead.objects.list_read_topic_pk(usr, context['topics'])
context['topic_read'] = TopicRead.objects.list_read_topic_pk(self.request.user, context['topics'])
return context


Expand Down

0 comments on commit 4c0ab5c

Please sign in to comment.