threadstracker.py 1.0 KB

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