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 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)
37 class AnswerAction(NodeEditAction):
40 def process_data(self, **data):
41 answer = Answer(author=self.user, parent=data['question'], **self.create_revision_data(True, **data))
45 def process_action(self):
46 self.node.question.reset_answer_count_cache()
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)
56 class CommentAction(ActionProxy):
59 def process_data(self, text='', parent=None):
60 comment = Comment(author=self.user, parent=parent, body=text)
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)
70 class ReviseAction(NodeEditAction):
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
78 def process_action(self):
79 self.node.last_edited = self
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)
88 def get_absolute_url(self):
89 return self.node.get_revisions_url()
91 class RetagAction(ActionProxy):
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
100 def process_action(self):
101 self.node.last_edited = self
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)
110 def get_absolute_url(self):
111 return self.node.get_revisions_url()
113 class RollbackAction(ActionProxy):
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)
121 def process_action(self):
122 self.node.last_edited = self
125 def describe(self, viewer=None):
126 revisions = [NodeRevision.objects.get(node=self.node, revision=int(n)) for n in self.extra.split(':')]
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,
135 def get_absolute_url(self):
136 return self.node.get_revisions_url()
138 class CloseAction(ActionProxy):
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)
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)
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),
159 class AnswerToCommentAction(ActionProxy):
160 verb = _("converted")
162 def process_data(self, new_parent=None):
163 self.node.parent = new_parent
164 self.node.node_type = "comment"
166 for comment in self.node.comments.all():
167 comment.parent = new_parent
170 self.node.last_edited = self
171 self.node.update_last_activity(self.user, save=True)
173 self.node.abs_parent.reset_answer_count_cache()
174 except AttributeError:
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),
183 class CommentToAnswerAction(ActionProxy):
184 verb = _("converted")
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)
192 # Now updated the cached data
193 question.reset_answer_count_cache()
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),
201 class AnswerToQuestionAction(NodeEditAction):
202 verb = _("converted to question")
204 def process_data(self, **data):
205 revision_data = self.create_revision_data(**data)
206 revision = self.node.create_revision(self.user, **revision_data)
208 original_question = self.node.question
211 'covert_revision': revision.revision,
212 'original_question': original_question
215 self.node.node_type = "question"
216 self.node.parent = None
217 self.node.abs_parent = None
219 original_question.reset_answer_count_cache()
221 def process_action(self):
222 self.node.last_edited = self
226 def describe(self, viewer=None):
227 return _("%(user)s converted an answer to %(question)s into a separate 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 WikifyAction(ActionProxy):
235 def process_action(self):
236 self.node.nstate.wiki = self
237 self.node.last_edited = self
238 self.node.update_last_activity(self.user, save=True)
240 def cancel_action(self):
241 self.node.nstate.wiki = None
242 self.node.update_last_activity(self.user, save=True)
244 def describe(self, viewer=None):
245 return _("%(user)s marked %(node)s as community wiki.") % {
246 'user': self.hyperlink(self.user.get_profile_url(), self.friendly_username(viewer, self.user)),
247 'node': self.describe_node(viewer, self.node),