c2cb0eef84a9f6fa5794b43dedcb7ec405f783d2
[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 django.core.exceptions import ValidationError
8 from django.utils.translation import ugettext_lazy as _
9
10 from catalogue.constants import STAGES
11 from librarian.document import Document
12
13
14 class DocumentTextSaveForm(forms.Form):
15     """
16     Form for saving document's text:
17
18         * parent_revision - revision which the modified text originated from.
19         * comment - user's verbose comment; will be used in commit.
20         * stage - change to this stage
21
22     """
23
24     parent_revision = forms.IntegerField(widget=forms.HiddenInput, required=False)
25     text = forms.CharField(widget=forms.HiddenInput)
26
27     author_name = forms.CharField(
28         required=True,
29         label=_(u"Author"),
30         help_text=_(u"Your name"),
31     )
32
33     author_email = forms.EmailField(
34         required=True,
35         label=_(u"Author's email"),
36         help_text=_(u"Your email address, so we can show a gravatar :)"),
37     )
38
39     comment = forms.CharField(
40         required=False,
41         widget=forms.Textarea,
42         label=_(u"Your comments"),
43         help_text=_(u"Describe changes you made."),
44     )
45
46     stage = forms.ChoiceField(
47         choices=STAGES,
48         required=False,
49         label=_(u"Stage"),
50         help_text=_(u"If completed a work stage, change to another one."),
51     )
52
53     def __init__(self, *args, **kwargs):
54         user = kwargs.pop('user')
55         super(DocumentTextSaveForm, self).__init__(*args, **kwargs)
56         if user and user.is_authenticated():
57             self.fields['author_name'].required = False
58             self.fields['author_email'].required = False
59
60     def clean_text(self):
61         text = self.cleaned_data['text']
62         try:
63             doc = Document.from_string(text)
64         except ValueError as e:
65             raise ValidationError(e.message)
66
67         from librarian import SSTNS, DCNS
68         root_elem = doc.edoc.getroot()
69         if len(root_elem) < 1 or root_elem[0].tag != SSTNS('metadata'):
70             raise ValidationError("The first tag in section should be metadata")
71         if len(root_elem) < 2 or root_elem[1].tag != SSTNS('header'):
72             raise ValidationError("The first tag after metadata should be header")
73         header = root_elem[1]
74         if not getattr(header, 'text', None) or not header.text.strip():
75             raise ValidationError(
76                 "The first header should contain the title in plain text (no links, emphasis etc.) and cannot be empty")
77
78         cover_url = doc.meta.get_one(DCNS('relation.coverimage.url'))
79         if cover_url:
80             ext = cover_url.rsplit('.', 1)[-1].lower()
81             if ext not in ('jpg', 'jpeg', 'png', 'gif', 'svg'):
82                 raise ValidationError('Invalid cover image format, should be an image file (jpg, png, gif, svg). '
83                                       'Change it in Metadata.')
84         return text
85
86
87 class DocumentTextRevertForm(forms.Form):
88     """
89     Form for reverting document's text:
90
91         * revision - revision to revert to.
92         * comment - user's verbose comment; will be used in commit.
93
94     """
95
96     revision = forms.IntegerField(widget=forms.HiddenInput)
97
98     author_name = forms.CharField(
99         required=False,
100         label=_(u"Author"),
101         help_text=_(u"Your name"),
102     )
103
104     author_email = forms.EmailField(
105         required=False,
106         label=_(u"Author's email"),
107         help_text=_(u"Your email address, so we can show a gravatar :)"),
108     )
109
110     comment = forms.CharField(
111         required=False,
112         widget=forms.Textarea,
113         label=_(u"Your comments"),
114         help_text=_(u"Describe the reason for reverting."),
115     )
116
117
118 class DocumentTextPublishForm(forms.Form):
119     revision = forms.IntegerField(widget=forms.HiddenInput)