def test_one_subscription(self): SubscriptionPrototype.create(self.account_1, subcategory=self.subcategory) self.assertEqual(len(mail.outbox), 0) self.message.process() self.assertTrue(self.message.state.is_PROCESSED) self.assertEqual(len(mail.outbox), 1) self.assertEqual(mail.outbox[0].to, [self.account_1.email]) self.assertTrue(self.thread.author.nick in mail.outbox[0].body) self.assertTrue(self.thread.caption in mail.outbox[0].body) self.assertTrue( self.thread.paginator.last_page_url in mail.outbox[0].body) self.assertTrue( self.thread.get_first_post().html in mail.outbox[0].body) self.assertTrue(project_settings.SITE_URL in mail.outbox[0].body) self.assertTrue( self.thread.author.nick in mail.outbox[0].alternatives[0][0]) self.assertTrue( self.thread.caption in mail.outbox[0].alternatives[0][0]) self.assertTrue(self.thread.paginator.last_page_url in mail.outbox[0].alternatives[0][0]) self.assertTrue(self.thread.get_first_post().html in mail.outbox[0].alternatives[0][0]) self.assertTrue( project_settings.SITE_URL in mail.outbox[0].alternatives[0][0])
def test_mail_send__to_system_user(self): from the_tale.accounts.logic import get_system_user SubscriptionPrototype.create(get_system_user(), subcategory=self.subcategory) self.assertEqual(len(mail.outbox), 0) self.message.process() self.assertTrue(self.message.state.is_PROCESSED) self.assertEqual(len(mail.outbox), 0)
def test_mail_send__to_system_user(self): from the_tale.accounts.logic import get_system_user SubscriptionPrototype.create(get_system_user(), self.thread) self.assertEqual(len(mail.outbox), 0) self.message.process() self.assertTrue(self.message.state.is_PROCESSED) self.assertEqual(len(mail.outbox), 0)
def subscriptions(self): return self.template( 'forum/subscriptions.html', { 'threads': SubscriptionPrototype.get_threads_for_account(self.account), 'subcategories': SubscriptionPrototype.get_subcategories_for_account( self.account) })
def test_many_subscriptions(self): account_2 = self.accounts_factory.create_account() SubscriptionPrototype.create(self.account_1, subcategory=self.subcategory) SubscriptionPrototype.create(account_2, subcategory=self.subcategory) self.assertEqual(len(mail.outbox), 0) self.message.process() self.assertTrue(self.message.state.is_PROCESSED) self.assertEqual(len(mail.outbox), 2) self.assertEqual(mail.outbox[0].to, [self.account_1.email]) self.assertEqual(mail.outbox[1].to, [account_2.email])
def test_many_subscriptions(self): register_user('user_2', '*****@*****.**', '111111') account_2 = AccountPrototype.get_by_nick('user_2') SubscriptionPrototype.create(self.account_1, self.thread) SubscriptionPrototype.create(account_2, self.thread) self.assertEqual(len(mail.outbox), 0) self.message.process() self.assertTrue(self.message.state.is_PROCESSED) self.assertEqual(len(mail.outbox), 2) self.assertEqual(mail.outbox[0].to, [self.account_1.email]) self.assertEqual(mail.outbox[1].to, [account_2.email])
def test_many_subscriptions(self): register_user('user_2', '*****@*****.**', '111111') account_2 = AccountPrototype.get_by_nick('user_2') SubscriptionPrototype.create(self.account_1, subcategory=self.subcategory) SubscriptionPrototype.create(account_2, subcategory=self.subcategory) self.assertEqual(len(mail.outbox), 0) self.message.process() self.assertTrue(self.message.state.is_PROCESSED) self.assertEqual(len(mail.outbox), 2) self.assertEqual(mail.outbox[0].to, [self.account_1.email]) self.assertEqual(mail.outbox[1].to, [account_2.email])
def unsubscribe(self): subscription = SubscriptionPrototype.get_for(self.account, thread=self.thread) if subscription: subscription.remove() return self.json_ok()
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 unsubscribe(self): subscription = SubscriptionPrototype.get_for(self.account, subcategory=self.subcategory) if subscription: subscription.remove() return self.json_ok()
def test_one_subscription(self): SubscriptionPrototype.create(self.account_1, self.thread) self.assertEqual(len(mail.outbox), 0) self.message.process() self.assertTrue(self.message.state.is_PROCESSED) self.assertEqual(len(mail.outbox), 1) self.assertEqual(mail.outbox[0].to, [self.account_1.email]) self.assertTrue(self.post.author.nick in mail.outbox[0].body) self.assertTrue(self.post.thread.caption in mail.outbox[0].body) self.assertTrue(self.post.thread.paginator.last_page_url in mail.outbox[0].body) self.assertTrue(project_settings.SITE_URL in mail.outbox[0].body) self.assertTrue(self.post.author.nick in mail.outbox[0].alternatives[0][0]) self.assertTrue(self.post.thread.caption in mail.outbox[0].alternatives[0][0]) self.assertTrue(self.post.thread.paginator.last_page_url in mail.outbox[0].alternatives[0][0]) self.assertTrue(project_settings.SITE_URL in mail.outbox[0].alternatives[0][0])
def unsubscribe(self): subscription = SubscriptionPrototype.get_for( self.account, subcategory=self.subcategory) if subscription: subscription.remove() return self.json_ok()
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 initialize(self, account, thread, page, inline=False): from the_tale.game.heroes.prototypes import HeroPrototype self.account = account self.thread = thread url_builder = UrlBuilder(reverse('forum:threads:show', args=[self.thread.id]), arguments={'page': page}) page -= 1 self.paginator = Paginator(page, thread.posts_count+1, forum_settings.POSTS_ON_PAGE, url_builder) if self.paginator.wrong_page_number: return False post_from, post_to = self.paginator.page_borders(page) self.post_from = post_from self.posts = [PostPrototype(post_model) for post_model in Post.objects.filter(thread=self.thread._model).order_by('created_at')[post_from:post_to]] self.authors = {author.id:author for author in AccountPrototype.get_list_by_id([post.author_id for post in self.posts])} self.game_objects = {game_object.account_id:game_object for game_object in HeroPrototype.get_list_by_id([post.author_id for post in self.posts])} pages_on_page_slice = self.posts if post_from == 0: pages_on_page_slice = pages_on_page_slice[1:] self.has_post_on_page = any([post.author.id == self.account.id for post in pages_on_page_slice]) self.new_post_form = forms.NewPostForm() self.start_posts_from = page * forum_settings.POSTS_ON_PAGE self.inline = inline self.can_delete_posts = can_delete_posts(self.account, self.thread) self.can_change_posts = can_change_posts(self.account) self.can_delete_thread = not self.inline and can_delete_thread(self.account) self.can_change_thread = not self.inline and can_change_thread(self.account, self.thread) self.ignore_first_post = (self.inline and self.paginator.current_page_number==0) self.can_post = self.account.is_authenticated() and not self.account.is_fast self.no_posts = (len(self.posts) == 0) or (self.ignore_first_post and len(self.posts) == 1) self.can_subscribe = self.account.is_authenticated() and not self.account.is_fast self.has_subscription = SubscriptionPrototype.has_subscription(self.account, self.thread) return True
def process(self): from the_tale.forum.prototypes import PostPrototype, SubscriptionPrototype post = PostPrototype.get_by_id(self.post_id) if post is None: return True # post can be removed by admins or with removed thread accounts = SubscriptionPrototype.get_accounts_for_thread(post.thread) subject = '«Сказка»: %s' % post.thread.caption context = {'post': post} html_content = jinja2.render(self.EMAIL_HTML_TEMPLATE, context) text_content = jinja2.render(self.EMAIL_TEXT_TEMPLATE, context) return logic.send_mail(accounts, subject, text_content, html_content)
def process(self): from the_tale.forum.prototypes import ThreadPrototype, SubscriptionPrototype thread = ThreadPrototype.get_by_id(self.thread_id) if thread is None: return True # thread can be removed by admins or with removed thread post = thread.get_first_post() accounts = SubscriptionPrototype.get_accounts_for_subcategory(thread.subcategory) subject = '«Сказка»: новая тема на форуме' context = {'thread': thread, 'post': post} html_content = jinja2.render(self.EMAIL_HTML_TEMPLATE, context) text_content = jinja2.render(self.EMAIL_TEXT_TEMPLATE, context) return logic.send_mail(accounts, subject, text_content, html_content)
def subscribe(self): SubscriptionPrototype.create(self.account, subcategory=self.subcategory) return self.json_ok()
def initialize(self, account, thread, page, inline=False): self.account = account self.thread = thread url_builder = UrlBuilder(reverse('forum:threads:show', args=[self.thread.id]), arguments={'page': page}) page -= 1 self.paginator = Paginator(page, thread.posts_count + 1, forum_settings.POSTS_ON_PAGE, url_builder) if self.paginator.wrong_page_number: return False post_from, post_to = self.paginator.page_borders(page) self.post_from = post_from self.posts = [ PostPrototype(post_model) for post_model in Post.objects.filter(thread=self.thread._model). order_by('created_at')[post_from:post_to] ] self.authors = { author.id: author for author in AccountPrototype.get_list_by_id( [post.author_id for post in self.posts]) } self.game_objects = { game_object.account_id: game_object for game_object in heroes_logic.load_heroes_by_account_ids( [post.author_id for post in self.posts]) } pages_on_page_slice = self.posts if post_from == 0: pages_on_page_slice = pages_on_page_slice[1:] self.has_post_on_page = any([ post.author.id == self.account.id for post in pages_on_page_slice ]) self.new_post_form = forms.NewPostForm() self.start_posts_from = page * forum_settings.POSTS_ON_PAGE self.inline = inline self.can_delete_posts = can_delete_posts(self.account, self.thread) self.can_change_posts = can_change_posts(self.account) self.can_delete_thread = not self.inline and can_delete_thread( self.account) self.can_change_thread = not self.inline and can_change_thread( self.account, self.thread) self.ignore_first_post = (self.inline and self.paginator.current_page_number == 0) self.can_post = self.account.is_authenticated( ) and not self.account.is_fast self.no_posts = (len(self.posts) == 0) or (self.ignore_first_post and len(self.posts) == 1) self.can_subscribe = self.account.is_authenticated( ) and not self.account.is_fast self.has_subscription = SubscriptionPrototype.has_subscription( self.account, self.thread) return True
def test_remove_all_in_subcategory(self): SubscriptionPrototype.create(account=self.account, subcategory=self.subcategory_1) subscr_2 = SubscriptionPrototype.create(account=self.account, subcategory=self.subcategory_2) SubscriptionPrototype.create(account=self.account, thread=self.thread_1_1) SubscriptionPrototype.create(account=self.account, thread=self.thread_1_2) subscr_5 = SubscriptionPrototype.create(account=self.account_2, thread=self.thread_1_2) subscr_6 = SubscriptionPrototype.create(account=self.account_2, thread=self.thread_1_3) subscr_7 = SubscriptionPrototype.create(account=self.account, thread=self.thread_2_1) self.assertEqual(SubscriptionPrototype._db_count(), 7) SubscriptionPrototype.remove_all_in_subcategory(account_id=self.account.id, subcategory_id=self.subcategory_1.id) self.assertEqual(SubscriptionPrototype._db_count(), 4) self.assertEqual(set(s.id for s in SubscriptionPrototype._db_all()), set((subscr_2.id, subscr_5.id, subscr_6.id, subscr_7.id)))
def subscribe(self): SubscriptionPrototype.create(self.account, thread=self.thread) return self.json_ok()
def subscriptions(self): return self.template('forum/subscriptions.html', {'threads': SubscriptionPrototype.get_threads_for_account(self.account), 'subcategories': SubscriptionPrototype.get_subcategories_for_account(self.account)} )