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
6 class NodeEditAction(ActionProxy):
\r
7 def create_revision_data(self, initial=False, **data):
\r
8 revision_data = dict(summary=data.get('summary', (initial and _('Initial revision') or '')), body=data['text'])
\r
10 if data.get('title', None):
\r
11 revision_data['title'] = strip_tags(data['title'].strip())
\r
13 if data.get('tags', None):
\r
14 revision_data['tagnames'] = data['tags'].strip()
\r
16 return revision_data
\r
18 class AskAction(NodeEditAction):
\r
21 def process_data(self, **data):
\r
22 processed_data = self.create_revision_data(True, **data)
\r
23 if 'added_at' in data:
\r
24 processed_data['added_at'] = data['added_at']
\r
26 question = Question(author=self.user, **processed_data)
\r
28 self.node = question
\r
30 def describe(self, viewer=None):
\r
31 return _("%(user)s asked %(question)s") % {
\r
32 'user': self.hyperlink(self.user.get_profile_url(), self.friendly_username(viewer, self.user)),
\r
33 'question': self.hyperlink(self.node.get_absolute_url(), self.node.title)
\r
36 class AnswerAction(NodeEditAction):
\r
37 verb = _("answered")
\r
39 def process_data(self, **data):
\r
40 answer = Answer(author=self.user, parent=data['question'], **self.create_revision_data(True, **data))
\r
44 def process_action(self):
\r
45 self.node.question.reset_answer_count_cache()
\r
47 def describe(self, viewer=None):
\r
48 question = self.node.parent
\r
49 return _("%(user)s answered %(asker)s on %(question)s") % {
\r
50 'user': self.hyperlink(self.user.get_profile_url(), self.friendly_username(viewer, self.user)),
\r
51 'asker': self.hyperlink(question.author.get_profile_url(), self.friendly_username(viewer, question.author)),
\r
52 'question': self.hyperlink(self.node.get_absolute_url(), question.title)
\r
55 class CommentAction(ActionProxy):
\r
56 verb = _("commented")
\r
58 def process_data(self, text='', parent=None):
\r
59 comment = Comment(author=self.user, parent=parent, body=text)
\r
63 def describe(self, viewer=None):
\r
64 return _("%(user)s commented on %(post_desc)s") % {
\r
65 'user': self.hyperlink(self.node.author.get_profile_url(), self.friendly_username(viewer, self.node.author)),
\r
66 'post_desc': self.describe_node(viewer, self.node.parent)
\r
69 class ReviseAction(NodeEditAction):
\r
72 def process_data(self, **data):
\r
73 revision_data = self.create_revision_data(**data)
\r
74 revision = self.node.create_revision(self.user, **revision_data)
\r
75 self.extra = revision.revision
\r
77 def process_action(self):
\r
78 self.node.last_edited = self
\r
81 def describe(self, viewer=None):
\r
82 return _("%(user)s edited %(post_desc)s") % {
\r
83 'user': self.hyperlink(self.user.get_profile_url(), self.friendly_username(viewer, self.user)),
\r
84 'post_desc': self.describe_node(viewer, self.node)
\r
87 def get_absolute_url(self):
\r
88 return self.node.get_revisions_url()
\r
90 class RetagAction(ActionProxy):
\r
91 verb = _("retagged")
\r
93 def process_data(self, tagnames=''):
\r
94 active = self.node.active_revision
\r
95 revision_data = dict(summary=_('Retag'), title=active.title, tagnames=strip_tags(tagnames.strip()), body=active.body)
\r
96 revision = self.node.create_revision(self.user, **revision_data)
\r
97 self.extra = revision.revision
\r
99 def process_action(self):
\r
100 self.node.last_edited = self
\r
103 def describe(self, viewer=None):
\r
104 return _("%(user)s retagged %(post_desc)s") % {
\r
105 'user': self.hyperlink(self.user.get_profile_url(), self.friendly_username(viewer, self.user)),
\r
106 'post_desc': self.describe_node(viewer, self.node)
\r
109 def get_absolute_url(self):
\r
110 return self.node.get_revisions_url()
\r
112 class RollbackAction(ActionProxy):
\r
113 verb = _("reverted")
\r
115 def process_data(self, activate=None):
\r
116 previous = self.node.active_revision
\r
117 self.node.activate_revision(self.user, activate)
\r
118 self.extra = "%d:%d" % (previous.revision, activate.revision)
\r
120 def process_action(self):
\r
121 self.node.last_edited = self
\r
124 def describe(self, viewer=None):
\r
125 revisions = [NodeRevision.objects.get(node=self.node, revision=int(n)) for n in self.extra.split(':')]
\r
127 return _("%(user)s reverted %(post_desc)s from revision %(initial)d (%(initial_sum)s) to revision %(final)d (%(final_sum)s)") % {
\r
128 'user': self.hyperlink(self.user.get_profile_url(), self.friendly_username(viewer, self.user)),
\r
129 'post_desc': self.describe_node(viewer, self.node),
\r
130 'initial': revisions[0].revision, 'initial_sum': revisions[0].summary,
\r
131 'final': revisions[1].revision, 'final_sum': revisions[1].summary,
\r
134 def get_absolute_url(self):
\r
135 return self.node.get_revisions_url()
\r
137 class CloseAction(ActionProxy):
\r
140 def process_action(self):
\r
141 self.node.marked = True
\r
142 self.node.nstate.closed = self
\r
143 self.node.last_edited = self
\r
144 self.node.update_last_activity(self.user, save=True)
\r
146 def cancel_action(self):
\r
147 self.node.marked = False
\r
148 self.node.nstate.closed = None
\r
149 self.node.update_last_activity(self.user, save=True)
\r
151 def describe(self, viewer=None):
\r
152 return _("%(user)s closed %(post_desc)s: %(reason)s") % {
\r
153 'user': self.hyperlink(self.user.get_profile_url(), self.friendly_username(viewer, self.user)),
\r
154 'post_desc': self.describe_node(viewer, self.node),
\r
155 'reason': self.extra
\r
158 class AnswerToCommentAction(ActionProxy):
\r
159 verb = _("converted")
\r
161 def process_data(self, new_parent=None):
\r
162 self.node.parent = new_parent
\r
163 self.node.node_type = "comment"
\r
165 for comment in self.node.comments.all():
\r
166 comment.parent = new_parent
\r
169 self.node.last_edited = self
\r
170 self.node.update_last_activity(self.user, save=True)
\r
172 self.node.abs_parent.reset_answer_count_cache()
\r
173 except AttributeError:
\r
176 def describe(self, viewer=None):
\r
177 return _("%(user)s converted an answer to %(question)s into a comment") % {
\r
178 'user': self.hyperlink(self.user.get_profile_url(), self.friendly_username(viewer, self.user)),
\r
179 'question': self.describe_node(viewer, self.node.abs_parent),
\r
182 class AnswerToQuestionAction(ActionProxy):
\r
183 verb = _("converted to question")
\r
185 def process_data(self, title):
\r
186 self.node.node_type = "question"
\r
187 self.node.title = title
\r
188 self.node.last_edited = self
\r
189 self.node.update_last_activity(self.user, save=True)
\r
192 self.node.abs_parent.reset_answer_count_cache()
\r
193 except AttributeError:
\r
197 def describe(self, viewer=None):
\r
198 return _("%(user)s converted an answer to %(question)s into a separate question") % {
\r
199 'user': self.hyperlink(self.user.get_profile_url(), self.friendly_username(viewer, self.user)),
\r
200 'question': self.describe_node(viewer, self.node.abs_parent),
\r
203 class WikifyAction(ActionProxy):
\r
204 verb = _("wikified")
\r
206 def process_action(self):
\r
207 self.node.nstate.wiki = self
\r
208 self.node.last_edited = self
\r
209 self.node.update_last_activity(self.user, save=True)
\r
211 def cancel_action(self):
\r
212 self.node.nstate.wiki = None
\r
213 self.node.update_last_activity(self.user, save=True)
\r
215 def describe(self, viewer=None):
\r
216 return _("%(user)s marked %(node)s as community wiki.") % {
\r
217 'user': self.hyperlink(self.user.get_profile_url(), self.friendly_username(viewer, self.user)),
\r
218 'node': self.describe_node(viewer, self.node),
\r