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
19 def process_data(self, **data):
\r
20 question = Question(author=self.user, **self.create_revision_data(True, **data))
\r
22 self.node = question
\r
24 def describe(self, viewer=None):
\r
25 return _("%(user)s asked %(question)s") % {
\r
26 'user': self.hyperlink(self.user.get_profile_url(), self.friendly_username(viewer, self.user)),
\r
27 'question': self.hyperlink(self.node.get_absolute_url(), self.node.title)
\r
30 class AnswerAction(NodeEditAction):
\r
31 def process_data(self, **data):
\r
32 answer = Answer(author=self.user, parent=data['question'], **self.create_revision_data(True, **data))
\r
36 def process_action(self):
\r
37 self.node.question.reset_answer_count_cache()
\r
39 def describe(self, viewer=None):
\r
40 question = self.node.parent
\r
41 return _("%(user)s answered %(asker)s %(question)s") % {
\r
42 'user': self.hyperlink(self.user.get_profile_url(), self.friendly_username(viewer, self.user)),
\r
43 'asker': self.hyperlink(question.author.get_profile_url(), self.friendly_username(viewer, question.author)),
\r
44 'question': self.hyperlink(self.node.get_absolute_url(), question.title)
\r
47 class CommentAction(ActionProxy):
\r
48 def process_data(self, text='', parent=None):
\r
49 comment = Comment(author=self.user, parent=parent, body=text)
\r
53 def describe(self, viewer=None):
\r
54 return _("%(user)s commented on %(post_desc)s") % {
\r
55 'user': self.hyperlink(self.node.author.get_profile_url(), self.friendly_username(viewer, self.node.author)),
\r
56 'post_desc': self.describe_node(viewer, self.node.parent)
\r
59 class ReviseAction(NodeEditAction):
\r
60 def process_data(self, **data):
\r
61 revision_data = self.create_revision_data(**data)
\r
62 revision = self.node.create_revision(self.user, action=self, **revision_data)
\r
63 self.extra = revision.revision
\r
65 def describe(self, viewer=None):
\r
66 return _("%(user)s edited %(post_desc)s") % {
\r
67 'user': self.hyperlink(self.user.get_profile_url(), self.friendly_username(viewer, self.user)),
\r
68 'post_desc': self.describe_node(viewer, self.node)
\r
71 class RetagAction(ActionProxy):
\r
72 def process_data(self, tagnames=''):
\r
73 active = self.node.active_revision
\r
74 revision_data = dict(summary=_('Retag'), title=active.title, tagnames=strip_tags(tagnames.strip()), body=active.body)
\r
75 self.node.create_revision(self.user, action=self, **revision_data)
\r
77 def describe(self, viewer=None):
\r
78 return _("%(user)s retagged %(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 class RollbackAction(ActionProxy):
\r
84 def process_data(self, activate=None):
\r
85 previous = self.node.active_revision
\r
86 self.node.activate_revision(self.user, activate, self)
\r
87 self.extra = "%d:%d" % (previous.revision, activate.revision)
\r
89 def describe(self, viewer=None):
\r
90 revisions = [NodeRevision.objects.get(node=self.node, revision=int(n)) for n in self.extra.split(':')]
\r
92 return _("%(user)s reverted %(post_desc)s from revision %(initial)d (%(initial_sum)s) to revision %(final)d (%(final_sum)s)") % {
\r
93 'user': self.hyperlink(self.user.get_profile_url(), self.friendly_username(viewer, self.user)),
\r
94 'post_desc': self.describe_node(viewer, self.node),
\r
95 'initial': revisions[0].revision, 'initial_sum': revisions[0].summary,
\r
96 'final': revisions[1].revision, 'final_sum': revisions[1].summary,
\r
99 class CloseAction(ActionProxy):
\r
100 def process_action(self):
\r
101 self.node.extra_action = self
\r
102 self.node.marked = True
\r
105 def cancel_action(self):
\r
106 self.node.extra_action = None
\r
107 self.node.marked = False
\r
110 def describe(self, viewer=None):
\r
111 return _("%(user)s closed %(post_desc)s: %(reason)s") % {
\r
112 'user': self.hyperlink(self.user.get_profile_url(), self.friendly_username(viewer, self.user)),
\r
113 'post_desc': self.describe_node(viewer, self.node),
\r
114 'reason': self.extra
\r