categoriestracker.py 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. from django.db import transaction
  2. from django.utils import timezone
  3. from misago.threads.models import Post, Thread
  4. from misago.threads.permissions import exclude_invisible_posts, exclude_invisible_threads
  5. from .dates import get_cutoff_date
  6. from .models import CategoryRead, ThreadRead
  7. def make_read_aware(user, categories):
  8. if not categories:
  9. return
  10. if not hasattr(categories, '__iter__'):
  11. categories = [categories]
  12. make_read(categories)
  13. if user.is_anonymous:
  14. return
  15. threads = Thread.objects.filter(category__in=categories)
  16. threads = exclude_invisible_threads(user, categories, threads)
  17. queryset = Post.objects.filter(
  18. category__in=categories,
  19. thread__in=threads,
  20. posted_on__gt=get_cutoff_date(user),
  21. ).values_list('category', flat=True).distinct()
  22. queryset = queryset.exclude(id__in=user.postread_set.values('post'))
  23. queryset = exclude_invisible_posts(user, categories, queryset)
  24. unread_categories = list(queryset)
  25. for category in categories:
  26. if category.pk in unread_categories:
  27. category.is_read = False
  28. category.is_new = True
  29. def make_read(threads):
  30. for thread in threads:
  31. thread.is_read = True
  32. thread.is_new = False
  33. # Deprecated stuff goes here
  34. def start_record(user, category):
  35. from misago.core import deprecations
  36. deprecations.warn("categoriestracker.start_record has been deprecated")
  37. def sync_record(user, category):
  38. from misago.core import deprecations
  39. deprecations.warn("categoriestracker.sync_record has been deprecated")
  40. def read_category(user, category):
  41. from misago.core import deprecations
  42. deprecations.warn("categoriestracker.read_category has been deprecated")