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 _
11 from catalogue.constants import MASTERS
12 from catalogue.models import Document, Template
14 class DocumentCreateForm(forms.Form):
16 Form used for creating new documents.
18 owner_organization = forms.CharField(required=False)
19 title = forms.CharField(required=True)
20 language = forms.CharField(required=True)
21 publisher = forms.CharField(required=False)
22 description = forms.CharField(required=False)
23 rights = forms.CharField(required=False)
24 audience = forms.CharField()
26 cover = forms.FileField(required=False)
28 #summary = forms.CharField(required=True)
29 #template = forms.ModelChoiceField(Template.objects, required=False)
33 #exclude = ['parent', 'parent_number', 'project', 'gallery', 'public']
35 #def __init__(self, *args, org=None, **kwargs):
36 # super(DocumentCreateForm, self).__init__(*args, **kwargs)
37 #self.fields['slug'].widget.attrs={'class': 'autoslug'}
38 #self.fields['title'].widget.attrs={'class': 'autoslug-source'}
39 #self.fields['template'].queryset = Template.objects.filter(is_main=True)
42 #~ super(DocumentCreateForm, self).clean()
43 #template = self.cleaned_data['template']
44 #self.cleaned_data['gallery'] = self.cleaned_data['slug']
46 #~ if template is not None:
47 #~ self.cleaned_data['text'] = template.content
49 #~ if not self.cleaned_data.get("text"):
50 #~ self._errors["template"] = self.error_class([_("You must select a template")])
52 #~ return self.cleaned_data
55 class DocumentsUploadForm(forms.Form):
57 Form used for uploading new documents.
59 file = forms.FileField(required=True, label=_('ZIP file'))
60 dirs = forms.BooleanField(label=_('Directories are documents in chunks'),
61 widget = forms.CheckboxInput(attrs={'disabled':'disabled'}))
64 file = self.cleaned_data['file']
68 z = self.cleaned_data['zip'] = zipfile.ZipFile(file)
69 except zipfile.BadZipfile:
70 raise forms.ValidationError("Should be a ZIP file.")
72 raise forms.ValidationError("ZIP file corrupt.")
74 return self.cleaned_data
77 class DocumentForm(forms.ModelForm):
79 Form used for editing a chunk.
81 user = forms.ModelChoiceField(queryset=
82 User.objects.order_by('last_name', 'first_name'), required=False,
83 label=_('Assigned to'))
87 fields = ['user', 'stage']
90 class DocumentAddForm(DocumentForm):
92 Form used for adding a chunk to a document.
96 slug = self.cleaned_data['slug']
98 user = Chunk.objects.get(book=self.instance.book, slug=slug)
99 except Chunk.DoesNotExist:
101 raise forms.ValidationError(_('Chunk with this slug already exists'))
104 class BookForm(forms.ModelForm):
105 """Form used for editing a Book."""
109 exclude = ['project']
111 def __init__(self, *args, **kwargs):
112 ret = super(BookForm, self).__init__(*args, **kwargs)
113 self.fields['slug'].widget.attrs.update({"class": "autoslug"})
114 self.fields['title'].widget.attrs.update({"class": "autoslug-source"})
118 class ReadonlyBookForm(BookForm):
119 """Form used for not editing a Book."""
121 def __init__(self, *args, **kwargs):
122 ret = super(ReadonlyBookForm, self).__init__(*args, **kwargs)
123 for field in self.fields.values():
124 field.widget.attrs.update({"readonly": True})
128 class ChooseMasterForm(forms.Form):
130 Form used for fixing the chunks in a book.
133 master = forms.ChoiceField(choices=((m, m) for m in MASTERS))
136 class DocumentForkForm(forms.Form):
138 Form used for forking documents.
140 owner_organization = forms.CharField(required=False)