categoriestracker.py 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. from misago.threads.models import Post, Thread
  2. from misago.threads.permissions import exclude_invisible_posts, exclude_invisible_threads
  3. from .dates import get_cutoff_date
  4. def make_read_aware(user, user_acl, categories):
  5. if not categories:
  6. return
  7. if not hasattr(categories, '__iter__'):
  8. categories = [categories]
  9. make_read(categories)
  10. if user.is_anonymous:
  11. return
  12. threads = Thread.objects.filter(category__in=categories)
  13. threads = exclude_invisible_threads(user_acl, categories, threads)
  14. queryset = Post.objects.filter(
  15. category__in=categories,
  16. thread__in=threads,
  17. posted_on__gt=get_cutoff_date(user),
  18. ).values_list('category', flat=True).distinct()
  19. queryset = queryset.exclude(id__in=user.postread_set.values('post'))
  20. queryset = exclude_invisible_posts(user_acl, categories, queryset)
  21. unread_categories = list(queryset)
  22. for category in categories:
  23. if category.pk in unread_categories:
  24. category.is_read = False
  25. category.is_new = True
  26. def make_read(threads):
  27. for thread in threads:
  28. thread.is_read = True
  29. thread.is_new = False