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