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 catalogue.models import Category
7 from catalogue.models import Tag
8 from django import forms
9 from django.utils.translation import ugettext_lazy as _
11 from catalogue.constants import MASTERS
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)
19 class DocumentCreateForm(forms.Form):
21 Form used for creating new documents.
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()
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', 'tif', 'tiff'):
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, *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
48 self.field().initial = self.initial()
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())
56 raise NotImplementedError
59 raise NotImplementedError
61 def cleaned_tags(self):
62 raise NotImplementedError
65 class TagSingleForm(TagForm):
66 tag = forms.ModelChoiceField(Tag.objects.none())
69 return self.fields['tag']
72 return self.instance.tags.get(category=self.category)
74 def cleaned_tags(self):
75 return [self.cleaned_data['tag']]
78 class TagMultipleForm(TagForm):
79 tags = forms.ModelMultipleChoiceField(Tag.objects.none(), required=False)
82 return self.fields['tags']
85 return self.instance.tags.filter(category=self.category)
87 def cleaned_tags(self):
88 return self.cleaned_data['tags']
91 class DocumentsUploadForm(forms.Form):
93 Form used for uploading new documents.
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'}))
101 file = self.cleaned_data['file']
105 z = self.cleaned_data['zip'] = zipfile.ZipFile(file)
106 except zipfile.BadZipfile:
107 raise forms.ValidationError("Should be a ZIP file.")
109 raise forms.ValidationError("ZIP file corrupt.")
111 return self.cleaned_data
114 class ChooseMasterForm(forms.Form):
116 Form used for fixing the chunks in a book.
119 master = forms.ChoiceField(choices=((m, m) for m in MASTERS))
122 class DocumentForkForm(forms.Form):
124 Form used for forking documents.
126 owner_organization = forms.CharField(required=False)