dc811ab47b2b53443502ad767e9e8987ebcfa132
[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 django.utils.encoding import force_text
7 from django.utils.html import format_html
8 from django.utils.safestring import mark_safe
9
10 from catalogue.models import Category
11 from catalogue.models import Tag
12 from django import forms
13 from django.utils.translation import ugettext_lazy as _
14
15 from catalogue.constants import MASTERS
16
17
18 def tag_field(category_tag, required=True):
19     category = Category.objects.get(dc_tag=category_tag)
20     return forms.ModelMultipleChoiceField(queryset=category.tag_set.all(), required=required)
21
22
23 class DocumentCreateForm(forms.Form):
24     """
25         Form used for creating new documents.
26     """
27     owner_organization = forms.CharField(required=False)
28     title = forms.CharField()
29     publisher = forms.CharField(required=False)
30     description = forms.CharField(required=False)
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', 'svg'):
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, tutorial_no=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.capitalize()
47         if tutorial_no and category.tutorial:
48             self.field().widget.attrs.update({
49                 'data-toggle': 'tutorial',
50                 'data-tutorial': str(tutorial_no),
51                 'data-placement': 'bottom',
52                 'data-content': category.tutorial,
53             })
54         if self.instance:
55             self.field().initial = self.initial()
56
57     def save(self, instance=None):
58         instance = instance or self.instance
59         assert instance, 'No instance provided'
60         instance.tags.remove(*instance.tags.filter(category=self.category))
61         instance.tags.add(*self.cleaned_tags())
62
63     def field(self):
64         raise NotImplementedError
65
66     def initial(self):
67         raise NotImplementedError
68
69     def cleaned_tags(self):
70         raise NotImplementedError
71
72     def metadata_rows(self):
73         return '\n'.join(
74             '<dc:%(name)s>%(value)s</dc:%(name)s>' % {'name': tag.category.dc_tag, 'value': tag.dc_value}
75             for tag in self.cleaned_tags())
76
77
78 class TagSelect(forms.Select):
79     def render_option(self, selected_choices, option_value, option_label):
80         if option_value is None:
81             option_value = ''
82         help_html = ''
83         if option_value:
84             tag = Tag.objects.get(id=int(option_value))
85             if tag.help_text:
86                 help_html = mark_safe(' data-help="%s"' % tag.help_text)
87         option_value = force_text(option_value)
88         if option_value in selected_choices:
89             selected_html = mark_safe(' selected="selected"')
90             if not self.allow_multiple_selected:
91                 # Only allow for a single selection.
92                 selected_choices.remove(option_value)
93         else:
94             selected_html = ''
95         return format_html(
96             u'<option value="{}"{}{}>{}</option>',
97             option_value, selected_html, help_html, force_text(option_label))
98
99
100 class TagSingleForm(TagForm):
101     tag = forms.ModelChoiceField(
102         Tag.objects.none(),
103         widget=TagSelect(attrs={
104             'class': 'form-control',
105         })
106     )
107
108     def field(self):
109         return self.fields['tag']
110
111     def initial(self):
112         return self.instance.tags.get(category=self.category)
113
114     def cleaned_tags(self):
115         return [self.cleaned_data['tag']]
116
117
118 class TagMultipleForm(TagForm):
119     tags = forms.ModelMultipleChoiceField(
120         Tag.objects.none(), required=False,
121         widget=forms.SelectMultiple(attrs={
122             'class': 'chosen-select',
123             'data-placeholder': _('Choose'),
124         }))
125
126     def field(self):
127         return self.fields['tags']
128
129     def initial(self):
130         return self.instance.tags.filter(category=self.category)
131
132     def cleaned_tags(self):
133         return self.cleaned_data['tags']
134
135
136 class DocumentsUploadForm(forms.Form):
137     """
138         Form used for uploading new documents.
139     """
140     file = forms.FileField(required=True, label=_('ZIP file'))
141     dirs = forms.BooleanField(
142         label=_('Directories are documents in chunks'),
143         widget=forms.CheckboxInput(attrs={'disabled': 'disabled'}))
144
145     def clean(self):
146         file = self.cleaned_data['file']
147
148         import zipfile
149         try:
150             z = self.cleaned_data['zip'] = zipfile.ZipFile(file)
151         except zipfile.BadZipfile:
152             raise forms.ValidationError("Should be a ZIP file.")
153         if z.testzip():
154             raise forms.ValidationError("ZIP file corrupt.")
155
156         return self.cleaned_data
157
158
159 class ChooseMasterForm(forms.Form):
160     """
161         Form used for fixing the chunks in a book.
162     """
163
164     master = forms.ChoiceField(choices=((m, m) for m in MASTERS))
165
166
167 class DocumentForkForm(forms.Form):
168     """
169         Form used for forking documents.
170     """
171     owner_organization = forms.CharField(required=False)