threadslists.py 5.4 KB

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