fix preview for en
[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 Category
7 from catalogue.models import User
8 from django import forms
9 from django.utils.translation import ugettext_lazy as _
10
11 from catalogue.constants import MASTERS
12 from catalogue.models import Document
13
14
15 def tag_field(category_tag, required=True):
16     category = Category.objects.get(dc_tag=category_tag)
17     return forms.ModelMultipleChoiceField(queryset=category.tag_set.all(), required=required)
18
19
20 class DocumentCreateForm(forms.Form):
21     """
22         Form used for creating new documents.
23     """
24     owner_organization = forms.CharField(required=False)
25     title = forms.CharField()
26     language = forms.CharField()
27     publisher = forms.CharField(required=False)
28     description = forms.CharField(required=False)
29     rights = forms.CharField(required=False)
30     audience = forms.CharField()
31     
32     cover = forms.FileField(required=False)
33     
34     # summary = forms.CharField(required=True)
35     # template = forms.ModelChoiceField(Template.objects, required=False)
36     #
37     # class Meta:
38     #     model = Book
39     #     exclude = ['parent', 'parent_number', 'project', 'gallery', 'public']
40     #
41     # def __init__(self, *args, org=None, **kwargs):
42     #     super(DocumentCreateForm, self).__init__(*args, **kwargs)
43     #     self.fields['slug'].widget.attrs={'class': 'autoslug'}
44     #     self.fields['title'].widget.attrs={'class': 'autoslug-source'}
45     #     self.fields['template'].queryset = Template.objects.filter(is_main=True)
46     #
47     # def clean(self):
48     #     super(DocumentCreateForm, self).clean()
49     #     template = self.cleaned_data['template']
50     #     self.cleaned_data['gallery'] = self.cleaned_data['slug']
51     #
52     #     if template is not None:
53     #         self.cleaned_data['text'] = template.content
54     #
55     #     if not self.cleaned_data.get("text"):
56     #         self._errors["template"] = self.error_class([_("You must select a template")])
57     #
58     #     return self.cleaned_data
59
60
61 class DocumentsUploadForm(forms.Form):
62     """
63         Form used for uploading new documents.
64     """
65     file = forms.FileField(required=True, label=_('ZIP file'))
66     dirs = forms.BooleanField(
67         label=_('Directories are documents in chunks'),
68         widget=forms.CheckboxInput(attrs={'disabled': 'disabled'}))
69
70     def clean(self):
71         file = self.cleaned_data['file']
72
73         import zipfile
74         try:
75             z = self.cleaned_data['zip'] = zipfile.ZipFile(file)
76         except zipfile.BadZipfile:
77             raise forms.ValidationError("Should be a ZIP file.")
78         if z.testzip():
79             raise forms.ValidationError("ZIP file corrupt.")
80
81         return self.cleaned_data
82
83
84 class DocumentForm(forms.ModelForm):
85     """
86         Form used for editing a chunk.
87     """
88     user = forms.ModelChoiceField(
89         queryset=User.objects.order_by('last_name', 'first_name'),
90         required=False, label=_('Assigned to'))
91
92     class Meta:
93         model = Document
94         fields = ['user', 'stage']
95
96
97 class BookForm(forms.ModelForm):
98     """Form used for editing a Book."""
99
100     class Meta:
101         model = Document
102         exclude = ['project']
103
104     def __init__(self, *args, **kwargs):
105         super(BookForm, self).__init__(*args, **kwargs)
106         self.fields['slug'].widget.attrs.update({"class": "autoslug"})
107         self.fields['title'].widget.attrs.update({"class": "autoslug-source"})
108
109
110 class ReadonlyBookForm(BookForm):
111     """Form used for not editing a Book."""
112
113     def __init__(self, *args, **kwargs):
114         super(ReadonlyBookForm, self).__init__(*args, **kwargs)
115         for field in self.fields.values():
116             field.widget.attrs.update({"readonly": True})
117
118
119 class ChooseMasterForm(forms.Form):
120     """
121         Form used for fixing the chunks in a book.
122     """
123
124     master = forms.ChoiceField(choices=((m, m) for m in MASTERS))
125
126
127 class DocumentForkForm(forms.Form):
128     """
129         Form used for forking documents.
130     """
131     owner_organization = forms.CharField(required=False)