1 from django.utils.html import strip_tags
\r
2 from django.utils.translation import ugettext as _
\r
3 from forum.models.action import ActionProxy
\r
4 from forum.models import Comment, Question, Answer, NodeRevision
\r
7 class NodeEditAction(ActionProxy):
\r
8 def create_revision_data(self, initial=False, **data):
\r
9 revision_data = dict(summary=data.get('summary', (initial and _('Initial revision') or '')), body=data['text'])
\r
11 if data.get('title', None):
\r
12 revision_data['title'] = strip_tags(data['title'].strip())
\r
14 if data.get('tags', None):
\r
15 revision_data['tagnames'] = data['tags'].strip()
\r
17 return revision_data
\r
19 class AskAction(NodeEditAction):
\r
22 def process_data(self, **data):
\r
23 processed_data = self.create_revision_data(True, **data)
\r
24 if 'added_at' in data:
\r
25 processed_data['added_at'] = data['added_at']
\r
27 question = Question(author=self.user, **processed_data)
\r
29 self.node = question
\r
31 def describe(self, viewer=None):
\r
32 return _("%(user)s asked %(question)s") % {
\r
33 'user': self.hyperlink(self.user.get_profile_url(), self.friendly_username(viewer, self.user)),
\r
34 'question': self.hyperlink(self.node.get_absolute_url(), self.node.title)
\r
37 class AnswerAction(NodeEditAction):
\r
38 verb = _("answered")
\r
40 def process_data(self, **data):
\r
41 answer = Answer(author=self.user, parent=data['question'], **self.create_revision_data(True, **data))
\r
45 def process_action(self):
\r
46 self.node.question.reset_answer_count_cache()
\r
48 def describe(self, viewer=None):
\r
49 question = self.node.parent
\r
50 return _("%(user)s answered %(asker)s on %(question)s") % {
\r
51 'user': self.hyperlink(self.user.get_profile_url(), self.friendly_username(viewer, self.user)),
\r
52 'asker': self.hyperlink(question.author.get_profile_url(), self.friendly_username(viewer, question.author)),
\r
53 'question': self.hyperlink(self.node.get_absolute_url(), question.title)
\r
56 class CommentAction(ActionProxy):
\r
57 verb = _("commented")
\r
59 def process_data(self, text='', parent=None):
\r
60 comment = Comment(author=self.user, parent=parent, body=text)
\r
64 def describe(self, viewer=None):
\r
65 return _("%(user)s commented on %(post_desc)s") % {
\r
66 'user': self.hyperlink(self.node.author.get_profile_url(), self.friendly_username(viewer, self.node.author)),
\r
67 'post_desc': self.describe_node(viewer, self.node.parent)
\r
70 class ReviseAction(NodeEditAction):
\r
73 def process_data(self, **data):
\r
74 revision_data = self.create_revision_data(**data)
\r
75 revision = self.node.create_revision(self.user, **revision_data)
\r
76 self.extra = revision.revision
\r
78 def process_action(self):
\r
79 self.node.last_edited = self
\r
82 def describe(self, viewer=None):
\r
83 return _("%(user)s edited %(post_desc)s") % {
\r
84 'user': self.hyperlink(self.user.get_profile_url(), self.friendly_username(viewer, self.user)),
\r
85 'post_desc': self.describe_node(viewer, self.node)
\r
88 def get_absolute_url(self):
\r
89 return self.node.get_revisions_url()
\r
91 class RetagAction(ActionProxy):
\r
92 verb = _("retagged")
\r
94 def process_data(self, tagnames=''):
\r
95 active = self.node.active_revision
\r
96 revision_data = dict(summary=_('Retag'), title=active.title, tagnames=strip_tags(tagnames.strip()), body=active.body)
\r
97 revision = self.node.create_revision(self.user, **revision_data)
\r
98 self.extra = revision.revision
\r
100 def process_action(self):
\r
101 self.node.last_edited = self
\r
104 def describe(self, viewer=None):
\r
105 return _("%(user)s retagged %(post_desc)s") % {
\r
106 'user': self.hyperlink(self.user.get_profile_url(), self.friendly_username(viewer, self.user)),
\r
107 'post_desc': self.describe_node(viewer, self.node)
\r
110 def get_absolute_url(self):
\r
111 return self.node.get_revisions_url()
\r
113 class RollbackAction(ActionProxy):
\r
114 verb = _("reverted")
\r
116 def process_data(self, activate=None):
\r
117 previous = self.node.active_revision
\r
118 self.node.activate_revision(self.user, activate)
\r
119 self.extra = "%d:%d" % (previous.revision, activate.revision)
\r
121 def process_action(self):
\r
122 self.node.last_edited = self
\r
125 def describe(self, viewer=None):
\r
126 revisions = [NodeRevision.objects.get(node=self.node, revision=int(n)) for n in self.extra.split(':')]
\r
128 return _("%(user)s reverted %(post_desc)s from revision %(initial)d (%(initial_sum)s) to revision %(final)d (%(final_sum)s)") % {
\r
129 'user': self.hyperlink(self.user.get_profile_url(), self.friendly_username(viewer, self.user)),
\r
130 'post_desc': self.describe_node(viewer, self.node),
\r
131 'initial': revisions[0].revision, 'initial_sum': revisions[0].summary,
\r
132 'final': revisions[1].revision, 'final_sum': revisions[1].summary,
\r
135 def get_absolute_url(self):
\r
136 return self.node.get_revisions_url()
\r
138 class CloseAction(ActionProxy):
\r
141 def process_action(self):
\r
142 self.node.marked = True
\r
143 self.node.nstate.closed = self
\r
144 self.node.last_edited = self
\r
145 self.node.update_last_activity(self.user, save=True)
\r
147 def cancel_action(self):
\r
148 self.node.marked = False
\r
149 self.node.nstate.closed = None
\r
150 self.node.update_last_activity(self.user, save=True)
\r
152 def describe(self, viewer=None):
\r
153 return _("%(user)s closed %(post_desc)s: %(reason)s") % {
\r
154 'user': self.hyperlink(self.user.get_profile_url(), self.friendly_username(viewer, self.user)),
\r
155 'post_desc': self.describe_node(viewer, self.node),
\r
156 'reason': self.extra
\r
159 class AnswerToCommentAction(ActionProxy):
\r
160 verb = _("converted")
\r
162 def process_data(self, new_parent=None):
\r
163 self.node.parent = new_parent
\r
164 self.node.node_type = "comment"
\r
166 for comment in self.node.comments.all():
\r
167 comment.parent = new_parent
\r
170 self.node.last_edited = self
\r
171 self.node.update_last_activity(self.user, save=True)
\r
173 self.node.abs_parent.reset_answer_count_cache()
\r
174 except AttributeError:
\r
177 def describe(self, viewer=None):
\r
178 return _("%(user)s converted an answer to %(question)s into a comment") % {
\r
179 'user': self.hyperlink(self.user.get_profile_url(), self.friendly_username(viewer, self.user)),
\r
180 'question': self.describe_node(viewer, self.node.abs_parent),
\r
183 class CommentToAnswerAction(ActionProxy):
\r
184 verb = _("converted")
\r
186 def process_data(self, question):
\r
187 self.node.parent = question
\r
188 self.node.node_type = "answer"
\r
189 self.node.last_edited = self
\r
190 self.node.update_last_activity(self.user, save=True)
\r
192 # Now updated the cached data
\r
193 question.reset_answer_count_cache()
\r
195 def describe(self, viewer=None):
\r
196 return _("%(user)s converted comment on %(question)s into an answer") % {
\r
197 'user': self.hyperlink(self.user.get_profile_url(), self.friendly_username(viewer, self.user)),
\r
198 'question': self.describe_node(viewer, self.node.abs_parent),
\r
201 class AnswerToQuestionAction(NodeEditAction):
\r
202 verb = _("converted to question")
\r
204 def process_data(self, **data):
\r
205 revision_data = self.create_revision_data(**data)
\r
206 revision = self.node.create_revision(self.user, **revision_data)
\r
208 original_question = self.node.question
\r
211 'covert_revision': revision.revision,
\r
212 'original_question': original_question
\r
215 self.node.node_type = "question"
\r
216 self.node.parent = None
\r
217 self.node.abs_parent = None
\r
219 original_question.reset_answer_count_cache()
\r
221 def process_action(self):
\r
222 self.node.last_edited = self
\r
226 def describe(self, viewer=None):
\r
227 return _("%(user)s converted an answer to %(question)s into a separate question") % {
\r
228 'user': self.hyperlink(self.user.get_profile_url(), self.friendly_username(viewer, self.user)),
\r
229 'question': self.describe_node(viewer, self.node.abs_parent),
\r
232 class WikifyAction(ActionProxy):
\r
233 verb = _("wikified")
\r
235 def process_action(self):
\r
236 self.node.nstate.wiki = self
\r
237 self.node.last_edited = self
\r
238 self.node.update_last_activity(self.user, save=True)
\r
240 def cancel_action(self):
\r
241 self.node.nstate.wiki = None
\r
242 self.node.update_last_activity(self.user, save=True)
\r
244 def describe(self, viewer=None):
\r
245 return _("%(user)s marked %(node)s as community wiki.") % {
\r
246 'user': self.hyperlink(self.user.get_profile_url(), self.friendly_username(viewer, self.user)),
\r
247 'node': self.describe_node(viewer, self.node),
\r