import documents from Hg
[redakcja.git] / apps / wiki / forms.py
1 # -*- coding: utf-8 -*-
2 #
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.
5 #
6 from django import forms
7 from wiki.constants import DOCUMENT_TAGS, DOCUMENT_STAGES
8 from wiki.models import Book
9 from django.utils.translation import ugettext_lazy as _
10
11
12 class DocumentTagForm(forms.Form):
13     """
14         Form for tagging revisions.
15     """
16
17     id = forms.CharField(widget=forms.HiddenInput)
18     tag = forms.ChoiceField(choices=DOCUMENT_TAGS)
19     revision = forms.IntegerField(widget=forms.HiddenInput)
20
21
22 class DocumentCreateForm(forms.ModelForm):
23     """
24         Form used for creating new documents.
25     """
26     file = forms.FileField(required=False)
27     text = forms.CharField(required=False, widget=forms.Textarea)
28
29     class Meta:
30         model = Book
31         exclude = ['gallery']
32
33     def clean(self):
34         super(DocumentCreateForm, self).clean()
35         file = self.cleaned_data['file']
36
37         if file is not None:
38             try:
39                 self.cleaned_data['text'] = file.read().decode('utf-8')
40             except UnicodeDecodeError:
41                 raise forms.ValidationError("Text file must be UTF-8 encoded.")
42
43         if not self.cleaned_data["text"]:
44             raise forms.ValidationError("You must either enter text or upload a file")
45
46         return self.cleaned_data
47
48
49 class DocumentsUploadForm(forms.Form):
50     """
51         Form used for uploading new documents.
52     """
53     file = forms.FileField(required=True, label=_('ZIP file'))
54
55     def clean(self):
56         file = self.cleaned_data['file']
57
58         import zipfile
59         try:
60             z = self.cleaned_data['zip'] = zipfile.ZipFile(file)
61         except zipfile.BadZipfile:
62             raise forms.ValidationError("Should be a ZIP file.")
63         if z.testzip():
64             raise forms.ValidationError("ZIP file corrupt.")
65
66         return self.cleaned_data
67
68
69 class DocumentTextSaveForm(forms.Form):
70     """
71     Form for saving document's text:
72
73         * parent_revision - revision which the modified text originated from.
74         * comment - user's verbose comment; will be used in commit.
75         * stage_completed - mark this change as end of given stage.
76
77     """
78
79     parent_revision = forms.IntegerField(widget=forms.HiddenInput)
80     text = forms.CharField(widget=forms.HiddenInput)
81
82     author_name = forms.CharField(
83         required=False,
84         label=_(u"Author"),
85         help_text=_(u"Your name"),
86     )
87
88     author_email = forms.EmailField(
89         required=False,
90         label=_(u"Author's email"),
91         help_text=_(u"Your email address, so we can show a gravatar :)"),
92     )
93
94     comment = forms.CharField(
95         required=True,
96         widget=forms.Textarea,
97         label=_(u"Your comments"),
98         help_text=_(u"Describe changes you made."),
99     )
100
101     stage_completed = forms.ChoiceField(
102         choices=DOCUMENT_STAGES,
103         required=False,
104         label=_(u"Completed"),
105         help_text=_(u"If you completed a life cycle stage, select it."),
106     )
107
108
109 class DocumentTextRevertForm(forms.Form):
110     """
111     Form for reverting document's text:
112
113         * revision - revision to revert to.
114         * comment - user's verbose comment; will be used in commit.
115
116     """
117
118     revision = forms.IntegerField(widget=forms.HiddenInput)
119
120     author_name = forms.CharField(
121         required=False,
122         label=_(u"Author"),
123         help_text=_(u"Your name"),
124     )
125
126     author_email = forms.EmailField(
127         required=False,
128         label=_(u"Author's email"),
129         help_text=_(u"Your email address, so we can show a gravatar :)"),
130     )
131
132     comment = forms.CharField(
133         required=True,
134         widget=forms.Textarea,
135         label=_(u"Your comments"),
136         help_text=_(u"Describe the reason for reverting."),
137     )