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 django.utils.encoding import force_text
7 from django.utils.html import format_html
8 from django.utils.safestring import mark_safe
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 _
15 from catalogue.constants import MASTERS
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)
23 class DocumentCreateForm(forms.Form):
25 Form used for creating new documents.
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)
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)'))
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,
55 self.field().initial = self.initial()
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())
64 raise NotImplementedError
67 raise NotImplementedError
69 def cleaned_tags(self):
70 raise NotImplementedError
72 def metadata_rows(self):
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())
78 class TagSelect(forms.Select):
79 def render_option(self, selected_choices, option_value, option_label):
80 if option_value is None:
84 tag = Tag.objects.get(id=int(option_value))
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)
96 u'<option value="{}"{}{}>{}</option>',
97 option_value, selected_html, help_html, force_text(option_label))
100 class TagSingleForm(TagForm):
101 tag = forms.ModelChoiceField(
103 widget=TagSelect(attrs={
104 'class': 'form-control',
109 return self.fields['tag']
112 return self.instance.tags.get(category=self.category)
114 def cleaned_tags(self):
115 return [self.cleaned_data['tag']]
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'),
127 return self.fields['tags']
130 return self.instance.tags.filter(category=self.category)
132 def cleaned_tags(self):
133 return self.cleaned_data['tags']
136 class DocumentsUploadForm(forms.Form):
138 Form used for uploading new documents.
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'}))
146 file = self.cleaned_data['file']
150 z = self.cleaned_data['zip'] = zipfile.ZipFile(file)
151 except zipfile.BadZipfile:
152 raise forms.ValidationError("Should be a ZIP file.")
154 raise forms.ValidationError("ZIP file corrupt.")
156 return self.cleaned_data
159 class ChooseMasterForm(forms.Form):
161 Form used for fixing the chunks in a book.
164 master = forms.ChoiceField(choices=((m, m) for m in MASTERS))
167 class DocumentForkForm(forms.Form):
169 Form used for forking documents.
171 owner_organization = forms.CharField(required=False)