threadslist.py 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. from datetime import timedelta
  2. from django.conf import settings
  3. from django.core.exceptions import PermissionDenied
  4. from django.db.models import F, Q
  5. from django.http import Http404
  6. from django.shortcuts import render
  7. from django.views.generic import View
  8. from django.utils import timezone
  9. from django.utils.translation import ugettext as _, ugettext_lazy
  10. from misago.categories.models import CATEGORIES_TREE_ID, Category
  11. from misago.categories.permissions import (
  12. allow_see_category, allow_browse_category)
  13. from misago.core.shortcuts import (
  14. get_object_or_404, paginate, pagination_dict, validate_slug)
  15. from misago.readtracker import threadstracker
  16. from misago.threads.models import Thread
  17. from misago.threads.permissions import exclude_invisible_threads
  18. from misago.threads.utils import add_categories_to_threads
  19. LISTS_NAMES = {
  20. 'my': ugettext_lazy("Your threads"),
  21. 'new': ugettext_lazy("New threads"),
  22. 'unread': ugettext_lazy("Unread threads"),
  23. 'subscribed': ugettext_lazy("Subscribed threads"),
  24. }
  25. def filter_threads_queryset(user, categories, list_type, queryset):
  26. if list_type == 'my':
  27. return queryset.filter(starter=user)
  28. elif list_type == 'subscribed':
  29. return queryset # TODO: filter(id__in=[subscribed threads])
  30. else:
  31. # grab cutoffs for categories
  32. cutoff_date = timezone.now() - timedelta(
  33. days=settings.MISAGO_FRESH_CONTENT_PERIOD
  34. )
  35. if cutoff_date < user.reads_cutoff:
  36. cutoff_date = user.reads_cutoff
  37. categories_dict = {}
  38. for record in user.categoryread_set.filter(category__in=categories):
  39. if record.last_read_on > cutoff_date:
  40. categories_dict[record.category_id] = record.last_read_on
  41. if list_type == 'new':
  42. # new threads have no entry in reads table
  43. # AND were started after cutoff date
  44. read_threads = user.threadread_set.values('thread_id')
  45. if categories_dict:
  46. condition = Q(
  47. started_on__lte=cutoff_date,
  48. id__in=read_threads,
  49. )
  50. for category_id, category_cutoff in categories_dict.items():
  51. condition = condition | Q(
  52. category_id=category_id,
  53. started_on__lte=category_cutoff,
  54. )
  55. return queryset.exclude(condition)
  56. else:
  57. return queryset.exclude(
  58. started_on__lte=cutoff_date,
  59. id__in=read_threads,
  60. )
  61. elif list_type == 'unread':
  62. # unread threads were read in past but have new posts
  63. # after cutoff date
  64. read_threads = user.threadread_set.filter(
  65. thread__last_post_on__gt=cutoff_date,
  66. last_read_on__lt=F('thread__last_post_on')
  67. ).values('thread_id')
  68. queryset = queryset.filter(id__in=read_threads)
  69. # unread threads have last reply after read/cutoff date
  70. if categories_dict:
  71. conditions = None
  72. for category_id, category_cutoff in categories_dict.items():
  73. condition = Q(
  74. category_id=category_id,
  75. last_post_on__lte=category_cutoff,
  76. )
  77. if conditions:
  78. conditions = conditions | condition
  79. else:
  80. conditions = condition
  81. return queryset.exclude(conditions)
  82. else:
  83. return queryset
  84. def get_threads_queryset(user, categories, list_type):
  85. queryset = Thread.objects
  86. queryset = exclude_invisible_threads(user, categories, queryset)
  87. if list_type == 'all':
  88. return queryset
  89. else:
  90. return filter_threads_queryset(user, categories, list_type, queryset)
  91. class BaseList(View):
  92. template_name = 'misago/threadslist/threads.html'
  93. preloaded_data_prefix = ''
  94. def allow_see_list(self, request, category, list_type):
  95. if request.user.is_anonymous():
  96. if list_type == 'my':
  97. raise PermissionDenied( _("You have to sign in to see list of "
  98. "threads that you have started."))
  99. if list_type == 'new':
  100. raise PermissionDenied(_("You have to sign in to see list of "
  101. "threads you haven't read."))
  102. if list_type == 'unread':
  103. raise PermissionDenied(_("You have to sign in to see list of "
  104. "threads with new replies."))
  105. if list_type == 'subscribed':
  106. raise PermissionDenied(_("You have to sign in to see list of "
  107. "threads you are subscribing."))
  108. def get_subcategories(self, request, category):
  109. if category.is_leaf_node():
  110. return []
  111. visible_categories = request.user.acl['visible_categories']
  112. queryset = category.get_descendants().filter(id__in=visible_categories)
  113. return list(queryset.order_by('lft'))
  114. def get_extra_context(self, request, category, subcategories, list_type):
  115. return {}
  116. def get(self, request, **kwargs):
  117. try:
  118. page = int(request.GET.get('page', 0))
  119. if page == 1:
  120. page = None
  121. except ValueError:
  122. raise Http404()
  123. list_type = kwargs['list_type']
  124. category = self.get_category(request, **kwargs)
  125. self.allow_see_list(request, category, list_type)
  126. subcategories = self.get_subcategories(request, category)
  127. categories = [category] + subcategories
  128. queryset = self.get_queryset(
  129. request, categories, list_type).order_by('-last_post_on')
  130. page = paginate(queryset, page, 24, 6)
  131. paginator = pagination_dict(page)
  132. threadstracker.make_threads_read_aware(request.user, page.object_list)
  133. add_categories_to_threads(categories, page.object_list)
  134. visible_subcategories = []
  135. for thread in page.object_list:
  136. if (thread.top_category and
  137. thread.top_category not in visible_subcategories):
  138. visible_subcategories.append(thread.top_category.pk)
  139. category.subcategories = []
  140. for subcategory in subcategories:
  141. if subcategory.pk in visible_subcategories:
  142. category.subcategories.append(subcategory)
  143. extra_context = self.get_extra_context(
  144. request, category, subcategories, list_type)
  145. show_toolbar = False
  146. if paginator['count']:
  147. if category.subcategories:
  148. show_toolbar = True
  149. if request.user.is_authenticated():
  150. show_toolbar = True
  151. return render(request, self.template_name, dict(
  152. category=category,
  153. show_toolbar=show_toolbar,
  154. list_type=list_type,
  155. list_name=LISTS_NAMES.get(list_type),
  156. threads=page.object_list,
  157. paginator=paginator,
  158. count=paginator['count'],
  159. **extra_context
  160. ))
  161. class ThreadsList(BaseList):
  162. template_name = 'misago/threadslist/threads.html'
  163. def get_category(self, request, **kwargs):
  164. return Category.objects.root_category()
  165. def get_queryset(self, request, categories, list_type):
  166. # [:1] cos we are cutting off root caregory on forum threads list
  167. # as it includes nedless extra condition to DB filter
  168. return get_threads_queryset(request.user, categories[1:], list_type)
  169. def get_extra_context(self, request, category, subcategories, list_type):
  170. return {
  171. 'is_index': not settings.MISAGO_CATEGORIES_ON_INDEX
  172. }
  173. class CategoryThreadsList(ThreadsList):
  174. template_name = 'misago/threadslist/category.html'
  175. preloaded_data_prefix = 'CATEGORY_'
  176. def get_category(self, request, **kwargs):
  177. category = get_object_or_404(Category.objects.select_related('parent'),
  178. tree_id=CATEGORIES_TREE_ID,
  179. id=kwargs['category_id'],
  180. )
  181. allow_see_category(request.user, category)
  182. allow_browse_category(request.user, category)
  183. validate_slug(category, kwargs['category_slug'])
  184. return category
  185. def get_queryset(self, request, categories, list_type):
  186. return get_threads_queryset(request.user, categories, list_type)
  187. class PrivateThreadsList(ThreadsList):
  188. template_name = 'misago/threadslist/private_threads.html'
  189. preloaded_data_prefix = 'PRIVATE_'
  190. def get_category(self, request, **kwargs):
  191. return Category.objects.private_threads()
  192. def get_subcategories(self, request, category):
  193. return []