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.contrib.auth.models import User
7 from django.db.models import Count
8 from django import forms
9 from django.utils.translation import ugettext_lazy as _
11 from dvcs.models import Tag
12 from wiki.constants import MASTERS
13 from wiki.models import Book, Chunk
15 class DocumentTagForm(forms.Form):
17 Form for tagging revisions.
20 id = forms.CharField(widget=forms.HiddenInput)
21 tag = forms.ModelChoiceField(queryset=Tag.objects.all())
22 revision = forms.IntegerField(widget=forms.HiddenInput)
25 class DocumentPubmarkForm(forms.Form):
27 Form for marking revisions for publishing.
30 id = forms.CharField(widget=forms.HiddenInput)
31 publishable = forms.BooleanField(required=False, initial=True,
32 label=_('Publishable'))
33 revision = forms.IntegerField(widget=forms.HiddenInput)
36 class DocumentCreateForm(forms.ModelForm):
38 Form used for creating new documents.
40 file = forms.FileField(required=False)
41 text = forms.CharField(required=False, widget=forms.Textarea)
45 exclude = ['gallery', 'parent', 'parent_number']
46 prepopulated_fields = {'slug': ['title']}
49 super(DocumentCreateForm, self).clean()
50 file = self.cleaned_data['file']
54 self.cleaned_data['text'] = file.read().decode('utf-8')
55 except UnicodeDecodeError:
56 raise forms.ValidationError("Text file must be UTF-8 encoded.")
58 if not self.cleaned_data["text"]:
59 raise forms.ValidationError("You must either enter text or upload a file")
61 return self.cleaned_data
64 class DocumentsUploadForm(forms.Form):
66 Form used for uploading new documents.
68 file = forms.FileField(required=True, label=_('ZIP file'))
71 file = self.cleaned_data['file']
75 z = self.cleaned_data['zip'] = zipfile.ZipFile(file)
76 except zipfile.BadZipfile:
77 raise forms.ValidationError("Should be a ZIP file.")
79 raise forms.ValidationError("ZIP file corrupt.")
81 return self.cleaned_data
84 class DocumentTextSaveForm(forms.Form):
86 Form for saving document's text:
88 * parent_revision - revision which the modified text originated from.
89 * comment - user's verbose comment; will be used in commit.
90 * stage_completed - mark this change as end of given stage.
94 parent_revision = forms.IntegerField(widget=forms.HiddenInput)
95 text = forms.CharField(widget=forms.HiddenInput)
97 author_name = forms.CharField(
100 help_text=_(u"Your name"),
103 author_email = forms.EmailField(
105 label=_(u"Author's email"),
106 help_text=_(u"Your email address, so we can show a gravatar :)"),
109 comment = forms.CharField(
111 widget=forms.Textarea,
112 label=_(u"Your comments"),
113 help_text=_(u"Describe changes you made."),
116 stage_completed = forms.ModelChoiceField(
117 queryset=Tag.objects.all(),
119 label=_(u"Completed"),
120 help_text=_(u"If you completed a life cycle stage, select it."),
124 class DocumentTextRevertForm(forms.Form):
126 Form for reverting document's text:
128 * revision - revision to revert to.
129 * comment - user's verbose comment; will be used in commit.
133 revision = forms.IntegerField(widget=forms.HiddenInput)
135 author_name = forms.CharField(
138 help_text=_(u"Your name"),
141 author_email = forms.EmailField(
143 label=_(u"Author's email"),
144 help_text=_(u"Your email address, so we can show a gravatar :)"),
147 comment = forms.CharField(
149 widget=forms.Textarea,
150 label=_(u"Your comments"),
151 help_text=_(u"Describe the reason for reverting."),
155 class ChunkForm(forms.ModelForm):
157 Form used for editing a chunk.
159 user = forms.ModelChoiceField(queryset=
160 User.objects.annotate(count=Count('document')).
161 order_by('-count', 'last_name', 'first_name'))
168 def clean_slug(self):
169 slug = self.cleaned_data['slug']
171 chunk = Chunk.objects.get(book=self.instance.book, slug=slug)
172 except Chunk.DoesNotExist:
174 if chunk == self.instance:
176 raise forms.ValidationError(_('Chunk with this slug already exists'))
179 class ChunkAddForm(ChunkForm):
181 Form used for adding a chunk to a document.
184 def clean_slug(self):
185 slug = self.cleaned_data['slug']
187 user = Chunk.objects.get(book=self.instance.book, slug=slug)
188 except Chunk.DoesNotExist:
190 raise forms.ValidationError(_('Chunk with this slug already exists'))
195 class BookAppendForm(forms.Form):
197 Form for appending a book to another book.
198 It means moving all chunks from book A to book B and deleting A.
201 append_to = forms.ModelChoiceField(queryset=Book.objects.all(),
202 label=_("Append to"))
205 class BookForm(forms.ModelForm):
207 Form used for editing a Book.
214 class ChooseMasterForm(forms.Form):
216 Form used for fixing the chunks in a book.
219 master = forms.ChoiceField(choices=((m, m) for m in MASTERS))