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')
27 class BadgesQuerySet(models.query.QuerySet):
28 def get(self, *args, **kwargs):
30 pk = [v for (k,v) in kwargs.items() if k in ('pk', 'pk__exact', 'id', 'id__exact')][0]
32 return super(BadgesQuerySet, self).get(*args, **kwargs)
34 from forum.badges.base import BadgesMeta
35 badge = BadgesMeta.by_id.get(int(pk), None)
37 return super(BadgesQuerySet, self).get(*args, **kwargs)
41 class BadgeManager(models.Manager):
42 use_for_related_fields = True
44 def get_query_set(self):
45 return BadgesQuerySet(self.model)
47 class Badge(models.Model):
52 type = models.SmallIntegerField()
53 cls = models.CharField(max_length=50, null=True)
54 awarded_count = models.PositiveIntegerField(default=0)
56 awarded_to = models.ManyToManyField(User, through='Award', related_name='badges')
58 objects = BadgeManager()
62 cls = self.__dict__.get('_class', None)
63 return cls and cls.name or _("Unknown")
66 def description(self):
67 cls = self.__dict__.get('_class', None)
68 return cls and cls.description or _("No description available")
71 def get_absolute_url(self):
72 return ('badge', [], {'id': self.id, 'slug': slugify(self.name)})
74 def save(self, *args, **kwargs):
75 if isinstance(self.awarded_count, models.expressions.ExpressionNode):
76 super(Badge, self).save(*args, **kwargs)
77 self.awarded_count = self.__class__.objects.filter(id=self.id).values_list('awarded_count', flat=True)[0]
79 super(Badge, self).save(*args, **kwargs)
86 class Award(models.Model):
87 user = models.ForeignKey(User)
88 badge = models.ForeignKey('Badge', related_name="awards")
89 node = models.ForeignKey(Node, null=True)
91 awarded_at = models.DateTimeField(default=datetime.datetime.now)
93 trigger = models.ForeignKey(Action, related_name="awards", null=True)
94 action = models.OneToOneField(Action, related_name="award")
98 unique_together = ('user', 'badge', 'node')