newthreads.py 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  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.utils import timezone
  7. from django.utils.translation import ugettext as _
  8. from misago.threads.models import Thread
  9. from misago.threads.permissions import exclude_invisible_threads
  10. from misago.threads.views.generic.threads import Threads, ThreadsView
  11. class NewThreads(Threads):
  12. def get_queryset(self):
  13. cutoff_days = settings.MISAGO_FRESH_CONTENT_PERIOD
  14. cutoff_date = timezone.now() - timedelta(days=cutoff_days)
  15. if cutoff_date < self.user.reads_cutoff:
  16. cutoff_date = self.user.reads_cutoff
  17. queryset = Thread.objects.filter(started_on__gte=cutoff_date)
  18. queryset = queryset.select_related('forum')
  19. tracked_threads = self.user.threadread_set.all()
  20. queryset = queryset.exclude(id__in=tracked_threads.values('thread_id'))
  21. queryset = exclude_invisible_threads(queryset, self.user)
  22. return queryset
  23. class NewThreadsView(ThreadsView):
  24. link_name = 'misago:new_threads'
  25. template = 'misago/threads/new.html'
  26. Threads = NewThreads
  27. def process_context(self, request, context):
  28. context['show_threads_locations'] = True
  29. context['fresh_period'] = settings.MISAGO_FRESH_CONTENT_PERIOD
  30. if request.user.new_threads != context['threads_count']:
  31. request.user.new_threads.set(context['threads_count'])
  32. return context
  33. def dispatch(self, request, *args, **kwargs):
  34. if request.user.is_anonymous():
  35. message = _("You have to sign in to see your list of new threads.")
  36. raise PermissionDenied(message)
  37. else:
  38. return super(NewThreadsView, self).dispatch(
  39. request, *args, **kwargs)
  40. @uiview("new_threads")
  41. @deny_guests
  42. def event_sender(request, resolver_match):
  43. return int(request.user.new_threads)