cbb1ad9d227d982133cfbb5e21fc28c0e9f67488
[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 Tag
8 from django import forms
9 from django.utils.translation import ugettext_lazy as _
10
11 from catalogue.constants import MASTERS
12
13
14 def tag_field(category_tag, required=True):
15     category = Category.objects.get(dc_tag=category_tag)
16     return forms.ModelMultipleChoiceField(queryset=category.tag_set.all(), required=required)
17
18
19 class DocumentCreateForm(forms.Form):
20     """
21         Form used for creating new documents.
22     """
23     owner_organization = forms.CharField(required=False)
24     title = forms.CharField()
25     language = forms.CharField()
26     publisher = forms.CharField(required=False)
27     description = forms.CharField(required=False)
28     rights = forms.CharField(required=False)
29     audience = forms.CharField()
30     
31     cover = forms.FileField(required=False)
32
33     def clean_cover(self):
34         cover = self.cleaned_data['cover']
35         if cover and cover.name.rsplit('.', 1)[-1].lower() not in ('jpg', 'jpeg', 'png', 'gif', 'tif', 'tiff'):
36             raise forms.ValidationError(_('The cover should be an image file (jpg/png/gif)'))
37         return file
38
39
40 class TagForm(forms.Form):
41     def __init__(self, category, instance=None, *args, **kwargs):
42         super(TagForm, self).__init__(*args, **kwargs)
43         self.category = category
44         self.instance = instance
45         self.field().queryset = Tag.objects.filter(category=self.category)
46         self.field().label = self.category.label
47         if self.instance:
48             self.field().initial = self.initial()
49
50     def save(self):
51         assert self.instance, 'No instance provided'
52         self.instance.tags.remove(*self.instance.tags.filter(category=self.category))
53         self.instance.tags.add(self.cleaned_tags())
54
55     def field(self):
56         raise NotImplementedError
57
58     def initial(self):
59         raise NotImplementedError
60
61     def cleaned_tags(self):
62         raise NotImplementedError
63
64
65 class TagSingleForm(TagForm):
66     tag = forms.ModelChoiceField(Tag.objects.none())
67
68     def field(self):
69         return self.fields['tag']
70
71     def initial(self):
72         return self.instance.tags.get(category=self.category)
73
74     def cleaned_tags(self):
75         return [self.cleaned_data['tag']]
76
77
78 class TagMultipleForm(TagForm):
79     tags = forms.ModelMultipleChoiceField(Tag.objects.none(), required=False)
80
81     def field(self):
82         return self.fields['tags']
83
84     def initial(self):
85         return self.instance.tags.filter(category=self.category)
86
87     def cleaned_tags(self):
88         return self.cleaned_data['tags']
89
90
91 class DocumentsUploadForm(forms.Form):
92     """
93         Form used for uploading new documents.
94     """
95     file = forms.FileField(required=True, label=_('ZIP file'))
96     dirs = forms.BooleanField(
97         label=_('Directories are documents in chunks'),
98         widget=forms.CheckboxInput(attrs={'disabled': 'disabled'}))
99
100     def clean(self):
101         file = self.cleaned_data['file']
102
103         import zipfile
104         try:
105             z = self.cleaned_data['zip'] = zipfile.ZipFile(file)
106         except zipfile.BadZipfile:
107             raise forms.ValidationError("Should be a ZIP file.")
108         if z.testzip():
109             raise forms.ValidationError("ZIP file corrupt.")
110
111         return self.cleaned_data
112
113
114 class ChooseMasterForm(forms.Form):
115     """
116         Form used for fixing the chunks in a book.
117     """
118
119     master = forms.ChoiceField(choices=((m, m) for m in MASTERS))
120
121
122 class DocumentForkForm(forms.Form):
123     """
124         Form used for forking documents.
125     """
126     owner_organization = forms.CharField(required=False)