def test_content_ordering(self): category_1 = ContentCategoryFactory() category_2 = ContentCategoryFactory() subcategory_1 = SubCategoryFactory(category=category_1) subcategory_1.position = 5 subcategory_1.save() subcategory_2 = SubCategoryFactory(category=category_1) subcategory_2.position = 1 subcategory_2.save() subcategory_3 = SubCategoryFactory(category=category_2) tuto_1 = PublishableContentFactory(type='TUTORIAL') tuto_1.subcategory.add(subcategory_1) tuto_1_draft = tuto_1.load_version() publish_content(tuto_1, tuto_1_draft, is_major_update=True) top_categories_tuto = top_categories_content('TUTORIAL').get( 'categories') expected = [(subcategory_1.title, subcategory_1.slug, category_1.slug)] self.assertEqual(top_categories_tuto[category_1.title], expected) tuto_2 = PublishableContentFactory(type='TUTORIAL') tuto_2.subcategory.add(subcategory_2) tuto_2_draft = tuto_2.load_version() publish_content(tuto_2, tuto_2_draft, is_major_update=True) top_categories_tuto = top_categories_content('TUTORIAL').get( 'categories') # New subcategory is now first is the list expected.insert( 0, (subcategory_2.title, subcategory_2.slug, category_1.slug)) self.assertEqual(top_categories_tuto[category_1.title], expected) article_1 = PublishableContentFactory(type='TUTORIAL') article_1.subcategory.add(subcategory_3) article_1_draft = tuto_2.load_version() publish_content(article_1, article_1_draft, is_major_update=True) # New article has no impact top_categories_tuto = top_categories_content('TUTORIAL').get( 'categories') self.assertEqual(top_categories_tuto[category_1.title], expected) top_categories_contents = top_categories_content( ['TUTORIAL', 'ARTICLE']).get('categories') expected_2 = [(subcategory_3.title, subcategory_3.slug, category_2.slug)] self.assertEqual(top_categories_contents[category_1.title], expected) self.assertEqual(top_categories_contents[category_2.title], expected_2)
def get_context_data(self, **kwargs): context = super(ListOnlineContents, self).get_context_data(**kwargs) for public_content in context["public_contents"]: if public_content.content.last_note is not None: public_content.content.last_note.related_content = public_content.content public_content.content.public_version = public_content public_content.content.count_note = public_content.count_note context['tag'] = self.tag context['top_categories'] = top_categories_content( self.current_content_type) return context
def test_top_tags_content(self): """Unit testing top_categories_content method """ tags_tuto = ['a', 'b', 'c'] tags_article = ['a', 'd', 'e'] content = PublishedContentFactory(type='TUTORIAL', author_list=[ProfileFactory().user]) content.add_tags(tags_tuto) content.save() tags_tuto = content.tags.all() content = PublishedContentFactory(type='ARTICLE', author_list=[ProfileFactory().user]) content.add_tags(tags_article) content.save() tags_article = content.tags.all() top_tags_tuto = top_categories_content('TUTORIAL').get('tags') top_tags_article = top_categories_content('ARTICLE').get('tags') self.assertEqual(list(top_tags_tuto), list(tags_tuto)) self.assertEqual(list(top_tags_article), list(tags_article))