smarter mark final form
[redakcja.git] / apps / catalogue / forms.py
1 # -*- coding: utf-8 -*-
2 #
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.
5 #
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
11
12 from catalogue.constants import MASTERS
13 from catalogue.models import Book, Chunk, Image
14
15 class DocumentCreateForm(forms.ModelForm):
16     """
17         Form used for creating new documents.
18     """
19     file = forms.FileField(required=False)
20     text = forms.CharField(required=False, widget=forms.Textarea)
21
22     class Meta:
23         model = Book
24         exclude = ['parent', 'parent_number', 'project']
25
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'}
31
32     def clean(self):
33         super(DocumentCreateForm, self).clean()
34         file = self.cleaned_data['file']
35
36         if file is not None:
37             try:
38                 self.cleaned_data['text'] = file.read().decode('utf-8')
39             except UnicodeDecodeError:
40                 raise forms.ValidationError(_("Text file must be UTF-8 encoded."))
41
42         if not self.cleaned_data["text"]:
43             self._errors["file"] = self.error_class([_("You must either enter text or upload a file")])
44
45         return self.cleaned_data
46
47
48 class DocumentsUploadForm(forms.Form):
49     """
50         Form used for uploading new documents.
51     """
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'}))
55
56     def clean(self):
57         file = self.cleaned_data['file']
58
59         import zipfile
60         try:
61             z = self.cleaned_data['zip'] = zipfile.ZipFile(file)
62         except zipfile.BadZipfile:
63             raise forms.ValidationError("Should be a ZIP file.")
64         if z.testzip():
65             raise forms.ValidationError("ZIP file corrupt.")
66
67         return self.cleaned_data
68
69
70 class ChunkForm(forms.ModelForm):
71     """
72         Form used for editing a chunk.
73     """
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')) 
78
79     class Meta:
80         model = Chunk
81         fields = ['title', 'slug', 'gallery_start', 'user', 'stage']
82         exclude = ['number']
83
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'}
89
90     def clean_slug(self):
91         slug = self.cleaned_data['slug']
92         try:
93             chunk = Chunk.objects.get(book=self.instance.book, slug=slug)
94         except Chunk.DoesNotExist:
95             return slug
96         if chunk == self.instance:
97             return slug
98         raise forms.ValidationError(_('Chunk with this slug already exists'))
99
100
101 class ChunkAddForm(ChunkForm):
102     """
103         Form used for adding a chunk to a document.
104     """
105
106     def clean_slug(self):
107         slug = self.cleaned_data['slug']
108         try:
109             user = Chunk.objects.get(book=self.instance.book, slug=slug)
110         except Chunk.DoesNotExist:
111             return slug
112         raise forms.ValidationError(_('Chunk with this slug already exists'))
113
114
115 class BookAppendForm(forms.Form):
116     """
117         Form for appending a book to another book.
118         It means moving all chunks from book A to book B and deleting A.
119     """
120     append_to = forms.ModelChoiceField(queryset=Book.objects.all(),
121             label=_("Append to"))
122
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)
126         return ret
127
128
129 class BookForm(forms.ModelForm):
130     """Form used for editing a Book."""
131
132     class Meta:
133         model = Book
134         exclude = ['project']
135
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"})
140         return ret
141
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:
147             import shutil
148             import os.path
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)
153
154
155 class ReadonlyBookForm(BookForm):
156     """Form used for not editing a Book."""
157
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"})
162         return ret
163
164
165 class ChooseMasterForm(forms.Form):
166     """
167         Form used for fixing the chunks in a book.
168     """
169
170     master = forms.ChoiceField(choices=((m, m) for m in MASTERS))
171
172
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')) 
179
180     class Meta:
181         model = Image
182         fields = ['title', 'slug', 'user', 'stage']
183
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'}
188
189
190 class ReadonlyImageForm(ImageForm):
191     """Form used for not editing an Image."""
192
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"})
197
198
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')
203
204     def clean_books(self):
205         books_value = self.cleaned_data['books']
206         slugs = [line.strip().strip('/').split('/')[-1] 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)))
211         return books
212
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')
217         return username
218
219     def save(self):
220         for book in self.cleaned_data['books']:
221             for chunk in book.chunk_set.all():
222                 src = chunk.head.materialize()
223                 chunk.commit(
224                     text=src,
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')],
228                     publishable=True
229                 )
230
231
232 class PublishOptionsForm(forms.Form):
233     days = forms.IntegerField(label=u'po ilu dniach udostępnienić (0 = od razu)', min_value=0, initial=0)
234     beta = forms.BooleanField(label=u'Opublikuj na wersji testowej', required=False)