unreadthreads.py 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. from datetime import timedelta
  2. from django.conf import settings
  3. from django.contrib import messages
  4. from django.core.exceptions import PermissionDenied
  5. from django.db.models import F
  6. from django.db.transaction import atomic
  7. from django.shortcuts import redirect
  8. from django.utils import timezone
  9. from django.utils.translation import ugettext as _
  10. from django.views.decorators.cache import never_cache
  11. from django.views.decorators.csrf import csrf_protect
  12. from misago.core.decorators import require_POST
  13. from misago.core.uiviews import uiview
  14. from misago.users.decorators import deny_guests
  15. from misago.threads.models import Thread
  16. from misago.threads.permissions import exclude_invisible_threads
  17. from misago.threads.views.generic.threads import Threads, ThreadsView
  18. class UnreadThreads(Threads):
  19. def get_queryset(self):
  20. cutoff_days = settings.MISAGO_FRESH_CONTENT_PERIOD
  21. cutoff_date = timezone.now() - timedelta(days=cutoff_days)
  22. if cutoff_date < self.user.reads_cutoff:
  23. cutoff_date = self.user.reads_cutoff
  24. if cutoff_date < self.user.unread_threads_cutoff:
  25. cutoff_date = self.user.unread_threads_cutoff
  26. queryset = Thread.objects.filter(last_post_on__gte=cutoff_date)
  27. queryset = queryset.select_related('forum')
  28. queryset = queryset.filter(threadread__user=self.user)
  29. queryset = queryset.filter(
  30. threadread__last_read_on__lt=F('last_post_on'))
  31. queryset = exclude_invisible_threads(queryset, self.user)
  32. return queryset
  33. class UnreadThreadsView(ThreadsView):
  34. link_name = 'misago:unread_threads'
  35. template = 'misago/threads/unread.html'
  36. Threads = UnreadThreads
  37. def process_context(self, request, context):
  38. context['show_threads_locations'] = True
  39. context['fresh_period'] = settings.MISAGO_FRESH_CONTENT_PERIOD
  40. if request.user.unread_threads != context['threads_count']:
  41. request.user.unread_threads.set(context['threads_count'])
  42. return context
  43. def dispatch(self, request, *args, **kwargs):
  44. if request.user.is_anonymous():
  45. message = _("You have to sign in to see your list of "
  46. "threads with unread replies.")
  47. raise PermissionDenied(message)
  48. return super(UnreadThreadsView, self).dispatch(
  49. request, *args, **kwargs)
  50. @deny_guests
  51. @require_POST
  52. @csrf_protect
  53. @never_cache
  54. @atomic
  55. def clear_unread_threads(request):
  56. request.user.unread_threads_cutoff = timezone.now()
  57. request.user.save(update_fields=['unread_threads_cutoff'])
  58. request.user.unread_threads.set(0)
  59. messages.success(request, _("Unread threads list has been cleared."))
  60. return redirect('misago:unread_threads')
  61. @uiview("unread_threads")
  62. @deny_guests
  63. def event_sender(request, resolver_match):
  64. return int(request.user.unread_threads)