]> git.openstreetmap.org Git - osqa.git/blob - forum/actions/node.py
the email in the revent activity should be visible only to superusers, staff and...
[osqa.git] / forum / actions / node.py
1 from django.utils.html import strip_tags
2 from django.utils.translation import ugettext as _
3 from forum.models.action import ActionProxy
4 from forum.models import Comment, Question, Answer, NodeRevision
5 import logging
6
7 class NodeEditAction(ActionProxy):
8     def create_revision_data(self, initial=False, **data):
9         revision_data = dict(summary=data.get('summary', (initial and _('Initial revision') or '')), body=data['text'])
10
11         if data.get('title', None):
12             revision_data['title'] = strip_tags(data['title'].strip())
13
14         if data.get('tags', None):
15             revision_data['tagnames'] = data['tags'].strip()
16
17         return revision_data
18
19 class AskAction(NodeEditAction):
20     verb = _("asked")
21
22     def process_data(self, **data):
23         processed_data = self.create_revision_data(True, **data)
24         if 'added_at' in data:
25             processed_data['added_at'] = data['added_at']
26
27         question = Question(author=self.user, **processed_data)
28         question.save()
29         self.node = question
30
31     def describe(self, viewer=None):
32         return _("%(user)s asked %(question)s") % {
33             'user': self.hyperlink(self.user.get_profile_url(), self.friendly_username(viewer, self.user)),
34             'question': self.hyperlink(self.node.get_absolute_url(), self.node.title)
35         }
36
37 class AnswerAction(NodeEditAction):
38     verb = _("answered")
39
40     def process_data(self, **data):
41         answer = Answer(author=self.user, parent=data['question'], **self.create_revision_data(True, **data))
42         answer.save()
43         self.node = answer
44
45     def process_action(self):
46         self.node.question.reset_answer_count_cache()
47
48     def describe(self, viewer=None):
49         question = self.node.parent
50         return _("%(user)s answered %(asker)s on %(question)s") % {
51             'user': self.hyperlink(self.user.get_profile_url(), self.friendly_username(viewer, self.user)),
52             'asker': self.hyperlink(question.author.get_profile_url(), self.friendly_username(viewer, question.author)),
53             'question': self.hyperlink(self.node.get_absolute_url(), question.title)
54         }
55
56 class CommentAction(ActionProxy):
57     verb = _("commented")
58
59     def process_data(self, text='', parent=None):
60         comment = Comment(author=self.user, parent=parent, body=text)
61         comment.save()
62         self.node = comment
63
64     def describe(self, viewer=None):
65         return _("%(user)s commented on %(post_desc)s") % {
66             'user': self.hyperlink(self.node.author.get_profile_url(), self.friendly_username(viewer, self.node.author)),
67             'post_desc': self.describe_node(viewer, self.node.parent)
68         }
69
70 class ReviseAction(NodeEditAction):
71     verb = _("edited")
72
73     def process_data(self, **data):
74         revision_data = self.create_revision_data(**data)
75         revision = self.node.create_revision(self.user, **revision_data)
76         self.extra = revision.revision
77
78     def process_action(self):
79         self.node.last_edited = self
80         self.node.save()
81
82     def describe(self, viewer=None):
83         return _("%(user)s edited %(post_desc)s") % {
84             'user': self.hyperlink(self.user.get_profile_url(), self.friendly_username(viewer, self.user)),
85             'post_desc': self.describe_node(viewer, self.node)
86         }
87
88     def get_absolute_url(self):
89         return self.node.get_revisions_url()
90
91 class RetagAction(ActionProxy):
92     verb = _("retagged")
93
94     def process_data(self, tagnames=''):
95         active = self.node.active_revision
96         revision_data = dict(summary=_('Retag'), title=active.title, tagnames=strip_tags(tagnames.strip()), body=active.body)
97         revision = self.node.create_revision(self.user, **revision_data)
98         self.extra = revision.revision
99
100     def process_action(self):
101         self.node.last_edited = self
102         self.node.save()
103
104     def describe(self, viewer=None):
105         return _("%(user)s retagged %(post_desc)s") % {
106             'user': self.hyperlink(self.user.get_profile_url(), self.friendly_username(viewer, self.user)),
107             'post_desc': self.describe_node(viewer, self.node)
108         }
109
110     def get_absolute_url(self):
111         return self.node.get_revisions_url()
112
113 class RollbackAction(ActionProxy):
114     verb = _("reverted")
115
116     def process_data(self, activate=None):
117         previous = self.node.active_revision
118         self.node.activate_revision(self.user, activate)
119         self.extra = "%d:%d" % (previous.revision, activate.revision)
120
121     def process_action(self):
122         self.node.last_edited = self
123         self.node.save()
124
125     def describe(self, viewer=None):
126         revisions = [NodeRevision.objects.get(node=self.node, revision=int(n)) for n in self.extra.split(':')]
127
128         return _("%(user)s reverted %(post_desc)s from revision %(initial)d (%(initial_sum)s) to revision %(final)d (%(final_sum)s)") % {
129             'user': self.hyperlink(self.user.get_profile_url(), self.friendly_username(viewer, self.user)),
130             'post_desc': self.describe_node(viewer, self.node),
131             'initial': revisions[0].revision, 'initial_sum': revisions[0].summary,
132             'final': revisions[1].revision, 'final_sum': revisions[1].summary,
133         }
134
135     def get_absolute_url(self):
136         return self.node.get_revisions_url()
137
138 class CloseAction(ActionProxy):
139     verb = _("closed")
140
141     def process_action(self):
142         self.node.marked = True
143         self.node.nstate.closed = self
144         self.node.last_edited = self
145         self.node.update_last_activity(self.user, save=True)
146
147     def cancel_action(self):
148         self.node.marked = False
149         self.node.nstate.closed = None
150         self.node.update_last_activity(self.user, save=True)
151
152     def describe(self, viewer=None):
153         return _("%(user)s closed %(post_desc)s: %(reason)s") % {
154             'user': self.hyperlink(self.user.get_profile_url(), self.friendly_username(viewer, self.user)),
155             'post_desc': self.describe_node(viewer, self.node),
156             'reason': self.extra
157         }
158
159 class AnswerToCommentAction(ActionProxy):
160     verb = _("converted")
161
162     def process_data(self, new_parent=None):
163         self.node.parent = new_parent
164         self.node.node_type = "comment"
165
166         for comment in self.node.comments.all():
167             comment.parent = new_parent
168             comment.save()
169
170         self.node.last_edited = self
171         self.node.update_last_activity(self.user, save=True)
172         try:
173             self.node.abs_parent.reset_answer_count_cache()
174         except AttributeError:
175             pass
176
177     def describe(self, viewer=None):
178         return _("%(user)s converted an answer to %(question)s into a comment") % {
179             'user': self.hyperlink(self.user.get_profile_url(), self.friendly_username(viewer, self.user)),
180             'question': self.describe_node(viewer, self.node.abs_parent),
181         }
182
183 class CommentToAnswerAction(ActionProxy):
184     verb = _("converted")
185
186     def process_data(self, question):
187         self.node.parent = question
188         self.node.node_type = "answer"
189         self.node.last_edited = self
190         self.node.update_last_activity(self.user, save=True)
191
192         # Now updated the cached data
193         question.reset_answer_count_cache()
194
195     def describe(self, viewer=None):
196         return _("%(user)s converted comment on %(question)s into an answer") % {
197             'user': self.hyperlink(self.user.get_profile_url(), self.friendly_username(viewer, self.user)),
198             'question': self.describe_node(viewer, self.node.abs_parent),
199         }
200 class CommentToQuestionAction(NodeEditAction):
201     verb = _("converted")
202
203     def process_data(self, **data):
204         revision_data = self.create_revision_data(**data)
205         revision = self.node.create_revision(self.user, **revision_data)
206
207         self.extra = {
208             'covert_revision': revision.revision,
209         }
210
211         self.node.node_type = "question"
212         self.node.parent = None
213         self.node.abs_parent = None
214
215     def process_action(self):
216         self.node.last_edited = self
217         self.node.save()
218
219     def describe(self, viewer=None):
220         return _("%(user)s converted comment on %(question)s to a new question") % {
221             'user': self.hyperlink(self.user.get_profile_url(), self.friendly_username(viewer, self.user)),
222             'question': self.describe_node(viewer, self.node.abs_parent),
223         }
224
225 class AnswerToQuestionAction(NodeEditAction):
226     verb = _("converted to question")
227
228     def process_data(self,  **data):
229         revision_data = self.create_revision_data(**data)
230         revision = self.node.create_revision(self.user, **revision_data)
231
232         original_question = self.node.question
233
234         self.extra = {
235             'covert_revision': revision.revision,
236             'original_question': original_question
237         }
238
239         self.node.node_type = "question"
240         self.node.parent = None
241         self.node.abs_parent = None
242
243         original_question.reset_answer_count_cache()
244
245     def process_action(self):
246         self.node.last_edited = self
247         self.node.save()
248
249
250     def describe(self, viewer=None):
251         return _("%(user)s converted an answer to %(question)s into a separate question") % {
252             'user': self.hyperlink(self.user.get_profile_url(), self.friendly_username(viewer, self.user)),
253             'question': self.describe_node(viewer, self.node.abs_parent),
254         }
255
256 class WikifyAction(ActionProxy):
257     verb = _("wikified")
258
259     def process_action(self):
260         self.node.nstate.wiki = self
261         self.node.last_edited = self
262         self.node.update_last_activity(self.user, save=True)
263
264     def cancel_action(self):
265         self.node.nstate.wiki = None
266         self.node.update_last_activity(self.user, save=True)
267
268     def describe(self, viewer=None):
269         return _("%(user)s marked %(node)s as community wiki.") % {
270             'user': self.hyperlink(self.user.get_profile_url(), self.friendly_username(viewer, self.user)),
271             'node': self.describe_node(viewer, self.node),
272         }
273