Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def save_article(self, data):
slug = data['url'].rsplit('/',2)[1]
try:
Article.objects.filter(slug=slug).delete()
except:
pass
try:
author = Person.objects.get(full_name=data['author'])
except:
author = Person.objects.create(full_name=data['author'])
article = Article()
title = str(BeautifulSoup(data['title'], 'html.parser'))
article.headline = title
article.slug = slug
article.snippet = data['description']
article.seo_keyword = data['keyword']
def _find_article(slug, section=None):
if section is not None:
return Article.objects.get(slug=slug, section__name=section, head=True)
else:
return Article.objects.get(slug=slug, head=True)
def recent_articles(person, count=5):
time = datetime.datetime.now() - datetime.timedelta(days=7)
actions = Action.objects.filter(person=person, object_type='article', timestamp__gt=time).order_by('-timestamp')
articles = []
for action in actions:
try:
article = Article.objects.get(parent_id=action.object_id, head=True)
if article not in articles:
articles.append(article)
except:
pass
return articles
def topic(self, request, pk=None):
try:
topic = Topic.objects.get(id=pk)
except Topic.DoesNotExist:
raise Http404("Topic does not exist.")
articles = Article.objects.filter(topic=topic, is_published=True).order_by('-published_at')
context = {
'meta': {
'title': topic.name
},
'section': topic,
'type': 'topic',
'articles': {
'first': articles[0] if articles else None,
'rest': articles[1:9]
}
}
return render(request, 'section.html', context)
def save(self, count=None):
Article.objects.filter(section__slug=self.articles[0]['post_type']).delete()
n = 0
for article in self.articles:
self.save_article(article)
n += 1
if count is not None and n >= count:
return
def _find_article(slug, section=None):
if section is not None:
return Article.objects.get(slug=slug, section__name=section, head=True)
else:
return Article.objects.get(slug=slug, head=True)
def article_delete(request, id):
article = Article.objects.get(pk=id)
section_slug = article.section.slug
article.is_active = False
article.save(update_fields=['is_active'])
return redirect(section, section_slug)
def home(self, request):
frontpage = Article.objects.get_frontpage()
frontpage_ids = [int(a.id) for a in frontpage[:2]]
sections = Article.objects.get_sections(exclude=('blog',),frontpage=frontpage_ids)
articles = {
'primary': frontpage[0],
'secondary': frontpage[1],
'thumbs': frontpage[2:4],
'bullets': frontpage[4:6],
}
#page = Homepage()
popular = Article.objects.get_most_popular(5)
def home(self, request):
frontpage = Article.objects.get_frontpage(sections=('news', 'culture', 'opinion', 'sports', 'features', 'science'))
frontpage_ids = [int(a.id) for a in frontpage[:2]]
sections = Article.objects.get_sections(exclude=('blog',),frontpage=frontpage_ids)
try:
articles = {
'primary': frontpage[0],
'secondary': frontpage[1],
'thumbs': frontpage[2:4],
'bullets': frontpage[4:6],
}
except IndexError:
raise Exception("Not enough articles to populate the frontpage!")
component_set = Homepage()
def home(self, request):
frontpage = Article.objects.get_frontpage()
frontpage_ids = [int(a.id) for a in frontpage[:2]]
sections = Article.objects.get_sections(exclude=('blog',),frontpage=frontpage_ids)
articles = {
'primary': frontpage[0],
'secondary': frontpage[1],
'thumbs': frontpage[2:4],
'bullets': frontpage[4:6],
}
#page = Homepage()
popular = Article.objects.get_most_popular(5)
context = {
'articles': articles,
'sections': sections,
'popular': popular,