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