threadslists.py 5.0 KB

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