add tags to document and organization
[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 User
7 from django.db.models import Count
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, Template
13
14 class DocumentCreateForm(forms.Form):
15     """
16         Form used for creating new documents.
17     """
18     owner_organization = forms.CharField(required=False)
19     title = forms.CharField(required=True)
20     language = forms.CharField(required=True)
21     publisher = forms.CharField(required=False)
22     description = forms.CharField(required=False)
23     rights = forms.CharField(required=False)
24     audience = forms.CharField()
25     
26     cover = forms.FileField(required=False)
27     
28     #summary = forms.CharField(required=True)
29     #template = forms.ModelChoiceField(Template.objects, required=False)
30
31     #class Meta:
32         #model = Book
33         #exclude = ['parent', 'parent_number', 'project', 'gallery', 'public']
34
35     #def __init__(self, *args, org=None, **kwargs):
36     #    super(DocumentCreateForm, self).__init__(*args, **kwargs)
37         #self.fields['slug'].widget.attrs={'class': 'autoslug'}
38         #self.fields['title'].widget.attrs={'class': 'autoslug-source'}
39         #self.fields['template'].queryset = Template.objects.filter(is_main=True)
40
41     #~ def clean(self):
42         #~ super(DocumentCreateForm, self).clean()
43         #template = self.cleaned_data['template']
44         #self.cleaned_data['gallery'] = self.cleaned_data['slug']
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(label=_('Directories are documents in chunks'),
61             widget = forms.CheckboxInput(attrs={'disabled':'disabled'}))
62
63     def clean(self):
64         file = self.cleaned_data['file']
65
66         import zipfile
67         try:
68             z = self.cleaned_data['zip'] = zipfile.ZipFile(file)
69         except zipfile.BadZipfile:
70             raise forms.ValidationError("Should be a ZIP file.")
71         if z.testzip():
72             raise forms.ValidationError("ZIP file corrupt.")
73
74         return self.cleaned_data
75
76
77 class DocumentForm(forms.ModelForm):
78     """
79         Form used for editing a chunk.
80     """
81     user = forms.ModelChoiceField(queryset=
82         User.objects.order_by('last_name', 'first_name'), required=False,
83         label=_('Assigned to')) 
84
85     class Meta:
86         model = Document
87         fields = ['user', 'stage']
88
89
90 class DocumentAddForm(DocumentForm):
91     """
92         Form used for adding a chunk to a document.
93     """
94
95     def clean_slug(self):
96         slug = self.cleaned_data['slug']
97         try:
98             user = Chunk.objects.get(book=self.instance.book, slug=slug)
99         except Chunk.DoesNotExist:
100             return slug
101         raise forms.ValidationError(_('Chunk with this slug already exists'))
102
103
104 class BookForm(forms.ModelForm):
105     """Form used for editing a Book."""
106
107     class Meta:
108         model = Document
109         exclude = ['project']
110
111     def __init__(self, *args, **kwargs):
112         ret = super(BookForm, self).__init__(*args, **kwargs)
113         self.fields['slug'].widget.attrs.update({"class": "autoslug"})
114         self.fields['title'].widget.attrs.update({"class": "autoslug-source"})
115         return ret
116
117
118 class ReadonlyBookForm(BookForm):
119     """Form used for not editing a Book."""
120
121     def __init__(self, *args, **kwargs):
122         ret = super(ReadonlyBookForm, self).__init__(*args, **kwargs)
123         for field in self.fields.values():
124             field.widget.attrs.update({"readonly": True})
125         return ret
126
127
128 class ChooseMasterForm(forms.Form):
129     """
130         Form used for fixing the chunks in a book.
131     """
132
133     master = forms.ChoiceField(choices=((m, m) for m in MASTERS))
134
135
136 class DocumentForkForm(forms.Form):
137     """
138         Form used for forking documents.
139     """
140     owner_organization = forms.CharField(required=False)