trackers.py 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. from datetime import timedelta
  2. from django.conf import settings
  3. from django.utils import timezone
  4. from misago.readstracker.models import Record
  5. from misago.threads.models import Thread
  6. class ForumsTracker(object):
  7. def __init__(self, user):
  8. self.user = user
  9. self.cutoff = timezone.now() - timedelta(days=settings.READS_TRACKER_LENGTH)
  10. self.forums = {}
  11. if self.user.is_authenticated() and settings.READS_TRACKER_LENGTH > 0:
  12. for forum in Record.objects.filter(user=user).filter(updated__gte=self.cutoff).values('id', 'forum_id', 'updated', 'cleared'):
  13. self.forums[forum['forum_id']] = forum
  14. def is_read(self, forum):
  15. if not self.user.is_authenticated() or not forum.last_thread_date:
  16. return True
  17. try:
  18. return forum.last_thread_date <= self.cutoff or forum.last_thread_date <= self.forums[forum.pk]['cleared']
  19. except KeyError:
  20. return False
  21. class ThreadsTracker(object):
  22. def __init__(self, user, forum):
  23. self.need_sync = False
  24. self.need_update = False
  25. self.user = user
  26. self.forum = forum
  27. self.cutoff = timezone.now() - timedelta(days=settings.READS_TRACKER_LENGTH)
  28. try:
  29. self.record = Record.objects.get(user=user,forum=forum)
  30. except Record.DoesNotExist:
  31. self.record = Record(user=user,forum=forum,cleared=self.cutoff)
  32. self.threads = self.record.get_threads()
  33. def get_read_date(self, thread):
  34. if not self.user.is_authenticated():
  35. return timezone.now()
  36. try:
  37. if self.threads[thread.pk] > self.cutoff:
  38. return self.threads[thread.pk]
  39. except KeyError:
  40. pass
  41. return self.cutoff
  42. def is_read(self, thread):
  43. if not self.user.is_authenticated():
  44. return True
  45. try:
  46. if thread.last <= self.cutoff and thread.pk in self.threads:
  47. del self.threads[thread.pk]
  48. self.need_update = True
  49. return thread.last <= self.cutoff or thread.last <= self.threads[thread.pk]
  50. except KeyError:
  51. return False
  52. def set_read(self, thread, post):
  53. if self.user.is_authenticated():
  54. try:
  55. if self.threads[thread.pk] < post.date:
  56. self.threads[thread.pk] = post.date
  57. self.need_sync = True
  58. except KeyError:
  59. self.threads[thread.pk] = post.date
  60. self.need_sync = True
  61. def sync(self):
  62. now = timezone.now()
  63. if self.need_sync:
  64. unread_threads = 0
  65. for thread in Thread.objects.filter(last__gte=self.record.cleared).all():
  66. if not self.is_read(thread):
  67. unread_threads += 1
  68. if not unread_threads:
  69. self.record.cleared = now
  70. if self.need_sync or self.need_update:
  71. self.record.updated = now
  72. self.record.set_threads(self.threads)
  73. self.record.save(force_update=self.record.pk)