Another Step in the Quest to clean up the code base.
This commit is contained in:
@@ -11,10 +11,12 @@ from . import models
|
||||
|
||||
|
||||
class ArticleForm(forms.ModelForm):
|
||||
""" Django form for adding and editing news articles. """
|
||||
error_css_class = 'error'
|
||||
required_css_class = 'required'
|
||||
|
||||
class Meta(object):
|
||||
"""Only the content should be visible, hide the meta data."""
|
||||
fields = (
|
||||
'headline_de', 'content_de',
|
||||
'headline_en', 'content_en',
|
||||
@@ -23,15 +25,18 @@ class ArticleForm(forms.ModelForm):
|
||||
)
|
||||
model = models.Article
|
||||
|
||||
def save(self, force_insert=False, force_update=False, commit=True):
|
||||
article = super(ArticleForm, self).save(commit=False)
|
||||
article.slug = slugify(article.headline_de)[:50]
|
||||
if commit:
|
||||
article.save(force_insert=force_insert, force_update=force_update)
|
||||
return article
|
||||
def save(self, commit=True):
|
||||
""" slugify the german headline and set is as slug before saving.
|
||||
|
||||
:param commit: Save this form's self.instance object if True
|
||||
:return: self.instance object."""
|
||||
self.instance.slug = slugify(self.instance.headline_de)[:50]
|
||||
return super(ArticleForm, self).save(commit=commit)
|
||||
|
||||
|
||||
class PageForm(forms.ModelForm):
|
||||
""" Django form for adding and editing static pages."""
|
||||
|
||||
error_css_class = 'error'
|
||||
required_css_class = 'required'
|
||||
content_type = forms.ChoiceField(
|
||||
@@ -40,13 +45,14 @@ class PageForm(forms.ModelForm):
|
||||
)
|
||||
|
||||
class Meta(object):
|
||||
"""Only the content should be visible, hide the meta data."""
|
||||
exclude = ('position',)
|
||||
model = models.Page
|
||||
|
||||
def clean(self):
|
||||
"""Check if a file has been uploaded if the page is marked as PDF."""
|
||||
cleaned_data = super(PageForm, self).clean()
|
||||
content_type = cleaned_data.get("content_type")
|
||||
cleaned_data.get("pdf_de")
|
||||
if content_type == "2" and not cleaned_data.get("pdf_de"):
|
||||
msg = _('Please upload a PDF-File to this PDF-Page.')
|
||||
self._errors["content_type"] = self.error_class([msg])
|
||||
|
||||
Reference in New Issue
Block a user