1 from django.utils.translation import ugettext as _
4 class Vote(models.Model):
5 user = models.ForeignKey(User, related_name="votes")
6 node = models.ForeignKey(Node, related_name="votes")
7 value = models.SmallIntegerField()
8 action = models.OneToOneField(Action, related_name="vote")
9 voted_at = models.DateTimeField(default=datetime.datetime.now)
13 unique_together = ('user', 'node')
16 class Flag(models.Model):
17 user = models.ForeignKey(User, related_name="flags")
18 node = models.ForeignKey(Node, related_name="flags")
19 reason = models.CharField(max_length=300)
20 action = models.OneToOneField(Action, related_name="flag")
21 flagged_at = models.DateTimeField(default=datetime.datetime.now)
25 unique_together = ('user', 'node')
28 class Badge(BaseModel):
33 type = models.SmallIntegerField()
34 cls = models.CharField(max_length=50, null=True)
35 awarded_count = models.PositiveIntegerField(default=0)
37 awarded_to = models.ManyToManyField(User, through='Award', related_name='badges')
39 def get_handler(self):
40 from forum.badges import BadgesMeta
41 return BadgesMeta.by_id.get(self.id, None)
45 cls = self.get_handler()
46 return cls and cls.name or _("Unknown")
49 def description(self):
50 cls = self.get_handler()
51 return cls and cls.description or _("No description available")
54 def get_absolute_url(self):
55 return ('badge', [], {'id': self.id, 'slug': slugify(self.name)})
57 def save(self, *args, **kwargs):
58 if isinstance(self.awarded_count, models.expressions.ExpressionNode):
59 super(Badge, self).save(*args, **kwargs)
60 self.awarded_count = self.__class__.objects.filter(id=self.id).values_list('awarded_count', flat=True)[0]
62 super(Badge, self).save(*args, **kwargs)
69 class Award(models.Model):
70 user = models.ForeignKey(User)
71 badge = models.ForeignKey('Badge', related_name="awards")
72 node = models.ForeignKey(Node, null=True)
74 awarded_at = models.DateTimeField(default=datetime.datetime.now)
76 trigger = models.ForeignKey(Action, related_name="awards", null=True)
77 action = models.OneToOneField(Action, related_name="award")
81 unique_together = ('user', 'badge', 'node')