+from django.utils.translation import ugettext as _
from base import *
-import re
-class Vote(MetaContent, UserContent):
- VOTE_UP = +1
- VOTE_DOWN = -1
- VOTE_CHOICES = (
- (VOTE_UP, u'Up'),
- (VOTE_DOWN, u'Down'),
- )
+class Vote(models.Model):
+ user = models.ForeignKey(User, related_name="votes")
+ node = models.ForeignKey(Node, related_name="votes")
+ value = models.SmallIntegerField()
+ action = models.OneToOneField(Action, related_name="vote")
+ voted_at = models.DateTimeField(default=datetime.datetime.now)
- vote = models.SmallIntegerField(choices=VOTE_CHOICES)
- voted_at = models.DateTimeField(default=datetime.datetime.now)
- canceled = models.BooleanField(default=False)
-
- active = ActiveObjectManager()
+ class Meta:
+ app_label = 'forum'
+ unique_together = ('user', 'node')
- class Meta(MetaContent.Meta):
- db_table = u'vote'
- def __unicode__(self):
- return '[%s] voted at %s: %s' %(self.user, self.voted_at, self.vote)
+class Flag(models.Model):
+ user = models.ForeignKey(User, related_name="flags")
+ node = models.ForeignKey(Node, related_name="flags")
+ reason = models.CharField(max_length=300)
+ action = models.OneToOneField(Action, related_name="flag")
+ flagged_at = models.DateTimeField(default=datetime.datetime.now)
- def _update_post_vote_count(self, diff):
- post = self.content_object
- field = self.vote == 1 and 'vote_up_count' or 'vote_down_count'
- post.__dict__[field] = post.__dict__[field] + diff
- post.save()
+ class Meta:
+ app_label = 'forum'
+ unique_together = ('user', 'node')
- def save(self, *args, **kwargs):
- super(Vote, self).save(*args, **kwargs)
- if self._is_new:
- self._update_post_vote_count(1)
+class BadgesQuerySet(models.query.QuerySet):
+ def get(self, *args, **kwargs):
+ try:
+ pk = [v for (k,v) in kwargs.items() if k in ('pk', 'pk__exact', 'id', 'id__exact')][0]
+ except:
+ return super(BadgesQuerySet, self).get(*args, **kwargs)
- def cancel(self):
- if not self.canceled:
- self.canceled = True
- self.save()
- self._update_post_vote_count(-1)
- vote_canceled.send(sender=Vote, instance=self)
+ from forum.badges.base import BadgesMeta
+ badge = BadgesMeta.by_id.get(int(pk), None)
+ if not badge:
+ return super(BadgesQuerySet, self).get(*args, **kwargs)
+ return badge.ondb
- def is_upvote(self):
- return self.vote == self.VOTE_UP
- def is_downvote(self):
- return self.vote == self.VOTE_DOWN
+class BadgeManager(models.Manager):
+ use_for_related_fields = True
-vote_canceled = django.dispatch.Signal(providing_args=['instance'])
+ def get_query_set(self):
+ return BadgesQuerySet(self.model)
-class FlaggedItem(MetaContent, UserContent):
- """A flag on a Question or Answer indicating offensive content."""
- flagged_at = models.DateTimeField(default=datetime.datetime.now)
- reason = models.CharField(max_length=300, null=True)
- canceled = models.BooleanField(default=False)
+class Badge(models.Model):
+ GOLD = 1
+ SILVER = 2
+ BRONZE = 3
- active = ActiveObjectManager()
+ type = models.SmallIntegerField()
+ cls = models.CharField(max_length=50, null=True)
+ awarded_count = models.PositiveIntegerField(default=0)
+
+ awarded_to = models.ManyToManyField(User, through='Award', related_name='badges')
- class Meta(MetaContent.Meta):
- db_table = u'flagged_item'
+ objects = BadgeManager()
- def __unicode__(self):
- return '[%s] flagged at %s' %(self.user, self.flagged_at)
+ @property
+ def name(self):
+ cls = self.__dict__.get('_class', None)
+ return cls and cls.name or _("Unknown")
- def _update_post_flag_count(self, diff):
- post = self.content_object
- post.offensive_flag_count = post.offensive_flag_count + diff
- post.save()
+ @property
+ def description(self):
+ cls = self.__dict__.get('_class', None)
+ return cls and cls.description or _("No description available")
- 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)
-
-
-class Comment(MetaContent, UserContent, DeletableContent):
- comment = models.CharField(max_length=300)
- added_at = models.DateTimeField(default=datetime.datetime.now)
- score = models.IntegerField(default=0)
- liked_by = models.ManyToManyField(User, through='LikedComment', related_name='comments_liked')
-
- class Meta(MetaContent.Meta):
- ordering = ('-added_at',)
- db_table = u'comment'
-
- def _update_post_comment_count(self, diff):
- post = self.content_object
- post.comment_count = post.comment_count + diff
- post.save()
+ @models.permalink
+ def get_absolute_url(self):
+ return ('badge', [], {'id': self.id, 'slug': slugify(self.name)})
def save(self, *args, **kwargs):
- super(Comment,self).save(*args, **kwargs)
-
- if self._is_new:
- self._update_post_comment_count(1)
-
- try:
- ping_google()
- except Exception:
- logging.debug('problem pinging google did you register you sitemap with google?')
+ if isinstance(self.awarded_count, models.expressions.ExpressionNode):
+ super(Badge, self).save(*args, **kwargs)
+ self.awarded_count = self.__class__.objects.filter(id=self.id).values_list('awarded_count', flat=True)[0]
+ else:
+ super(Badge, self).save(*args, **kwargs)
- def mark_deleted(self, user):
- if super(Comment, self).mark_deleted(user):
- self._update_post_comment_count(-1)
-
- def unmark_deleted(self):
- if super(Comment, self).unmark_deleted():
- self._update_post_comment_count(1)
-
- def is_reply_to(self, user):
- inreply = re.search('@\w+', self.comment)
- if inreply is not None:
- return user.username.startswith(inreply[1:])
-
- return False
-
- def __unicode__(self):
- return self.comment
-
-
-class LikedComment(models.Model):
- comment = models.ForeignKey(Comment)
- user = models.ForeignKey(User)
- added_at = models.DateTimeField(default=datetime.datetime.now)
- canceled = models.BooleanField(default=False)
-
- active = ActiveObjectManager()
class Meta:
app_label = 'forum'
- def _update_comment_score(self, diff):
- self.comment.score = self.comment.score + diff
- self.comment.save()
- def save(self, *args, **kwargs):
- super(LikedComment, self).save(*args, **kwargs)
- if self._is_new:
- self._update_comment_score(1)
+class Award(models.Model):
+ user = models.ForeignKey(User)
+ badge = models.ForeignKey('Badge', related_name="awards")
+ node = models.ForeignKey(Node, null=True)
- def cancel(self):
- if not self.canceled:
- self.canceled = True
- self.save()
- self._update_comment_score(-1)
+ awarded_at = models.DateTimeField(default=datetime.datetime.now)
+ trigger = models.ForeignKey(Action, related_name="awards", null=True)
+ action = models.OneToOneField(Action, related_name="award")
+
+ class Meta:
+ unique_together = ('user', 'badge', 'node')
+ app_label = 'forum'
\ No newline at end of file