]> git.openstreetmap.org Git - osqa.git/blobdiff - forum/models/meta.py
osqa-90
[osqa.git] / forum / models / meta.py
index c01c018f77daa5c4cef9cdd6e7decab5cf39a36d..3fd068081e3ef1d7cc65b2744d22dc9f38a52b88 100644 (file)
@@ -1,75 +1,94 @@
-from base import *
-
-class Vote(MetaContent, CancelableContent, UserContent):
-    VOTE_UP = +1
-    VOTE_DOWN = -1
-    VOTE_CHOICES = (
-        (VOTE_UP,   u'Up'),
-        (VOTE_DOWN, u'Down'),
-    )
-
-    vote           = models.SmallIntegerField(choices=VOTE_CHOICES)
-    voted_at       = models.DateTimeField(default=datetime.datetime.now)
-
-    active = ActiveObjectManager()
-
-    class Meta(MetaContent.Meta):
-        db_table = u'vote'
-
-    def __unicode__(self):
-        return '[%s] voted at %s: %s' %(self.user, self.voted_at, self.vote)
-
-    def _update_post_vote_count(self, diff):
-        post = self.node
-        field = self.vote == 1 and 'vote_up_count' or 'vote_down_count'
-        post.__dict__[field] = post.__dict__[field] + diff
-        post.save()
-
-    def cancel(self):
-        if super(Vote, self).cancel():
-            self._update_post_vote_count(-1)
-
-    def save(self, *args, **kwargs):
-        super(Vote, self).save(*args, **kwargs)
-        if self._is_new:
-            self._update_post_vote_count(1)
-
-    def is_upvote(self):
-        return self.vote == self.VOTE_UP
-
-    def is_downvote(self):
-        return self.vote == self.VOTE_DOWN
-
-
-class FlaggedItem(MetaContent, UserContent):
-    flagged_at     = models.DateTimeField(default=datetime.datetime.now)
-    reason         = models.CharField(max_length=300, null=True)
-    canceled       = models.BooleanField(default=False)
-
-    active = ActiveObjectManager()
-
-    class Meta(MetaContent.Meta):
-        db_table = u'flagged_item'
-
-    def __unicode__(self):
-        return '[%s] flagged at %s' %(self.user, self.flagged_at)
-
-    def _update_post_flag_count(self, diff):
-        post = self.node
-        post.offensive_flag_count = post.offensive_flag_count + diff
-        post.save()
-
-    def save(self, *args, **kwargs):
-        super(FlaggedItem, self).save(*args, **kwargs)
-        if self._is_new:
-            self._update_post_flag_count(1)
-
-    def cancel(self):
-        if not self.canceled:
-            self.canceled = True
-            self.save()
-            self._update_post_flag_count(-1)
-            
-
-
-
+from django.utils.translation import ugettext as _\r
+from base import *\r
+\r
+class Vote(models.Model):\r
+    user = models.ForeignKey(User, related_name="votes")\r
+    node = models.ForeignKey(Node, related_name="votes")\r
+    value = models.SmallIntegerField()\r
+    action = models.OneToOneField(Action, related_name="vote")\r
+    voted_at = models.DateTimeField(default=datetime.datetime.now)\r
+\r
+    class Meta:\r
+        app_label = 'forum'\r
+        unique_together = ('user', 'node')\r
+\r
+\r
+class Flag(models.Model):\r
+    user = models.ForeignKey(User, related_name="flags")\r
+    node = models.ForeignKey(Node, related_name="flags")\r
+    reason = models.CharField(max_length=300)\r
+    action = models.OneToOneField(Action, related_name="flag")\r
+    flagged_at = models.DateTimeField(default=datetime.datetime.now)\r
+\r
+    class Meta:\r
+        app_label = 'forum'\r
+        unique_together = ('user', 'node')\r
+\r
+class BadgeManager(models.Manager):\r
+    use_for_related_fields = True\r
+    \r
+    def get(self, *args, **kwargs):\r
+        try:\r
+            pk = [v for (k,v) in kwargs.items() if k in ('pk', 'pk__exact', 'id', 'id__exact')][0]\r
+        except:\r
+            return super(BadgeManager, self).get(*args, **kwargs)\r
+\r
+        from forum.badges.base import BadgesMeta\r
+        badge = BadgesMeta.by_id.get(int(pk), None)\r
+        if not badge:\r
+            return super(BadgeManager, self).get(*args, **kwargs)\r
+        return badge.ondb\r
+\r
+class Badge(models.Model):\r
+    GOLD = 1\r
+    SILVER = 2\r
+    BRONZE = 3\r
+\r
+    type        = models.SmallIntegerField()\r
+    cls         = models.CharField(max_length=50, null=True)\r
+    awarded_count = models.PositiveIntegerField(default=0)\r
+    \r
+    awarded_to    = models.ManyToManyField(User, through='Award', related_name='badges')\r
+\r
+    objects = BadgeManager()\r
+\r
+    @property\r
+    def name(self):\r
+        cls = self.__dict__.get('_class', None)\r
+        return cls and cls.name or _("Unknown")\r
+\r
+    @property\r
+    def description(self):\r
+        cls = self.__dict__.get('_class', None)\r
+        return cls and cls.description or _("No description available")\r
+\r
+    @models.permalink\r
+    def get_absolute_url(self):\r
+        return ('badge', [], {'id': self.id, 'slug': slugify(self.name)})\r
+\r
+    def save(self, *args, **kwargs):\r
+        if isinstance(self.awarded_count, models.expressions.ExpressionNode):\r
+            super(Badge, self).save(*args, **kwargs)\r
+            self.awarded_count = self.__class__.objects.filter(id=self.id).values_list('awarded_count', flat=True)[0]\r
+        else:\r
+            super(Badge, self).save(*args, **kwargs)\r
+\r
+\r
+    class Meta:\r
+        app_label = 'forum'\r
+\r
+\r
+class Award(models.Model):\r
+    user = models.ForeignKey(User)\r
+    badge = models.ForeignKey('Badge', related_name="awards")\r
+    node = models.ForeignKey(Node, null=True)\r
+\r
+    awarded_at = models.DateTimeField(default=datetime.datetime.now)\r
+\r
+    trigger = models.ForeignKey(Action, related_name="awards", null=True)\r
+    action = models.OneToOneField(Action, related_name="award")\r
+\r
+\r
+    class Meta:\r
+        unique_together = ('user', 'badge', 'node')\r
+        app_label = 'forum'
\ No newline at end of file