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 from django.contrib import messages
9 class NodeEditAction(ActionProxy):
10 def create_revision_data(self, initial=False, **data):
11 revision_data = dict(summary=data.get('summary', (initial and _('Initial revision') or '')), body=data['text'])
13 if data.get('title', None):
14 revision_data['title'] = strip_tags(data['title'].strip())
16 if data.get('tags', None):
17 revision_data['tagnames'] = data['tags'].strip()
21 class AskAction(NodeEditAction):
24 def process_data(self, **data):
25 processed_data = self.create_revision_data(True, **data)
26 if 'added_at' in data:
27 processed_data['added_at'] = data['added_at']
29 question = Question(author=self.user, **processed_data)
33 messages.info(request, self.describe(self.user))
35 def describe(self, viewer=None):
36 return _("%(user)s asked %(question)s") % {
37 'user': self.hyperlink(self.user.get_profile_url(), self.friendly_username(viewer, self.user)),
38 'question': self.hyperlink(self.node.get_absolute_url(), self.node.title)
41 class AnswerAction(NodeEditAction):
44 def process_data(self, **data):
45 answer = Answer(author=self.user, parent=data['question'], **self.create_revision_data(True, **data))
49 def process_action(self):
50 self.node.question.reset_answer_count_cache()
52 messages.info(request, self.describe(self.user))
55 def describe(self, viewer=None):
56 question = self.node.parent
57 return _("%(user)s answered %(asker)s on %(question)s") % {
58 'user': self.hyperlink(self.user.get_profile_url(), self.friendly_username(viewer, self.user)),
59 'asker': self.hyperlink(question.author.get_profile_url(), self.friendly_username(viewer, question.author)),
60 'question': self.hyperlink(self.node.get_absolute_url(), question.title)
63 class CommentAction(ActionProxy):
66 def process_data(self, text='', parent=None):
67 comment = Comment(author=self.user, parent=parent, body=text)
71 def describe(self, viewer=None):
72 return _("%(user)s commented on %(post_desc)s") % {
73 'user': self.hyperlink(self.node.author.get_profile_url(), self.friendly_username(viewer, self.node.author)),
74 'post_desc': self.describe_node(viewer, self.node.parent)
77 class ReviseAction(NodeEditAction):
80 def process_data(self, **data):
81 revision_data = self.create_revision_data(**data)
82 revision = self.node.create_revision(self.user, **revision_data)
83 self.extra = revision.revision
85 def process_action(self):
86 self.node.last_edited = self
89 def describe(self, viewer=None):
90 return _("%(user)s edited %(post_desc)s") % {
91 'user': self.hyperlink(self.user.get_profile_url(), self.friendly_username(viewer, self.user)),
92 'post_desc': self.describe_node(viewer, self.node)
95 def get_absolute_url(self):
96 return self.node.get_revisions_url()
98 class RetagAction(ActionProxy):
101 def process_data(self, tagnames=''):
102 active = self.node.active_revision
103 revision_data = dict(summary=_('Retag'), title=active.title, tagnames=strip_tags(tagnames.strip()), body=active.body)
104 revision = self.node.create_revision(self.user, **revision_data)
105 self.extra = revision.revision
107 def process_action(self):
108 self.node.last_edited = self
111 def describe(self, viewer=None):
112 return _("%(user)s retagged %(post_desc)s") % {
113 'user': self.hyperlink(self.user.get_profile_url(), self.friendly_username(viewer, self.user)),
114 'post_desc': self.describe_node(viewer, self.node)
117 def get_absolute_url(self):
118 return self.node.get_revisions_url()
120 class RollbackAction(ActionProxy):
123 def process_data(self, activate=None):
124 previous = self.node.active_revision
125 self.node.activate_revision(self.user, activate)
126 self.extra = "%d:%d" % (previous.revision, activate.revision)
128 def process_action(self):
129 self.node.last_edited = self
132 def describe(self, viewer=None):
133 revisions = [NodeRevision.objects.get(node=self.node, revision=int(n)) for n in self.extra.split(':')]
135 return _("%(user)s reverted %(post_desc)s from revision %(initial)d (%(initial_sum)s) to revision %(final)d (%(final_sum)s)") % {
136 'user': self.hyperlink(self.user.get_profile_url(), self.friendly_username(viewer, self.user)),
137 'post_desc': self.describe_node(viewer, self.node),
138 'initial': revisions[0].revision, 'initial_sum': revisions[0].summary,
139 'final': revisions[1].revision, 'final_sum': revisions[1].summary,
142 def get_absolute_url(self):
143 return self.node.get_revisions_url()
145 class CloseAction(ActionProxy):
148 def process_action(self):
149 self.node.marked = True
150 self.node.nstate.closed = self
151 self.node.last_edited = self
152 self.node.update_last_activity(self.user, save=True)
154 def cancel_action(self):
155 self.node.marked = False
156 self.node.nstate.closed = None
157 self.node.update_last_activity(self.user, save=True)
159 def describe(self, viewer=None):
160 return _("%(user)s closed %(post_desc)s: %(reason)s") % {
161 'user': self.hyperlink(self.user.get_profile_url(), self.friendly_username(viewer, self.user)),
162 'post_desc': self.describe_node(viewer, self.node),
166 class AnswerToCommentAction(ActionProxy):
167 verb = _("converted")
169 def process_data(self, new_parent=None):
170 self.node.parent = new_parent
171 self.node.node_type = "comment"
173 for comment in self.node.comments.all():
174 comment.parent = new_parent
177 self.node.last_edited = self
178 self.node.update_last_activity(self.user, save=True)
180 self.node.abs_parent.reset_answer_count_cache()
181 except AttributeError:
184 def describe(self, viewer=None):
185 return _("%(user)s converted an answer to %(question)s into a comment") % {
186 'user': self.hyperlink(self.user.get_profile_url(), self.friendly_username(viewer, self.user)),
187 'question': self.describe_node(viewer, self.node.abs_parent),
190 class CommentToAnswerAction(ActionProxy):
191 verb = _("converted")
193 def process_data(self, question):
194 self.node.parent = question
195 self.node.node_type = "answer"
196 self.node.last_edited = self
197 self.node.update_last_activity(self.user, save=True)
199 # Now updated the cached data
200 question.reset_answer_count_cache()
202 def describe(self, viewer=None):
203 return _("%(user)s converted comment on %(question)s into an answer") % {
204 'user': self.hyperlink(self.user.get_profile_url(), self.friendly_username(viewer, self.user)),
205 'question': self.describe_node(viewer, self.node.abs_parent),
207 class CommentToQuestionAction(NodeEditAction):
208 verb = _("converted")
210 def process_data(self, **data):
211 revision_data = self.create_revision_data(**data)
212 revision = self.node.create_revision(self.user, **revision_data)
215 'covert_revision': revision.revision,
218 self.node.node_type = "question"
219 self.node.parent = None
220 self.node.abs_parent = None
222 def process_action(self):
223 self.node.last_edited = self
226 def describe(self, viewer=None):
227 return _("%(user)s converted comment on %(question)s to a new question") % {
228 'user': self.hyperlink(self.user.get_profile_url(), self.friendly_username(viewer, self.user)),
229 'question': self.describe_node(viewer, self.node.abs_parent),
232 class AnswerToQuestionAction(NodeEditAction):
233 verb = _("converted to question")
235 def process_data(self, **data):
236 revision_data = self.create_revision_data(**data)
237 revision = self.node.create_revision(self.user, **revision_data)
239 original_question = self.node.question
242 'covert_revision': revision.revision,
243 'original_question': original_question
246 self.node.node_type = "question"
247 self.node.parent = None
248 self.node.abs_parent = None
250 original_question.reset_answer_count_cache()
252 def process_action(self):
253 self.node.last_edited = self
257 def describe(self, viewer=None):
258 return _("%(user)s converted an answer to %(question)s into a separate question") % {
259 'user': self.hyperlink(self.user.get_profile_url(), self.friendly_username(viewer, self.user)),
260 'question': self.describe_node(viewer, self.node.abs_parent),
263 class WikifyAction(ActionProxy):
266 def process_action(self):
267 self.node.nstate.wiki = self
268 self.node.last_edited = self
269 self.node.update_last_activity(self.user, save=True)
271 def cancel_action(self):
272 self.node.nstate.wiki = None
273 self.node.update_last_activity(self.user, save=True)
275 def describe(self, viewer=None):
276 return _("%(user)s marked %(node)s as community wiki.") % {
277 'user': self.hyperlink(self.user.get_profile_url(), self.friendly_username(viewer, self.user)),
278 'node': self.describe_node(viewer, self.node),