2 from django.contrib.contenttypes.models import ContentType
3 from forum.models import User
5 from django.utils.translation import ugettext as _
7 class Badge(BaseModel):
8 """Awarded for notable actions performed on the site by Users."""
14 (SILVER, _('silver')),
15 (BRONZE, _('bronze')),
18 name = models.CharField(max_length=50)
19 type = models.SmallIntegerField(choices=TYPE_CHOICES)
20 slug = models.SlugField(max_length=50, blank=True)
21 description = models.CharField(max_length=300)
22 multiple = models.BooleanField(default=False)
24 awarded_count = models.PositiveIntegerField(default=0)
25 awarded_to = models.ManyToManyField(User, through='Award', related_name='badges')
31 unique_together = ('name', 'type')
33 def __unicode__(self):
34 return u'%s: %s' % (self.get_type_display(), self.name)
36 def save(self, *args, **kwargs):
38 self.slug = self.name#slugify(self.name)
39 super(Badge, self).save(*args, **kwargs)
41 def get_absolute_url(self):
42 return '%s%s/' % (reverse('badge', args=[self.id]), self.slug)
45 class AwardManager(models.Manager):
46 def get_recent_awards(self):
47 awards = super(AwardManager, self).extra(
48 select={'badge_id': 'badge.id', 'badge_name':'badge.name',
49 'badge_description': 'badge.description', 'badge_type': 'badge.type',
50 'user_id': 'auth_user.id', 'user_name': 'auth_user.username'
52 tables=['award', 'badge', 'auth_user'],
53 order_by=['-awarded_at'],
54 where=['auth_user.id=award.user_id AND badge_id=badge.id'],
55 ).values('badge_id', 'badge_name', 'badge_description', 'badge_type', 'user_id', 'user_name')
58 class Award(GenericContent, UserContent):
59 """The awarding of a Badge to a User."""
60 badge = models.ForeignKey('Badge', related_name='award_badge')
61 awarded_at = models.DateTimeField(default=datetime.datetime.now)
62 notified = models.BooleanField(default=False)
64 objects = AwardManager()
66 def __unicode__(self):
67 return u'[%s] is awarded a badge [%s] at %s' % (self.user.username, self.badge.name, self.awarded_at)
69 def save(self, *args, **kwargs):
70 super(Award, self).save(*args, **kwargs)
73 self.badge.awarded_count += 1
76 if self.badge.type == Badge.GOLD:
78 if self.badge.type == Badge.SILVER:
80 if self.badge.type == Badge.BRONZE:
85 unique_together = ('content_type', 'object_id', 'user', 'badge')
90 class Repute(UserContent):
91 """The reputation histories for user"""
92 value = models.SmallIntegerField(default=0)
93 question = models.ForeignKey('Question')
94 reputed_at = models.DateTimeField(default=datetime.datetime.now)
95 reputation_type = models.SmallIntegerField(choices=TYPE_REPUTATION)
96 user_previous_rep = models.IntegerField(default=0)
98 def __unicode__(self):
99 return u'[%s]\' reputation changed at %s' % (self.user.username, self.reputed_at)
103 if self.value > 0: return self.value
108 if self.value < 0: return self.value
112 def reputation(self):
113 return self.user_previous_rep + self.value
115 def save(self, *args, **kwargs):
116 self.user_previous_rep = self.user.reputation
117 self.user.reputation = self.user.reputation + self.value
119 super(Repute, self).save(*args, **kwargs)