categoriestracker.py 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. from misago.threads.models import Post, Thread
  2. from misago.threads.permissions import (
  3. exclude_invisible_posts,
  4. exclude_invisible_threads,
  5. )
  6. from .dates import get_cutoff_date
  7. def make_read_aware(user, user_acl, 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_acl, categories, threads)
  17. queryset = (
  18. Post.objects.filter(
  19. category__in=categories,
  20. thread__in=threads,
  21. posted_on__gt=get_cutoff_date(user),
  22. )
  23. .values_list("category", flat=True)
  24. .distinct()
  25. )
  26. queryset = queryset.exclude(id__in=user.postread_set.values("post"))
  27. queryset = exclude_invisible_posts(user_acl, categories, queryset)
  28. unread_categories = list(queryset)
  29. for category in categories:
  30. if category.pk in unread_categories:
  31. category.is_read = False
  32. category.is_new = True
  33. def make_read(threads):
  34. for thread in threads:
  35. thread.is_read = True
  36. thread.is_new = False