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 question = Question(author=self.user, **self.create_revision_data(True, **data))
\r
24 self.node = question
\r
26 def describe(self, viewer=None):
\r
27 return _("%(user)s asked %(question)s") % {
\r
28 'user': self.hyperlink(self.user.get_profile_url(), self.friendly_username(viewer, self.user)),
\r
29 'question': self.hyperlink(self.node.get_absolute_url(), self.node.title)
\r
32 class AnswerAction(NodeEditAction):
\r
33 verb = _("answered")
\r
35 def process_data(self, **data):
\r
36 answer = Answer(author=self.user, parent=data['question'], **self.create_revision_data(True, **data))
\r
40 def process_action(self):
\r
41 self.node.question.reset_answer_count_cache()
\r
43 def describe(self, viewer=None):
\r
44 question = self.node.parent
\r
45 return _("%(user)s answered %(asker)s %(question)s") % {
\r
46 'user': self.hyperlink(self.user.get_profile_url(), self.friendly_username(viewer, self.user)),
\r
47 'asker': self.hyperlink(question.author.get_profile_url(), self.friendly_username(viewer, question.author)),
\r
48 'question': self.hyperlink(self.node.get_absolute_url(), question.title)
\r
51 class CommentAction(ActionProxy):
\r
52 verb = _("commented")
\r
54 def process_data(self, text='', parent=None):
\r
55 comment = Comment(author=self.user, parent=parent, body=text)
\r
59 def describe(self, viewer=None):
\r
60 return _("%(user)s commented on %(post_desc)s") % {
\r
61 'user': self.hyperlink(self.node.author.get_profile_url(), self.friendly_username(viewer, self.node.author)),
\r
62 'post_desc': self.describe_node(viewer, self.node.parent)
\r
65 class ReviseAction(NodeEditAction):
\r
68 def process_data(self, **data):
\r
69 revision_data = self.create_revision_data(**data)
\r
70 revision = self.node.create_revision(self.user, **revision_data)
\r
71 self.extra = revision.revision
\r
73 def process_action(self):
\r
74 self.node.last_edited = self
\r
77 def describe(self, viewer=None):
\r
78 return _("%(user)s edited %(post_desc)s") % {
\r
79 'user': self.hyperlink(self.user.get_profile_url(), self.friendly_username(viewer, self.user)),
\r
80 'post_desc': self.describe_node(viewer, self.node)
\r
83 def get_absolute_url(self):
\r
84 return self.node.get_revisions_url()
\r
86 class RetagAction(ActionProxy):
\r
87 verb = _("retagged")
\r
89 def process_data(self, tagnames=''):
\r
90 active = self.node.active_revision
\r
91 revision_data = dict(summary=_('Retag'), title=active.title, tagnames=strip_tags(tagnames.strip()), body=active.body)
\r
92 revision = self.node.create_revision(self.user, **revision_data)
\r
93 self.extra = revision.revision
\r
95 def process_action(self):
\r
96 self.node.last_edited = self
\r
99 def describe(self, viewer=None):
\r
100 return _("%(user)s retagged %(post_desc)s") % {
\r
101 'user': self.hyperlink(self.user.get_profile_url(), self.friendly_username(viewer, self.user)),
\r
102 'post_desc': self.describe_node(viewer, self.node)
\r
105 def get_absolute_url(self):
\r
106 return self.node.get_revisions_url()
\r
108 class RollbackAction(ActionProxy):
\r
109 verb = _("reverted")
\r
111 def process_data(self, activate=None):
\r
112 previous = self.node.active_revision
\r
113 self.node.activate_revision(self.user, activate)
\r
114 self.extra = "%d:%d" % (previous.revision, activate.revision)
\r
116 def process_action(self):
\r
117 self.node.last_edited = self
\r
120 def describe(self, viewer=None):
\r
121 revisions = [NodeRevision.objects.get(node=self.node, revision=int(n)) for n in self.extra.split(':')]
\r
123 return _("%(user)s reverted %(post_desc)s from revision %(initial)d (%(initial_sum)s) to revision %(final)d (%(final_sum)s)") % {
\r
124 'user': self.hyperlink(self.user.get_profile_url(), self.friendly_username(viewer, self.user)),
\r
125 'post_desc': self.describe_node(viewer, self.node),
\r
126 'initial': revisions[0].revision, 'initial_sum': revisions[0].summary,
\r
127 'final': revisions[1].revision, 'final_sum': revisions[1].summary,
\r
130 def get_absolute_url(self):
\r
131 return self.node.get_revisions_url()
\r
133 class CloseAction(ActionProxy):
\r
136 def process_action(self):
\r
137 self.node.marked = True
\r
138 self.node.nstate.closed = self
\r
139 self.node.update_last_activity(self.user, save=True)
\r
141 def cancel_action(self):
\r
142 self.node.marked = False
\r
143 self.node.nstate.closed = None
\r
144 self.node.update_last_activity(self.user, save=True)
\r
146 def describe(self, viewer=None):
\r
147 return _("%(user)s closed %(post_desc)s: %(reason)s") % {
\r
148 'user': self.hyperlink(self.user.get_profile_url(), self.friendly_username(viewer, self.user)),
\r
149 'post_desc': self.describe_node(viewer, self.node),
\r
150 'reason': self.extra
\r
153 class AnswerToCommentAction(ActionProxy):
\r
154 verb = _("converted")
\r
156 def process_data(self, new_parent=None):
\r
157 self.node.parent = new_parent
\r
158 self.node.node_type = "comment"
\r
160 for comment in self.node.comments.all():
\r
161 comment.parent = new_parent
\r
164 self.node.last_edited = self
\r
165 self.node.update_last_activity(self.user, save=True)
\r
167 self.node.abs_parent.reset_answer_count_cache()
\r
168 except AttributeError:
\r
171 def describe(self, viewer=None):
\r
172 return _("%(user)s converted an answer to %(question)s into a comment") % {
\r
173 'user': self.hyperlink(self.user.get_profile_url(), self.friendly_username(viewer, self.user)),
\r
174 'question': self.describe_node(viewer, self.node.abs_parent),
\r
177 class WikifyAction(ActionProxy):
\r
178 verb = _("wikified")
\r
180 def process_action(self):
\r
181 self.node.nstate.wiki = self
\r
182 self.node.last_edited = self
\r
183 self.node.update_last_activity(self.user, save=True)
\r
185 def cancel_action(self):
\r
186 self.node.nstate.wiki = None
\r
187 self.node.update_last_activity(self.user, save=True)
\r
189 def describe(self, viewer=None):
\r
190 return _("%(user)s marked %(node)s as community wiky.") % {
\r
191 'user': self.hyperlink(self.user.get_profile_url(), self.friendly_username(viewer, self.user)),
\r
192 'node': self.describe_node(viewer, self.node),
\r