def setUp(self): self.mk_main() main = Main.objects.all().first() self.english = SiteLanguageRelation.objects.create( language_setting=Languages.for_site(main.get_site()), locale='en', is_active=True) self.french = SiteLanguageRelation.objects.create( language_setting=Languages.for_site(main.get_site()), locale='fr', is_active=True) self.spanish = SiteLanguageRelation.objects.create( language_setting=Languages.for_site(main.get_site()), locale='es', is_active=True) self.arabic = SiteLanguageRelation.objects.create( language_setting=Languages.for_site(main.get_site()), locale='ar', is_active=True) self.yourmind = self.mk_section( self.section_index, title='Your mind') self.yourbody = self.mk_section( self.section_index, title='Your body') self.yourmind_sub = self.mk_section( self.yourmind, title='Your mind subsection') self.yourmind_fr = self.mk_section_translation( self.yourmind, self.french, title='Your mind in french') self.yourmind_sub_fr = self.mk_section_translation( self.yourmind_sub, self.french, title='Your mind subsection in french') self.mk_main2() self.main2 = Main.objects.all().last() self.language_setting2 = Languages.objects.create( site_id=self.main2.get_site().pk) self.english2 = SiteLanguageRelation.objects.create( language_setting=self.language_setting2, locale='en', is_active=True) self.spanish = SiteLanguageRelation.objects.create( language_setting=self.language_setting2, locale='es', is_active=True) self.yourmind2 = self.mk_section( self.section_index2, title='Your mind2') self.yourmind_sub2 = self.mk_section( self.yourmind2, title='Your mind subsection2') self.site_settings = SiteSettings.for_site(main.get_site()) self.site_settings2 = SiteSettings.for_site(self.main2.get_site()) self.site_settings.enable_clickable_tags = True self.site_settings.enable_tag_navigation = True self.site_settings.save()
def test_that_only_translated_sections_show_with_tag_navigation(self): site_settings = SiteSettings.for_site(self.main.get_site()) site_settings.enable_tag_navigation = True site_settings.show_only_translated_pages = True site_settings.save() self.mk_section_translation(self.english_section, self.french, title=self.english_section.title + ' in french') article1 = self.mk_article( self.english_section, title='English article1 in English Section', featured_in_homepage_start_date=timezone.now(), featured_in_homepage=True) self.mk_article_translation( article1, self.french, title=article1.title + ' in french', ) promote_articles() response = self.client.get('/') self.assertContains(response, 'English section') response = self.client.get('/locale/fr/') response = self.client.get('/') self.assertContains( response, '<a href="/sections-main-1/english-section-in-french/"' ' class="section-listing__theme-bg-link">' 'English section in french</a>')
def test_latest_rotation_no_time(self): """This test that if the date range and weekdays are set for content rotation, that the content doesn't rotates with no times set""" site = Site.objects.get(is_default_site=True) site_settings = SiteSettings.for_site(site) site_settings.monday_rotation = True site_settings.content_rotation_start_date = timezone.now() site_settings.content_rotation_end_date = timezone.now() + timedelta( days=1) site_settings.save() for i in range(5): self.footer = FooterPage( title='Footer Page %s', slug='footer-page-%s' % (i, )) self.footer_index.add_child(instance=self.footer) self.assertEqual(FooterPage.objects.live().count(), 5) self.assertEqual(self.main.latest_articles().count(), 0) self.mk_articles( self.yourmind_sub, count=10, featured_in_latest_start_date=timezone.now()) promote_articles() self.mk_articles(self.yourmind_sub, count=10, featured_in_latest=False) self.assertEqual(self.main.latest_articles().count(), 10) first_article_old = self.main.latest_articles()[0].pk last_article_old = self.main.latest_articles()[9].pk rotate_content(0) self.assertEqual(first_article_old, self.main.latest_articles()[0].pk) self.assertEqual(last_article_old, self.main.latest_articles()[9].pk)
def test_latest_rotation_no_time(self): """This test that if the date range and weekdays are set for content rotation, that the content doesn't rotates with no times set""" site = Site.objects.get(is_default_site=True) site_settings = SiteSettings.for_site(site) site_settings.monday_rotation = True site_settings.content_rotation_start_date = timezone.now() site_settings.content_rotation_end_date = timezone.now() + timedelta( days=1) site_settings.save() for i in range(5): self.footer = FooterPage(title='Footer Page %s', slug='footer-page-%s' % (i, )) self.footer_index.add_child(instance=self.footer) self.assertEqual(FooterPage.objects.live().count(), 5) self.assertEqual(self.main.latest_articles().count(), 0) self.mk_articles(self.yourmind_sub, count=10, featured_in_latest_start_date=timezone.now()) promote_articles() self.mk_articles(self.yourmind_sub, count=10, featured_in_latest=False) self.assertEqual(self.main.latest_articles().count(), 10) first_article_old = self.main.latest_articles()[0].pk last_article_old = self.main.latest_articles()[9].pk rotate_content(0) self.assertEqual(first_article_old, self.main.latest_articles()[0].pk) self.assertEqual(last_article_old, self.main.latest_articles()[9].pk)
def process_response(self, request, response): if hasattr(settings, 'GOOGLE_ANALYTICS_IGNORE_PATH'): exclude = [ p for p in settings.GOOGLE_ANALYTICS_IGNORE_PATH if request.path.startswith(p) ] if any(exclude): return response # Only track 200 and 302 responses for request.site if not (response.status_code == 200 or response.status_code == 302): return response # exclude requests that contain sensitive sensitive information(email) if (request.get_full_path().find('/search/?q=') > -1): try: search_string = urllib.parse.unquote( request.get_full_path().replace('/search/?q=', '')) except AttributeError: search_string = urllib.unquote(request.get_full_path().replace( '/search/?q=', '')) if re.match(r'[^@]+@[^@]+\.[^@]+', search_string): return response site_settings = SiteSettings.for_site(request.site) response = self.submit_to_local_account(request, response, site_settings) response = self.submit_to_global_account(request, response, site_settings) return response
def get_next_article(context, article): locale_code = context.get('locale_code') section = article.get_parent_section() articles = load_child_articles_for_section(context, section, count=None) article_len = len(articles) if article_len > 1: try: if article_len > articles.index(article) + 1: next_article = articles[articles.index(article) + 1] else: next_article = articles[0] try: return next_article.translated_pages.get( language__locale=locale_code) except: if next_article.language.locale == locale_code or not \ SiteSettings.for_site( context['request'].site).show_only_translated_pages: return next_article except ValueError: return None return None
def test_that_only_translated_sections_show_with_tag_navigation(self): site_settings = SiteSettings.for_site(self.main.get_site()) site_settings.enable_tag_navigation = True site_settings.show_only_translated_pages = True site_settings.save() self.mk_section_translation( self.english_section, self.french, title=self.english_section.title + ' in french') article1 = self.mk_article( self.english_section, title='English article1 in English Section', featured_in_homepage_start_date=timezone.now(), featured_in_homepage=True) self.mk_article_translation( article1, self.french, title=article1.title + ' in french',) promote_articles() response = self.client.get('/') self.assertContains(response, 'English section') response = self.client.get('/locale/fr/') response = self.client.get('/') self.assertContains( response, '<a href="/sections-main-1/english-section-in-french/"' ' class="section-listing__theme-bg-link">' 'English section in french</a>')
def social_media_article(context, page=None): locale = context.get('locale_code') site_settings = SiteSettings.for_site(context['request'].site) viber = False twitter = False facebook = False whatsapp = False telegram = False if site_settings: facebook = site_settings.facebook_image twitter = site_settings.twitter_image whatsapp = site_settings.whatsapp_image viber = site_settings.viber_image telegram = site_settings.telegram_image data = { 'page': page, 'viber': viber, 'twitter': twitter, 'facebook': facebook, 'whatsapp': whatsapp, 'telegram': telegram, 'locale_code': locale, 'request': context['request'], } return data
def setUp(self): self.main = self.mk_main(title='main1', slug='main1', path='00010002', url_path='/main1/') self.client = Client(HTTP_HOST=self.main.get_site().hostname) self.form_index = FormsIndexPage.objects.child_of(self.main).first() self.section_index = SectionIndexPage.objects.child_of( self.main).first() self.site_settings = SiteSettings.for_site(self.main.get_site()) self.site_settings.enable_tag_navigation = True self.site_settings.save() self.banner_message = ("Share your opinions and stories, " + "take polls, win fun prizes.") self.english = SiteLanguageRelation.objects.create( language_setting=Languages.for_site(self.main.get_site()), locale='en', is_active=True) self.french = SiteLanguageRelation.objects.create( language_setting=Languages.for_site(Site.objects.first()), locale='fr', is_active=True) self.yourmind = self.mk_section(self.section_index, title='Your mind') self.yourmind_fr = self.mk_section_translation( self.yourmind, self.french, title='Your mind in french')
def get_pages(context, qs, locale): language = SiteLanguage.objects.filter(locale=locale).first() request = context['request'] site_settings = SiteSettings.for_site(request.site) if site_settings.show_only_translated_pages: if language and language.is_main_language: return [a for a in qs.live()] else: pages = [] for a in qs: translation = a.get_translation_for(locale) if translation: pages.append(translation) return pages else: if language and language.is_main_language: return [a for a in qs.live()] else: pages = [] for a in qs: translation = a.get_translation_for(locale) if translation: pages.append(translation) elif a.live: pages.append(a) return pages
def setUp(self): self.main = self.mk_main( title='main1', slug='main1', path='00010002', url_path='/main1/') self.client = Client(HTTP_HOST=self.main.get_site().hostname) self.site_settings = SiteSettings.for_site(self.main.get_site()) self.yourmind = self.mk_section( SectionIndexPage.objects.child_of(self.main).first(), title='Your mind') self.article = self.mk_article(self.yourmind, title='article') self.article.save_revision().publish() self.english = SiteLanguageRelation.objects.create( language_setting=Languages.for_site(self.main.get_site()), locale='en', is_active=True) self.french = SiteLanguageRelation.objects.create( language_setting=Languages.for_site(self.main.get_site()), locale='fr', is_active=True) self.fr_article = self.mk_article_translation( self.article, self.french) self.fr_yourmind = self.mk_section_translation( self.yourmind, self.french)
def process_response(self, request, response): if hasattr(settings, 'GOOGLE_ANALYTICS_IGNORE_PATH'): exclude = [ p for p in settings.GOOGLE_ANALYTICS_IGNORE_PATH if request.path.startswith(p) ] if any(exclude): return response # Only track 200 and 302 responses for request.site if not (response.status_code == 200 or response.status_code == 302): return response site_settings = SiteSettings.for_site(request.site) local_ga_account = site_settings.local_ga_tracking_code or \ settings.GOOGLE_ANALYTICS.get('google_analytics_id') if local_ga_account: response = self.submit_tracking(local_ga_account, request, response) if site_settings.global_ga_tracking_code: response = self.submit_tracking( site_settings.global_ga_tracking_code, request, response) return response
def load_child_articles_for_section( context, section, featured_in_section=None, count=5): """ Returns all child articles If the `locale_code` in the context is not the main language, it will return the translations of the live articles. """ request = context.get('request') locale = context.get('locale_code') main_language_page = section.get_main_language_page() settings = SiteSettings.for_site(request.site) \ if request else None # TODO: Consider caching the pks of these articles using a timestamp on # section as the key so tha twe don't always do these joins article_ordering = settings and settings.article_ordering_within_section order_by = ArticleOrderingChoices.\ get(settings.article_ordering_within_section).name.lower() \ if article_ordering \ and settings.article_ordering_within_section !=\ ArticleOrderingChoices.CMS_DEFAULT_SORTING\ else '-first_published_at' order_by = order_by if order_by.find('_desc') == -1 \ else '-{}'.format(order_by.replace('_desc', '')) child_articles = ArticlePage.objects.child_of( main_language_page).filter( language__is_main_language=True).order_by(order_by) if featured_in_section is not None: child_articles = child_articles.filter( featured_in_section=featured_in_section)\ .order_by('-featured_in_section_start_date') related_articles = ArticlePage.objects.filter( related_sections__section__slug=main_language_page.slug) qs = list(chain( get_pages(context, child_articles, locale), get_pages(context, related_articles, locale))) # Pagination if count: p = context.get('p', 1) paginator = Paginator(qs, count) try: articles = paginator.page(p) except PageNotAnInteger: articles = paginator.page(1) except EmptyPage: articles = paginator.page(paginator.num_pages) else: articles = qs if not locale: return articles context.update({'articles_paginated': articles}) return articles
def test_latest_rotation_on_draft_articles(self): site = Site.objects.get(is_default_site=True) site_settings = SiteSettings.for_site(site) site_settings.content_rotation_start_date = timezone.now() site_settings.content_rotation_end_date = timezone.now() + timedelta( days=1) time1 = str(timezone.now().time())[:8] time2 = str((timezone.now() + timedelta(minutes=1)).time())[:8] site_settings.time = dumps([{ 'type': 'time', 'value': time1 }, { 'type': 'time', 'value': time2 }]) site_settings.monday_rotation = True site_settings.save() article = self.mk_article(self.yourmind, title='article', slug='article') article.featured_in_latest_start_date = timezone.now() article.save() article2 = self.mk_article(self.yourmind, title='article2', slug='article2') article2.featured_in_latest_start_date = timezone.now() article2.save() article3 = self.mk_article(self.yourmind, title='article3', slug='article3') article3.save() promote_articles() article.refresh_from_db() article2.refresh_from_db() article3.refresh_from_db() self.assertTrue(article.live) self.assertTrue(article2.live) self.assertTrue(article3.live) article.unpublish() article.refresh_from_db() self.assertTrue(article.featured_in_latest) self.assertTrue(article2.featured_in_latest) self.assertFalse(article3.featured_in_latest) rotate_content(0) article.refresh_from_db() article2.refresh_from_db() article3.refresh_from_db() self.assertFalse(article.live) self.assertTrue(article2.live) self.assertTrue(article3.live)
def test_show_join_banner(self): template_settings = deepcopy(settings.TEMPLATES) template_settings[0]['DIRS'] = [ join(settings.PROJECT_ROOT, 'templates', 'springster') ] with self.settings(TEMPLATES=template_settings): molo_form_page = MoloFormPage( title='from title', slug='form-slug', homepage_introduction='Introduction to Test Form ...', thank_you_text='Thank you for taking the Test Form', allow_anonymous_submissions=False) self.form_index.add_child(instance=molo_form_page) molo_form_page2 = MoloFormPage( title='form title', slug='another-form-slug', homepage_introduction='Introduction to Test Form ...', thank_you_text='Thank you for taking the Test Form', allow_anonymous_submissions=True) self.form_index.add_child(instance=molo_form_page2) MoloFormField.objects.create(page=molo_form_page, sort_order=1, label='Your favourite animal', field_type='singleline', required=True) MoloFormField.objects.create(page=molo_form_page2, sort_order=1, label='Your birthday month', field_type='singleline', required=True) molo_form_page.save_revision().publish() molo_form_page2.save_revision().publish() setting = GemSettings.for_site(self.main.get_site()) self.assertFalse(setting.show_join_banner) response = self.client.get('%s?next=%s' % (reverse('molo.profiles:auth_logout'), reverse('molo.profiles:auth_login'))) response = self.client.get('/') self.assertNotContains(response, self.banner_message) setting.show_join_banner = True setting.save() self.assertTrue( GemSettings.for_site(self.main.get_site()).show_join_banner) self.assertTrue( SiteSettings.for_site( self.main.get_site()).enable_tag_navigation) response = self.client.get('/') self.assertContains(response, self.banner_message) # test that the join banner only shows up once soup = BeautifulSoup(response.content, 'html.parser') self.assertEqual(soup.get_text().count(self.banner_message), 1)
def test_latest_rotation_on(self): """This test that if the date range, weekdays and times are set for content rotation, that the content rotates accordingly""" # sets the site settings site = Site.objects.get(is_default_site=True) site_settings = SiteSettings.for_site(site) site_settings.content_rotation_start_date = timezone.now() site_settings.content_rotation_end_date = timezone.now() + timedelta( days=1) time1 = str(timezone.now().time())[:8] time2 = str((timezone.now() + timedelta(minutes=1)).time())[:8] site_settings.time = dumps([{ 'type': 'time', 'value': time1 }, { 'type': 'time', 'value': time2 }]) site_settings.monday_rotation = True site_settings.save() # creates articles and pages, some set to feature in latest, others not for i in range(5): self.footer = FooterPage(title='Footer Page %s', slug='footer-page-%s' % (i, )) self.footer_index.add_child(instance=self.footer) self.assertEqual(FooterPage.objects.live().count(), 5) self.assertEqual(self.main.latest_articles().count(), 0) self.mk_articles(self.yourmind_sub, count=10, featured_in_latest_start_date=timezone.now()) promote_articles() self.mk_articles(self.yourmind_sub, count=10, featured_in_latest=False) self.assertEqual(self.main.latest_articles().count(), 10) # gets the first and last articles of the list before it rotates first_article_old = self.main.latest_articles()[0].pk last_article_old = self.main.latest_articles()[9].pk rotate_content(day=0) # checks to see that the number of latest articles has not increased self.assertEqual(self.main.latest_articles().count(), 10) # checks to see the the old first articles is not still the first one self.assertNotEqual(first_article_old, self.main.latest_articles()[0].pk) # checks to see the old first article has moved up 2 places self.assertEqual(first_article_old, self.main.latest_articles()[2].pk) # checks to see the the old last article is not still last self.assertNotEqual(last_article_old, self.main.latest_articles()[8].pk)
def social_media_footer(context, page=None): locale = context.get('locale_code') social_media = SiteSettings.for_site(context['request'].site).\ social_media_links_on_footer_page data = { 'social_media': social_media, 'request': context['request'], 'locale_code': locale, 'page': page, } return data
def social_media_footer(context, page=None): request = context['request'] locale = context.get('locale_code') social_media = SiteSettings.for_site(request.site).\ social_media_links_on_footer_page data = { 'social_media': social_media, 'request': context['request'], 'locale_code': locale, 'page': page, } return data
def load_descendant_articles_for_section(context, section, featured_in_homepage=None, featured_in_section=None, featured_in_latest=None, count=5): """ Returns all descendant articles (filtered using the parameters) If the `locale_code` in the context is not the main language, it will return the translations of the live articles. """ request = context.get('request') locale = context.get('locale_code') page = section.get_main_language_page() settings = SiteSettings.for_site(request.site) \ if request else None qs = ArticlePage.objects.descendant_of(page).filter( language__is_main_language=True) article_ordering = getattr(settings, 'article_ordering_within_section', None) if article_ordering: order_by = ArticleOrderingChoices.\ get(settings.article_ordering_within_section).name.lower() order_by = order_by if order_by.find('_desc') == -1 \ else '-{}'.format(order_by.replace('_desc', '')) # if the sort order is equal to CMS_DEFAULT_SORTING # do not order QS, CMS handles it if article_ordering != ArticleOrderingChoices.CMS_DEFAULT_SORTING: qs = qs.order_by(order_by) if featured_in_homepage: qs = qs.filter(featured_in_homepage=featured_in_homepage)\ .order_by('-featured_in_homepage_start_date') if featured_in_latest: qs = qs.filter(featured_in_latest=featured_in_latest) if featured_in_section: qs = qs.filter(featured_in_section=featured_in_section)\ .order_by('-featured_in_section_start_date') if not locale: return qs.live()[:count] return get_pages(context, qs, locale)[:count]
def setUp(self): self.main = self.mk_main( title='main1', slug='main1', path='00010002', url_path='/main1/') self.site = self.main.get_site() self.client = Client(HTTP_HOST=self.main.get_site().hostname) self.site_settings = SiteSettings.for_site(self.site) self.user = User.objects.create_user( username='******', is_staff=True, email='*****@*****.**', password='******') self.user.profile.admin_sites.add(self.site) self.user.user_permissions.add(self.get_admin_perms())
def test_latest_rotation_on_draft_articles(self): site = Site.objects.get(is_default_site=True) site_settings = SiteSettings.for_site(site) site_settings.content_rotation_start_date = timezone.now() site_settings.content_rotation_end_date = timezone.now() + timedelta( days=1) time1 = str(timezone.now().time())[:8] time2 = str((timezone.now() + timedelta(minutes=1)).time())[:8] site_settings.time = dumps([{ 'type': 'time', 'value': time1}, {'type': 'time', 'value': time2}]) site_settings.monday_rotation = True site_settings.save() article = self.mk_article( self.yourmind, title='article', slug='article') article.featured_in_latest_start_date = timezone.now() article.save() article2 = self.mk_article( self.yourmind, title='article2', slug='article2') article2.featured_in_latest_start_date = timezone.now() article2.save() article3 = self.mk_article( self.yourmind, title='article3', slug='article3') article3.save() promote_articles() article.refresh_from_db() article2.refresh_from_db() article3.refresh_from_db() self.assertTrue(article.live) self.assertTrue(article2.live) self.assertTrue(article3.live) article.unpublish() article.refresh_from_db() self.assertTrue(article.featured_in_latest) self.assertTrue(article2.featured_in_latest) self.assertFalse(article3.featured_in_latest) rotate_content(0) article.refresh_from_db() article2.refresh_from_db() article3.refresh_from_db() self.assertFalse(article.live) self.assertTrue(article2.live) self.assertTrue(article3.live)
def test_latest_rotation_on(self): """This test that if the date range, weekdays and times are set for content rotation, that the content rotates accordingly""" # sets the site settings site = Site.objects.get(is_default_site=True) site_settings = SiteSettings.for_site(site) site_settings.content_rotation_start_date = timezone.now() site_settings.content_rotation_end_date = timezone.now() + timedelta( days=1) time1 = str(timezone.now().time())[:8] time2 = str((timezone.now() + timedelta(minutes=1)).time())[:8] site_settings.time = dumps([{ 'type': 'time', 'value': time1}, {'type': 'time', 'value': time2}]) site_settings.monday_rotation = True site_settings.save() # creates articles and pages, some set to feature in latest, others not for i in range(5): self.footer = FooterPage( title='Footer Page %s', slug='footer-page-%s' % (i, )) self.footer_index.add_child(instance=self.footer) self.assertEqual(FooterPage.objects.live().count(), 5) self.assertEqual(self.main.latest_articles().count(), 0) self.mk_articles( self.yourmind_sub, count=10, featured_in_latest_start_date=timezone.now()) promote_articles() self.mk_articles(self.yourmind_sub, count=10, featured_in_latest=False) self.assertEqual(self.main.latest_articles().count(), 10) # gets the first and last articles of the list before it rotates first_article_old = self.main.latest_articles()[0].pk last_article_old = self.main.latest_articles()[9].pk rotate_content(day=0) # checks to see that the number of latest articles has not increased self.assertEqual(self.main.latest_articles().count(), 10) # checks to see the the old first articles is not still the first one self.assertNotEqual( first_article_old, self.main.latest_articles()[0].pk) # checks to see the old first article has moved up 2 places self.assertEqual( first_article_old, self.main.latest_articles()[2].pk) # checks to see the the old last article is not still last self.assertNotEqual( last_article_old, self.main.latest_articles()[8].pk)
def setUp(self): self.main = self.mk_main( title='main1', slug='main1', path='00010002', url_path='/main1/') self.client = Client(HTTP_HOST=self.main.get_site().hostname) self.site_settings = SiteSettings.for_site(self.main.get_site()) self.site_settings.local_ga_tracking_code = 'local_ga_tracking_code' self.site_settings.save() GemSettings.objects.create( site_id=self.main.get_site().id, bbm_ga_account_subdomain='bbm', bbm_ga_tracking_code='bbm_tracking_code', ) self.yourmind = self.mk_section( SectionIndexPage.objects.child_of(self.main).first(), title='Your mind') self.article = self.mk_article( self.yourmind, title='article', subtitle='article with nav_tags', slug='article') self.article.save_revision().publish() self.article2 = self.mk_article( self.yourmind, title='article2', subtitle='artitle without nav_tags', slug='article2') self.article2.save_revision().publish() self.tag_index = TagIndexPage.objects.child_of(self.main).first() self.tag = Tag(title='Tag1') self.tag2 = Tag(title='Tag2') self.tag_index.add_child(instance=self.tag) self.tag.save_revision().publish() self.tag_index.add_child(instance=self.tag2) self.tag2.save_revision().publish() self.article.nav_tags.create(tag=self.tag) self.article.save_revision().publish() self.article.nav_tags.create(tag=self.tag2) self.article.save_revision().publish() # get footerpage self.footer_index = FooterIndexPage.objects.child_of(self.main).first() self.footer = FooterPage(title='Test Footer Page') self.footer_index.add_child(instance=self.footer) self.footer.save_revision().publish() self.response = self.client.get('/')
def load_descendant_articles_for_section( context, section, featured_in_homepage=None, featured_in_section=None, featured_in_latest=None, count=5): """ Returns all descendant articles (filtered using the parameters) If the `locale_code` in the context is not the main language, it will return the translations of the live articles. """ request = context.get('request') locale = context.get('locale_code') page = section.get_main_language_page() settings = SiteSettings.for_site(request.site) \ if request else None qs = ArticlePage.objects.descendant_of(page).filter( language__is_main_language=True) article_ordering = settings \ and settings.article_ordering_within_section cms_ordering = article_ordering \ and settings.article_ordering_within_section !=\ ArticleOrderingChoices.CMS_DEFAULT_SORTING if article_ordering and cms_ordering: order_by = ArticleOrderingChoices.\ get(settings.article_ordering_within_section).name.lower() order_by = order_by if order_by.find('_desc') == -1 \ else '-{}'.format(order_by.replace('_desc', '')) qs = qs.order_by(order_by) if featured_in_homepage is not None: qs = qs.filter(featured_in_homepage=featured_in_homepage)\ .order_by('-featured_in_homepage_start_date') if featured_in_latest is not None: qs = qs.filter(featured_in_latest=featured_in_latest) if featured_in_section is not None: qs = qs.filter(featured_in_section=featured_in_section)\ .order_by('-featured_in_section_start_date') if not locale: return qs.live()[:count] return get_pages(context, qs, locale)[:count]
def rotate_content(day=None): """ this method gets the parameters that are needed for rotate_latest and rotate_featured_in_homepage methods, and calls them both""" # getting the content rotation settings from site settings for main in Main.objects.all(): site = main.sites_rooted_here.all().first() main_lang = Languages.for_site(site).languages.filter( is_main_language=True).first() index = SectionIndexPage.objects.live().child_of(main).first() site_settings = SiteSettings.for_site(site) if day is None: day = timezone.now().weekday() # calls the two rotate methods with the necessary params if main and index: rotate_latest(main_lang, index, main, site_settings, day) rotate_featured_in_homepage(main_lang, day, main)
def get_next_article(context, article): locale_code = context.get('locale_code') section = article.get_parent_section() articles = load_child_articles_for_section(context, section, count=None) if len(articles) > 1: if len(articles) > articles.index(article) + 1: next_article = articles[articles.index(article) + 1] else: next_article = articles[0] else: return None try: return next_article.translated_pages.get(language__locale=locale_code) except: if next_article.language.locale == locale_code or not \ SiteSettings.for_site( context['request'].site).show_only_translated_pages: return next_article return None
def social_media_article(context, page=None): request = context['request'] locale = context.get('locale_code') site_settings = SiteSettings.for_site(request.site) if site_settings.facebook_sharing: facebook = site_settings.facebook_image else: facebook = False if site_settings.twitter_sharing: twitter = site_settings.twitter_image else: twitter = False if site_settings.whatsapp_sharing: whatsapp = site_settings.whatsapp_image else: whatsapp = False if site_settings.viber_sharing: viber = site_settings.viber_image else: viber = False if site_settings.telegram_sharing: telegram = site_settings.telegram_image else: telegram = False data = { 'facebook': facebook, 'twitter': twitter, 'whatsapp': whatsapp, 'viber': viber, 'telegram': telegram, 'request': context['request'], 'locale_code': locale, 'page': page, } return data
def get_queryset(self, *args, **kwargs): site_settings = SiteSettings.for_site(self.request.site) main = self.request.site.root_page tag = self.kwargs["tag_name"] if site_settings.enable_tag_navigation: count = self.request.GET.get("count") context = {'request': self.request} locale = self.request.LANGUAGE_CODE tag = Tag.objects.filter(slug=tag).descendant_of(main) if tag.exists(): tag = tag.first() articles = [] for article_tag in ArticlePageTags.objects.filter( tag=tag.get_main_language_page()).all(): articles.append(article_tag.page.pk) articles = ArticlePage.objects.filter( pk__in=articles).descendant_of(main).order_by( '-first_published_at') return get_pages(context, articles[:count], locale) raise Http404 return ArticlePage.objects.descendant_of(main).filter( tags__name__in=[tag]).order_by('-first_published_at')
def test_site_exists_if_no_iems_translated_for_translated_only(self): site_settings = SiteSettings.for_site(self.main.get_site()) site_settings.enable_tag_navigation = True site_settings.show_only_translated_pages = True site_settings.save() tag = self.mk_tag(parent=self.tag_index) tag.feature_in_homepage = True tag.save_revision().publish() articles = self.mk_articles( parent=self.english_section, featured_in_latest_start_date=timezone.now(), featured_in_homepage_start_date=timezone.now(), count=30) for article in articles: ArticlePageTags.objects.create(page=article, tag=tag) promote_articles() response = self.client.get('/') self.assertEqual(response.status_code, 200) response = self.client.get('/locale/fr/') response = self.client.get('/') self.assertEqual(response.status_code, 200)
def get_queryset(self, *args, **kwargs): site_settings = SiteSettings.for_site(self.request.site) main = self.request.site.root_page tag = self.kwargs["tag_name"] if site_settings.enable_tag_navigation: count = self.request.GET.get("count") context = {'request': self.request} locale = self.request.LANGUAGE_CODE tag = Tag.objects.filter(slug=tag).descendant_of(main) if tag.exists(): tag = tag.first() articles = [] for article_tag in ArticlePageTags.objects.filter( tag=tag.get_main_language_page()).all(): articles.append(article_tag.page.pk) articles = ArticlePage.objects.filter( pk__in=articles).descendant_of(main).order_by( '-first_published_at') return get_pages(context, articles[:count], locale) raise Http404 return ArticlePage.objects.descendant_of(main).filter( tags__name__in=[tag]).order_by( '-first_published_at')
def process_response(self, request, response): if hasattr(settings, 'GOOGLE_ANALYTICS_IGNORE_PATH'): exclude = [p for p in settings.GOOGLE_ANALYTICS_IGNORE_PATH if request.path.startswith(p)] if any(exclude): return response # Only track 200 and 302 responses for request.site if not (response.status_code == 200 or response.status_code == 302): return response site_settings = SiteSettings.for_site(request.site) local_ga_account = site_settings.local_ga_tracking_code or \ settings.GOOGLE_ANALYTICS.get('google_analytics_id') if local_ga_account: response = self.submit_tracking( local_ga_account, request, response) if site_settings.global_ga_tracking_code: response = self.submit_tracking( site_settings.global_ga_tracking_code, request, response) return response
def load_child_articles_for_section(context, section, featured_in_section=None, count=5): """ Returns all child articles If the `locale_code` in the context is not the main language, it will return the translations of the live articles. """ if not section: return None request = context.get('request') locale = context.get('locale_code') main_language_page = section.specific.get_main_language_page() settings = SiteSettings.for_site(request.site) \ if request else None # TODO: Consider caching the pks of these articles using a timestamp on # section as the key so tha twe don't always do these joins article_ordering = getattr(settings, 'article_ordering_within_section', None) order_by = ArticleOrderingChoices.\ get(settings.article_ordering_within_section).name.lower() \ if article_ordering else '-first_published_at' order_by = order_by if order_by.find('_desc') == -1 \ else '-{}'.format(order_by.replace('_desc', '')) child_articles = ArticlePage.objects.child_of(main_language_page).filter( language__is_main_language=True) # if the sort order is equal to CMS_DEFAULT_SORTING # do not order QS, CMS handles it if article_ordering != ArticleOrderingChoices.CMS_DEFAULT_SORTING: child_articles = child_articles.order_by(order_by) if featured_in_section is not None: child_articles = child_articles.filter( featured_in_section=featured_in_section)\ .order_by('-featured_in_section_start_date') related_articles = ArticlePage.objects.filter( related_sections__section__slug=main_language_page.slug) qs = list( chain(get_pages(context, child_articles, locale), get_pages(context, related_articles, locale))) # Pagination if count: p = context.get('p', 1) paginator = Paginator(qs, count) try: articles = paginator.page(p) except PageNotAnInteger: articles = paginator.page(1) except EmptyPage: articles = paginator.page(paginator.num_pages) else: articles = qs if not locale: return articles context.update({'articles_paginated': articles}) return articles