示例#1
0
    def article(self, request, section=None, slug=None):
        try:
            article = ArticleHelper.get_article(request, slug)
        except:
            raise Http404('Article could not be found.')

        article.add_view()

        breaking = ArticleHelper.get_breaking_news().exclude(id=article.id).first()

        # determine if user is viewing from mobile
        article_type = 'desktop'
        user_agent = get_user_agent(request)
        if user_agent.is_mobile:
            article_type = 'mobile'


        if article.template == 'timeline':
            timeline_tag = article.tags.filter(name__icontains='timeline-')
            timelineArticles = Article.objects.filter(tags__in=timeline_tag, is_published=True)
            temp = list(timelineArticles.values('parent_id', 'template_data', 'slug', 'headline', 'featured_image'))
            try:
                temp = sorted(temp, key=lambda article: json.loads(article['template_data'])['timeline_date'])
            except:
                pass
            for i, a in enumerate(timelineArticles) :
                try:
                    temp[i]['featured_image'] = a.featured_image.image.get_thumbnail_url()
                except:
                    temp[i]['featured_image'] = None
            article.timeline_articles = json.dumps(temp)
            article.timeline_title = list(timeline_tag)[0].name.replace('timeline-', '').replace('-', ' ')


        ref = request.GET.get('ref', None)
        dur = request.GET.get('dur', None)

        if not ArticleHelper.is_explicit(article):
            article.content = ArticleHelper.insert_ads(article.content, article_type)

        popular = ArticleHelper.get_popular()[:5]

        context = {
            'title': '%s - %s' % (article.headline, self.SITE_TITLE),
            'meta': ArticleHelper.get_meta(article),
            'article': article,
            'reading_list': ArticleHelper.get_reading_list(article, ref=ref, dur=dur),
            # 'suggested': lambda: ArticleHelper.get_random_articles(2, section, exclude=article.id),
            'base_template': 'base.html',
            'popular': popular,
            'reading_time': ArticleHelper.get_reading_time(article),
            'explicit': ArticleHelper.is_explicit(article),
            'breaking': breaking,
        }

        template = article.get_template_path()
        t = loader.select_template(['%s/%s' % (article.section.slug, template), template, 'article/default.html'])
        return HttpResponse(t.render(context))
示例#2
0
    def article(self, request, slug=None):
        """Magazine article page view."""

        try:
            article = ArticleHelper.get_article(request, slug)
        except:
            raise Http404('Article could not be found.')

        article.add_view()
        year = article.tags.get(name__icontains="20").name

        magazine_title = self.mag_titles[year]

        subsection = article.subsection.name.lower() if article.subsection else ""

        # determine if user is viewing from mobile
        article_type = 'desktop'
        user_agent = get_user_agent(request)
        if user_agent.is_mobile:
            article_type = 'mobile'

        if not ArticleHelper.is_explicit(article):
            article.content = ArticleHelper.insert_ads(article.content, article_type)

        context = {
            'title': '%s - %s' % (article.headline, self.SITE_TITLE),
            'meta': ArticleHelper.get_meta(article, default_image=static('images/magazine/cover-social.png')),
            'article': article,
            'subsection': subsection,
            'specific_css': 'css/magazine-' + year + '.css',
            'suggested': ArticleHelper.get_random_articles(2, 'magazine', exclude=article.id),
            'base_template': 'magazine/base.html',
            'magazine_title': magazine_title
        }

        t = loader.select_template(['%s/%s' % (article.section.slug, article.get_template_path()), article.get_template_path()])

        return HttpResponse(t.render(context))