1 from django.utils.translation import ugettext as _
\r
4 class Vote(models.Model):
\r
5 user = models.ForeignKey(User, related_name="votes")
\r
6 node = models.ForeignKey(Node, related_name="votes")
\r
7 value = models.SmallIntegerField()
\r
8 action = models.OneToOneField(Action, related_name="vote")
\r
9 voted_at = models.DateTimeField(default=datetime.datetime.now)
\r
13 unique_together = ('user', 'node')
\r
16 class Flag(models.Model):
\r
17 user = models.ForeignKey(User, related_name="flags")
\r
18 node = models.ForeignKey(Node, related_name="flags")
\r
19 reason = models.CharField(max_length=300)
\r
20 action = models.OneToOneField(Action, related_name="flag")
\r
21 flagged_at = models.DateTimeField(default=datetime.datetime.now)
\r
25 unique_together = ('user', 'node')
\r
27 class BadgeManager(models.Manager):
\r
28 use_for_related_fields = True
\r
30 def get(self, *args, **kwargs):
\r
32 pk = [v for (k,v) in kwargs.items() if k in ('pk', 'pk__exact', 'id', 'id__exact')][0]
\r
34 return super(BadgeManager, self).get(*args, **kwargs)
\r
36 from forum.badges.base import BadgesMeta
\r
37 badge = BadgesMeta.by_id.get(int(pk), None)
\r
39 return super(BadgeManager, self).get(*args, **kwargs)
\r
42 class Badge(models.Model):
\r
47 type = models.SmallIntegerField()
\r
48 cls = models.CharField(max_length=50, null=True)
\r
49 awarded_count = models.PositiveIntegerField(default=0)
\r
51 awarded_to = models.ManyToManyField(User, through='Award', related_name='badges')
\r
53 objects = BadgeManager()
\r
57 cls = self.__dict__.get('_class', None)
\r
58 return cls and cls.name or _("Unknown")
\r
61 def description(self):
\r
62 cls = self.__dict__.get('_class', None)
\r
63 return cls and cls.description or _("No description available")
\r
66 def get_absolute_url(self):
\r
67 return ('badge', [], {'id': self.id, 'slug': slugify(self.name)})
\r
69 def save(self, *args, **kwargs):
\r
70 if isinstance(self.awarded_count, models.expressions.ExpressionNode):
\r
71 super(Badge, self).save(*args, **kwargs)
\r
72 self.awarded_count = self.__class__.objects.filter(id=self.id).values_list('awarded_count', flat=True)[0]
\r
74 super(Badge, self).save(*args, **kwargs)
\r
81 class Award(models.Model):
\r
82 user = models.ForeignKey(User)
\r
83 badge = models.ForeignKey('Badge', related_name="awards")
\r
84 node = models.ForeignKey(Node, null=True)
\r
86 awarded_at = models.DateTimeField(default=datetime.datetime.now)
\r
88 trigger = models.ForeignKey(Action, related_name="awards", null=True)
\r
89 action = models.OneToOneField(Action, related_name="award")
\r
93 unique_together = ('user', 'badge', 'node')
\r