validate cover extension server-side
[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     # def __init__(self, *args, org=None, **kwargs):
38     #     super(DocumentCreateForm, self).__init__(*args, **kwargs)
39     #     self.fields['title'].widget.attrs={'class': 'autoslug-source'}
40     #     self.fields['template'].queryset = Template.objects.filter(is_main=True)
41     #
42     # def clean(self):
43     #     super(DocumentCreateForm, self).clean()
44     #     template = self.cleaned_data['template']
45     #
46     #     if template is not None:
47     #         self.cleaned_data['text'] = template.content
48     #
49     #     if not self.cleaned_data.get("text"):
50     #         self._errors["template"] = self.error_class([_("You must select a template")])
51     #
52     #     return self.cleaned_data
53
54
55 class DocumentsUploadForm(forms.Form):
56     """
57         Form used for uploading new documents.
58     """
59     file = forms.FileField(required=True, label=_('ZIP file'))
60     dirs = forms.BooleanField(
61         label=_('Directories are documents in chunks'),
62         widget=forms.CheckboxInput(attrs={'disabled': 'disabled'}))
63
64     def clean(self):
65         file = self.cleaned_data['file']
66
67         import zipfile
68         try:
69             z = self.cleaned_data['zip'] = zipfile.ZipFile(file)
70         except zipfile.BadZipfile:
71             raise forms.ValidationError("Should be a ZIP file.")
72         if z.testzip():
73             raise forms.ValidationError("ZIP file corrupt.")
74
75         return self.cleaned_data
76
77
78 class ChooseMasterForm(forms.Form):
79     """
80         Form used for fixing the chunks in a book.
81     """
82
83     master = forms.ChoiceField(choices=((m, m) for m in MASTERS))
84
85
86 class DocumentForkForm(forms.Form):
87     """
88         Form used for forking documents.
89     """
90     owner_organization = forms.CharField(required=False)