4 class Vote(MetaContent, UserContent):
12 vote = models.SmallIntegerField(choices=VOTE_CHOICES)
13 voted_at = models.DateTimeField(default=datetime.datetime.now)
14 canceled = models.BooleanField(default=False)
16 active = ActiveObjectManager()
18 class Meta(MetaContent.Meta):
21 def __unicode__(self):
22 return '[%s] voted at %s: %s' %(self.user, self.voted_at, self.vote)
24 def _update_post_vote_count(self, diff):
26 field = self.vote == 1 and 'vote_up_count' or 'vote_down_count'
27 post.__dict__[field] = post.__dict__[field] + diff
30 def save(self, *args, **kwargs):
31 super(Vote, self).save(*args, **kwargs)
33 self._update_post_vote_count(1)
39 self._update_post_vote_count(-1)
40 vote_canceled.send(sender=Vote, instance=self)
43 return self.vote == self.VOTE_UP
45 def is_downvote(self):
46 return self.vote == self.VOTE_DOWN
48 vote_canceled = django.dispatch.Signal(providing_args=['instance'])
50 class FlaggedItem(MetaContent, UserContent):
51 """A flag on a Question or Answer indicating offensive content."""
52 flagged_at = models.DateTimeField(default=datetime.datetime.now)
53 reason = models.CharField(max_length=300, null=True)
54 canceled = models.BooleanField(default=False)
56 active = ActiveObjectManager()
58 class Meta(MetaContent.Meta):
59 db_table = u'flagged_item'
61 def __unicode__(self):
62 return '[%s] flagged at %s' %(self.user, self.flagged_at)
64 def _update_post_flag_count(self, diff):
66 post.offensive_flag_count = post.offensive_flag_count + diff
69 def save(self, *args, **kwargs):
70 super(FlaggedItem, self).save(*args, **kwargs)
72 self._update_post_flag_count(1)
78 self._update_post_flag_count(-1)
81 class Comment(MetaContent, UserContent, DeletableContent):
82 comment = models.CharField(max_length=300)
83 added_at = models.DateTimeField(default=datetime.datetime.now)
84 score = models.IntegerField(default=0)
85 liked_by = models.ManyToManyField(User, through='LikedComment', related_name='comments_liked')
87 class Meta(MetaContent.Meta):
88 ordering = ('-added_at',)
91 def _update_post_comment_count(self, diff):
93 post.comment_count = post.comment_count + diff
96 def save(self, *args, **kwargs):
97 super(Comment,self).save(*args, **kwargs)
100 self._update_post_comment_count(1)
105 logging.debug('problem pinging google did you register you sitemap with google?')
107 def mark_deleted(self, user):
108 if super(Comment, self).mark_deleted(user):
109 self._update_post_comment_count(-1)
111 def unmark_deleted(self):
112 if super(Comment, self).unmark_deleted():
113 self._update_post_comment_count(1)
115 def is_reply_to(self, user):
116 inreply = re.search('@\w+', self.comment)
117 if inreply is not None:
118 return user.username.startswith(inreply.group(0))
122 def __unicode__(self):
126 class LikedComment(models.Model):
127 comment = models.ForeignKey(Comment)
128 user = models.ForeignKey(User)
129 added_at = models.DateTimeField(default=datetime.datetime.now)
130 canceled = models.BooleanField(default=False)
132 active = ActiveObjectManager()
137 def _update_comment_score(self, diff):
138 self.comment.score = self.comment.score + diff
141 def save(self, *args, **kwargs):
142 super(LikedComment, self).save(*args, **kwargs)
144 self._update_comment_score(1)
147 if not self.canceled:
150 self._update_comment_score(-1)