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 import forms
7 from wiki.models import Book, Chunk
8 from django.utils.translation import ugettext_lazy as _
10 from dvcs.models import Tag
11 from wiki.constants import MASTERS
13 class DocumentTagForm(forms.Form):
15 Form for tagging revisions.
18 id = forms.CharField(widget=forms.HiddenInput)
19 tag = forms.ModelChoiceField(queryset=Tag.objects.all())
20 revision = forms.IntegerField(widget=forms.HiddenInput)
23 class DocumentPubmarkForm(forms.Form):
25 Form for marking revisions for publishing.
28 id = forms.CharField(widget=forms.HiddenInput)
29 publishable = forms.BooleanField(required=False, initial=True,
30 label=_('Publishable'))
31 revision = forms.IntegerField(widget=forms.HiddenInput)
34 class DocumentCreateForm(forms.ModelForm):
36 Form used for creating new documents.
38 file = forms.FileField(required=False)
39 text = forms.CharField(required=False, widget=forms.Textarea)
43 exclude = ['gallery', 'parent', 'parent_number']
44 prepopulated_fields = {'slug': ['title']}
47 super(DocumentCreateForm, self).clean()
48 file = self.cleaned_data['file']
52 self.cleaned_data['text'] = file.read().decode('utf-8')
53 except UnicodeDecodeError:
54 raise forms.ValidationError("Text file must be UTF-8 encoded.")
56 if not self.cleaned_data["text"]:
57 raise forms.ValidationError("You must either enter text or upload a file")
59 return self.cleaned_data
62 class DocumentsUploadForm(forms.Form):
64 Form used for uploading new documents.
66 file = forms.FileField(required=True, label=_('ZIP file'))
69 file = self.cleaned_data['file']
73 z = self.cleaned_data['zip'] = zipfile.ZipFile(file)
74 except zipfile.BadZipfile:
75 raise forms.ValidationError("Should be a ZIP file.")
77 raise forms.ValidationError("ZIP file corrupt.")
79 return self.cleaned_data
82 class DocumentTextSaveForm(forms.Form):
84 Form for saving document's text:
86 * parent_revision - revision which the modified text originated from.
87 * comment - user's verbose comment; will be used in commit.
88 * stage_completed - mark this change as end of given stage.
92 parent_revision = forms.IntegerField(widget=forms.HiddenInput)
93 text = forms.CharField(widget=forms.HiddenInput)
95 author_name = forms.CharField(
98 help_text=_(u"Your name"),
101 author_email = forms.EmailField(
103 label=_(u"Author's email"),
104 help_text=_(u"Your email address, so we can show a gravatar :)"),
107 comment = forms.CharField(
109 widget=forms.Textarea,
110 label=_(u"Your comments"),
111 help_text=_(u"Describe changes you made."),
114 stage_completed = forms.ModelChoiceField(
115 queryset=Tag.objects.all(),
117 label=_(u"Completed"),
118 help_text=_(u"If you completed a life cycle stage, select it."),
122 class DocumentTextRevertForm(forms.Form):
124 Form for reverting document's text:
126 * revision - revision to revert to.
127 * comment - user's verbose comment; will be used in commit.
131 revision = forms.IntegerField(widget=forms.HiddenInput)
133 author_name = forms.CharField(
136 help_text=_(u"Your name"),
139 author_email = forms.EmailField(
141 label=_(u"Author's email"),
142 help_text=_(u"Your email address, so we can show a gravatar :)"),
145 comment = forms.CharField(
147 widget=forms.Textarea,
148 label=_(u"Your comments"),
149 help_text=_(u"Describe the reason for reverting."),
153 class ChunkForm(forms.ModelForm):
155 Form used for editing a chunk.
162 def clean_slug(self):
163 slug = self.cleaned_data['slug']
165 chunk = Chunk.objects.get(book=self.instance.book, slug=slug)
166 except Chunk.DoesNotExist:
168 if chunk == self.instance:
170 raise forms.ValidationError(_('Chunk with this slug already exists'))
173 class ChunkAddForm(ChunkForm):
175 Form used for adding a chunk to a document.
178 def clean_slug(self):
179 slug = self.cleaned_data['slug']
181 user = Chunk.objects.get(book=self.instance.book, slug=slug)
182 except Chunk.DoesNotExist:
184 raise forms.ValidationError(_('Chunk with this slug already exists'))
189 class BookAppendForm(forms.Form):
191 Form for appending a book to another book.
192 It means moving all chunks from book A to book B and deleting A.
195 append_to = forms.ModelChoiceField(queryset=Book.objects.all(),
196 label=_("Append to"))
199 class BookForm(forms.ModelForm):
201 Form used for editing a Book.
208 class ChooseMasterForm(forms.Form):
210 Form used for fixing the chunks in a book.
213 master = forms.ChoiceField(choices=((m, m) for m in MASTERS))