def create(cls, subcategory, caption, author, text, markup_method=MARKUP_METHOD.POSTMARKUP, technical=False): from the_tale.post_service.prototypes import MessagePrototype from the_tale.post_service.message_handlers import ForumThreadHandler if isinstance(subcategory, int): subcategory = SubCategoryPrototype.get_by_id(subcategory) thread_model = Thread.objects.create(subcategory=subcategory._model, caption=caption, author=author._model, last_poster=author._model, technical=technical, posts_count=0) Post.objects.create(thread=thread_model, author=author._model, markup_method=markup_method, technical=technical, text=text, created_at_turn=TimePrototype.get_current_turn_number(), state=POST_STATE.DEFAULT) prototype = cls(model=thread_model) subcategory.update() MessagePrototype.create(ForumThreadHandler(thread_id=prototype.id)) return prototype
def send_mails(news): from the_tale.post_service.prototypes import MessagePrototype as PostServiceMessagePrototype from the_tale.post_service.message_handlers import NewsHandler PostServiceMessagePrototype.create(NewsHandler(news_id=news.id)) news.emailed = relations.EMAILED_STATE.EMAILED save_news(news)
def request_email_confirmation(self): from the_tale.post_service.prototypes import MessagePrototype from the_tale.post_service.message_handlers import ChangeEmailNotificationHandler if self._model.new_email is None: raise exceptions.NewEmailNotSpecifiedError() MessagePrototype.create(ChangeEmailNotificationHandler(task_id=self.id), now=True)
def notify_post_service(answer, recipients_ids): from the_tale.post_service.prototypes import MessagePrototype from the_tale.post_service.message_handlers import PersonalMessageHandler for recipient_id in recipients_ids: MessagePrototype.create( PersonalMessageHandler(message_id=answer.message_id, account_id=recipient_id))
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 create(cls, account): from the_tale.post_service.prototypes import MessagePrototype from the_tale.post_service.message_handlers import ResetPasswordHandler model = cls._model_class.objects.create(account=account._model, uuid=uuid.uuid4().hex) prototype = cls(model=model) MessagePrototype.create(ResetPasswordHandler(account_id=account.id, task_uuid=prototype.uuid), now=True) return prototype
def test_get_priority_message_no_messages(self): message_1 = MessagePrototype.create(handler=TestHandler()) message_2 = MessagePrototype.create(handler=TestHandler()) message_1._model.state = MESSAGE_STATE.PROCESSED message_1.save() message_2._model.state = MESSAGE_STATE.ERROR message_2.save() self.assertEqual(MessagePrototype.get_priority_message(), None)
def create(cls, sender, recipient, text): from the_tale.post_service.prototypes import MessagePrototype as PostServiceMessagePrototype from the_tale.post_service.message_handlers import PersonalMessageHandler model = Message.objects.create(recipient=recipient._model, sender=sender._model, text=text) recipient.increment_new_messages_number() prototype = cls(model=model) PostServiceMessagePrototype.create(PersonalMessageHandler(message_id=prototype.id)) return prototype
def create(cls, sender, recipient, text): from the_tale.post_service.prototypes import MessagePrototype as PostServiceMessagePrototype from the_tale.post_service.message_handlers import PersonalMessageHandler model = Message.objects.create(recipient=recipient._model, sender=sender._model, text=text) recipient.increment_new_messages_number() prototype = cls(model=model) PostServiceMessagePrototype.create( PersonalMessageHandler(message_id=prototype.id)) return prototype
def create_task_and_message(self, account, new_nick): task = ChangeCredentialsTaskPrototype.create(account=account, new_email='*****@*****.**' % new_nick, new_password='******', new_nick=new_nick) task.request_email_confirmation() return task, MessagePrototype.get_priority_message()
def setUp(self): super(MessageSenderTests, self).setUp() self.message = MessagePrototype.create(TestHandler()) environment.deinitialize() environment.initialize() self.worker = environment.workers.message_sender
def setUp(self): super(ResetPasswordTests, self).setUp() create_test_map() self.account_1 = self.accounts_factory.create_account() self.reset_task = ResetPasswordTaskPrototype.create(self.account_1) self.message = MessagePrototype.get_priority_message()
def setUp(self): super(ResetPasswordTests, self).setUp() create_test_map() register_user('user_1', '*****@*****.**', '111111') self.account_1 = AccountPrototype.get_by_nick('user_1') self.reset_task = ResetPasswordTaskPrototype.create(self.account_1) self.message = MessagePrototype.get_priority_message()
def setUp(self): super(PersonalMessagesTests, self).setUp() create_test_map() self.account_1 = self.accounts_factory.create_account() self.account_2 = self.accounts_factory.create_account() self.personal_message = PersonalMessagePrototype.create(self.account_1, self.account_2, 'test text') self.message = MessagePrototype.get_priority_message()
def setUp(self): super(PersonalMessagesTests, self).setUp() create_test_map() self.account_1 = self.accounts_factory.create_account() self.account_2 = self.accounts_factory.create_account() self.personal_message = PersonalMessagePrototype.create( self.account_1, self.account_2, 'test text') self.message = MessagePrototype.get_priority_message()
def test_mail_send__to_system_user(self): from the_tale.accounts.logic import get_system_user Message.objects.all().delete() ResetPasswordTaskPrototype.create(get_system_user()) message = MessagePrototype.get_priority_message() self.assertEqual(len(mail.outbox), 0) message.process() self.assertTrue(message.state.is_PROCESSED) self.assertEqual(len(mail.outbox), 0)
def setUp(self): super(NewForumThreadTests, self).setUp() create_test_map() register_user('user_1', '*****@*****.**', '111111') self.account_1 = AccountPrototype.get_by_nick('user_1') self.category = CategoryPrototype.create(caption='cat-caption', slug='cat-slug', order=0) self.subcategory = SubCategoryPrototype.create(category=self.category, caption='subcat-caption', order=0) self.thread = ThreadPrototype.create(self.subcategory, 'thread_1-caption', self.account_1, 'thread-text') self.message = MessagePrototype.get_priority_message()
def setUp(self): super(NewForumThreadTests, self).setUp() create_test_map() self.account_1 = self.accounts_factory.create_account() self.category = CategoryPrototype.create(caption='cat-caption', slug='cat-slug', order=0) self.subcategory = SubCategoryPrototype.create(category=self.category, caption='subcat-caption', order=0) self.thread = ThreadPrototype.create(self.subcategory, 'thread_1-caption', self.account_1, 'thread-text') self.message = MessagePrototype.get_priority_message()
def setUp(self): super(PersonalMessagesTests, self).setUp() create_test_map() self.account_1 = self.accounts_factory.create_account() self.account_2 = self.accounts_factory.create_account() pm_logic.send_message(self.account_1.id, [self.account_2.id], 'test text') self.message = MessagePrototype.get_priority_message()
def create(cls, thread, author, text, technical=False): from the_tale.post_service.prototypes import MessagePrototype from the_tale.post_service.message_handlers import ForumPostHandler post = Post.objects.create(thread=thread._model, author=author._model, text=text, technical=technical, markup_method=MARKUP_METHOD.POSTMARKUP, created_at_turn=TimePrototype.get_current_turn_number(), state=POST_STATE.DEFAULT) thread.update() thread.subcategory.update() prototype = cls(post) MessagePrototype.create(ForumPostHandler(post_id=prototype.id)) return prototype
def setUp(self): super(PersonalMessagesTests, self).setUp() create_test_map() register_user('user_1', '*****@*****.**', '111111') self.account_1 = AccountPrototype.get_by_nick('user_1') register_user('user_2', '*****@*****.**', '111111') self.account_2 = AccountPrototype.get_by_nick('user_2') self.personal_message = PersonalMessagePrototype.create(self.account_1, self.account_2, 'test text') self.message = MessagePrototype.get_priority_message()
def test_mail_send__to_system_user(self): from the_tale.accounts.logic import get_system_user Message.objects.all().delete() pm_logic.send_message(self.account_1.id, [get_system_user().id], 'test text') message = MessagePrototype.get_priority_message() self.assertEqual(len(mail.outbox), 0) message.process() self.assertTrue(message.state.is_PROCESSED) self.assertEqual(len(mail.outbox), 0)
def setUp(self): super(PersonalMessagesTests, self).setUp() create_test_map() register_user('user_1', '*****@*****.**', '111111') self.account_1 = AccountPrototype.get_by_nick('user_1') register_user('user_2', '*****@*****.**', '111111') self.account_2 = AccountPrototype.get_by_nick('user_2') self.personal_message = PersonalMessagePrototype.create( self.account_1, self.account_2, 'test text') self.message = MessagePrototype.get_priority_message()
def test_mail_send_for_fast_account(self): account_2 = self.accounts_factory.create_account() task, message = self.create_task_and_message(account_2, 'user_2_new') self.assertEqual(len(mail.outbox), 0) message = MessagePrototype.get_priority_message() message.process() self.assertTrue(message.state.is_PROCESSED) self.assertEqual(len(mail.outbox), 1) self.assertEqual(mail.outbox[0].to, ['*****@*****.**']) self.assertTrue(task.uuid in mail.outbox[0].body) self.assertTrue(task.uuid in mail.outbox[0].alternatives[0][0])
def setUp(self): super(NewNewsTests, self).setUp() create_test_map() self.account_1 = self.accounts_factory.create_account() self.account_2 = self.accounts_factory.create_account() self.account_3 = self.accounts_factory.create_account() self.news = news_logic.create_news(caption='news-caption', description='news-description', content='news-content') news_logic.send_mails(self.news) self.message = MessagePrototype.get_priority_message() # enshure that system user exists accounts_logic.get_system_user()
def setUp(self): super(NewForumThreadTests, self).setUp() create_test_map() self.account_1 = self.accounts_factory.create_account() self.category = CategoryPrototype.create(caption='cat-caption', slug='cat-slug', order=0) self.subcategory = SubCategoryPrototype.create( category=self.category, caption='subcat-caption', order=0) self.thread = ThreadPrototype.create(self.subcategory, 'thread_1-caption', self.account_1, 'thread-text') self.message = MessagePrototype.get_priority_message()
def test_remove_old_messages(self): message_1 = MessagePrototype.create(handler=TestHandler()) message_1._model.state = MESSAGE_STATE.PROCESSED message_1.save() message_2 = MessagePrototype.create(handler=TestHandler()) message_2._model.created_at -= datetime.timedelta(seconds=post_service_settings.MESSAGE_LIVE_TIME) message_2._model.state = MESSAGE_STATE.PROCESSED message_2.save() message_3 = MessagePrototype.create(handler=TestHandler()) message_3._model.created_at -= datetime.timedelta(seconds=post_service_settings.MESSAGE_LIVE_TIME) message_3.save() MessagePrototype.remove_old_messages() self.assertEqual(MessagePrototype._db_count(), 2) self.assertEqual(MessagePrototype._db_get_object(0).id, message_1.id) self.assertEqual(MessagePrototype._db_get_object(1).id, message_3.id)
def test_remove_old_messages(self): message_1 = MessagePrototype.create(handler=TestHandler()) message_1._model.state = MESSAGE_STATE.PROCESSED message_1.save() message_2 = MessagePrototype.create(handler=TestHandler()) message_2._model.created_at -= datetime.timedelta( seconds=post_service_settings.MESSAGE_LIVE_TIME) message_2._model.state = MESSAGE_STATE.PROCESSED message_2.save() message_3 = MessagePrototype.create(handler=TestHandler()) message_3._model.created_at -= datetime.timedelta( seconds=post_service_settings.MESSAGE_LIVE_TIME) message_3.save() MessagePrototype.remove_old_messages() self.assertEqual(MessagePrototype._db_count(), 2) self.assertEqual(MessagePrototype._db_get_object(0).id, message_1.id) self.assertEqual(MessagePrototype._db_get_object(1).id, message_3.id)
def process_no_cmd(self): if self.next_message_process_time < datetime.datetime.now(): if not self.send_message(MessagePrototype.get_priority_message()): self.next_message_process_time = datetime.datetime.now( ) + datetime.timedelta( seconds=post_service_settings.MESSAGE_SENDER_DELAY)
def test_create_without_now(self): with mock.patch('the_tale.post_service.workers.message_sender.Worker.cmd_send_now') as cmd_send_now: MessagePrototype.create(TestHandler()) self.assertEqual(cmd_send_now.call_count, 0)
def test_create_with_now(self): with mock.patch('the_tale.post_service.workers.message_sender.Worker.cmd_send_now') as cmd_send_now: message = MessagePrototype.create(TestHandler(), now=True) self.assertEqual(cmd_send_now.call_args, mock.call(message.id))
def test_create_with_now(self): with mock.patch( 'the_tale.post_service.workers.message_sender.Worker.cmd_send_now' ) as cmd_send_now: message = MessagePrototype.create(TestHandler(), now=True) self.assertEqual(cmd_send_now.call_args, mock.call(message.id))
def process_no_cmd(self): if self.next_message_process_time < datetime.datetime.now(): if not self.send_message(MessagePrototype.get_priority_message()): self.next_message_process_time = datetime.datetime.now() + datetime.timedelta(seconds=post_service_settings.MESSAGE_SENDER_DELAY)
def process_send_now(self, message_id): self.send_message(MessagePrototype.get_by_id(message_id))
def test_create_without_now(self): with mock.patch( 'the_tale.post_service.workers.message_sender.Worker.cmd_send_now' ) as cmd_send_now: MessagePrototype.create(TestHandler()) self.assertEqual(cmd_send_now.call_count, 0)