]> git.openstreetmap.org Git - osqa.git/blob - forum/actions/node.py
Fixed http://jira.osqa.net/browse/OSQA-216.
[osqa.git] / forum / actions / node.py
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
5 \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
9 \r
10         if data.get('title', None):\r
11             revision_data['title'] = strip_tags(data['title'].strip())\r
12 \r
13         if data.get('tags', None):\r
14             revision_data['tagnames'] = data['tags'].strip()\r
15 \r
16         return revision_data\r
17 \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
21         question.save()\r
22         self.node = question\r
23 \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
28         }\r
29 \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
33         answer.save()\r
34         self.node = answer\r
35 \r
36     def process_action(self):\r
37         self.node.question.reset_answer_count_cache()\r
38 \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
45         }\r
46 \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
50         comment.save()\r
51         self.node = comment\r
52 \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
57         }\r
58 \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
64 \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
69         }\r
70 \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
76 \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
81         }\r
82 \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
88 \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
91 \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
97         }\r
98 \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
103         self.node.save()\r
104 \r
105     def cancel_action(self):\r
106         self.node.extra_action = None\r
107         self.node.marked = False\r
108         self.node.save()\r
109 \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
115         }