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