threadstracker.py 982 B

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