X-Git-Url: https://git.mdrn.pl/redakcja.git/blobdiff_plain/e977f7187b10b1bc0a30794cd585c6b840568996..fbc10cff4c1ea91b0a42d2435755d538cce4a241:/src/documents/forms.py?ds=sidebyside diff --git a/src/documents/forms.py b/src/documents/forms.py index f5f2901d..4c970339 100644 --- a/src/documents/forms.py +++ b/src/documents/forms.py @@ -3,11 +3,12 @@ # from django.db.models import Count from django import forms -from django.utils.translation import ugettext_lazy as _ +from django.utils.translation import gettext_lazy as _ from django.conf import settings - +from slugify import slugify from .constants import MASTERS from .models import Book, Chunk, Image, User +from .docx import xml_from_docx class DocumentCreateForm(forms.ModelForm): """ @@ -15,16 +16,21 @@ class DocumentCreateForm(forms.ModelForm): """ file = forms.FileField(required=False) text = forms.CharField(required=False, widget=forms.Textarea) + docx = forms.FileField(required=False) class Meta: model = Book - exclude = ['parent', 'parent_number', 'project'] + fields = [ + 'title', 'slug', 'public', 'gallery' + ] def __init__(self, *args, **kwargs): super(DocumentCreateForm, self).__init__(*args, **kwargs) self.fields['slug'].widget.attrs={'class': 'autoslug'} + self.fields['slug'].required = False self.fields['gallery'].widget.attrs={'class': 'autoslug'} self.fields['title'].widget.attrs={'class': 'autoslug-source'} + self.fields['title'].required = False def clean(self): super(DocumentCreateForm, self).clean() @@ -36,8 +42,27 @@ class DocumentCreateForm(forms.ModelForm): except UnicodeDecodeError: raise forms.ValidationError(_("Text file must be UTF-8 encoded.")) + docx = self.cleaned_data['docx'] + if docx is not None: + try: + text, meta = xml_from_docx(docx) + except Exception as e: + raise forms.ValidationError(e) + else: + self.cleaned_data['text'] = text + if not self.cleaned_data['title']: + self.cleaned_data['title'] = meta.get('title', '') + if not self.cleaned_data['slug']: + self.cleaned_data['slug'] = slugify(meta.get('title', '')) + + if not self.cleaned_data["title"]: + self._errors["title"] = self.error_class([_("Title not set")]) + + if not self.cleaned_data["slug"]: + self._errors["slug"] = self.error_class([_("Slug not set")]) + if not self.cleaned_data["text"]: - self._errors["file"] = self.error_class([_("You must either enter text or upload a file")]) + self._errors["text"] = self.error_class([_("You must either enter text or upload a file")]) return self.cleaned_data @@ -128,7 +153,7 @@ class BookForm(forms.ModelForm): class Meta: model = Book - exclude = ['project'] + exclude = ['project', 'cover', 'legimi_id', 'woblink_id'] def __init__(self, *args, **kwargs): ret = super(BookForm, self).__init__(*args, **kwargs) @@ -228,4 +253,5 @@ class MarkFinalForm(forms.Form): class PublishOptionsForm(forms.Form): days = forms.IntegerField(label=u'po ilu dniach udostępnienić (0 = od razu)', min_value=0, initial=0) + hidden = forms.BooleanField(label='tekst ukryty przed wyszukiwaniem', required=False, initial=False) beta = forms.BooleanField(label=u'Opublikuj na wersji testowej', required=False)