1 # -*- coding: utf-8 -*-
3 # This file is part of FNP-Redakcja, licensed under GNU Affero GPLv3 or later.
4 # Copyright © Fundacja Nowoczesna Polska. See NOTICE for more information.
6 from catalogue.models import User
7 from django.db.models import Count
8 from django import forms
9 from django.utils.translation import ugettext_lazy as _
10 from django.conf import settings
12 from catalogue.constants import MASTERS
13 from catalogue.models import Book, Chunk, Image
15 class DocumentCreateForm(forms.ModelForm):
17 Form used for creating new documents.
19 file = forms.FileField(required=False)
20 text = forms.CharField(required=False, widget=forms.Textarea)
24 exclude = ['parent', 'parent_number', 'project']
26 def __init__(self, *args, **kwargs):
27 super(DocumentCreateForm, self).__init__(*args, **kwargs)
28 self.fields['slug'].widget.attrs={'class': 'autoslug'}
29 self.fields['gallery'].widget.attrs={'class': 'autoslug'}
30 self.fields['title'].widget.attrs={'class': 'autoslug-source'}
33 super(DocumentCreateForm, self).clean()
34 file = self.cleaned_data['file']
38 self.cleaned_data['text'] = file.read().decode('utf-8')
39 except UnicodeDecodeError:
40 raise forms.ValidationError(_("Text file must be UTF-8 encoded."))
42 if not self.cleaned_data["text"]:
43 self._errors["file"] = self.error_class([_("You must either enter text or upload a file")])
45 return self.cleaned_data
48 class DocumentsUploadForm(forms.Form):
50 Form used for uploading new documents.
52 file = forms.FileField(required=True, label=_('ZIP file'))
53 dirs = forms.BooleanField(label=_('Directories are documents in chunks'),
54 widget = forms.CheckboxInput(attrs={'disabled':'disabled'}))
57 file = self.cleaned_data['file']
61 z = self.cleaned_data['zip'] = zipfile.ZipFile(file)
62 except zipfile.BadZipfile:
63 raise forms.ValidationError("Should be a ZIP file.")
65 raise forms.ValidationError("ZIP file corrupt.")
67 return self.cleaned_data
70 class ChunkForm(forms.ModelForm):
72 Form used for editing a chunk.
74 user = forms.ModelChoiceField(queryset=
75 User.objects.annotate(count=Count('chunk')).
76 order_by('last_name', 'first_name'), required=False,
77 label=_('Assigned to'))
81 fields = ['title', 'slug', 'gallery_start', 'user', 'stage']
84 def __init__(self, *args, **kwargs):
85 super(ChunkForm, self).__init__(*args, **kwargs)
86 self.fields['gallery_start'].widget.attrs={'class': 'number-input'}
87 self.fields['slug'].widget.attrs={'class': 'autoslug'}
88 self.fields['title'].widget.attrs={'class': 'autoslug-source'}
91 slug = self.cleaned_data['slug']
93 chunk = Chunk.objects.get(book=self.instance.book, slug=slug)
94 except Chunk.DoesNotExist:
96 if chunk == self.instance:
98 raise forms.ValidationError(_('Chunk with this slug already exists'))
101 class ChunkAddForm(ChunkForm):
103 Form used for adding a chunk to a document.
106 def clean_slug(self):
107 slug = self.cleaned_data['slug']
109 user = Chunk.objects.get(book=self.instance.book, slug=slug)
110 except Chunk.DoesNotExist:
112 raise forms.ValidationError(_('Chunk with this slug already exists'))
115 class BookAppendForm(forms.Form):
117 Form for appending a book to another book.
118 It means moving all chunks from book A to book B and deleting A.
120 append_to = forms.ModelChoiceField(queryset=Book.objects.all(),
121 label=_("Append to"))
123 def __init__(self, book, *args, **kwargs):
124 ret = super(BookAppendForm, self).__init__(*args, **kwargs)
125 self.fields['append_to'].queryset = Book.objects.exclude(pk=book.pk)
129 class BookForm(forms.ModelForm):
130 """Form used for editing a Book."""
134 exclude = ['project']
136 def __init__(self, *args, **kwargs):
137 ret = super(BookForm, self).__init__(*args, **kwargs)
138 self.fields['slug'].widget.attrs.update({"class": "autoslug"})
139 self.fields['title'].widget.attrs.update({"class": "autoslug-source"})
142 def save(self, **kwargs):
143 orig_instance = Book.objects.get(pk=self.instance.pk)
144 old_gallery = orig_instance.gallery
145 new_gallery = self.cleaned_data['gallery']
146 if new_gallery != old_gallery:
149 from django.conf import settings
150 shutil.move(orig_instance.gallery_path(),
151 os.path.join(settings.MEDIA_ROOT, settings.IMAGE_DIR, new_gallery))
152 super(BookForm, self).save(**kwargs)
155 class ReadonlyBookForm(BookForm):
156 """Form used for not editing a Book."""
158 def __init__(self, *args, **kwargs):
159 ret = super(ReadonlyBookForm, self).__init__(*args, **kwargs)
160 for field in self.fields.values():
161 field.widget.attrs.update({"disabled": "disabled"})
165 class ChooseMasterForm(forms.Form):
167 Form used for fixing the chunks in a book.
170 master = forms.ChoiceField(choices=((m, m) for m in MASTERS))
173 class ImageForm(forms.ModelForm):
174 """Form used for editing an Image."""
175 user = forms.ModelChoiceField(queryset=
176 User.objects.annotate(count=Count('chunk')).
177 order_by('-count', 'last_name', 'first_name'), required=False,
178 label=_('Assigned to'))
182 fields = ['title', 'slug', 'user', 'stage']
184 def __init__(self, *args, **kwargs):
185 super(ImageForm, self).__init__(*args, **kwargs)
186 self.fields['slug'].widget.attrs={'class': 'autoslug'}
187 self.fields['title'].widget.attrs={'class': 'autoslug-source'}
190 class ReadonlyImageForm(ImageForm):
191 """Form used for not editing an Image."""
193 def __init__(self, *args, **kwargs):
194 super(ReadonlyImageForm, self).__init__(*args, **kwargs)
195 for field in self.fields.values():
196 field.widget.attrs.update({"disabled": "disabled"})
199 class MarkFinalForm(forms.Form):
200 username = forms.CharField(initial=settings.LITERARY_DIRECTOR_USERNAME)
201 comment = forms.CharField(initial=u'Ostateczna akceptacja merytoryczna przez kierownika literackiego.')
202 books = forms.CharField(widget=forms.Textarea, help_text=u'linki do książek w redakcji, po jednym na wiersz')
204 def clean_books(self):
205 books_value = self.cleaned_data['books']
206 slugs = [line.split('/')[-2] for line in books_value.split('\n') if line.strip()]
207 books = Book.objects.filter(slug__in=slugs)
208 if len(books) != len(slugs):
209 raise forms.ValidationError(
210 'Incorrect slug(s): %s' % ' '.join(slug for slug in slugs if not Book.objects.filter(slug=slug)))
213 def clean_username(self):
214 username = self.cleaned_data['username']
215 if not User.objects.filter(username=username):
216 raise forms.ValidationError('Invalid username')
220 for book in self.cleaned_data['books']:
221 for chunk in book.chunk_set.all():
222 src = chunk.head.materialize()
225 author=User.objects.get(username=self.cleaned_data['username']),
226 description=self.cleaned_data['comment'],
227 tags=[Chunk.tag_model.objects.get(slug='editor-proofreading')],