Skip to content

Commit

Permalink
Documentation et transformation en liste.
Browse files Browse the repository at this point in the history
  • Loading branch information
artragis committed Jul 6, 2015
1 parent 3820047 commit e2d1d06
Show file tree
Hide file tree
Showing 2 changed files with 22 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)
10 changes: 7 additions & 3 deletions zds/forum/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,9 @@ def get(self, request, *args, **kwargs):

def get_context_data(self, **kwargs):
context = super(ForumTopicsListView, self).get_context_data(**kwargs)
context['topics'] = context['topics'].all()
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 @@ -87,7 +89,7 @@ def get_context_data(self, **kwargs):
def get_object(self, queryset=None):
forum = Forum.objects\
.select_related('category')\
.filter( slug=self.kwargs.get('forum_slug'))\
.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')))
Expand Down Expand Up @@ -361,7 +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'] = context['topics'].all()
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

0 comments on commit e2d1d06

Please sign in to comment.