+from django.utils.translation import ugettext as _\r
from base import *\r
\r
-class VoteManager(models.Manager):\r
- def get_up_vote_count_from_user(self, user):\r
- if user is not None:\r
- return self.filter(user=user, vote=1).count()\r
- else:\r
- return 0\r
-\r
- def get_down_vote_count_from_user(self, user):\r
- if user is not None:\r
- return self.filter(user=user, vote=-1).count()\r
- else:\r
- return 0\r
-\r
- def get_votes_count_today_from_user(self, user):\r
- if user is not None:\r
- today = datetime.date.today()\r
- return self.filter(user=user, voted_at__range=(today, today + datetime.timedelta(1))).count()\r
-\r
- else:\r
- return 0\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 Vote(MetaContent):\r
- VOTE_UP = +1\r
- VOTE_DOWN = -1\r
- VOTE_CHOICES = (\r
- (VOTE_UP, u'Up'),\r
- (VOTE_DOWN, u'Down'),\r
- )\r
+ class Meta:\r
+ app_label = 'forum'\r
+ unique_together = ('user', 'node')\r
\r
- vote = models.SmallIntegerField(choices=VOTE_CHOICES)\r
- voted_at = models.DateTimeField(default=datetime.datetime.now)\r
\r
- objects = VoteManager()\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(MetaContent.Meta):\r
- unique_together = ('content_type', 'object_id', 'user')\r
- db_table = u'vote'\r
+ class Meta:\r
+ app_label = 'forum'\r
+ unique_together = ('user', 'node')\r
\r
- def __unicode__(self):\r
- return '[%s] voted at %s: %s' %(self.user, self.voted_at, self.vote)\r
-\r
- def is_upvote(self):\r
- return self.vote == self.VOTE_UP\r
-\r
- def is_downvote(self):\r
- return self.vote == self.VOTE_DOWN\r
-\r
-\r
-class FlaggedItemManager(models.Manager):\r
- def get_flagged_items_count_today(self, user):\r
- if user is not None:\r
- today = datetime.date.today()\r
- return self.filter(user=user, flagged_at__range=(today, today + datetime.timedelta(1))).count()\r
+class BadgesQuerySet(models.query.QuerySet):\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(BadgesQuerySet, 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(BadgesQuerySet, self).get(*args, **kwargs)\r
+ return badge.ondb\r
+\r
+\r
+class BadgeManager(models.Manager):\r
+ use_for_related_fields = True\r
+\r
+ def get_query_set(self):\r
+ return BadgesQuerySet(self.model)\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
- return 0\r
+ super(Badge, self).save(*args, **kwargs)\r
\r
-class FlaggedItem(MetaContent):\r
- """A flag on a Question or Answer indicating offensive content."""\r
- flagged_at = models.DateTimeField(default=datetime.datetime.now)\r
\r
- objects = FlaggedItemManager()\r
+ class Meta:\r
+ app_label = 'forum'\r
\r
- class Meta(MetaContent.Meta):\r
- unique_together = ('content_type', 'object_id', 'user')\r
- db_table = u'flagged_item'\r
\r
- def __unicode__(self):\r
- return '[%s] flagged at %s' %(self.user, self.flagged_at)\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
-class Comment(MetaContent):\r
- comment = models.CharField(max_length=300)\r
- added_at = models.DateTimeField(default=datetime.datetime.now)\r
+ awarded_at = models.DateTimeField(default=datetime.datetime.now)\r
\r
- class Meta(MetaContent.Meta):\r
- ordering = ('-added_at',)\r
- db_table = u'comment'\r
+ trigger = models.ForeignKey(Action, related_name="awards", null=True)\r
+ action = models.OneToOneField(Action, related_name="award")\r
\r
- def save(self,**kwargs):\r
- super(Comment,self).save(**kwargs)\r
- try:\r
- ping_google()\r
- except Exception:\r
- logging.debug('problem pinging google did you register you sitemap with google?')\r
\r
- def __unicode__(self):\r
- return self.comment
\ No newline at end of file
+ class Meta:\r
+ unique_together = ('user', 'badge', 'node')\r
+ app_label = 'forum'
\ No newline at end of file