threads.py 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  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.utils import timezone
  7. from django.utils.translation import ugettext as _
  8. from django.utils.translation import ugettext_lazy
  9. from misago.acl import add_acl
  10. from misago.core.shortcuts import paginate, pagination_dict
  11. from misago.readtracker import threadstracker
  12. from ..models import Thread
  13. from ..permissions import exclude_invisible_threads
  14. from ..serializers import ThreadListSerializer
  15. from ..subscriptions import make_subscription_aware
  16. from ..utils import add_categories_to_threads
  17. LISTS_NAMES = {
  18. 'all': None,
  19. 'my': ugettext_lazy("Your threads"),
  20. 'new': ugettext_lazy("New threads"),
  21. 'unread': ugettext_lazy("Unread threads"),
  22. 'subscribed': ugettext_lazy("Subscribed threads"),
  23. 'unapproved': ugettext_lazy("Unapproved content"),
  24. }
  25. LIST_DENIED_MESSAGES = {
  26. 'my': ugettext_lazy("You have to sign in to see list of threads that you have started."),
  27. 'new': ugettext_lazy("You have to sign in to see list of threads you haven't read."),
  28. 'unread': ugettext_lazy("You have to sign in to see list of threads with new replies."),
  29. 'subscribed': ugettext_lazy("You have to sign in to see list of threads you are subscribing."),
  30. 'unapproved': ugettext_lazy("You have to sign in to see list of threads with unapproved posts."),
  31. }
  32. class ViewModel(object):
  33. def __init__(self, request, category, list_type, page):
  34. self.allow_see_list(request, category, list_type)
  35. base_queryset = self.get_base_queryset(request, category.categories, list_type)
  36. threads_categories = [category.category] + category.subcategories
  37. threads_queryset = self.get_remaining_threads_queryset(base_queryset, category.category, threads_categories)
  38. list_page = paginate(threads_queryset, page, settings.MISAGO_THREADS_PER_PAGE, settings.MISAGO_THREADS_TAIL)
  39. paginator = pagination_dict(list_page, include_page_range=False)
  40. if list_page.number > 1:
  41. threads = list(list_page.object_list)
  42. else:
  43. pinned_threads = list(self.get_pinned_threads(base_queryset, category.category, threads_categories))
  44. threads = list(pinned_threads) + list(list_page.object_list)
  45. if list_type in ('new', 'unread'):
  46. # we already know all threads on list are unread
  47. threadstracker.make_unread(threads)
  48. else:
  49. threadstracker.make_threads_read_aware(request.user, threads)
  50. add_categories_to_threads(category.category, category.categories, threads)
  51. add_acl(request.user, threads)
  52. make_subscription_aware(request.user, threads)
  53. # set state on object for easy access from hooks
  54. self.category = category
  55. self.threads = threads
  56. self.list_type = list_type
  57. self.paginator = paginator
  58. def allow_see_list(self, request, category, list_type):
  59. if list_type not in LISTS_NAMES:
  60. raise Http404()
  61. if request.user.is_anonymous():
  62. if list_type in LIST_DENIED_MESSAGES:
  63. raise PermissionDenied(LIST_DENIED_MESSAGES[list_type])
  64. else:
  65. if list_type == 'unapproved' and not request.user.acl['can_see_unapproved_content_lists']:
  66. raise PermissionDenied(_("You don't have permission to see unapproved content lists."))
  67. def get_list_name(self, list_type):
  68. return LISTS_NAMES[list_type]
  69. def get_base_queryset(self, request, threads_categories, list_type):
  70. return get_threads_queryset(request.user, threads_categories, list_type).order_by('-last_post_id')
  71. def get_pinned_threads(self, queryset, category, threads_categories):
  72. return []
  73. def get_remaining_threads_queryset(self, queryset, category, threads_categories):
  74. return []
  75. def get_frontend_context(self):
  76. context = {
  77. 'THREADS': {
  78. 'results': ThreadListSerializer(self.threads, many=True).data,
  79. 'subcategories': [c.pk for c in self.category.children]
  80. },
  81. }
  82. context['THREADS'].update(self.paginator)
  83. return context
  84. def get_template_context(self):
  85. return {
  86. 'list_name': self.get_list_name(self.list_type),
  87. 'list_type': self.list_type,
  88. 'threads': self.threads,
  89. 'paginator': self.paginator
  90. }
  91. class ForumThreads(ViewModel):
  92. def get_pinned_threads(self, queryset, category, threads_categories):
  93. if category.level:
  94. return list(queryset.filter(weight=2)) + list(queryset.filter(
  95. weight=1,
  96. category__in=threads_categories
  97. ))
  98. else:
  99. return queryset.filter(weight=2)
  100. def get_remaining_threads_queryset(self, queryset, category, threads_categories):
  101. if category.level:
  102. return queryset.filter(
  103. weight=0,
  104. category__in=threads_categories,
  105. )
  106. else:
  107. return queryset.filter(
  108. weight__lt=2,
  109. category__in=threads_categories,
  110. )
  111. class PrivateThreads(ViewModel):
  112. def get_remaining_threads_queryset(self, queryset, category, threads_categories):
  113. return queryset.filter(category__in=threads_categories)
  114. """
  115. Thread queryset utils
  116. """
  117. def get_threads_queryset(user, categories, list_type):
  118. queryset = exclude_invisible_threads(user, categories, Thread.objects)
  119. if list_type == 'all':
  120. return queryset
  121. else:
  122. return filter_threads_queryset(user, categories, list_type, queryset)
  123. def filter_threads_queryset(user, categories, list_type, queryset):
  124. if list_type == 'my':
  125. return queryset.filter(starter=user)
  126. elif list_type == 'subscribed':
  127. subscribed_threads = user.subscription_set.values('thread_id')
  128. return queryset.filter(id__in=subscribed_threads)
  129. elif list_type == 'unapproved':
  130. return queryset.filter(has_unapproved_posts=True)
  131. elif list_type in ('new', 'unread'):
  132. return filter_read_threads_queryset(user, categories, list_type, queryset)
  133. else:
  134. return queryset
  135. def filter_read_threads_queryset(user, categories, list_type, queryset):
  136. # grab cutoffs for categories
  137. cutoff_date = timezone.now() - timedelta(days=settings.MISAGO_READTRACKER_CUTOFF)
  138. if cutoff_date < user.joined_on:
  139. cutoff_date = user.joined_on
  140. categories_dict = {}
  141. for record in user.categoryread_set.filter(category__in=categories):
  142. if record.last_read_on > cutoff_date:
  143. categories_dict[record.category_id] = record.last_read_on
  144. if list_type == 'new':
  145. # new threads have no entry in reads table
  146. # AND were started after cutoff date
  147. read_threads = user.threadread_set.filter(
  148. category__in=categories
  149. ).values('thread_id')
  150. condition = Q(last_post_on__lte=cutoff_date)
  151. condition = condition | Q(id__in=read_threads)
  152. if categories_dict:
  153. for category_id, category_cutoff in categories_dict.items():
  154. condition = condition | Q(
  155. category_id=category_id,
  156. last_post_on__lte=category_cutoff,
  157. )
  158. return queryset.exclude(condition)
  159. elif list_type == 'unread':
  160. # unread threads were read in past but have new posts
  161. # after cutoff date
  162. read_threads = user.threadread_set.filter(
  163. category__in=categories,
  164. thread__last_post_on__gt=cutoff_date,
  165. last_read_on__lt=F('thread__last_post_on')
  166. ).values('thread_id')
  167. queryset = queryset.filter(id__in=read_threads)
  168. # unread threads have last reply after read/cutoff date
  169. if categories_dict:
  170. conditions = None
  171. for category_id, category_cutoff in categories_dict.items():
  172. condition = Q(
  173. category_id=category_id,
  174. last_post_on__lte=category_cutoff,
  175. )
  176. if conditions:
  177. conditions = conditions | condition
  178. else:
  179. conditions = condition
  180. return queryset.exclude(conditions)
  181. else:
  182. return queryset