]> git.openstreetmap.org Git - osqa.git/blob - forum/actions/node.py
5283a8e98587feff199b64a2eed8c343e775dcf1
[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     verb = _("asked")\r
20 \r
21     def process_data(self, **data):\r
22         question = Question(author=self.user, **self.create_revision_data(True, **data))\r
23         question.save()\r
24         self.node = question\r
25 \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
30         }\r
31 \r
32 class AnswerAction(NodeEditAction):\r
33     verb = _("answered")\r
34 \r
35     def process_data(self, **data):\r
36         answer = Answer(author=self.user, parent=data['question'], **self.create_revision_data(True, **data))\r
37         answer.save()\r
38         self.node = answer\r
39 \r
40     def process_action(self):\r
41         self.node.question.reset_answer_count_cache()\r
42 \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
49         }\r
50 \r
51 class CommentAction(ActionProxy):\r
52     verb = _("commented")\r
53 \r
54     def process_data(self, text='', parent=None):\r
55         comment = Comment(author=self.user, parent=parent, body=text)\r
56         comment.save()\r
57         self.node = comment\r
58 \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
63         }\r
64 \r
65 class ReviseAction(NodeEditAction):\r
66     verb = _("edited")\r
67 \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
72 \r
73     def process_action(self):\r
74         self.node.last_edited = self\r
75         self.node.save()\r
76 \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
81         }\r
82 \r
83     def get_absolute_url(self):\r
84         return self.node.get_revisions_url()\r
85 \r
86 class RetagAction(ActionProxy):\r
87     verb = _("retagged")\r
88 \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
94 \r
95     def process_action(self):\r
96         self.node.last_edited = self\r
97         self.node.save()\r
98 \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
103         }\r
104 \r
105     def get_absolute_url(self):\r
106         return self.node.get_revisions_url()\r
107 \r
108 class RollbackAction(ActionProxy):\r
109     verb = _("reverted")\r
110 \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
115 \r
116     def process_action(self):\r
117         self.node.last_edited = self\r
118         self.node.save()\r
119 \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
122 \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
128         }\r
129 \r
130     def get_absolute_url(self):\r
131         return self.node.get_revisions_url()\r
132 \r
133 class CloseAction(ActionProxy):\r
134     verb = _("closed")\r
135 \r
136     def process_action(self):\r
137         self.node.extra_action = self\r
138         self.node.marked = True\r
139         self.node.update_last_activity(self.user, save=True)\r
140 \r
141     def cancel_action(self):\r
142         self.node.extra_action = None\r
143         self.node.marked = False\r
144         self.node.update_last_activity(self.user, save=True)\r
145 \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
151         }\r
152 \r
153 class AnswerToCommentAction(ActionProxy):\r
154     verb = _("converted")\r
155 \r
156     def process_data(self, new_parent=None):\r
157         self.node.parent = new_parent\r
158         self.node.node_type = "comment"\r
159 \r
160         for comment in self.node.comments.all():\r
161             comment.parent = new_parent\r
162             comment.save()\r
163 \r
164         self.node.save()\r
165         try:\r
166             self.node.abs_parent.reset_answer_count_cache()\r
167         except AttributeError:\r
168             pass\r
169 \r
170     def describe(self, viewer=None):\r
171         return _("%(user)s converted an answer to %(question)s into a comment") % {\r
172             'user': self.hyperlink(self.user.get_profile_url(), self.friendly_username(viewer, self.user)),\r
173             'question': self.describe_node(viewer, self.node.abs_parent),\r
174         }\r
175 \r