def test_success(self): self.check_ajax_ok(self.client.post(url('forum:read-all-in-subcategory', self.subcat1.id))) self.assertEqual(SubCategoryReadInfoPrototype._db_count(), 1) read_info = SubCategoryReadInfoPrototype._db_get_object(0) self.assertEqual(read_info.account_id, self.account.id) self.assertEqual(read_info.subcategory_id, self.subcat1.id) self.assertTrue(read_info.all_read_at > datetime.datetime.now() - datetime.timedelta(seconds=1))
def get_subcategory(self, page=1): threads_query = Thread.objects.filter(subcategory=self.subcategory._model) url_builder = UrlBuilder(reverse('forum:subcategories:show', args=[self.subcategory.id]), arguments={'page': page}) page -= 1 paginator = Paginator(page, threads_query.count(), forum_settings.THREADS_ON_PAGE, url_builder) if paginator.wrong_page_number: return self.redirect(paginator.last_page_url, permanent=False) thread_from, thread_to = paginator.page_borders(page) threads = ThreadPrototype.from_query(threads_query.select_related().order_by('-important', '-updated_at')[thread_from:thread_to]) important_threads = sorted([t for t in threads if t.important], key=lambda t: t.caption) threads = [t for t in threads if not t.important] read_state = ReadState(account=self.account) if self.account.is_authenticated: SubCategoryReadInfoPrototype.read_subcategory(subcategory=self.subcategory, account=self.account) return self.template('forum/subcategory.html', {'category': self.category, 'subcategory': self.subcategory, 'can_create_thread': can_create_thread(self.account, self.subcategory), 'paginator': paginator, 'can_subscribe': self.account.is_authenticated and not self.account.is_fast, 'has_subscription': SubscriptionPrototype.has_subscription(self.account, subcategory=self.subcategory), 'threads': threads, 'important_threads': important_threads, 'read_state': read_state } )
def test_create_when_created(self): read_info_1 = SubCategoryReadInfoPrototype.read_subcategory( self.subcategory, self.account) read_info_2 = SubCategoryReadInfoPrototype.read_subcategory( self.subcategory, self.account) self.assertEqual(read_info_1.id, read_info_2.id)
def test_thread_has_new_messages__category_read(self): read_state = self.get_read_state() self.assertTrue(read_state.thread_is_new(self.thread)) SubCategoryReadInfoPrototype.read_all_in_subcategory(self.subcategory, self.account) read_state = self.get_read_state() self.assertFalse(read_state.thread_has_new_messages(self.thread))
def test_subcategory_has_new_messages__unread_state_expired(self): SubCategoryReadInfoPrototype.read_subcategory( subcategory=self.subcategory, account=self.account) self.subcategory._model.updated_at = datetime.datetime.now() self.subcategory.save() self.assertFalse(self.get_read_state().subcategory_has_new_messages( self.subcategory))
def test_subcategory_has_new_messages__new_post_from_request_from_read_account(self): SubCategoryReadInfoPrototype.read_all_in_subcategory(subcategory=self.subcategory, account=self.account) self.assertFalse(self.get_read_state().subcategory_has_new_messages(self.subcategory)) self.request_login(self.account.email) self.client.post(url('forum:threads:create-post', self.thread.id), {'text': 'thread3-test-post'}) self.assertFalse(self.get_read_state().subcategory_has_new_messages(self.subcategory))
def test_read_all_in_subcategory__unexisted_info(self): self.assertEqual(SubCategoryReadInfoPrototype._db_count(), 0) read_info = SubCategoryReadInfoPrototype.read_all_in_subcategory(self.subcategory, self.account) self.assertEqual(SubCategoryReadInfoPrototype._db_count(), 1) self.assertEqual(read_info.subcategory_id, self.subcategory.id) self.assertEqual(read_info.account_id, self.account.id) self.assertTrue(read_info.read_at > datetime.datetime.now() - datetime.timedelta(seconds=1)) self.assertTrue(read_info.all_read_at > datetime.datetime.now() - datetime.timedelta(seconds=1))
def test_subcategory_has_new_messages__new_post(self): SubCategoryReadInfoPrototype.read_all_in_subcategory( subcategory=self.subcategory, account=self.account) self.assertFalse(self.get_read_state().subcategory_has_new_messages( self.subcategory)) PostPrototype.create(self.thread, self.account_2, 'post-new-text') self.assertTrue(self.get_read_state().subcategory_has_new_messages( self.subcategory))
def test_subcategory_has_new_messages__new_thread(self): SubCategoryReadInfoPrototype.read_all_in_subcategory( subcategory=self.subcategory, account=self.account) self.assertFalse(self.get_read_state().subcategory_has_new_messages( self.subcategory)) ThreadPrototype.create(self.subcategory, 'new-threwad', self.account_2, 'thread-new-text') self.assertTrue(self.get_read_state().subcategory_has_new_messages( self.subcategory))
def test_subcategory_has_new_messages__unread_but_thread_read(self): SubCategoryReadInfoPrototype.read_subcategory(subcategory=self.subcategory, account=self.account) self.subcategory._model.updated_at = datetime.datetime.now() self.subcategory.save() self.assertTrue(self.get_read_state().subcategory_has_new_messages(self.subcategory)) ThreadReadInfoPrototype.read_thread(self.thread, self.account) ThreadReadInfoPrototype.read_thread(self.thread_2, self.account) ThreadReadInfoPrototype.read_thread(self.thread_3, self.account) self.assertFalse(self.get_read_state().subcategory_has_new_messages(self.subcategory))
def test_thread_has_new_messages__category_read(self): read_state = self.get_read_state() self.assertTrue(read_state.thread_is_new(self.thread)) SubCategoryReadInfoPrototype.read_all_in_subcategory( self.subcategory, self.account) read_state = self.get_read_state() self.assertFalse(read_state.thread_has_new_messages(self.thread))
def get_subcategory(self, page=1): threads_query = Thread.objects.filter( subcategory=self.subcategory._model) url_builder = UrlBuilder(reverse('forum:subcategories:show', args=[self.subcategory.id]), arguments={'page': page}) page -= 1 paginator = Paginator(page, threads_query.count(), forum_settings.THREADS_ON_PAGE, url_builder) if paginator.wrong_page_number: return self.redirect(paginator.last_page_url, permanent=False) thread_from, thread_to = paginator.page_borders(page) threads = ThreadPrototype.from_query( threads_query.select_related().order_by( '-important', '-updated_at')[thread_from:thread_to]) important_threads = sorted(filter(lambda t: t.important, threads), key=lambda t: t.caption) threads = filter(lambda t: not t.important, threads) read_state = ReadState(account=self.account) if self.account.is_authenticated(): SubCategoryReadInfoPrototype.read_subcategory( subcategory=self.subcategory, account=self.account) return self.template( 'forum/subcategory.html', { 'category': self.category, 'subcategory': self.subcategory, 'can_create_thread': can_create_thread(self.account, self.subcategory), 'paginator': paginator, 'can_subscribe': self.account.is_authenticated() and not self.account.is_fast, 'has_subscription': SubscriptionPrototype.has_subscription( self.account, subcategory=self.subcategory), 'threads': threads, 'important_threads': important_threads, 'read_state': read_state })
def test_thread_is_new__read(self): SubCategoryReadInfoPrototype.read_subcategory(self.subcategory, self.account) read_state = self.get_read_state() self.assertFalse(read_state.thread_is_new(self.thread)) self.thread._model.created_at = datetime.datetime.now() self.thread.save() self.assertTrue(read_state.thread_is_new(self.thread))
def handle(self, *args, **options): from the_tale.common.postponed_tasks.prototypes import PostponedTaskPrototype from the_tale.forum.prototypes import SubCategoryReadInfoPrototype, ThreadReadInfoPrototype from the_tale.post_service.prototypes import MessagePrototype PostponedTaskPrototype.remove_old_tasks() ThreadReadInfoPrototype.remove_old_infos() SubCategoryReadInfoPrototype.remove_old_infos() MessagePrototype.remove_old_messages()
def test_subcategory_has_new_messages__new_post_from_request(self): SubCategoryReadInfoPrototype.read_all_in_subcategory( subcategory=self.subcategory, account=self.account) self.assertFalse(self.get_read_state().subcategory_has_new_messages( self.subcategory)) self.request_login(self.account_2.email) self.client.post(url('forum:threads:create-post', self.thread.id), {'text': 'thread3-test-post'}) self.assertTrue(self.get_read_state().subcategory_has_new_messages( self.subcategory))
def test_subcategory_has_new_messages__unread_but_thread_read(self): SubCategoryReadInfoPrototype.read_subcategory( subcategory=self.subcategory, account=self.account) self.subcategory._model.updated_at = datetime.datetime.now() self.subcategory.save() self.assertTrue(self.get_read_state().subcategory_has_new_messages( self.subcategory)) ThreadReadInfoPrototype.read_thread(self.thread, self.account) ThreadReadInfoPrototype.read_thread(self.thread_2, self.account) ThreadReadInfoPrototype.read_thread(self.thread_3, self.account) self.assertFalse(self.get_read_state().subcategory_has_new_messages( self.subcategory))
def test_subcategory(self): self.request_logout() self.request_login(self.account_2.email) self.assertEqual(SubCategoryReadInfoPrototype._db_count(), 0) texts = ['cat1-caption', 'subcat1-caption', 'thread1-caption', 'thread2-caption', 'pgf-new-thread-marker', self.fixture.clan_1.abbr] self.check_html_ok(self.request_html(url('forum:subcategories:show', self.subcat1.id)), texts=texts) self.assertEqual(SubCategoryReadInfoPrototype._db_count(), 1) read_info = SubCategoryReadInfoPrototype._db_get_object(0) self.assertEqual(read_info.account_id, self.account_2.id) self.assertEqual(read_info.subcategory_id, self.subcat1.id) self.check_html_ok(self.request_html(url('forum:subcategories:show', self.subcat1.id)), texts=[('pgf-new-thread-marker', 0)])
def test_unlogined(self): self.request_logout() self.check_ajax_error( self.client.post( url('forum:read-all-in-subcategory', self.subcat1.id)), 'common.login_required') self.assertEqual(SubCategoryReadInfoPrototype._db_count(), 0)
def test_subcategory__unlogined(self): texts = [ 'cat1-caption', 'subcat1-caption', 'thread1-caption', 'thread2-caption', self.fixture.clan_1.abbr ] self.request_logout() self.check_html_ok(self.request_html( url('forum:subcategories:show', self.subcat1.id)), texts=texts) self.assertEqual(SubCategoryReadInfoPrototype._db_count(), 0)
def test_remove_old_infos(self): read_info_1 = SubCategoryReadInfoPrototype.read_subcategory(self.subcategory, self.account) read_info_2 = SubCategoryReadInfoPrototype.read_subcategory(self.subcategory, self.account_2) removed_time = datetime.datetime.now() - datetime.timedelta(seconds=forum_settings.UNREAD_STATE_EXPIRE_TIME) SubCategoryReadInfoPrototype._model_class.objects.filter(id=read_info_2.id).update(read_at=removed_time) self.assertEqual(SubCategoryReadInfoPrototype._db_count(), 2) SubCategoryReadInfoPrototype.remove_old_infos() self.assertEqual(SubCategoryReadInfoPrototype._db_count(), 1) self.assertEqual(SubCategoryReadInfoPrototype._db_get_object(0).id, read_info_1.id)
def read_all__one(self): SubCategoryReadInfoPrototype.read_all_in_subcategory( subcategory=self.subcategory, account=self.account) return self.json_ok()
def test_subcategory_has_new_messages__read_all(self): SubCategoryReadInfoPrototype.read_all_in_subcategory(subcategory=self.subcategory, account=self.account) self.assertFalse(self.get_read_state().subcategory_has_new_messages(self.subcategory))
def test_create_when_created(self): read_info_1 = SubCategoryReadInfoPrototype.read_subcategory(self.subcategory, self.account) read_info_2 = SubCategoryReadInfoPrototype.read_subcategory(self.subcategory, self.account) self.assertEqual(read_info_1.id, read_info_2.id)
def test_subcategory_has_new_messages__new_thread(self): SubCategoryReadInfoPrototype.read_all_in_subcategory(subcategory=self.subcategory, account=self.account) self.assertFalse(self.get_read_state().subcategory_has_new_messages(self.subcategory)) ThreadPrototype.create(self.subcategory, 'new-threwad', self.account_2, 'thread-new-text') self.assertTrue(self.get_read_state().subcategory_has_new_messages(self.subcategory))
def test_subcategory_has_new_messages__unread_state_expired(self): SubCategoryReadInfoPrototype.read_subcategory(subcategory=self.subcategory, account=self.account) self.subcategory._model.updated_at = datetime.datetime.now() self.subcategory.save() self.assertFalse(self.get_read_state().subcategory_has_new_messages(self.subcategory))
def test_subcategory_has_new_messages__new_post(self): SubCategoryReadInfoPrototype.read_all_in_subcategory(subcategory=self.subcategory, account=self.account) self.assertFalse(self.get_read_state().subcategory_has_new_messages(self.subcategory)) PostPrototype.create(self.thread, self.account_2, 'post-new-text') self.assertTrue(self.get_read_state().subcategory_has_new_messages(self.subcategory))
def test_unlogined(self): self.request_logout() self.check_ajax_error(self.client.post(url('forum:read-all')), 'common.login_required') self.assertEqual(SubCategoryReadInfoPrototype._db_count(), 0)
def test_subcategory__unlogined(self): texts = ['cat1-caption', 'subcat1-caption', 'thread1-caption', 'thread2-caption', self.fixture.clan_1.abbr] self.request_logout() self.check_html_ok(self.request_html(url('forum:subcategories:show', self.subcat1.id)), texts=texts) self.assertEqual(SubCategoryReadInfoPrototype._db_count(), 0)
def test_subcategory_expired__no_read_info(self): SubCategoryReadInfoPrototype._db_all().delete() self.assertFalse(self.get_read_state().subcategory_has_new_messages(self.subcategory))
def read_all__one(self): SubCategoryReadInfoPrototype.read_all_in_subcategory(subcategory=self.subcategory, account=self.account) return self.json_ok()
def test_subcategory_expired__no_read_info(self): SubCategoryReadInfoPrototype._db_all().delete() self.assertFalse(self.get_read_state().subcategory_has_new_messages( self.subcategory))
def read_all__all(self): for subcategory in SubCategoryPrototype.subcategories_visible_to_account(account=self.account): SubCategoryReadInfoPrototype.read_all_in_subcategory(subcategory=subcategory, account=self.account) return self.json_ok()
def get_subcategories_read_info(self): if not self._account.is_authenticated: return {} return SubCategoryReadInfoPrototype.get_subcategories_info( account_id=self._account.id)
def test_subcategory_has_new_messages__read_all(self): SubCategoryReadInfoPrototype.read_all_in_subcategory( subcategory=self.subcategory, account=self.account) self.assertFalse(self.get_read_state().subcategory_has_new_messages( self.subcategory))
def get_subcategories_read_info(self): if not self._account.is_authenticated(): return {} return SubCategoryReadInfoPrototype.get_subcategories_info(account_id=self._account.id)
def read_all__all(self): for subcategory in SubCategoryPrototype.subcategories_visible_to_account( account=self.account): SubCategoryReadInfoPrototype.read_all_in_subcategory( subcategory=subcategory, account=self.account) return self.json_ok()