1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
|
"""This file contains data on badges that is not stored in the database.
there are no django models in this file.
This data is static, so there is no point storing it in the db.
However, the database does have model BadgeData, that contains
additional mutable data pertaining to the badges - denormalized award counts
and lists of recipients.
BadgeData django model is located in askbot/models/repute.py
Badges in this file are connected with the contents of BadgeData
via key, determined as a slugified version of badge name.
To implement a new badge, one must create a subclass of Badge,
adde it to BADGES dictionary, register with event in EVENTS_TO_BADGES
and make sure that a signal `award_badges_signal` is sent with the
corresponding event name, actor (user object), context_object and optionally
- timestamp
"""
import datetime
from django.template.defaultfilters import slugify
from django.contrib.contenttypes.models import ContentType
from django.utils.translation import ugettext as _
from django.dispatch import Signal
from askbot.models.repute import BadgeData, Award
from askbot.models.user import Activity
from askbot.models.meta import Comment
from askbot.models.question import FavoriteQuestion as Fave#name collision
from askbot import const
from askbot.conf import settings as askbot_settings
from askbot.utils.decorators import auto_now_timestamp
class Badge(object):
"""base class for the badges
badges must implement method consider_award
which returns a boolean True if award succeds
and False otherwise
consider_award assumes that the function is called
upon correct event, i.e. it is the responsibility of
the caller to try awarding badges at appropriate times
"""
def __init__(self,
key = '',
name = '',
level = None,
description = None,
multiple = False):
#key - must be an ASCII only word
self.key = key
self.name = name
self.level = level
self.description = description
self.multiple = multiple
self.css_class = const.BADGE_CSS_CLASSES[self.level]
def get_stored_data(self):
data, created = BadgeData.objects.get_or_create(slug = self.key)
return data
@property
def awarded_count(self):
return self.get_stored_data().awarded_count
@property
def awarded_to(self):
return self.get_stored_data().awarded_to
@property
def award_badge(self):
"""related name from `askbot.models.Award`
the name of this property is confusing, but for now
left in sync with the name on the `Award` model
the goal is that any badge recalled from this
module would behave just like the instance of BadgeData
and vice versa
"""
return self.get_stored_data().award_badge
def get_level_display(self):
"""display name for the level of the badge"""
return dict(const.BADGE_TYPE_CHOICES).get(self.level)
def award(self, recipient = None, context_object = None, timestamp = None):
"""do award, the recipient was proven to deserve"""
if self.multiple == False:
if recipient.badges.filter(slug = self.key).count() != 0:
return False
else:
content_type = ContentType.objects.get_for_model(context_object)
filters = {
'user': recipient,
'object_id': context_object.id,
'content_type': content_type,
'badge__slug': self.key,
}
#multiple badge is not re-awarded for the same post
if Award.objects.filter(**filters).count() != 0:
return False
badge = self.get_stored_data()
award = Award(
user = recipient,
badge = badge,
awarded_at = timestamp,
content_object = context_object
)
award.save()#note: there are signals that listen to saving the Award
return True
def consider_award(self, actor = None,
context_object = None, timestamp = None):
"""Normally this method should be reimplemented
in subclass, but some badges are awarded without
checks. Those do no need to override this method
actor - user who committed some action, context_object -
the object related to the award situation, e.g. answer
"""
return self.award(actor, context_object, timestamp)
class Disciplined(Badge):
def __init__(self):
description = _(
'Deleted own post with %(votes)s or more upvotes'
) % {'votes': askbot_settings.DISCIPLINED_BADGE_MIN_UPVOTES}
super(Disciplined, self).__init__(
key = 'disciplined',
name = _('Disciplined'),
description = description,
level = const.BRONZE_BADGE,
multiple = True
)
def consider_award(self, actor = None,
context_object = None, timestamp = None):
if context_object.author != actor:
return False
if context_object.score >= \
askbot_settings.DISCIPLINED_BADGE_MIN_UPVOTES:
return self.award(actor, context_object, timestamp)
class PeerPressure(Badge):
def __init__(self):
description = _(
u'Deleted own post with %(votes)s or more downvotes'
) % {'votes': askbot_settings.PEER_PRESSURE_BADGE_MIN_DOWNVOTES}
super(PeerPressure, self).__init__(
key = 'peer-pressure',
name = _('Peer Pressure'),
description = description,
level = const.BRONZE_BADGE,
multiple = True
)
def consider_award(self, actor = None,
context_object = None, timestamp = None):
if context_object.author != actor:
return False
if context_object.score <= \
-1 * askbot_settings.PEER_PRESSURE_BADGE_MIN_DOWNVOTES:
return self.award(actor, context_object, timestamp)
return False
class Teacher(Badge):
def __init__(self):
description = _(
'Received at least %(votes)s upvote for an answer for the first time'
) % {'votes': askbot_settings.TEACHER_BADGE_MIN_UPVOTES}
super(Teacher, self).__init__(
key = 'teacher',
name = _('Teacher'),
description = description,
level = const.BRONZE_BADGE,
multiple = False
)
def consider_award(self, actor = None,
context_object = None, timestamp = None):
if context_object.post_type != 'answer':
return False
if context_object.score >= askbot_settings.TEACHER_BADGE_MIN_UPVOTES:
return self.award(context_object.author, context_object, timestamp)
return False
class FirstVote(Badge):
"""this badge is not awarded directly, but through
Supporter and Critic, which must provide
* key, name and description properties through __new__ call
"""
def __init__(self):
super(FirstVote, self).__init__(
key = self.key,
name = self.name,
description = self.description,
level = const.BRONZE_BADGE,
multiple = False
)
def consider_award(self, actor = None,
context_object = None, timestamp = None):
if context_object.post_type not in ('question', 'answer'):
return False
return self.award(actor, context_object, timestamp)
class Supporter(FirstVote):
"""first upvote"""
def __new__(cls):
self = super(Supporter, cls).__new__(cls)
self.key = 'supporter'
self.name = _('Supporter')
self.description = _('First upvote')
return self
class Critic(FirstVote):
"""like supporter, but for downvote"""
def __new__(cls):
self = super(Critic, cls).__new__(cls)
self.key = 'critic'
self.name = _('Critic')
self.description = _('First downvote')
return self
class CivicDuty(Badge):
"""awarded once after a certain number of votes"""
def __init__(self):
min_votes = askbot_settings.CIVIC_DUTY_BADGE_MIN_VOTES
super(CivicDuty, self).__init__(
key = 'civic-duty',
name = _('Civic Duty'),
description = _('Voted %(num)s times') % {'num': min_votes},
level = const.SILVER_BADGE,
multiple = False
)
def consider_award(self, actor = None,
context_object = None, timestamp = None):
if context_object.post_type not in ('question', 'answer'):
return False
if actor.votes.count() == askbot_settings.CIVIC_DUTY_BADGE_MIN_VOTES:
return self.award(actor, context_object, timestamp)
class SelfLearner(Badge):
def __init__(self):
description = _('Answered own question with at least %(num)s up votes')
min_votes = askbot_settings.SELF_LEARNER_BADGE_MIN_UPVOTES
super(SelfLearner, self).__init__(
key = 'self-learner',
name = _('Self-Learner'),
description = description % {'num': min_votes},
level = const.BRONZE_BADGE,
multiple = True
)
def consider_award(self, actor = None,
context_object = None, timestamp = None):
if context_object.post_type != 'answer':
return False
min_upvotes = askbot_settings.SELF_LEARNER_BADGE_MIN_UPVOTES
question = context_object.question
answer = context_object
if question.author == answer.author and answer.score >= min_upvotes:
self.award(context_object.author, context_object, timestamp)
class QualityPost(Badge):
"""Generic Badge for Nice/Good/Great Question or Answer
this badge is not used directly but is instantiated
via subclasses created via __new__() method definitions
The subclass has a responsibility to specify properties:
* min_votes - a value from live settings
* post_type - string 'question' or 'answer'
* key, name, description, level and multiple - as intended in the Badge
"""
def __init__(self):
super(QualityPost, self).__init__(
key = self.key,
name = self.name,
description = self.description,
level = self.level,
multiple = self.multiple
)
def consider_award(self, actor = None,
context_object = None, timestamp = None):
if context_object.post_type not in ('answer', 'question'):
return False
if context_object.score >= self.min_votes:
return self.award(context_object.author, context_object, timestamp)
return False
class NiceAnswer(QualityPost):
def __new__(cls):
self = super(NiceAnswer, cls).__new__(cls)
self.name = _('Nice Answer')
self.key = 'nice-answer'
self.level = const.BRONZE_BADGE
self.multiple = True
self.min_votes = askbot_settings.NICE_ANSWER_BADGE_MIN_UPVOTES
self.description = _('Answer voted up %(num)s times') % {'num': self.min_votes}
self.post_type = 'answer'
return self
class GoodAnswer(QualityPost):
def __new__(cls):
self = super(GoodAnswer, cls).__new__(cls)
self.name = _('Good Answer')
self.key = 'good-answer'
self.level = const.SILVER_BADGE
self.multiple = True
self.min_votes = askbot_settings.GOOD_ANSWER_BADGE_MIN_UPVOTES
self.description = _('Answer voted up %(num)s times') % {'num': self.min_votes}
self.post_type = 'answer'
return self
class GreatAnswer(QualityPost):
def __new__(cls):
self = super(GreatAnswer, cls).__new__(cls)
self.name = _('Great Answer')
self.key = 'great-answer'
self.level = const.GOLD_BADGE
self.multiple = True
self.min_votes = askbot_settings.GREAT_ANSWER_BADGE_MIN_UPVOTES
self.description = _('Answer voted up %(num)s times') % {'num': self.min_votes}
self.post_type = 'answer'
return self
class NiceQuestion(QualityPost):
def __new__(cls):
self = super(NiceQuestion, cls).__new__(cls)
self.name = _('Nice Question')
self.key = 'nice-question'
self.level = const.BRONZE_BADGE
self.multiple = True
self.min_votes = askbot_settings.NICE_QUESTION_BADGE_MIN_UPVOTES
self.description = _('Question voted up %(num)s times') % {'num': self.min_votes}
self.post_type = 'question'
return self
class GoodQuestion(QualityPost):
def __new__(cls):
self = super(GoodQuestion, cls).__new__(cls)
self.name = _('Good Question')
self.key = 'good-question'
self.level = const.SILVER_BADGE
self.multiple = True
self.min_votes = askbot_settings.GOOD_QUESTION_BADGE_MIN_UPVOTES
self.description = _('Question voted up %(num)s times') % {'num': self.min_votes}
self.post_type = 'question'
return self
class GreatQuestion(QualityPost):
def __new__(cls):
self = super(GreatQuestion, cls).__new__(cls)
self.name = _('Great Question')
self.key = 'great-question'
self.level = const.GOLD_BADGE
self.multiple = True
self.min_votes = askbot_settings.GREAT_QUESTION_BADGE_MIN_UPVOTES
self.description = _('Question voted up %(num)s times') % {'num': self.min_votes}
self.post_type = 'question'
return self
class Student(QualityPost):
def __new__(cls):
self = super(Student , cls).__new__(cls)
self.name = _('Student')
self.key = 'student'
self.level = const.BRONZE_BADGE
self.multiple = False
self.min_votes = 1
self.description = _('Asked first question with at least one up vote')
self.post_type = 'question'
return self
class FrequentedQuestion(Badge):
"""this badge is not awarded directly
but must be subclassed by Popular, Notable and Famous Question
badges via __new__() method definitions
The subclass has a responsibility to specify properties:
* min_views - a value from live settings
* key, name, description and level and multiple - as intended in the Badge
"""
def __init__(self):
super(FrequentedQuestion, self).__init__(
key = self.key,
name = self.name,
description = self.description,
level = self.level,
multiple = True
)
def consider_award(self, actor = None,
context_object = None, timestamp = None):
if context_object.post_type != 'question':
return False
if context_object.view_count >= self.min_views:
return self.award(context_object.author, context_object, timestamp)
return False
class PopularQuestion(FrequentedQuestion):
def __new__(cls):
self = super(PopularQuestion, cls).__new__(cls)
self.name = _('Popular Question')
self.key = 'popular-question'
self.level = const.BRONZE_BADGE
self.min_views = askbot_settings.POPULAR_QUESTION_BADGE_MIN_VIEWS
self.description = _('Asked a question with %(views)s views') \
% {'views' : self.min_views}
return self
class NotableQuestion(FrequentedQuestion):
def __new__(cls):
self = super(NotableQuestion, cls).__new__(cls)
self.name = _('Notable Question')
self.key = 'notable-question'
self.level = const.SILVER_BADGE
self.min_views = askbot_settings.NOTABLE_QUESTION_BADGE_MIN_VIEWS
self.description = _('Asked a question with %(views)s views') \
% {'views' : self.min_views}
return self
class FamousQuestion(FrequentedQuestion):
def __new__(cls):
self = super(FamousQuestion, cls).__new__(cls)
self.name = _('Famous Question')
self.key = 'famous-question'
self.level = const.GOLD_BADGE
self.multiple = True
self.min_views = askbot_settings.FAMOUS_QUESTION_BADGE_MIN_VIEWS
self.description = _('Asked a question with %(views)s views') \
% {'views' : self.min_views}
return self
class Scholar(Badge):
"""scholar badge is awarded to the asker when
he/she accepts an answer for the first time
"""
def __init__(self):
description = _('Asked a question and accepted an answer')
super(Scholar, self).__init__(
key = 'scholar',
name = _('Scholar'),
level = const.BRONZE_BADGE,
multiple = False,
description = description
)
def consider_award(self, actor = None,
context_object = None, timestamp = None):
if context_object.post_type != 'answer':
return False
answer = context_object
if answer.question.author != actor:
return False
return self.award(actor, context_object, timestamp)
class VotedAcceptedAnswer(Badge):
"""superclass for Enlightened and Guru badges
not awarded directly
Subclasses must define __new__ function
"""
def __init__(self):
super(VotedAcceptedAnswer, self).__init__(
key = self.key,
name = self.name,
level = self.level,
multiple = self.multiple,
description = self.description
)
def consider_award(self, actor = None,
context_object = None, timestamp = None):
if context_object.post_type != 'answer':
return None
answer = context_object
if answer.score >= self.min_votes and answer.accepted:
return self.award(answer.author, answer, timestamp)
class Enlightened(VotedAcceptedAnswer):
def __new__(cls):
self = super(Enlightened, cls).__new__(cls)
self.key = 'enlightened'
self.name = _('Enlightened')
self.level = const.SILVER_BADGE
self.multiple = False
self.min_votes = askbot_settings.ENLIGHTENED_BADGE_MIN_UPVOTES
descr = _('First answer was accepted with %(num)s or more votes')
self.description = descr % {'num': self.min_votes}
return self
class Guru(VotedAcceptedAnswer):
def __new__(cls):
self = super(Guru, cls).__new__(cls)
self.key = 'guru'
self.name = _('Guru')
self.level = const.GOLD_BADGE
self.multiple = True
descr = _('Answer accepted with %(num)s or more votes')
self.min_votes = askbot_settings.GURU_BADGE_MIN_UPVOTES
self.description = descr % {'num': self.min_votes}
return self
class Necromancer(Badge):
def __init__(self):
description = _(
'Answered a question more than %(days)s days '
'later with at least %(votes)s votes'
)
days = askbot_settings.NECROMANCER_BADGE_MIN_DELAY
votes = askbot_settings.NECROMANCER_BADGE_MIN_UPVOTES
super(Necromancer, self).__init__(
key = 'necromancer',
name = _('Necromancer'),
level = const.SILVER_BADGE,
description = description % {'days':days, 'votes':votes},
multiple = True
)
def consider_award(self, actor = None,
context_object = None, timestamp = None):
if context_object.post_type != 'answer':
return False
answer = context_object
question = answer.question
delta = datetime.timedelta(askbot_settings.NECROMANCER_BADGE_MIN_DELAY)
min_score = askbot_settings.NECROMANCER_BADGE_MIN_UPVOTES
if answer.added_at - question.added_at >= delta \
and answer.score >= min_score:
return self.award(answer.author, answer, timestamp)
return False
class CitizenPatrol(Badge):
def __init__(self):
super(CitizenPatrol, self).__init__(
key = 'citizen-patrol',
name = _('Citizen Patrol'),
level = const.BRONZE_BADGE,
multiple = False,
description = _('First flagged post')
)
class Cleanup(Badge):
"""This badge is inactive right now.
to make it live we need to be able to either
detect "undo" actions or rewrite the view
correspondingly
"""
def __init__(self):
super(Cleanup, self).__init__(
key = 'cleanup',
name = _('Cleanup'),
level = const.BRONZE_BADGE,
multiple = False,
description = _('First rollback')
)
class Pundit(Badge):
"""Inactive until it is possible to vote
for comments.
Pundit is someone who makes good comments.
"""
def __init__(self):
super(Pundit, self).__init__(
key = 'pundit',
name = _('Pundit'),
level = const.SILVER_BADGE,
multiple = False,
description = _('Left 10 comments with score of 10 or more')
)
class EditorTypeBadge(Badge):
"""subclassing badges are types of editors
must provide usual parameters + min_edits
via __new__ function
"""
def __init__(self):
super(EditorTypeBadge, self).__init__(
key = self.key,
name = self.name,
level = self.level,
multiple = False,
description = self.description
)
def consider_award(self, actor = None,
context_object = None, timestamp = None):
atypes = (
const.TYPE_ACTIVITY_UPDATE_QUESTION,
const.TYPE_ACTIVITY_UPDATE_ANSWER
)
filters = {'user': actor, 'activity_type__in': atypes}
if Activity.objects.filter(**filters).count() == self.min_edits:
return self.award(actor, context_object, timestamp)
class Editor(EditorTypeBadge):
def __new__(cls):
self = super(Editor, cls).__new__(cls)
self.key = 'editor'
self.name = _('Editor')
self.level = const.BRONZE_BADGE
self.multiple = False
self.description = _('First edit')
self.min_edits = 1
return self
class AssociateEditor(EditorTypeBadge):
def __new__(cls):
self = super(AssociateEditor, cls).__new__(cls)
self.key = 'strunk-and-white'#legacy copycat name from stackoverflow
self.name = _('Associate Editor')
self.level = const.SILVER_BADGE
self.multiple = False
self.min_edits = askbot_settings.ASSOCIATE_EDITOR_BADGE_MIN_EDITS
self.description = _('Edited %(num)s entries') % {'num': self.min_edits}
return self
class Organizer(Badge):
def __init__(self):
super(Organizer, self).__init__(
key = 'organizer',
name = _('Organizer'),
level = const.BRONZE_BADGE,
multiple = False,
description = _('First retag')
)
class Autobiographer(Badge):
def __init__(self):
super(Autobiographer, self).__init__(
key = 'autobiographer',
name = _('Autobiographer'),
level = const.BRONZE_BADGE,
multiple = False,
description = _('Completed all user profile fields')
)
def consider_award(self, actor = None,
context_object = None, timestamp = None):
user = context_object
if user.email and user.real_name and user.website \
and user.location and user.about:
return self.award(user, user, timestamp)
return False
class FavoriteTypeBadge(Badge):
"""subclass must use __new__ and in addition
must provide min_stars property for the badge
"""
def __init__(self):
descr = _('Question favorited by %(num)s users')
super(FavoriteTypeBadge, self).__init__(
key = self.key,
name = self.name,
level = self.level,
multiple = True,
description = descr % {'num': self.min_stars}
)
def consider_award(self, actor = None,
context_object = None, timestamp = None):
question = context_object
#model FavoriteQuestion imported under alias of Fave
count = Fave.objects.filter(
question = question
).exclude(
user = question.author
).count()
if count == self.min_stars:
return self.award(question.author, question, timestamp)
return False
class StellarQuestion(FavoriteTypeBadge):
def __new__(cls):
self = super(StellarQuestion, cls).__new__(cls)
self.key = 'stellar-question'
self.name = _('Stellar Question')
self.level = const.GOLD_BADGE
self.min_stars = askbot_settings.STELLAR_QUESTION_BADGE_MIN_STARS
return self
class FavoriteQuestion(FavoriteTypeBadge):
def __new__(cls):
self = super(FavoriteQuestion, cls).__new__(cls)
self.key = 'favorite-question'
self.name = _('Favorite Question')
self.level = const.SILVER_BADGE
self.min_stars = askbot_settings.FAVORITE_QUESTION_BADGE_MIN_STARS
return self
class Enthusiast(Badge):
"""Awarded to a user who visits the site
for a certain number of days in a row
"""
def __init__(self):
super(Enthusiast, self).__init__(
key = 'enthusiast',
name = _('Enthusiast'),
level = const.SILVER_BADGE,
multiple = False,
description = _(
'Visited site every day for %(num)s days in a row'
) % {'num': askbot_settings.ENTHUSIAST_BADGE_MIN_DAYS}
)
def consider_award(self, actor = None,
context_object = None, timestamp = None):
min_days = askbot_settings.ENTHUSIAST_BADGE_MIN_DAYS
if actor.consecutive_days_visit_count == min_days:
return self.award(actor, context_object, timestamp)
return False
class Commentator(Badge):
"""Commentator is a bronze badge that is
awarded once when user posts a certain number of
comments"""
def __init__(self):
super(Commentator, self).__init__(
key = 'commentator',
name = _('Commentator'),
level = const.BRONZE_BADGE,
multiple = False,
description = _(
'Posted %(num_comments)s comments'
) % {'num_comments': askbot_settings.COMMENTATOR_BADGE_MIN_COMMENTS}
)
def consider_award(self, actor = None,
context_object = None, timestamp = None):
num_comments = Comment.objects.filter(user = actor).count()
if num_comments >= askbot_settings.COMMENTATOR_BADGE_MIN_COMMENTS:
return self.award(actor, context_object, timestamp)
return False
class Taxonomist(Badge):
"""Stub badge"""
def __init__(self):
super(Taxonomist, self).__init__(
key = 'taxonomist',
name = _('Taxonomist'),
level = const.SILVER_BADGE,
multiple = False,
description = _(
'Created a tag used by %(num)s questions'
) % {'num': askbot_settings.TAXONOMIST_BADGE_MIN_USE_COUNT}
)
def consider_award(self, actor = None,
context_object = None, timestamp = None):
tag = context_object
taxonomist_threshold = askbot_settings.TAXONOMIST_BADGE_MIN_USE_COUNT
#the "-1" is used because tag counts are updated in a bulk query
#that does not update the value in the python object
if tag.used_count == taxonomist_threshold - 1:
return self.award(tag.created_by, tag, timestamp)
return False
class Expert(Badge):
"""Stub badge"""
def __init__(self):
super(Expert, self).__init__(
key = 'expert',
name = _('Expert'),
level = const.SILVER_BADGE,
multiple = False,
description = _('Very active in one tag')
)
ORIGINAL_DATA = """
extra badges from stackexchange
* commentator - left n comments (single)
* enthusiast, fanatic - visited site n days in a row (s)
* epic, legendary - hit daily reputation cap on n days (s)
* mortarboard - hit the daily reputation cap for the first time (s)
* populist - provided an answer that outscored an accepted answer two-fold or by n points, whichever is higher (m)
* reversal - provided an answer with +n points to a question of -m points
(_('Yearling'), 2, _('yearling'), _('Active member for a year'), False, 0),
(_('Generalist'), 2, _('generalist'), _('Active in many different tags'), False, 0),
(_('Beta'), 2, _('beta'), _('Actively participated in the private beta'), False, 0),
(_('Alpha'), 2, _('alpha'), _('Actively participated in the private alpha'), False, 0),
"""
BADGES = {
'strunk-and-white': AssociateEditor,#legacy slug name
'autobiographer': Autobiographer,
'cleanup': Cleanup,
'citizen-patrol': CitizenPatrol,
'civic-duty': CivicDuty,
'commentator': Commentator,
'critic': Critic,
'disciplined': Disciplined,
'editor': Editor,
'enlightened': Enlightened,
'enthusiast': Enthusiast,
'expert': Expert,
'famous-question': FamousQuestion,
'favorite-question': FavoriteQuestion,
'good-answer': GoodAnswer,
'good-question': GoodQuestion,
'great-answer': GreatAnswer,
'great-question': GreatQuestion,
'guru': Guru,
'necromancer': Necromancer,
'nice-answer': NiceAnswer,
'nice-question': NiceQuestion,
'notable-question': NotableQuestion,
'organizer': Organizer,
'peer-pressure': PeerPressure,
'popular-question': PopularQuestion,
'pundit': Pundit,
'scholar': Scholar,
'self-learner': SelfLearner,
'stellar-question': StellarQuestion,
'student': Student,
'supporter': Supporter,
'teacher': Teacher,
'taxonomist': Taxonomist,
}
#events are sent as a parameter via signal award_badges_signal
#from appropriate locations in the code of askbot application
#most likely - from manipulator functions that are added to the User objects
EVENTS_TO_BADGES = {
'accept_best_answer': (Scholar, Guru, Enlightened),
'delete_post': (Disciplined, PeerPressure,),
'downvote': (Critic, CivicDuty),#no regard for question or answer for now
'edit_answer': (Editor, AssociateEditor),
'edit_question': (Editor, AssociateEditor),
'flag_post': (CitizenPatrol,),
'post_answer': (Necromancer,),
'post_comment': (Commentator,),
'retag_question': (Organizer,),
'select_favorite_question': (FavoriteQuestion, StellarQuestion,),
'site_visit': (Enthusiast,),
'update_tag': (Taxonomist,),
'update_user_profile': (Autobiographer,),
'upvote_answer': (
Teacher, NiceAnswer, GoodAnswer,
GreatAnswer, Supporter, SelfLearner, CivicDuty,
Guru, Enlightened, Necromancer
),
'upvote_question': (
NiceQuestion, GoodQuestion,
GreatQuestion, Student, Supporter, CivicDuty
),
'upvote_comment':(),#todo - add some badges here
'view_question': (PopularQuestion, NotableQuestion, FamousQuestion,),
}
def get_badge(name = None):
"""Get badge object by name, if none matches the name
raise KeyError
"""
key = slugify(name)
return BADGES[key]()
def init_badges():
"""Calling this function will set up badge record
int the database for each badge enumerated in the
`BADGES` dictionary
"""
#todo: maybe better to redo individual badges
#so that get_stored_data() is called implicitly
#from the __init__ function?
for key in BADGES.keys():
get_badge(key).get_stored_data()
award_badges_signal = Signal(
providing_args=[
'actor', 'event', 'context_object', 'timestamp'
]
)
#actor - user who triggers the event
#event - string name of the event, e.g 'downvote'
#context_object - database object related to the event, e.g. question
@auto_now_timestamp
def award_badges(event = None, actor = None,
context_object = None, timestamp = None, **kwargs):
"""function that is called when signal `award_badges_signal` is sent
"""
try:
consider_badges = EVENTS_TO_BADGES[event]
except KeyError:
raise NotImplementedError('event "%s" is not implemented' % event)
for badge in consider_badges:
badge_instance = badge()
badge_instance.consider_award(actor, context_object, timestamp)
award_badges_signal.connect(award_badges)
|