3 from django.utils.translation import ugettext as _
\r
5 class Badge(models.Model):
\r
6 """Awarded for notable actions performed on the site by Users."""
\r
12 (SILVER, _('silver')),
\r
13 (BRONZE, _('bronze')),
\r
16 name = models.CharField(max_length=50)
\r
17 type = models.SmallIntegerField(choices=TYPE_CHOICES)
\r
18 slug = models.SlugField(max_length=50, blank=True)
\r
19 description = models.CharField(max_length=300)
\r
20 multiple = models.BooleanField(default=False)
\r
22 awarded_count = models.PositiveIntegerField(default=0)
\r
24 awarded_to = models.ManyToManyField(User, through='Award', related_name='badges')
\r
29 ordering = ('name',)
\r
30 unique_together = ('name', 'type')
\r
32 def __unicode__(self):
\r
33 return u'%s: %s' % (self.get_type_display(), self.name)
\r
35 def save(self, **kwargs):
\r
37 self.slug = self.name#slugify(self.name)
\r
38 super(Badge, self).save(**kwargs)
\r
40 def get_absolute_url(self):
\r
41 return '%s%s/' % (reverse('badge', args=[self.id]), self.slug)
\r
43 class AwardManager(models.Manager):
\r
44 def get_recent_awards(self):
\r
45 awards = super(AwardManager, self).extra(
\r
46 select={'badge_id': 'badge.id', 'badge_name':'badge.name',
\r
47 'badge_description': 'badge.description', 'badge_type': 'badge.type',
\r
48 'user_id': 'auth_user.id', 'user_name': 'auth_user.username'
\r
50 tables=['award', 'badge', 'auth_user'],
\r
51 order_by=['-awarded_at'],
\r
52 where=['auth_user.id=award.user_id AND badge_id=badge.id'],
\r
53 ).values('badge_id', 'badge_name', 'badge_description', 'badge_type', 'user_id', 'user_name')
\r
56 class Award(models.Model):
\r
57 """The awarding of a Badge to a User."""
\r
58 user = models.ForeignKey(User, related_name='award_user')
\r
59 badge = models.ForeignKey('Badge', related_name='award_badge')
\r
60 content_type = models.ForeignKey(ContentType)
\r
61 object_id = models.PositiveIntegerField()
\r
62 content_object = generic.GenericForeignKey('content_type', 'object_id')
\r
63 awarded_at = models.DateTimeField(default=datetime.datetime.now)
\r
64 notified = models.BooleanField(default=False)
\r
66 objects = AwardManager()
\r
68 def __unicode__(self):
\r
69 return u'[%s] is awarded a badge [%s] at %s' % (self.user.username, self.badge.name, self.awarded_at)
\r
75 class ReputeManager(models.Manager):
\r
76 def get_reputation_by_upvoted_today(self, user):
\r
78 For one user in one day, he can only earn rep till certain score (ep. +200)
\r
79 by upvoted(also substracted from upvoted canceled). This is because we need
\r
80 to prohibit gaming system by upvoting/cancel again and again.
\r
82 if user is not None:
\r
83 today = datetime.date.today()
\r
84 sums = self.filter(models.Q(reputation_type=1) | models.Q(reputation_type=-8),
\r
85 user=user, reputed_at__range=(today, today + datetime.timedelta(1))). \
\r
86 agregate(models.Sum('positive'), models.Sum('negative'))
\r
88 return sums['positive__sum'] + sums['negative__sum']
\r
92 class Repute(models.Model):
\r
93 """The reputation histories for user"""
\r
94 user = models.ForeignKey(User)
\r
95 positive = models.SmallIntegerField(default=0)
\r
96 negative = models.SmallIntegerField(default=0)
\r
97 question = models.ForeignKey('Question')
\r
98 reputed_at = models.DateTimeField(default=datetime.datetime.now)
\r
99 reputation_type = models.SmallIntegerField(choices=TYPE_REPUTATION)
\r
100 reputation = models.IntegerField(default=1)
\r
102 objects = ReputeManager()
\r
104 def __unicode__(self):
\r
105 return u'[%s]\' reputation changed at %s' % (self.user.username, self.reputed_at)
\r
108 app_label = 'forum'
\r
109 db_table = u'repute'
\r