Anfänglicher Commit: Producion Version Stand: Oktober 2014
This commit is contained in:
58
src/content/forms.py
Normal file
58
src/content/forms.py
Normal file
@@ -0,0 +1,58 @@
|
||||
'''
|
||||
Created on 04.10.2011
|
||||
|
||||
@author: christian
|
||||
'''
|
||||
import django.forms
|
||||
from django.template.defaultfilters import slugify
|
||||
from django.utils.translation import ugettext as _
|
||||
|
||||
from utils.html5 import forms
|
||||
|
||||
from . import models
|
||||
|
||||
|
||||
class ArticleForm(forms.ModelForm):
|
||||
error_css_class = 'error'
|
||||
required_css_class = 'required'
|
||||
|
||||
class Meta(object):
|
||||
fields = ('headline_de', 'content_de', 'headline_en', 'content_en',
|
||||
'category', 'image')
|
||||
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
|
||||
|
||||
|
||||
class PageForm(forms.ModelForm):
|
||||
error_css_class = 'error'
|
||||
required_css_class = 'required'
|
||||
content_type = django.forms.ChoiceField(
|
||||
choices=models.CONTENT_CHOICES,
|
||||
widget=django.forms.RadioSelect
|
||||
)
|
||||
|
||||
class Meta(object):
|
||||
exclude = ('position',)
|
||||
model = models.Page
|
||||
|
||||
def clean(self):
|
||||
cleaned_data = super(PageForm, self).clean()
|
||||
content_type = cleaned_data.get("content_type")
|
||||
pdf_de = cleaned_data.get("pdf_de")
|
||||
if not pdf_de and content_type == "2":
|
||||
msg = _('Please upload a PDF-File to this PDF-Page.')
|
||||
self._errors["content_type"] = self.error_class([msg])
|
||||
self._errors["pdf_de"] = self.error_class([msg])
|
||||
# These fields are no longer valid. Remove them from the
|
||||
# cleaned data.
|
||||
del cleaned_data["content_type"]
|
||||
del cleaned_data["pdf_de"]
|
||||
|
||||
# Always return the full collection of cleaned data.
|
||||
return cleaned_data
|
||||
Reference in New Issue
Block a user