7cabe3250fec980f40ba2344d2e8dc15a1e5e1d4
[redakcja.git] / apps / wiki / 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 import forms
7 from wiki.models import Book, Chunk
8 from django.utils.translation import ugettext_lazy as _
9
10 from dvcs.models import Tag
11
12 class DocumentTagForm(forms.Form):
13     """
14         Form for tagging revisions.
15     """
16
17     id = forms.CharField(widget=forms.HiddenInput)
18     tag = forms.ModelChoiceField(queryset=Tag.objects.all())
19     revision = forms.IntegerField(widget=forms.HiddenInput)
20
21
22 class DocumentCreateForm(forms.ModelForm):
23     """
24         Form used for creating new documents.
25     """
26     file = forms.FileField(required=False)
27     text = forms.CharField(required=False, widget=forms.Textarea)
28
29     class Meta:
30         model = Book
31         exclude = ['gallery']
32
33     def clean(self):
34         super(DocumentCreateForm, self).clean()
35         file = self.cleaned_data['file']
36
37         if file is not None:
38             try:
39                 self.cleaned_data['text'] = file.read().decode('utf-8')
40             except UnicodeDecodeError:
41                 raise forms.ValidationError("Text file must be UTF-8 encoded.")
42
43         if not self.cleaned_data["text"]:
44             raise forms.ValidationError("You must either enter text or upload a file")
45
46         return self.cleaned_data
47
48
49 class DocumentsUploadForm(forms.Form):
50     """
51         Form used for uploading new documents.
52     """
53     file = forms.FileField(required=True, label=_('ZIP file'))
54
55     def clean(self):
56         file = self.cleaned_data['file']
57
58         import zipfile
59         try:
60             z = self.cleaned_data['zip'] = zipfile.ZipFile(file)
61         except zipfile.BadZipfile:
62             raise forms.ValidationError("Should be a ZIP file.")
63         if z.testzip():
64             raise forms.ValidationError("ZIP file corrupt.")
65
66         return self.cleaned_data
67
68
69 class DocumentTextSaveForm(forms.Form):
70     """
71     Form for saving document's text:
72
73         * parent_revision - revision which the modified text originated from.
74         * comment - user's verbose comment; will be used in commit.
75         * stage_completed - mark this change as end of given stage.
76
77     """
78
79     parent_revision = forms.IntegerField(widget=forms.HiddenInput)
80     text = forms.CharField(widget=forms.HiddenInput)
81
82     author_name = forms.CharField(
83         required=False,
84         label=_(u"Author"),
85         help_text=_(u"Your name"),
86     )
87
88     author_email = forms.EmailField(
89         required=False,
90         label=_(u"Author's email"),
91         help_text=_(u"Your email address, so we can show a gravatar :)"),
92     )
93
94     comment = forms.CharField(
95         required=True,
96         widget=forms.Textarea,
97         label=_(u"Your comments"),
98         help_text=_(u"Describe changes you made."),
99     )
100
101     stage_completed = forms.ModelChoiceField(
102         queryset=Tag.objects.all(),
103         required=False,
104         label=_(u"Completed"),
105         help_text=_(u"If you completed a life cycle stage, select it."),
106     )
107
108
109 class DocumentTextRevertForm(forms.Form):
110     """
111     Form for reverting document's text:
112
113         * revision - revision to revert to.
114         * comment - user's verbose comment; will be used in commit.
115
116     """
117
118     revision = forms.IntegerField(widget=forms.HiddenInput)
119
120     author_name = forms.CharField(
121         required=False,
122         label=_(u"Author"),
123         help_text=_(u"Your name"),
124     )
125
126     author_email = forms.EmailField(
127         required=False,
128         label=_(u"Author's email"),
129         help_text=_(u"Your email address, so we can show a gravatar :)"),
130     )
131
132     comment = forms.CharField(
133         required=True,
134         widget=forms.Textarea,
135         label=_(u"Your comments"),
136         help_text=_(u"Describe the reason for reverting."),
137     )
138
139
140 class ChunkForm(forms.ModelForm):
141     """
142         Form used for editing a chunk.
143     """
144
145     class Meta:
146         model = Chunk
147         exclude = ['number']
148
149     def clean_slug(self):
150         slug = self.cleaned_data['slug']
151         try:
152             chunk = Chunk.objects.get(book=self.instance.book, slug=slug)
153         except Chunk.DoesNotExist:
154             return slug
155         if chunk == self:
156             return slug
157         raise forms.ValidationError(_('Chunk with this slug already exists'))
158
159
160 class ChunkAddForm(ChunkForm):
161     """
162         Form used for adding a chunk to a document.
163     """
164
165     def clean_slug(self):
166         slug = self.cleaned_data['slug']
167         try:
168             user = Chunk.objects.get(book=self.instance.book, slug=slug)
169         except Chunk.DoesNotExist:
170             return slug
171         raise forms.ValidationError(_('Chunk with this slug already exists'))
172
173
174
175
176 class BookAppendForm(forms.Form):
177     """
178         Form for appending a book to another book.
179         It means moving all chunks from book A to book B and deleting A.
180     """
181
182     append_to = forms.ModelChoiceField(queryset=Book.objects.all())
183
184
185 class BookForm(forms.ModelForm):
186     """
187         Form used for editing a Book.
188     """
189
190     class Meta:
191         model = Book