threads.py 8.3 KB

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