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
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'])
11 if data.get('title', None):
12 revision_data['title'] = strip_tags(data['title'].strip())
14 if data.get('tags', None):
15 revision_data['tagnames'] = data['tags'].strip()
19 class AskAction(NodeEditAction):
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']
27 question = Question(author=self.user, **processed_data)
31 self.user.message_set.create(message=self.describe(self.user))
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)
39 class AnswerAction(NodeEditAction):
42 def process_data(self, **data):
43 answer = Answer(author=self.user, parent=data['question'], **self.create_revision_data(True, **data))
47 def process_action(self):
48 self.node.question.reset_answer_count_cache()
50 self.user.message_set.create(message=self.describe(self.user))
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)
61 class CommentAction(ActionProxy):
64 def process_data(self, text='', parent=None):
65 comment = Comment(author=self.user, parent=parent, body=text)
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)
75 class ReviseAction(NodeEditAction):
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
83 def process_action(self):
84 self.node.last_edited = self
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)
93 def get_absolute_url(self):
94 return self.node.get_revisions_url()
96 class RetagAction(ActionProxy):
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
105 def process_action(self):
106 self.node.last_edited = self
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)
115 def get_absolute_url(self):
116 return self.node.get_revisions_url()
118 class RollbackAction(ActionProxy):
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)
126 def process_action(self):
127 self.node.last_edited = self
130 def describe(self, viewer=None):
131 revisions = [NodeRevision.objects.get(node=self.node, revision=int(n)) for n in self.extra.split(':')]
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,
140 def get_absolute_url(self):
141 return self.node.get_revisions_url()
143 class CloseAction(ActionProxy):
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)
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)
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),
164 class AnswerToCommentAction(ActionProxy):
165 verb = _("converted")
167 def process_data(self, new_parent=None):
168 self.node.parent = new_parent
169 self.node.node_type = "comment"
171 for comment in self.node.comments.all():
172 comment.parent = new_parent
175 self.node.last_edited = self
176 self.node.update_last_activity(self.user, save=True)
178 self.node.abs_parent.reset_answer_count_cache()
179 except AttributeError:
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),
188 class CommentToAnswerAction(ActionProxy):
189 verb = _("converted")
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)
197 # Now updated the cached data
198 question.reset_answer_count_cache()
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),
205 class CommentToQuestionAction(NodeEditAction):
206 verb = _("converted")
208 def process_data(self, **data):
209 revision_data = self.create_revision_data(**data)
210 revision = self.node.create_revision(self.user, **revision_data)
213 'covert_revision': revision.revision,
216 self.node.node_type = "question"
217 self.node.parent = None
218 self.node.abs_parent = None
220 def process_action(self):
221 self.node.last_edited = self
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),
230 class AnswerToQuestionAction(NodeEditAction):
231 verb = _("converted to question")
233 def process_data(self, **data):
234 revision_data = self.create_revision_data(**data)
235 revision = self.node.create_revision(self.user, **revision_data)
237 original_question = self.node.question
240 'covert_revision': revision.revision,
241 'original_question': original_question
244 self.node.node_type = "question"
245 self.node.parent = None
246 self.node.abs_parent = None
248 original_question.reset_answer_count_cache()
250 def process_action(self):
251 self.node.last_edited = self
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),
261 class WikifyAction(ActionProxy):
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)
269 def cancel_action(self):
270 self.node.nstate.wiki = None
271 self.node.update_last_activity(self.user, save=True)
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),