1 # This file is part of FNP-Redakcja, licensed under GNU Affero GPLv3 or later.
2 # Copyright © Fundacja Nowoczesna Polska. See NOTICE for more information.
4 from django.db.models import Count
5 from django import forms
6 from django.utils.translation import gettext_lazy as _
7 from django.conf import settings
8 from slugify import slugify
9 from .constants import MASTERS
10 from .models import Book, Chunk, Image, User
11 from .docx import xml_from_docx
13 class DocumentCreateForm(forms.ModelForm):
15 Form used for creating new documents.
17 file = forms.FileField(required=False)
18 text = forms.CharField(required=False, widget=forms.Textarea)
19 docx = forms.FileField(required=False)
24 'title', 'slug', 'public', 'gallery'
27 def __init__(self, *args, **kwargs):
28 super(DocumentCreateForm, self).__init__(*args, **kwargs)
29 self.fields['slug'].widget.attrs={'class': 'autoslug'}
30 self.fields['slug'].required = False
31 self.fields['gallery'].widget.attrs={'class': 'autoslug'}
32 self.fields['title'].widget.attrs={'class': 'autoslug-source'}
33 self.fields['title'].required = False
36 super(DocumentCreateForm, self).clean()
37 file = self.cleaned_data['file']
41 self.cleaned_data['text'] = file.read().decode('utf-8')
42 except UnicodeDecodeError:
43 raise forms.ValidationError(_("Text file must be UTF-8 encoded."))
45 docx = self.cleaned_data['docx']
48 text, meta = xml_from_docx(docx)
49 except Exception as e:
50 raise forms.ValidationError(e)
52 self.cleaned_data['text'] = text
53 if not self.cleaned_data['title']:
54 self.cleaned_data['title'] = meta.get('title', '')
55 if not self.cleaned_data['slug']:
56 self.cleaned_data['slug'] = slugify(meta.get('title', ''))
58 if not self.cleaned_data["title"]:
59 self._errors["title"] = self.error_class([_("Title not set")])
61 if not self.cleaned_data["slug"]:
62 self._errors["slug"] = self.error_class([_("Slug not set")])
64 if not self.cleaned_data["text"]:
65 self._errors["text"] = self.error_class([_("You must either enter text or upload a file")])
67 return self.cleaned_data
70 class DocumentsUploadForm(forms.Form):
72 Form used for uploading new documents.
74 file = forms.FileField(required=True, label=_('ZIP file'))
75 dirs = forms.BooleanField(label=_('Directories are documents in chunks'),
76 widget = forms.CheckboxInput(attrs={'disabled':'disabled'}))
79 file = self.cleaned_data['file']
83 z = self.cleaned_data['zip'] = zipfile.ZipFile(file)
84 except zipfile.BadZipfile:
85 raise forms.ValidationError("Should be a ZIP file.")
87 raise forms.ValidationError("ZIP file corrupt.")
89 return self.cleaned_data
92 class ChunkForm(forms.ModelForm):
94 Form used for editing a chunk.
96 user = forms.ModelChoiceField(queryset=
97 User.objects.annotate(count=Count('chunk')).
98 order_by('last_name', 'first_name'), required=False,
99 label=_('Assigned to'))
103 fields = ['title', 'slug', 'gallery_start', 'user', 'stage']
106 def __init__(self, *args, **kwargs):
107 super(ChunkForm, self).__init__(*args, **kwargs)
108 self.fields['gallery_start'].widget.attrs={'class': 'number-input'}
109 self.fields['slug'].widget.attrs={'class': 'autoslug'}
110 self.fields['title'].widget.attrs={'class': 'autoslug-source'}
112 def clean_slug(self):
113 slug = self.cleaned_data['slug']
115 chunk = Chunk.objects.get(book=self.instance.book, slug=slug)
116 except Chunk.DoesNotExist:
118 if chunk == self.instance:
120 raise forms.ValidationError(_('Chunk with this slug already exists'))
123 class ChunkAddForm(ChunkForm):
125 Form used for adding a chunk to a document.
128 def clean_slug(self):
129 slug = self.cleaned_data['slug']
131 user = Chunk.objects.get(book=self.instance.book, slug=slug)
132 except Chunk.DoesNotExist:
134 raise forms.ValidationError(_('Chunk with this slug already exists'))
137 class BookAppendForm(forms.Form):
139 Form for appending a book to another book.
140 It means moving all chunks from book A to book B and deleting A.
142 append_to = forms.ModelChoiceField(queryset=Book.objects.all(),
143 label=_("Append to"))
145 def __init__(self, book, *args, **kwargs):
146 ret = super(BookAppendForm, self).__init__(*args, **kwargs)
147 self.fields['append_to'].queryset = Book.objects.exclude(pk=book.pk)
151 class BookForm(forms.ModelForm):
152 """Form used for editing a Book."""
156 exclude = ['project', 'cover']
158 def __init__(self, *args, **kwargs):
159 ret = super(BookForm, self).__init__(*args, **kwargs)
160 self.fields['slug'].widget.attrs.update({"class": "autoslug"})
161 self.fields['title'].widget.attrs.update({"class": "autoslug-source"})
164 def save(self, **kwargs):
165 orig_instance = Book.objects.get(pk=self.instance.pk)
166 old_gallery = orig_instance.gallery
167 new_gallery = self.cleaned_data['gallery']
168 if new_gallery and old_gallery and new_gallery != old_gallery:
171 from django.conf import settings
172 shutil.move(orig_instance.gallery_path(),
173 os.path.join(settings.MEDIA_ROOT, settings.IMAGE_DIR, new_gallery))
174 super(BookForm, self).save(**kwargs)
177 class ReadonlyBookForm(BookForm):
178 """Form used for not editing a Book."""
180 def __init__(self, *args, **kwargs):
181 ret = super(ReadonlyBookForm, self).__init__(*args, **kwargs)
182 for field in self.fields.values():
183 field.widget.attrs.update({"disabled": "disabled"})
187 class ChooseMasterForm(forms.Form):
189 Form used for fixing the chunks in a book.
192 master = forms.ChoiceField(choices=((m, m) for m in MASTERS))
195 class ImageForm(forms.ModelForm):
196 """Form used for editing an Image."""
197 user = forms.ModelChoiceField(queryset=
198 User.objects.annotate(count=Count('chunk')).
199 order_by('-count', 'last_name', 'first_name'), required=False,
200 label=_('Assigned to'))
204 fields = ['title', 'slug', 'user', 'stage']
206 def __init__(self, *args, **kwargs):
207 super(ImageForm, self).__init__(*args, **kwargs)
208 self.fields['slug'].widget.attrs={'class': 'autoslug'}
209 self.fields['title'].widget.attrs={'class': 'autoslug-source'}
212 class ReadonlyImageForm(ImageForm):
213 """Form used for not editing an Image."""
215 def __init__(self, *args, **kwargs):
216 super(ReadonlyImageForm, self).__init__(*args, **kwargs)
217 for field in self.fields.values():
218 field.widget.attrs.update({"disabled": "disabled"})
221 class MarkFinalForm(forms.Form):
222 username = forms.CharField(initial=settings.LITERARY_DIRECTOR_USERNAME)
223 comment = forms.CharField(initial=u'Ostateczna akceptacja merytoryczna przez kierownika literackiego.')
224 books = forms.CharField(widget=forms.Textarea, help_text=u'linki do książek w redakcji, po jednym na wiersz')
226 def clean_books(self):
227 books_value = self.cleaned_data['books']
228 slugs = [line.strip().strip('/').split('/')[-1] for line in books_value.split('\n') if line.strip()]
229 books = Book.objects.filter(slug__in=slugs)
230 if len(books) != len(slugs):
231 raise forms.ValidationError(
232 'Incorrect slug(s): %s' % ' '.join(slug for slug in slugs if not Book.objects.filter(slug=slug)))
235 def clean_username(self):
236 username = self.cleaned_data['username']
237 if not User.objects.filter(username=username):
238 raise forms.ValidationError('Invalid username')
242 for book in self.cleaned_data['books']:
243 for chunk in book.chunk_set.all():
244 src = chunk.head.materialize()
247 author=User.objects.get(username=self.cleaned_data['username']),
248 description=self.cleaned_data['comment'],
249 tags=[Chunk.tag_model.objects.get(slug='editor-proofreading')],
254 class PublishOptionsForm(forms.Form):
255 days = forms.IntegerField(label=u'po ilu dniach udostępnienić (0 = od razu)', min_value=0, initial=0)
256 hidden = forms.BooleanField(label='tekst ukryty przed wyszukiwaniem', required=False, initial=False)
257 beta = forms.BooleanField(label=u'Opublikuj na wersji testowej', required=False)