"""
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 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])
            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
