]> git.openstreetmap.org Git - osqa.git/blob - forum_modules/default_badges/badges.py
Fixes OSQA 282, Error in related_questions.
[osqa.git] / forum_modules / default_badges / badges.py
1 from datetime import datetime, timedelta
2 from django.utils.translation import ugettext as _
3 from forum.badges.base import AbstractBadge
4 from forum.models import Badge
5 from forum.actions import *
6 from forum.models import Vote, Flag
7
8 import settings
9
10 class QuestionViewBadge(AbstractBadge):
11     abstract = True
12     listen_to = (QuestionViewAction,)
13
14     @property
15     def description(self):
16         return _('Asked a question with %s views') % str(self.nviews)
17
18     def award_to(self, action):
19         if action.node.extra_count == int(self.nviews):
20             return action.node.author
21
22
23 class PopularQuestion(QuestionViewBadge):
24     name = _('Popular Question')
25     nviews = settings.POPULAR_QUESTION_VIEWS
26
27
28 class NotableQuestion(QuestionViewBadge):
29     type = Badge.SILVER
30     name = _('Notable Question')
31     nviews = settings.NOTABLE_QUESTION_VIEWS
32
33 class FamousQuestion(QuestionViewBadge):
34     type = Badge.GOLD
35     name = _('Famous Question')
36     nviews = settings.FAMOUS_QUESTION_VIEWS
37
38
39
40
41 class NodeScoreBadge(AbstractBadge):
42     abstract = True
43     listen_to = (VoteAction,)
44
45     @property
46     def description(self):
47         return _('Answer voted up %s times') % str(self.expected_score)
48
49     def award_to(self, action):
50         if (action.node.node_type == self.node_type) and (action.node.score == int(self.expected_score)):
51             return action.node.author
52             
53
54 class QuestionScoreBadge(NodeScoreBadge):
55     abstract = True
56     node_type = "question"
57
58 class NiceQuestion(QuestionScoreBadge):
59     expected_score = settings.NICE_QUESTION_VOTES_UP
60     name = _("Nice Question")
61
62 class GoodQuestion(QuestionScoreBadge):
63     type = Badge.SILVER
64     expected_score = settings.GOOD_QUESTION_VOTES_UP
65     name = _("Good Question")
66
67 class GreatQuestion(QuestionScoreBadge):
68     type = Badge.GOLD
69     expected_score = settings.GREAT_QUESTION_VOTES_UP
70     name = _("Great Question")
71
72
73 class AnswerScoreBadge(NodeScoreBadge):
74     abstract = True
75     node_type = "answer"
76
77 class NiceAnswer(AnswerScoreBadge):
78     expected_score = settings.NICE_ANSWER_VOTES_UP
79     name = _("Nice Answer")
80
81 class GoodAnswer(AnswerScoreBadge):
82     type = Badge.SILVER
83     expected_score = settings.GOOD_ANSWER_VOTES_UP
84     name = _("Good Answer")
85
86 class GreatAnswer(AnswerScoreBadge):
87     type = Badge.GOLD
88     expected_score = settings.GREAT_ANSWER_VOTES_UP
89     name = _("Great Answer")
90
91
92
93 class FavoriteQuestionBadge(AbstractBadge):
94     abstract = True
95     listen_to = (FavoriteAction,)
96
97     @property
98     def description(self):
99         return _('Question favorited by %s users') % str(self.expected_count)
100
101     def award_to(self, action):
102         if (action.node.node_type == "question") and (action.node.favorite_count == int(self.expected_count)):
103             return action.node.author
104
105 class FavoriteQuestion(FavoriteQuestionBadge):
106     type = Badge.SILVER
107     name = _("Favorite Question")
108     expected_count = settings.FAVORITE_QUESTION_FAVS
109
110 class StellarQuestion(FavoriteQuestionBadge):
111     type = Badge.GOLD
112     name = _("Stellar Question")
113     expected_count = settings.STELLAR_QUESTION_FAVS
114
115
116
117 class Disciplined(AbstractBadge):
118     listen_to = (DeleteAction,)
119     name = _("Disciplined")
120     description = _('Deleted own post with score of %s or higher') % settings.DISCIPLINED_MIN_SCORE
121
122     def award_to(self, action):
123         if (action.node.author == action.user) and (action.node.score >= int(settings.DISCIPLINED_MIN_SCORE)):
124             return action.user
125
126 class PeerPressure(AbstractBadge):
127     listen_to = (DeleteAction,)
128     name = _("Peer Pressure")
129     description = _('Deleted own post with score of %s or lower') % settings.PEER_PRESSURE_MAX_SCORE
130
131     def award_to(self, action):
132         if (action.node.author == action.user) and (action.node.score <= int(settings.PEER_PRESSURE_MAX_SCORE)):
133             return action.user
134
135
136 class Critic(AbstractBadge):
137     award_once = True
138     listen_to = (VoteDownAction,)
139     name = _("Critic")
140     description = _('First down vote')
141
142     def award_to(self, action):
143         if (action.user.vote_down_count == 1):
144             return action.user
145
146
147 class Supporter(AbstractBadge):
148     award_once = True
149     listen_to = (VoteUpAction,)
150     name = _("Supporter")
151     description = _('First up vote')
152
153     def award_to(self, action):
154         if (action.user.vote_up_count == 1):
155             return action.user
156
157
158 class FirstActionBadge(AbstractBadge):
159     award_once = True
160     abstract = True
161     
162     def award_to(self, action):
163         if (self.listen_to[0].objects.filter(user=action.user).count() == 1):
164             return action.user
165
166 class CitizenPatrol(FirstActionBadge):
167     listen_to = (FlagAction,)
168     name = _("Citizen Patrol")
169     description = _('First flagged post')
170
171 class Organizer(FirstActionBadge):
172     listen_to = (RetagAction,)
173     name = _("Organizer")
174     description = _('First retag')
175
176 class Editor(FirstActionBadge):
177     listen_to = (ReviseAction,)
178     name = _("Editor")
179     description = _('First edit')
180
181 class Scholar(FirstActionBadge):
182     listen_to = (AcceptAnswerAction,)
183     name = _("Scholar")
184     description = _('First accepted answer on your own question')
185
186 class Cleanup(FirstActionBadge):
187     listen_to = (RollbackAction,)
188     name = _("Cleanup")
189     description = _('First rollback')
190
191
192 class Autobiographer(AbstractBadge):
193     award_once = True
194     listen_to = (EditProfileAction,)
195     name = _("Autobiographer")
196     description = _('Completed all user profile fields')
197
198     def award_to(self, action):
199         user = action.user
200         if user.email and user.real_name and user.website and user.location and \
201                 user.date_of_birth and user.about:
202             return user
203
204
205
206 class CivicDuty(AbstractBadge):
207     type = Badge.SILVER
208     award_once = True
209     listen_to = (VoteUpAction, VoteDownAction)
210     name = _("Civic Duty")
211     description = _('Voted %s times') % settings.CIVIC_DUTY_VOTES
212
213     def award_to(self, action):
214         if (action.user.vote_up_count + action.user.vote_down_count) == int(settings.CIVIC_DUTY_VOTES):
215             return action.user
216
217
218 class Pundit(AbstractBadge):
219     award_once = True
220     listen_to = (CommentAction,)
221     name = _("Pundit")
222     description = _('Left %s comments') % settings.PUNDIT_COMMENT_COUNT
223
224     def award_to(self, action):
225         if (action.user.nodes.filter(node_type="comment", deleted=None)) == int(settings.CIVIC_DUTY_VOTES):
226             return action.user
227
228
229 class SelfLearner(AbstractBadge):
230     listen_to = (VoteUpAction, )
231     name = _("Self Learner")
232     description = _('Answered your own question with at least %s up votes') % settings.SELF_LEARNER_UP_VOTES
233
234     def award_to(self, action):
235         if (action.node.node_type == "answer") and (action.node.author == action.node.parent.author) and (
236             action.node.score == int(settings.SELF_LEARNER_UP_VOTES)):
237             return action.node.author
238
239
240 class StrunkAndWhite(AbstractBadge):
241     type = Badge.SILVER
242     award_once = True
243     listen_to = (ReviseAction,)
244     name = _("Strunk & White")
245     description = _('Edited %s entries') % settings.STRUNK_AND_WHITE_EDITS
246
247     def award_to(self, action):
248         if (ReviseAction.objects.filter(user=action.user).count() == int(settings.STRUNK_AND_WHITE_EDITS)):
249             return action.user
250
251
252 class Student(AbstractBadge):
253     award_once = True
254     listen_to = (VoteUpAction,)
255     name = _("Student")
256     description = _('Asked first question with at least one up vote')
257
258     def award_to(self, action):
259         if (action.node.node_type == "question") and (action.node.author.nodes.filter_state(deleted=False).filter(node_type="question", score=1).count() == 1):
260             return action.node.author
261
262
263 class Teacher(AbstractBadge):
264     award_once = True
265     listen_to = (VoteUpAction,)
266     name = _("Teacher")
267     description = _('Answered first question with at least one up vote')
268
269     def award_to(self, action):
270         if (action.node.node_type == "answer") and (action.node.author.nodes.filter_state(deleted=False).filter(node_type="answer", score=1).count() == 1):
271             return action.node.author
272
273
274 class Enlightened(AbstractBadge):
275     type = Badge.SILVER
276     award_once = True
277     listen_to = (VoteUpAction, AcceptAnswerAction)
278     name = _("Enlightened")
279     description = _('First answer was accepted with at least %s up votes') % settings.ENLIGHTENED_UP_VOTES
280
281     def award_to(self, action):
282         if (action.node.node_type == "answer") and (action.node.accepted) and (
283             action.node.score >= int(settings.ENLIGHTENED_UP_VOTES)):
284             return action.node.author
285
286
287 class Guru(AbstractBadge):
288     type = Badge.SILVER
289     listen_to = (VoteUpAction, AcceptAnswerAction)
290     name = _("Guru")
291     description = _('Accepted answer and voted up %s times') % settings.GURU_UP_VOTES
292
293     def award_to(self, action):
294         if (action.node.node_type == "answer") and (action.node.accepted) and (
295             action.node.score >= int(settings.ENLIGHTENED_UP_VOTES)):
296             return action.node.author
297
298
299 class Necromancer(AbstractBadge):
300     type = Badge.SILVER
301     listen_to = (VoteUpAction,)
302     name = _("Necromancer")
303     description = _('Answered a question more than %(dif_days)s days later with at least %(up_votes)s votes') % \
304             {'dif_days': settings.NECROMANCER_DIF_DAYS, 'up_votes': settings.NECROMANCER_UP_VOTES}
305
306     def award_to(self, action):
307         if (action.node.node_type == "answer") and (
308             action.node.added_at >= (action.node.question.added_at + timedelta(days=int(settings.NECROMANCER_DIF_DAYS)))):
309             return action.node.author
310
311 class Taxonomist(AbstractBadge):
312     type = Badge.SILVER
313     listen_to = tuple()
314     name = _("Taxonomist")
315     description = _('Created a tag used by %s questions') % settings.TAXONOMIST_USE_COUNT
316
317     def award_to(self, action):
318         return None
319