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']
article.seo_description = data['description']
date = dateutil.parser.parse(data['date'])
article.status = Article.DRAFT
article.is_published = True
article.published_at = date
try:
def save_featured_image(self, revision=True):
if self.instance.featured_image:
self.featured_image_form.data = self.data
if revision:
self.instance.featured_image.pk = None
self.featured_image_form.instance = self.instance.featured_image
else:
self.featured_image_form = FeaturedImageForm(self.data)
if self.featured_image_form.is_valid():
saved = self.featured_image_form.save()
saved.article = self.instance
saved.save()
self.instance.featured_image = saved
ImageAttachmentFormSet = inlineformset_factory(Article, ImageAttachment, form=ImageAttachmentForm, formset=BaseImageAttachmentFormSet, extra=0)
def section(request, section):
section = Section.objects.get(name=section)
article_list = Article.objects.filter(section=section,is_active=True,head=True).order_by('-created_at')
q = request.GET.get('q', '')
if q:
article_list = article_list.filter(headline__icontains=q)
unpublished = article_list.exclude(status=Article.PUBLISHED).count()
paginator = Paginator(article_list, 15) # Show 15 articles per page
page = request.GET.get('page')
try:
articles = paginator.page(page)
except PageNotAnInteger:
# If page is not an integer, deliver first page.
articles = paginator.page(1)
except EmptyPage:
# If page is out of range (e.g. 9999), deliver last page of results.
articles = paginator.page(paginator.num_pages)
return render_to_response(
"manager/article/list.html",
def create(self, validated_data):
# Create new Article instance!
instance = Article()
# Then save as usual
return self.update(instance, validated_data, action='create')
def update(self, instance, validated_data, action='update'):
status = validated_data.get('status', instance.status)
if status != instance.status and status == Article.PUBLISHED:
action = 'publish'
# Update all the basic fields
instance.headline = validated_data.get('headline', instance.headline)
instance.section_id = validated_data.get('section_id', instance.section_id)
instance.slug = validated_data.get('slug', instance.slug)
instance.snippet = validated_data.get('snippet', instance.snippet)
instance.template = validated_data.get('template', instance.template)
instance.status = status
instance.reading_time = validated_data.get('reading_time', instance.reading_time)
instance.importance = validated_data.get('importance', instance.importance)
instance.seo_keyword = validated_data.get('seo_keyword', instance.seo_keyword)
instance.seo_description = validated_data.get('seo_description', instance.seo_description)
# Save instance before processing/saving content in order to save associations to correct ID
instance.save()