newthreads.py 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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.transaction import atomic
  6. from django.shortcuts import redirect
  7. from django.utils import timezone
  8. from django.utils.translation import ugettext as _
  9. from django.views.decorators.cache import never_cache
  10. from django.views.decorators.csrf import csrf_protect
  11. from misago.core.decorators import require_POST
  12. from misago.core.uiviews import uiview
  13. from misago.users.decorators import deny_guests
  14. from misago.threads.models import Thread
  15. from misago.threads.permissions import exclude_invisible_threads
  16. from misago.threads.views.generic.threads import Threads, ThreadsView
  17. class NewThreads(Threads):
  18. def get_queryset(self):
  19. cutoff_days = settings.MISAGO_FRESH_CONTENT_PERIOD
  20. cutoff_date = timezone.now() - timedelta(days=cutoff_days)
  21. if cutoff_date < self.user.reads_cutoff:
  22. cutoff_date = self.user.reads_cutoff
  23. if cutoff_date < self.user.new_threads_cutoff:
  24. cutoff_date = self.user.new_threads_cutoff
  25. queryset = Thread.objects.filter(started_on__gte=cutoff_date)
  26. queryset = queryset.select_related('category')
  27. tracked_threads = self.user.threadread_set.all()
  28. queryset = queryset.exclude(id__in=tracked_threads.values('thread_id'))
  29. queryset = exclude_invisible_threads(queryset, self.user)
  30. return queryset
  31. class NewThreadsView(ThreadsView):
  32. link_name = 'misago:new_threads'
  33. template = 'misago/threads/new.html'
  34. Threads = NewThreads
  35. def process_context(self, request, context):
  36. context['show_threads_locations'] = True
  37. context['fresh_period'] = settings.MISAGO_FRESH_CONTENT_PERIOD
  38. if request.user.new_threads != context['threads_count']:
  39. request.user.new_threads.set(context['threads_count'])
  40. return context
  41. def dispatch(self, request, *args, **kwargs):
  42. if request.user.is_anonymous():
  43. message = _("You have to sign in to see your list of new threads.")
  44. raise PermissionDenied(message)
  45. return super(NewThreadsView, self).dispatch(
  46. request, *args, **kwargs)
  47. @deny_guests
  48. @require_POST
  49. @csrf_protect
  50. @never_cache
  51. @atomic
  52. def clear_new_threads(request):
  53. request.user.new_threads_cutoff = timezone.now()
  54. request.user.save(update_fields=['new_threads_cutoff'])
  55. request.user.new_threads.set(0)
  56. messages.success(request, _("New threads list has been cleared."))
  57. return redirect('misago:new_threads')
  58. @uiview("new_threads")
  59. @deny_guests
  60. def event_sender(request, resolver_match):
  61. return int(request.user.new_threads)