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