unreadthreads.py 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. from datetime import timedelta
  2. from django.conf import settings
  3. from django.core.exceptions import PermissionDenied
  4. from misago.core.uiviews import uiview
  5. from misago.users.decorators import deny_guests
  6. from django.db.models import F
  7. from django.utils import timezone
  8. from django.utils.translation import ugettext as _
  9. from misago.threads.models import Thread
  10. from misago.threads.permissions import exclude_invisible_threads
  11. from misago.threads.views.generic.threads import Threads, ThreadsView
  12. class UnreadThreads(Threads):
  13. def get_queryset(self):
  14. cutoff_days = settings.MISAGO_FRESH_CONTENT_PERIOD
  15. cutoff_date = timezone.now() - timedelta(days=cutoff_days)
  16. if cutoff_date < self.user.reads_cutoff:
  17. cutoff_date = self.user.reads_cutoff
  18. queryset = Thread.objects.filter(last_post_on__gte=cutoff_date)
  19. queryset = queryset.select_related('forum')
  20. queryset = queryset.filter(threadread__user=self.user)
  21. queryset = queryset.filter(
  22. threadread__last_read_on__lt=F('last_post_on'))
  23. queryset = exclude_invisible_threads(queryset, self.user)
  24. return queryset
  25. class UnreadThreadsView(ThreadsView):
  26. link_name = 'misago:unread_threads'
  27. template = 'misago/threads/unread.html'
  28. Threads = UnreadThreads
  29. def process_context(self, request, context):
  30. context['show_threads_locations'] = True
  31. context['fresh_period'] = settings.MISAGO_FRESH_CONTENT_PERIOD
  32. if request.user.unread_threads != context['threads_count']:
  33. request.user.unread_threads.set(context['threads_count'])
  34. return context
  35. def dispatch(self, request, *args, **kwargs):
  36. if request.user.is_anonymous():
  37. message = _("You have to sign in to see your list of "
  38. "threads with unread replies.")
  39. raise PermissionDenied(message)
  40. else:
  41. return super(UnreadThreadsView, self).dispatch(
  42. request, *args, **kwargs)
  43. @uiview("unread_threads")
  44. @deny_guests
  45. def event_sender(request, resolver_match):
  46. return int(request.user.unread_threads)