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 DocumentCreateForm(forms.ModelForm):
25 Form used for creating new documents.
27 file = forms.FileField(required=False)
28 text = forms.CharField(required=False, widget=forms.Textarea)
35 super(DocumentCreateForm, self).clean()
36 file = self.cleaned_data['file']
40 self.cleaned_data['text'] = file.read().decode('utf-8')
41 except UnicodeDecodeError:
42 raise forms.ValidationError("Text file must be UTF-8 encoded.")
44 if not self.cleaned_data["text"]:
45 raise forms.ValidationError("You must either enter text or upload a file")
47 return self.cleaned_data
50 class DocumentsUploadForm(forms.Form):
52 Form used for uploading new documents.
54 file = forms.FileField(required=True, label=_('ZIP file'))
57 file = self.cleaned_data['file']
61 z = self.cleaned_data['zip'] = zipfile.ZipFile(file)
62 except zipfile.BadZipfile:
63 raise forms.ValidationError("Should be a ZIP file.")
65 raise forms.ValidationError("ZIP file corrupt.")
67 return self.cleaned_data
70 class DocumentTextSaveForm(forms.Form):
72 Form for saving document's text:
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.
80 parent_revision = forms.IntegerField(widget=forms.HiddenInput)
81 text = forms.CharField(widget=forms.HiddenInput)
83 author_name = forms.CharField(
86 help_text=_(u"Your name"),
89 author_email = forms.EmailField(
91 label=_(u"Author's email"),
92 help_text=_(u"Your email address, so we can show a gravatar :)"),
95 comment = forms.CharField(
97 widget=forms.Textarea,
98 label=_(u"Your comments"),
99 help_text=_(u"Describe changes you made."),
102 stage_completed = forms.ModelChoiceField(
103 queryset=Tag.objects.all(),
105 label=_(u"Completed"),
106 help_text=_(u"If you completed a life cycle stage, select it."),
110 class DocumentTextRevertForm(forms.Form):
112 Form for reverting document's text:
114 * revision - revision to revert to.
115 * comment - user's verbose comment; will be used in commit.
119 revision = forms.IntegerField(widget=forms.HiddenInput)
121 author_name = forms.CharField(
124 help_text=_(u"Your name"),
127 author_email = forms.EmailField(
129 label=_(u"Author's email"),
130 help_text=_(u"Your email address, so we can show a gravatar :)"),
133 comment = forms.CharField(
135 widget=forms.Textarea,
136 label=_(u"Your comments"),
137 help_text=_(u"Describe the reason for reverting."),
141 class ChunkForm(forms.ModelForm):
143 Form used for editing a chunk.
150 def clean_slug(self):
151 slug = self.cleaned_data['slug']
153 chunk = Chunk.objects.get(book=self.instance.book, slug=slug)
154 except Chunk.DoesNotExist:
158 raise forms.ValidationError(_('Chunk with this slug already exists'))
161 class ChunkAddForm(ChunkForm):
163 Form used for adding a chunk to a document.
166 def clean_slug(self):
167 slug = self.cleaned_data['slug']
169 user = Chunk.objects.get(book=self.instance.book, slug=slug)
170 except Chunk.DoesNotExist:
172 raise forms.ValidationError(_('Chunk with this slug already exists'))
177 class BookAppendForm(forms.Form):
179 Form for appending a book to another book.
180 It means moving all chunks from book A to book B and deleting A.
183 append_to = forms.ModelChoiceField(queryset=Book.objects.all())
186 class BookForm(forms.ModelForm):
188 Form used for editing a Book.
195 class ChooseMasterForm(forms.Form):
197 Form used for fixing the chunks in a book.
200 master = forms.ChoiceField(choices=((m, m) for m in MASTERS))