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