def sync_record(user, forum): recorded_threads = forum.thread_set.filter(last_post_on__gt=cutoff_date()) recorded_threads = exclude_invisible_threads(user, forum, recorded_threads) all_threads_count = recorded_threads.count() read_threads = user.threadread_set.filter( forum=forum, last_read_on__gt=cutoff_date()) read_threads_count = read_threads.filter( thread__last_post_on__lte=F("last_read_on")).count() forum_is_read = read_threads_count == all_threads_count try: forum_record = user.forumread_set.filter(forum=forum).all()[0] forum_record.last_updated_on = timezone.now() if forum_is_read: forum_record.last_cleared_on = forum_record.last_updated_on else: forum_record.last_cleared_on = cutoff_date() forum_record.save(update_fields=['last_updated_on', 'last_cleared_on']) except IndexError: if forum_is_read: cleared_on = timezone.now() else: cleared_on = cutoff_date() forum_record = user.forumread_set.create( forum=forum, last_updated_on=timezone.now(), last_cleared_on=cleared_on)
def test_sync_record_for_forum_with_deleted_threads(self): """unread forum reverts to read after its emptied""" self.post_thread(cutoff_date() + timedelta(days=1)) self.post_thread(cutoff_date() + timedelta(days=1)) self.post_thread(cutoff_date() + timedelta(days=1)) add_acl(self.user, self.forums) forumstracker.sync_record(self.user, self.forum) forumstracker.make_read_aware(self.user, self.forums) self.assertFalse(self.forum.is_read) self.forum.thread_set.all().delete() self.forum.synchronize() self.forum.save() forumstracker.make_read_aware(self.user, self.forums) self.assertTrue(self.forum.is_read)
def test_sync_record_for_forum_with_new_thread(self): """ sync_record sets read flag on forum with old thread, then keeps flag to unread when new reply is posted """ self.post_thread(cutoff_date() + timedelta(days=1)) add_acl(self.user, self.forums) forumstracker.sync_record(self.user, self.forum) self.user.forumread_set.get(forum=self.forum) forumstracker.make_read_aware(self.user, self.forums) self.assertFalse(self.forum.is_read) self.post_thread(cutoff_date() + timedelta(days=1)) forumstracker.sync_record(self.user, self.forum) forumstracker.make_read_aware(self.user, self.forums) self.assertFalse(self.forum.is_read)
def test_sync_record_for_forum_with_many_threads(self): """sync_record sets unread flag on forum with many threads""" self.post_thread(cutoff_date() + timedelta(days=1)) self.post_thread(cutoff_date() - timedelta(days=1)) self.post_thread(cutoff_date() + timedelta(days=1)) self.post_thread(cutoff_date() - timedelta(days=1)) add_acl(self.user, self.forums) forumstracker.sync_record(self.user, self.forum) self.user.forumread_set.get(forum=self.forum) forumstracker.make_read_aware(self.user, self.forums) self.assertFalse(self.forum.is_read) self.post_thread(cutoff_date() + timedelta(days=1)) forumstracker.sync_record(self.user, self.forum) forumstracker.make_read_aware(self.user, self.forums) self.assertFalse(self.forum.is_read)
def make_thread_read_aware(user, thread): thread.is_read = True if user.is_authenticated() and is_date_tracked(thread.last_post_on): try: record = user.threadread_set.filter(thread=thread).all()[0] thread.last_read_on = record.last_read_on thread.is_read = thread.last_post_on <= record.last_read_on thread.read_record = record except IndexError: thread.read_record = None thread.is_read = False thread.last_read_on = cutoff_date()
def test_make_read_aware_sets_unread_flag_for_forum_with_new_thread(self): """make_read_aware sets unread flag on forum with new thread""" self.forum.last_post_on = cutoff_date() + timedelta(days=1) forumstracker.make_read_aware(self.user, self.forums) self.assertFalse(self.forum.is_read)
def test_make_read_aware_sets_read_flag_for_forum_with_old_thread(self): """make_read_aware sets read flag on forum with old thread""" self.forum.last_post_on = cutoff_date() - timedelta(days=1) forumstracker.make_read_aware(self.user, self.forums) self.assertTrue(self.forum.is_read)
def test_cutoff_date(self): """cutoff_date returns cut off date""" cutoff = cutoff_date() self.assertTrue(cutoff < timezone.now())
def test_is_date_tracked(self): """is_date_tracked validates dates""" self.assertFalse(is_date_tracked(None)) self.assertFalse(is_date_tracked(cutoff_date() - timedelta(seconds=1))) self.assertTrue(is_date_tracked(cutoff_date() + timedelta(minutes=1)))