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)
46 super(DocumentCreateForm, self).clean()
47 file = self.cleaned_data['file']
51 self.cleaned_data['text'] = file.read().decode('utf-8')
52 except UnicodeDecodeError:
53 raise forms.ValidationError("Text file must be UTF-8 encoded.")
55 if not self.cleaned_data["text"]:
56 raise forms.ValidationError("You must either enter text or upload a file")
58 return self.cleaned_data
61 class DocumentsUploadForm(forms.Form):
63 Form used for uploading new documents.
65 file = forms.FileField(required=True, label=_('ZIP file'))
68 file = self.cleaned_data['file']
72 z = self.cleaned_data['zip'] = zipfile.ZipFile(file)
73 except zipfile.BadZipfile:
74 raise forms.ValidationError("Should be a ZIP file.")
76 raise forms.ValidationError("ZIP file corrupt.")
78 return self.cleaned_data
81 class DocumentTextSaveForm(forms.Form):
83 Form for saving document's text:
85 * parent_revision - revision which the modified text originated from.
86 * comment - user's verbose comment; will be used in commit.
87 * stage_completed - mark this change as end of given stage.
91 parent_revision = forms.IntegerField(widget=forms.HiddenInput)
92 text = forms.CharField(widget=forms.HiddenInput)
94 author_name = forms.CharField(
97 help_text=_(u"Your name"),
100 author_email = forms.EmailField(
102 label=_(u"Author's email"),
103 help_text=_(u"Your email address, so we can show a gravatar :)"),
106 comment = forms.CharField(
108 widget=forms.Textarea,
109 label=_(u"Your comments"),
110 help_text=_(u"Describe changes you made."),
113 stage_completed = forms.ModelChoiceField(
114 queryset=Tag.objects.all(),
116 label=_(u"Completed"),
117 help_text=_(u"If you completed a life cycle stage, select it."),
121 class DocumentTextRevertForm(forms.Form):
123 Form for reverting document's text:
125 * revision - revision to revert to.
126 * comment - user's verbose comment; will be used in commit.
130 revision = forms.IntegerField(widget=forms.HiddenInput)
132 author_name = forms.CharField(
135 help_text=_(u"Your name"),
138 author_email = forms.EmailField(
140 label=_(u"Author's email"),
141 help_text=_(u"Your email address, so we can show a gravatar :)"),
144 comment = forms.CharField(
146 widget=forms.Textarea,
147 label=_(u"Your comments"),
148 help_text=_(u"Describe the reason for reverting."),
152 class ChunkForm(forms.ModelForm):
154 Form used for editing a chunk.
161 def clean_slug(self):
162 slug = self.cleaned_data['slug']
164 chunk = Chunk.objects.get(book=self.instance.book, slug=slug)
165 except Chunk.DoesNotExist:
169 raise forms.ValidationError(_('Chunk with this slug already exists'))
172 class ChunkAddForm(ChunkForm):
174 Form used for adding a chunk to a document.
177 def clean_slug(self):
178 slug = self.cleaned_data['slug']
180 user = Chunk.objects.get(book=self.instance.book, slug=slug)
181 except Chunk.DoesNotExist:
183 raise forms.ValidationError(_('Chunk with this slug already exists'))
188 class BookAppendForm(forms.Form):
190 Form for appending a book to another book.
191 It means moving all chunks from book A to book B and deleting A.
194 append_to = forms.ModelChoiceField(queryset=Book.objects.all(),
195 label=_("Append to"))
198 class BookForm(forms.ModelForm):
200 Form used for editing a Book.
207 class ChooseMasterForm(forms.Form):
209 Form used for fixing the chunks in a book.
212 master = forms.ChoiceField(choices=((m, m) for m in MASTERS))