]> git.openstreetmap.org Git - osqa.git/blob - forum/actions/meta.py
Set a maximum with of 640px on images in the body of questions and answers.
[osqa.git] / forum / actions / meta.py
1 from django.utils.translation import ugettext as _\r
2 from django.db.models import F\r
3 from forum.models.action import ActionProxy, DummyActionProxy\r
4 from forum.models import Vote, Flag\r
5 from forum import settings\r
6 \r
7 class VoteAction(ActionProxy):\r
8     def update_node_score(self, inc):\r
9         self.node.score = F('score') + inc\r
10         self.node.save()\r
11 \r
12     def process_vote_action(self, value):\r
13         self.update_node_score(value)\r
14         vote = Vote(node=self.node, user=self.user, action=self, value=value)\r
15         vote.save()\r
16 \r
17     def cancel_action(self):\r
18         vote = self.vote\r
19         self.update_node_score(-vote.value)\r
20         vote.delete()\r
21 \r
22     @classmethod\r
23     def get_for(cls, user, node):\r
24         try:\r
25             vote = Vote.objects.get(user=user, node=node)\r
26             return vote.value\r
27         except:\r
28             return None\r
29 \r
30     def describe_vote(self, vote_desc, viewer=None):\r
31         return _("%(user)s %(vote_desc)s %(post_desc)s") % {\r
32             'user': self.hyperlink(self.user.get_profile_url(), self.friendly_username(viewer, self.user)),\r
33             'vote_desc': vote_desc, 'post_desc': self.describe_node(viewer, self.node)\r
34         }\r
35 \r
36 \r
37 class VoteUpAction(VoteAction):\r
38     def repute_users(self):\r
39         self.repute(self.node.author, int(settings.REP_GAIN_BY_UPVOTED))\r
40 \r
41     def process_action(self):\r
42         self.process_vote_action(1)\r
43         self.user.reset_vote_up_count_cache()\r
44 \r
45     def cancel_action(self):\r
46         super(VoteUpAction, self).cancel_action()\r
47         self.user.reset_vote_up_count_cache()\r
48 \r
49     def describe(self, viewer=None):\r
50         return self.describe_vote(_("voted up"), viewer)\r
51 \r
52 class VoteDownAction(VoteAction):\r
53     def repute_users(self):\r
54         self.repute(self.node.author, -int(settings.REP_LOST_BY_DOWNVOTED))\r
55         self.repute(self.user, -int(settings.REP_LOST_BY_DOWNVOTING))\r
56 \r
57     def process_action(self):\r
58         self.process_vote_action(-1)\r
59         self.user.reset_vote_down_count_cache()\r
60 \r
61     def cancel_action(self):\r
62         super(VoteDownAction, self).cancel_action()\r
63         self.user.reset_vote_down_count_cache()\r
64 \r
65     def describe(self, viewer=None):\r
66         return self.describe_vote(_("voted down"), viewer)\r
67 \r
68 \r
69 class VoteUpCommentAction(VoteUpAction):\r
70     def repute_users(self):\r
71         pass\r
72 \r
73     def process_action(self):\r
74         self.process_vote_action(1)\r
75 \r
76     def cancel_action(self):\r
77         super(VoteUpAction, self).cancel_action()\r
78 \r
79     def describe(self, viewer=None):\r
80         return self.describe_vote(_("liked"), viewer)\r
81 \r
82 \r
83 class FlagAction(ActionProxy):\r
84     def repute_users(self):\r
85         self.repute(self.node.author, -int(settings.REP_LOST_BY_FLAGGED))\r
86 \r
87     def process_action(self):\r
88         flag = Flag(user=self.user, node=self.node, action=self, reason=self.extra)\r
89         flag.save()\r
90         self.node.reset_flag_count_cache()\r
91 \r
92         if self.node.flag_count == int(settings.FLAG_COUNT_TO_HIDE_POST):\r
93             self.repute(self.node.author, -int(settings.REP_LOST_BY_FLAGGED_3_TIMES))\r
94 \r
95         if self.node.flag_count == int(settings.FLAG_COUNT_TO_DELETE_POST):\r
96             self.repute(self.node.author, -int(settings.REP_LOST_BY_FLAGGED_5_TIMES))\r
97             if not self.node.deleted:\r
98                 DeleteAction(node=self.node, user=self.user, extra="BYFLAGGED").save()\r
99 \r
100     def cancel_action(self):\r
101         self.flag.delete()\r
102         self.node.reset_flag_count_cache()\r
103 \r
104     @classmethod\r
105     def get_for(cls, user, node):\r
106         try:\r
107             flag = Flag.objects.get(user=user, node=node)\r
108             return flag.reason or _("No reason given")\r
109         except:\r
110             return None\r
111 \r
112     def describe(self, viewer=None):\r
113         return _("%(user)s flagged %(post_desc)s: %(reason)s") % {\r
114             'user': self.hyperlink(self.user.get_profile_url(), self.friendly_username(viewer, self.user)),\r
115             'post_desc': self.describe_node(viewer, self.node), 'reason': self.extra\r
116         }\r
117 \r
118 \r
119 class AcceptAnswerAction(ActionProxy):\r
120     def repute_users(self):\r
121         if (self.user == self.node.parent.author) and (not self.user == self.node.author):\r
122             self.repute(self.user, int(settings.REP_GAIN_BY_ACCEPTING))\r
123 \r
124         if self.user != self.node.author:\r
125             self.repute(self.node.author, int(settings.REP_GAIN_BY_ACCEPTED))\r
126 \r
127     def process_action(self):\r
128         self.node.parent.extra_ref = self.node\r
129         self.node.parent.save()\r
130         self.node.marked = True\r
131         self.node.extra_action = self\r
132         self.node.save()\r
133 \r
134     def cancel_action(self):\r
135         self.node.parent.extra_ref = None\r
136         self.node.parent.save()\r
137         self.node.marked = False\r
138         self.node.extra_action = None\r
139         self.node.save()\r
140 \r
141     def describe(self, viewer=None):\r
142         answer = self.node\r
143         question = answer.parent\r
144 \r
145         if self.user == question.author:\r
146             asker = (self.user == viewer) and _("your") or _("his")\r
147         else:\r
148             asker = self.hyperlink(question.author.get_profile_url(), question.author.username)\r
149 \r
150         return _("%(user)s accepted %(answerer)s answer on %(asker)s question %(question)s") % {\r
151             'user': self.hyperlink(self.user.get_profile_url(), self.friendly_username(viewer, self.user)),\r
152             'answerer': self.hyperlink(answer.author.get_profile_url(), self.friendly_username(viewer, answer.author)),\r
153             'asker': asker,\r
154             'question': self.hyperlink(question.get_absolute_url(), question.title)\r
155         }\r
156 \r
157 \r
158 class FavoriteAction(ActionProxy):\r
159     def process_action(self):\r
160         self.node.reset_favorite_count_cache()\r
161 \r
162     def cancel_action(self):\r
163         self.process_action()\r
164 \r
165     def describe(self, viewer=None):\r
166         return _("%(user)s marked %(post_desc)s as favorite") % {\r
167             'user': self.hyperlink(self.user.get_profile_url(), self.friendly_username(viewer, self.user)),\r
168             'post_desc': self.describe_node(viewer, self.node),\r
169         }\r
170 \r
171 \r
172 class DeleteAction(ActionProxy):\r
173     def process_action(self):\r
174         self.node.deleted = self\r
175         self.node.save()\r
176         \r
177         if self.node.node_type == "answer":\r
178             self.node.question.reset_answer_count_cache()\r
179 \r
180     def cancel_action(self):\r
181         self.node.deleted = None\r
182         self.node.save()\r
183 \r
184         if self.node.node_type == "answer":\r
185             self.node.question.reset_answer_count_cache()\r
186 \r
187     def describe(self, viewer=None):\r
188         return _("%(user)s deleted %(post_desc)s: %(reason)s") % {\r
189             'user': self.hyperlink(self.user.get_profile_url(), self.friendly_username(viewer, self.user)),\r
190             'post_desc': self.describe_node(viewer, self.node), 'reason': self.reason(),\r
191         }\r
192 \r
193     def reason(self):\r
194         if self.extra != "BYFLAGGED":\r
195             return self.extra\r
196         else:\r
197             return _("flagged by multiple users: ") + "; ".join([f.extra for f in FlagAction.objects.filter(node=self.node)])\r
198 \r
199 \r
200 class QuestionViewAction(DummyActionProxy):\r
201     def __init__(self, node, user, ip=None):\r
202         self.viewuser = user\r
203         self.node = node\r
204         super(QuestionViewAction, self).__init__(ip)\r
205 \r
206     def process_action(self):\r
207         self.node.extra_count = F('extra_count') + 1\r
208         self.node.save()\r