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))
def home(self, request): frontpage = ArticleHelper.get_frontpage( sections=('news', 'culture', 'opinion', 'sports', 'features', 'science'), max_days=7 ) trending_article = ArticleHelper.get_trending() elections = ArticleHelper.get_topic('AMS Elections').order_by('-published_at') frontpage_ids = [int(a.id) for a in frontpage[:2]] sections = ArticleHelper.get_frontpage_sections(exclude=frontpage_ids) breaking = ArticleHelper.get_breaking_news().first() # determine if user is viewing from mobile user_agent = get_user_agent(request) try: articles = { 'primary': frontpage[0], 'secondary': frontpage[1], 'thumbs': frontpage[2:4], 'bullets': frontpage[4:6], # Get random trending article 'trending': trending_article, 'breaking': breaking } except IndexError: raise Exception('Not enough articles to populate the frontpage!') popular = ArticleHelper.get_popular()[:5] blog = ArticleHelper.get_frontpage(section='blog', limit=5) title = '%s - UBC\'s official student newspaper' % self.SITE_TITLE context = { 'title': title, 'meta': { 'title': title, 'description': 'Weekly student newspaper of the University of British Columbia.', 'url': self.SITE_URL }, 'title': '%s - UBC\'s official student newspaper' % self.SITE_TITLE, 'articles': articles, 'sections': sections, 'popular': popular, 'breaking': breaking, 'blog': blog, 'day_of_week': datetime.now().weekday(), 'is_mobile': user_agent.is_mobile } return render(request, 'homepage/base.html', context)