threadslists.py 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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.utils import timezone
  6. from django.utils.translation import ugettext as _
  7. from misago.categories.models import CATEGORIES_TREE_ID, Category
  8. from misago.categories.permissions import (
  9. allow_see_category, allow_browse_category)
  10. from misago.core.shortcuts import get_object_or_404, validate_slug
  11. from misago.readtracker import threadstracker
  12. from misago.threads.models import Thread
  13. from misago.threads.permissions import exclude_invisible_threads
  14. def filter_threads_queryset(user, categories, list_type, queryset):
  15. if list_type == 'my':
  16. return queryset.filter(starter=user)
  17. elif list_type == 'subscribed':
  18. subscribed_threads = user.subscription_set.values('thread_id')
  19. return queryset.filter(id__in=subscribed_threads)
  20. else:
  21. # grab cutoffs for categories
  22. cutoff_date = timezone.now() - timedelta(
  23. days=settings.MISAGO_FRESH_CONTENT_PERIOD
  24. )
  25. if cutoff_date < user.joined_on:
  26. cutoff_date = user.joined_on
  27. categories_dict = {}
  28. for record in user.categoryread_set.filter(category__in=categories):
  29. if record.last_read_on > cutoff_date:
  30. categories_dict[record.category_id] = record.last_read_on
  31. if list_type == 'new':
  32. # new threads have no entry in reads table
  33. # AND were started after cutoff date
  34. read_threads = user.threadread_set.filter(
  35. category__in=categories
  36. ).values('thread_id')
  37. condition = Q(last_post_on__lte=cutoff_date)
  38. condition = condition | Q(id__in=read_threads)
  39. if categories_dict:
  40. for category_id, category_cutoff in categories_dict.items():
  41. condition = condition | Q(
  42. category_id=category_id,
  43. last_post_on__lte=category_cutoff,
  44. )
  45. return queryset.exclude(condition)
  46. elif list_type == 'unread':
  47. # unread threads were read in past but have new posts
  48. # after cutoff date
  49. read_threads = user.threadread_set.filter(
  50. category__in=categories,
  51. thread__last_post_on__gt=cutoff_date,
  52. last_read_on__lt=F('thread__last_post_on')
  53. ).values('thread_id')
  54. queryset = queryset.filter(id__in=read_threads)
  55. # unread threads have last reply after read/cutoff date
  56. if categories_dict:
  57. conditions = None
  58. for category_id, category_cutoff in categories_dict.items():
  59. condition = Q(
  60. category_id=category_id,
  61. last_post_on__lte=category_cutoff,
  62. )
  63. if conditions:
  64. conditions = conditions | condition
  65. else:
  66. conditions = condition
  67. return queryset.exclude(conditions)
  68. else:
  69. return queryset
  70. def get_threads_queryset(user, categories, list_type):
  71. queryset = exclude_invisible_threads(user, categories, Thread.objects)
  72. if list_type == 'all':
  73. return queryset
  74. else:
  75. return filter_threads_queryset(user, categories, list_type, queryset)
  76. class ThreadsListMixin(object):
  77. def allow_see_list(self, request, category, list_type):
  78. if request.user.is_anonymous():
  79. if list_type == 'my':
  80. raise PermissionDenied( _("You have to sign in to see list of "
  81. "threads that you have started."))
  82. if list_type == 'new':
  83. raise PermissionDenied(_("You have to sign in to see list of "
  84. "threads you haven't read."))
  85. if list_type == 'unread':
  86. raise PermissionDenied(_("You have to sign in to see list of "
  87. "threads with new replies."))
  88. if list_type == 'subscribed':
  89. raise PermissionDenied(_("You have to sign in to see list of "
  90. "threads you are subscribing."))
  91. def get_categories(self, request):
  92. return [Category.objects.root_category()] + list(
  93. Category.objects.all_categories().filter(
  94. id__in=request.user.acl['visible_categories']
  95. ).select_related('parent'))
  96. def get_subcategories(self, request, category, all_categories):
  97. if category.is_leaf_node():
  98. return []
  99. visible_categories = request.user.acl['visible_categories']
  100. queryset = category.get_descendants().filter(id__in=visible_categories)
  101. return list(queryset.order_by('lft'))
  102. def get_queryset(self, request, categories, list_type):
  103. # [:1] cos we are cutting off root caregory on forum threads list
  104. # as it includes nedless extra condition to DB filter
  105. if categories[0].special_role:
  106. categories = categories[1:]
  107. queryset = get_threads_queryset(request.user, categories, list_type)
  108. return queryset.order_by('-last_post_id')
  109. def get_extra_context(self, request, category, subcategories, list_type):
  110. return {
  111. 'is_index': not settings.MISAGO_CATEGORIES_ON_INDEX
  112. }