def create_post(self): new_post_delay = PostPrototype.get_new_post_delay(self.account) if new_post_delay > 0: error_message = ( u'Создавать новые сообщения можно не чаще раза в %d секунд. <br/>Задержка увеличивается для игроков, только начинающих общаться на форуме.<br/> Вы сможете создать новое сообщение через %d сек.' % (forum_settings.POST_DELAY, int(new_post_delay))) return self.json_error('forum.create_post.delay', error_message) new_post_form = forms.NewPostForm(self.request.POST) if not new_post_form.is_valid(): return self.json_error('forum.create_post.form_errors', new_post_form.errors) post = PostPrototype.create(self.thread, self.account, new_post_form.c.text) if self.account.is_authenticated(): ThreadReadInfoPrototype.read_thread(self.thread, self.account) return self.json_ok( data={ 'next_url': url('forum:threads:show', self.thread.id, page=self.thread.paginator.pages_count) + ('#m%d' % post.id) })
def test_thread_is_new_messages__thread_read(self): read_state = self.get_read_state() self.assertTrue(read_state.thread_is_new(self.thread)) ThreadReadInfoPrototype.read_thread(self.thread, self.account) read_state = self.get_read_state() self.assertFalse(read_state.thread_is_new(self.thread))
def test_thread_is_new_messages__thread_read(self): read_state = self.get_read_state() self.assertTrue(read_state.thread_is_new(self.thread)) ThreadReadInfoPrototype.read_thread(self.thread, self.account) read_state = self.get_read_state() self.assertFalse(read_state.thread_is_new(self.thread))
def test_read_thread__existed_info(self): self.assertEqual(ThreadReadInfoPrototype._db_count(), 0) read_info = ThreadReadInfoPrototype.read_thread(self.thread, self.account) read_info_2 = ThreadReadInfoPrototype.read_thread(self.thread, self.account) self.assertEqual(read_info.id, read_info_2.id) self.assertEqual(ThreadReadInfoPrototype._db_count(), 1) self.assertEqual(read_info.thread_id, self.thread.id) self.assertEqual(read_info.account_id, self.account.id) self.assertTrue(read_info.read_at > datetime.datetime.now() - datetime.timedelta(seconds=1))
def test_read_thread__existed_info(self): self.assertEqual(ThreadReadInfoPrototype._db_count(), 0) read_info = ThreadReadInfoPrototype.read_thread(self.thread, self.account) read_info_2 = ThreadReadInfoPrototype.read_thread(self.thread, self.account) self.assertEqual(read_info.id, read_info_2.id) self.assertEqual(ThreadReadInfoPrototype._db_count(), 1) self.assertEqual(read_info.thread_id, self.thread.id) self.assertEqual(read_info.account_id, self.account.id) self.assertTrue(read_info.read_at > datetime.datetime.now() - datetime.timedelta(seconds=1))
def test_remove_old_infos(self): read_info_1 = ThreadReadInfoPrototype.read_thread(self.thread, self.account) read_info_2 = ThreadReadInfoPrototype.read_thread(self.thread, self.account_2) removed_time = datetime.datetime.now() - datetime.timedelta(seconds=forum_settings.UNREAD_STATE_EXPIRE_TIME) ThreadReadInfoPrototype._model_class.objects.filter(id=read_info_2.id).update(read_at=removed_time) self.assertEqual(ThreadReadInfoPrototype._db_count(), 2) ThreadReadInfoPrototype.remove_old_infos() self.assertEqual(ThreadReadInfoPrototype._db_count(), 1) self.assertEqual(ThreadReadInfoPrototype._db_get_object(0).id, read_info_1.id)
def test_thread_has_new_messages__read(self): ThreadReadInfoPrototype.read_thread(self.thread, self.account) read_state = self.get_read_state() self.assertFalse(read_state.thread_has_new_messages(self.thread)) self.thread._model.updated_at = datetime.datetime.now() self.thread.save() self.assertTrue(read_state.thread_has_new_messages(self.thread))
def test_remove_old_infos(self): read_info_1 = ThreadReadInfoPrototype.read_thread(self.thread, self.account) read_info_2 = ThreadReadInfoPrototype.read_thread(self.thread, self.account_2) removed_time = datetime.datetime.now() - datetime.timedelta(seconds=forum_settings.UNREAD_STATE_EXPIRE_TIME) ThreadReadInfoPrototype._model_class.objects.filter(id=read_info_2.id).update(read_at=removed_time) self.assertEqual(ThreadReadInfoPrototype._db_count(), 2) ThreadReadInfoPrototype.remove_old_infos() self.assertEqual(ThreadReadInfoPrototype._db_count(), 1) self.assertEqual(ThreadReadInfoPrototype._db_get_object(0).id, read_info_1.id)
def test_thread_has_new_messages__read(self): ThreadReadInfoPrototype.read_thread(self.thread, self.account) read_state = self.get_read_state() self.assertFalse(read_state.thread_has_new_messages(self.thread)) self.thread._model.updated_at = datetime.datetime.now() self.thread.save() self.assertTrue(read_state.thread_has_new_messages(self.thread))
def get_thread(self, page=1): thread_data = ThreadPageData() if not thread_data.initialize(account=self.account, thread=self.thread, page=page): return self.redirect(thread_data.paginator.last_page_url, permanent=False) if self.account.is_authenticated: ThreadReadInfoPrototype.read_thread(self.thread, self.account) return self.template('forum/thread.html', {'category': self.category, 'thread': self.thread, 'thread_data': thread_data} )
def get_thread(self, page=1): thread_data = ThreadPageData() if not thread_data.initialize( account=self.account, thread=self.thread, page=page): return self.redirect(thread_data.paginator.last_page_url, permanent=False) if self.account.is_authenticated(): ThreadReadInfoPrototype.read_thread(self.thread, self.account) return self.template( 'forum/thread.html', { 'category': self.category, 'thread': self.thread, 'thread_data': thread_data })
def create_post(self): new_post_delay = PostPrototype.get_new_post_delay(self.account) if new_post_delay > 0: error_message = ('Создавать новые сообщения можно не чаще раза в %d секунд. <br/>Задержка увеличивается для игроков, только начинающих общаться на форуме.<br/> Вы сможете создать новое сообщение через %d сек.' % ( forum_settings.POST_DELAY, int(new_post_delay))) return self.json_error('forum.create_post.delay', error_message) new_post_form = forms.NewPostForm(self.request.POST) if not new_post_form.is_valid(): return self.json_error('forum.create_post.form_errors', new_post_form.errors) post = PostPrototype.create(self.thread, self.account, new_post_form.c.text) if self.account.is_authenticated: ThreadReadInfoPrototype.read_thread(self.thread, self.account) return self.json_ok(data={'next_url': url('forum:threads:show', self.thread.id, page=self.thread.paginator.pages_count) + ('#m%d' % post.id)})
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_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))