From 3ffbe318d35204162a095c7d631ae136b9515554 Mon Sep 17 00:00:00 2001 From: artragis Date: Sun, 5 Jul 2015 16:04:51 +0200 Subject: [PATCH] fix #2873 avec mysql MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Mysql ne permet pas d'utiliser un queryset comme si c'était une liste, j'ai donc fait des "all" juste avant. --- zds/forum/managers.py | 15 +++++++++++++++ zds/forum/views.py | 14 +++++++++++++- zds/member/views.py | 4 ++-- 3 files changed, 30 insertions(+), 3 deletions(-) diff --git a/zds/forum/managers.py b/zds/forum/managers.py index 60ee5a3553..747dc49d68 100644 --- a/zds/forum/managers.py +++ b/zds/forum/managers.py @@ -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) @@ -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) diff --git a/zds/forum/views.py b/zds/forum/views.py index 28ed723b8d..61747a44c4 100644 --- a/zds/forum/views.py +++ b/zds/forum/views.py @@ -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( @@ -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) @@ -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']) diff --git a/zds/member/views.py b/zds/member/views.py index 0e39730d95..0196ee3a63 100644 --- a/zds/member/views.py +++ b/zds/member/views.py @@ -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