trackers.py 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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. if user.is_authenticated():
  29. try:
  30. self.record = Record.objects.get(user=user,forum=forum)
  31. except Record.DoesNotExist:
  32. self.record = Record(user=user,forum=forum,cleared=self.cutoff)
  33. self.threads = self.record.get_threads()
  34. def get_read_date(self, thread):
  35. if not self.user.is_authenticated():
  36. return timezone.now()
  37. try:
  38. if self.threads[thread.pk] > self.cutoff:
  39. return self.threads[thread.pk]
  40. except KeyError:
  41. pass
  42. return self.cutoff
  43. def is_read(self, thread):
  44. if not self.user.is_authenticated():
  45. return True
  46. try:
  47. if thread.last <= self.cutoff and thread.pk in self.threads:
  48. del self.threads[thread.pk]
  49. self.need_update = True
  50. return thread.last <= self.cutoff or thread.last <= self.threads[thread.pk]
  51. except KeyError:
  52. return False
  53. def set_read(self, thread, post):
  54. if self.user.is_authenticated():
  55. try:
  56. if self.threads[thread.pk] < post.date:
  57. self.threads[thread.pk] = post.date
  58. self.need_sync = True
  59. except KeyError:
  60. self.threads[thread.pk] = post.date
  61. self.need_sync = True
  62. def sync(self):
  63. now = timezone.now()
  64. if self.need_sync:
  65. unread_threads = 0
  66. for thread in Thread.objects.filter(last__gte=self.record.cleared).all():
  67. if not self.is_read(thread):
  68. unread_threads += 1
  69. if not unread_threads:
  70. self.record.cleared = now
  71. if self.need_sync or self.need_update:
  72. self.record.updated = now
  73. self.record.set_threads(self.threads)
  74. self.record.save(force_update=self.record.pk)