remove mismatched html tags
[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.utils.translation import ugettext_lazy as _
8
9 from catalogue.models import Chunk
10 from catalogue.xml_tools import remove_empty_elements
11
12
13 class DocumentPubmarkForm(forms.Form):
14     """
15         Form for marking revisions for publishing.
16     """
17
18     id = forms.CharField(widget=forms.HiddenInput)
19     publishable = forms.BooleanField(required=False, initial=True, label=_('Publishable'))
20     revision = forms.IntegerField(widget=forms.HiddenInput)
21
22
23 class DocumentTextSaveForm(forms.Form):
24     """
25     Form for saving document's text:
26
27         * parent_revision - revision which the modified text originated from.
28         * comment - user's verbose comment; will be used in commit.
29         * stage_completed - mark this change as end of given stage.
30
31     """
32
33     parent_revision = forms.IntegerField(widget=forms.HiddenInput, required=False)
34     text = forms.CharField(widget=forms.HiddenInput)
35
36     author_name = forms.CharField(
37         required=True,
38         label=_(u"Author"),
39         help_text=_(u"Your name"),
40     )
41
42     author_email = forms.EmailField(
43         required=True,
44         label=_(u"Author's email"),
45         help_text=_(u"Your email address, so we can show a gravatar :)"),
46     )
47
48     comment = forms.CharField(
49         required=False,
50         widget=forms.Textarea,
51         label=_(u"Your comments"),
52         help_text=_(u"Describe changes you made."),
53     )
54
55     stage_completed = forms.ModelChoiceField(
56         queryset=Chunk.tag_model.objects.all(),
57         required=False,
58         label=_(u"Completed"),
59         help_text=_(u"If you completed a life cycle stage, select it."),
60     )
61
62     publishable = forms.BooleanField(
63         required=False, initial=False,
64         label=_('Publishable'),
65         help_text=_(u"Mark this revision as publishable."))
66
67     for_cybernauts = forms.BooleanField(
68         required=False, initial=False,
69         label=_(u"For Cybernauts"),
70         help_text=_(u"Mark this document for Cybernauts.")
71     )
72
73     def __init__(self, *args, **kwargs):
74         self.user = kwargs.pop('user')
75         self.chunk = kwargs.pop('chunk')
76         super(DocumentTextSaveForm, self).__init__(*args, **kwargs)
77         if self.user and self.user.is_authenticated():
78             self.fields['author_name'].required = False
79             self.fields['author_email'].required = False
80         self.fields['for_cybernauts'].initial = self.chunk.book.for_cybernauts
81         self.fields['publishable'].initial = self.chunk.head.publishable
82
83     def clean_text(self):
84         text = self.cleaned_data.get('text', '')
85         # remove_empty_elements returns None on SyntaxError or when there's no change
86         return remove_empty_elements(text) or text
87
88     def save(self):
89         if self.user.is_authenticated():
90             author = self.user
91         else:
92             author = None
93         text = self.cleaned_data['text']
94         parent_revision = self.cleaned_data['parent_revision']
95         if parent_revision is not None:
96             parent = self.chunk.at_revision(parent_revision)
97         else:
98             parent = None
99         stage = self.cleaned_data['stage_completed']
100         tags = [stage] if stage else []
101         publishable = self.cleaned_data['publishable'] and self.user.has_perm('catalogue.can_pubmark')
102         self.chunk.commit(
103             author=author,
104             text=text,
105             parent=parent,
106             description=self.cleaned_data['comment'],
107             tags=tags,
108             author_name=self.cleaned_data['author_name'],
109             author_email=self.cleaned_data['author_email'],
110             publishable=publishable)
111         self.chunk.book.for_cybernauts = self.cleaned_data['for_cybernauts']
112         self.chunk.book.save()
113
114
115 class DocumentTextRevertForm(forms.Form):
116     """
117     Form for reverting document's text:
118
119         * revision - revision to revert to.
120         * comment - user's verbose comment; will be used in commit.
121
122     """
123
124     revision = forms.IntegerField(widget=forms.HiddenInput)
125
126     author_name = forms.CharField(
127         required=False,
128         label=_(u"Author"),
129         help_text=_(u"Your name"),
130     )
131
132     author_email = forms.EmailField(
133         required=False,
134         label=_(u"Author's email"),
135         help_text=_(u"Your email address, so we can show a gravatar :)"),
136     )
137
138     comment = forms.CharField(
139         required=False,
140         widget=forms.Textarea,
141         label=_(u"Your comments"),
142         help_text=_(u"Describe the reason for reverting."),
143     )