]> git.openstreetmap.org Git - osqa.git/blob - forum/models/meta.py
ALteration of the schema to a single content model. As a bonus there is a complete...
[osqa.git] / forum / models / meta.py
1 from base import *
2 import re
3
4 class Vote(MetaContent, UserContent):
5     VOTE_UP = +1
6     VOTE_DOWN = -1
7     VOTE_CHOICES = (
8         (VOTE_UP,   u'Up'),
9         (VOTE_DOWN, u'Down'),
10     )
11
12     vote           = models.SmallIntegerField(choices=VOTE_CHOICES)
13     voted_at       = models.DateTimeField(default=datetime.datetime.now)
14     canceled       = models.BooleanField(default=False)
15
16     active = ActiveObjectManager()
17
18     class Meta(MetaContent.Meta):
19         db_table = u'vote'
20
21     def __unicode__(self):
22         return '[%s] voted at %s: %s' %(self.user, self.voted_at, self.vote)
23
24     def _update_post_vote_count(self, diff):
25         post = self.node
26         field = self.vote == 1 and 'vote_up_count' or 'vote_down_count'
27         post.__dict__[field] = post.__dict__[field] + diff
28         post.save()
29
30     def save(self, *args, **kwargs):
31         super(Vote, self).save(*args, **kwargs)
32         if self._is_new:
33             self._update_post_vote_count(1)
34
35     def cancel(self):
36         if not self.canceled:
37             self.canceled = True
38             self.save()
39             self._update_post_vote_count(-1)
40             vote_canceled.send(sender=Vote, instance=self)
41
42     def is_upvote(self):
43         return self.vote == self.VOTE_UP
44
45     def is_downvote(self):
46         return self.vote == self.VOTE_DOWN
47
48 vote_canceled = django.dispatch.Signal(providing_args=['instance'])
49
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)
55
56     active = ActiveObjectManager()
57
58     class Meta(MetaContent.Meta):
59         db_table = u'flagged_item'
60
61     def __unicode__(self):
62         return '[%s] flagged at %s' %(self.user, self.flagged_at)
63
64     def _update_post_flag_count(self, diff):
65         post = self.node
66         post.offensive_flag_count = post.offensive_flag_count + diff
67         post.save()
68
69     def save(self, *args, **kwargs):
70         super(FlaggedItem, self).save(*args, **kwargs)
71         if self._is_new:
72             self._update_post_flag_count(1)
73
74     def cancel(self):
75         if not self.canceled:
76             self.canceled = True
77             self.save()
78             self._update_post_flag_count(-1)
79             
80
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')
86
87     class Meta(MetaContent.Meta):
88         ordering = ('-added_at',)
89         db_table = u'comment'
90
91     def _update_post_comment_count(self, diff):
92         post = self.node.leaf
93         post.comment_count = post.comment_count + diff
94         post.save()
95
96     def save(self, *args, **kwargs):
97         super(Comment,self).save(*args, **kwargs)
98
99         if self._is_new:
100             self._update_post_comment_count(1)
101
102         try:
103             ping_google()
104         except Exception:
105             logging.debug('problem pinging google did you register you sitemap with google?')
106
107     def mark_deleted(self, user):
108         if super(Comment, self).mark_deleted(user):
109             self._update_post_comment_count(-1)
110
111     def unmark_deleted(self):
112         if super(Comment, self).unmark_deleted():
113             self._update_post_comment_count(1)
114
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))
119
120         return False
121
122     def __unicode__(self):
123         return self.comment
124
125
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)
131
132     active = ActiveObjectManager()
133
134     class Meta:
135         app_label = 'forum'
136
137     def _update_comment_score(self, diff):
138         self.comment.score = self.comment.score + diff
139         self.comment.save()
140
141     def save(self, *args, **kwargs):
142         super(LikedComment, self).save(*args, **kwargs)
143         if self._is_new:
144             self._update_comment_score(1)
145
146     def cancel(self):
147         if not self.canceled:
148             self.canceled = True
149             self.save()
150             self._update_comment_score(-1)
151
152