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 wiki.constants import MASTERS
12 from wiki.models import Book, Chunk
14 class DocumentTagForm(forms.Form):
16 Form for tagging revisions.
19 id = forms.CharField(widget=forms.HiddenInput)
20 tag = forms.ModelChoiceField(queryset=Chunk.tag_model.objects.all())
21 revision = forms.IntegerField(widget=forms.HiddenInput)
24 class DocumentPubmarkForm(forms.Form):
26 Form for marking revisions for publishing.
29 id = forms.CharField(widget=forms.HiddenInput)
30 publishable = forms.BooleanField(required=False, initial=True,
31 label=_('Publishable'))
32 revision = forms.IntegerField(widget=forms.HiddenInput)
35 class DocumentCreateForm(forms.ModelForm):
37 Form used for creating new documents.
39 file = forms.FileField(required=False)
40 text = forms.CharField(required=False, widget=forms.Textarea)
44 exclude = ['gallery', 'parent', 'parent_number']
45 prepopulated_fields = {'slug': ['title']}
48 super(DocumentCreateForm, self).clean()
49 file = self.cleaned_data['file']
53 self.cleaned_data['text'] = file.read().decode('utf-8')
54 except UnicodeDecodeError:
55 raise forms.ValidationError("Text file must be UTF-8 encoded.")
57 if not self.cleaned_data["text"]:
58 raise forms.ValidationError("You must either enter text or upload a file")
60 return self.cleaned_data
63 class DocumentsUploadForm(forms.Form):
65 Form used for uploading new documents.
67 file = forms.FileField(required=True, label=_('ZIP file'))
70 file = self.cleaned_data['file']
74 z = self.cleaned_data['zip'] = zipfile.ZipFile(file)
75 except zipfile.BadZipfile:
76 raise forms.ValidationError("Should be a ZIP file.")
78 raise forms.ValidationError("ZIP file corrupt.")
80 return self.cleaned_data
83 class DocumentTextSaveForm(forms.Form):
85 Form for saving document's text:
87 * parent_revision - revision which the modified text originated from.
88 * comment - user's verbose comment; will be used in commit.
89 * stage_completed - mark this change as end of given stage.
93 parent_revision = forms.IntegerField(widget=forms.HiddenInput)
94 text = forms.CharField(widget=forms.HiddenInput)
96 author_name = forms.CharField(
99 help_text=_(u"Your name"),
102 author_email = forms.EmailField(
104 label=_(u"Author's email"),
105 help_text=_(u"Your email address, so we can show a gravatar :)"),
108 comment = forms.CharField(
110 widget=forms.Textarea,
111 label=_(u"Your comments"),
112 help_text=_(u"Describe changes you made."),
115 stage_completed = forms.ModelChoiceField(
116 queryset=Chunk.tag_model.objects.all(),
118 label=_(u"Completed"),
119 help_text=_(u"If you completed a life cycle stage, select it."),
123 class DocumentTextRevertForm(forms.Form):
125 Form for reverting document's text:
127 * revision - revision to revert to.
128 * comment - user's verbose comment; will be used in commit.
132 revision = forms.IntegerField(widget=forms.HiddenInput)
134 author_name = forms.CharField(
137 help_text=_(u"Your name"),
140 author_email = forms.EmailField(
142 label=_(u"Author's email"),
143 help_text=_(u"Your email address, so we can show a gravatar :)"),
146 comment = forms.CharField(
148 widget=forms.Textarea,
149 label=_(u"Your comments"),
150 help_text=_(u"Describe the reason for reverting."),
154 class ChunkForm(forms.ModelForm):
156 Form used for editing a chunk.
158 user = forms.ModelChoiceField(queryset=
159 User.objects.annotate(count=Count('chunk')).
160 order_by('-count', 'last_name', 'first_name'))
167 def clean_slug(self):
168 slug = self.cleaned_data['slug']
170 chunk = Chunk.objects.get(book=self.instance.book, slug=slug)
171 except Chunk.DoesNotExist:
173 if chunk == self.instance:
175 raise forms.ValidationError(_('Chunk with this slug already exists'))
178 class ChunkAddForm(ChunkForm):
180 Form used for adding a chunk to a document.
183 def clean_slug(self):
184 slug = self.cleaned_data['slug']
186 user = Chunk.objects.get(book=self.instance.book, slug=slug)
187 except Chunk.DoesNotExist:
189 raise forms.ValidationError(_('Chunk with this slug already exists'))
194 class BookAppendForm(forms.Form):
196 Form for appending a book to another book.
197 It means moving all chunks from book A to book B and deleting A.
200 append_to = forms.ModelChoiceField(queryset=Book.objects.all(),
201 label=_("Append to"))
204 class BookForm(forms.ModelForm):
206 Form used for editing a Book.
213 class ChooseMasterForm(forms.Form):
215 Form used for fixing the chunks in a book.
218 master = forms.ChoiceField(choices=((m, m) for m in MASTERS))