]> git.openstreetmap.org Git - osqa.git/blob - forum/models/meta.py
OSQA-141:
[osqa.git] / forum / models / meta.py
1 from base import *
2
3 class Vote(MetaContent, CancelableContent, UserContent):
4     VOTE_UP = +1
5     VOTE_DOWN = -1
6     VOTE_CHOICES = (
7         (VOTE_UP,   u'Up'),
8         (VOTE_DOWN, u'Down'),
9     )
10
11     vote           = models.SmallIntegerField(choices=VOTE_CHOICES)
12     voted_at       = models.DateTimeField(default=datetime.datetime.now)
13
14     active = ActiveObjectManager()
15
16     class Meta(MetaContent.Meta):
17         db_table = u'vote'
18
19     def __unicode__(self):
20         return '[%s] voted at %s: %s' %(self.user, self.voted_at, self.vote)
21
22     def _update_post_vote_count(self, diff):
23         post = self.node.leaf
24         field = self.vote == 1 and 'vote_up_count' or 'vote_down_count'
25         post.__dict__[field] = post.__dict__[field] + diff
26         post.save()
27
28     def cancel(self):
29         if super(Vote, self).cancel():
30             self._update_post_vote_count(-1)
31
32     def save(self, *args, **kwargs):
33         super(Vote, self).save(*args, **kwargs)
34         if self._is_new:
35             self._update_post_vote_count(1)
36
37     def is_upvote(self):
38         return self.vote == self.VOTE_UP
39
40     def is_downvote(self):
41         return self.vote == self.VOTE_DOWN
42
43
44 class FlaggedItem(MetaContent, UserContent):
45     flagged_at     = models.DateTimeField(default=datetime.datetime.now)
46     reason         = models.CharField(max_length=300, null=True)
47     canceled       = models.BooleanField(default=False)
48
49     active = ActiveObjectManager()
50
51     class Meta(MetaContent.Meta):
52         db_table = u'flagged_item'
53
54     def __unicode__(self):
55         return '[%s] flagged at %s' %(self.user, self.flagged_at)
56
57     def _update_post_flag_count(self, diff):
58         post = self.node
59         post.offensive_flag_count = post.offensive_flag_count + diff
60         post.save()
61
62     def save(self, *args, **kwargs):
63         super(FlaggedItem, self).save(*args, **kwargs)
64         if self._is_new:
65             self._update_post_flag_count(1)
66
67     def cancel(self):
68         if not self.canceled:
69             self.canceled = True
70             self.save()
71             self._update_post_flag_count(-1)
72             
73
74
75