bgneal@75
|
1 """
|
bgneal@75
|
2 Models for the forums application.
|
bgneal@75
|
3 """
|
bgneal@102
|
4 import datetime
|
bgneal@102
|
5
|
bgneal@75
|
6 from django.db import models
|
bgneal@100
|
7 from django.db.models import Q
|
bgneal@75
|
8 from django.contrib.auth.models import User, Group
|
bgneal@75
|
9 from django.template.loader import render_to_string
|
bgneal@75
|
10
|
bgneal@75
|
11
|
bgneal@75
|
12 class Category(models.Model):
|
bgneal@100
|
13 """
|
bgneal@100
|
14 Forums belong to a category, whose access may be assigned to groups.
|
bgneal@100
|
15 """
|
bgneal@75
|
16 name = models.CharField(max_length=80)
|
bgneal@75
|
17 slug = models.SlugField(max_length=80)
|
bgneal@75
|
18 position = models.IntegerField(blank=True, default=0)
|
bgneal@75
|
19 groups = models.ManyToManyField(Group, blank=True, null=True,
|
bgneal@75
|
20 help_text="If groups are assigned to this category, only members" \
|
bgneal@75
|
21 " of those groups can view this category.")
|
bgneal@75
|
22
|
bgneal@75
|
23 class Meta:
|
bgneal@75
|
24 ordering = ('position', )
|
bgneal@75
|
25 verbose_name_plural = 'Categories'
|
bgneal@75
|
26
|
bgneal@75
|
27 def __unicode__(self):
|
bgneal@75
|
28 return self.name
|
bgneal@75
|
29
|
bgneal@100
|
30 def can_access(self, user):
|
bgneal@100
|
31 """
|
bgneal@100
|
32 Checks to see if the given user has permission to access
|
bgneal@100
|
33 this category.
|
bgneal@100
|
34 If this category has no groups assigned to it, return true.
|
bgneal@100
|
35 Else, return true if the user belongs to a group that has been
|
bgneal@100
|
36 assigned to this category, and false otherwise.
|
bgneal@100
|
37 """
|
bgneal@100
|
38 if self.groups.count() == 0:
|
bgneal@100
|
39 return True
|
bgneal@100
|
40 if user.is_authenticated():
|
bgneal@100
|
41 return self.groups.filter(user__pk=user.id).count() > 0
|
bgneal@100
|
42 return False
|
bgneal@100
|
43
|
bgneal@100
|
44
|
bgneal@100
|
45 class ForumManager(models.Manager):
|
bgneal@100
|
46 """
|
bgneal@100
|
47 The manager for the Forum model. Provides a centralized place to
|
bgneal@100
|
48 put commonly used and useful queries.
|
bgneal@100
|
49 """
|
bgneal@100
|
50
|
bgneal@100
|
51 def forums_for_user(self, user):
|
bgneal@100
|
52 """
|
bgneal@100
|
53 Returns a queryset containing the forums that the given user can
|
bgneal@100
|
54 "see" due to authenticated status, superuser status and group membership.
|
bgneal@100
|
55 """
|
bgneal@100
|
56 if user.is_superuser:
|
bgneal@100
|
57 qs = self.all()
|
bgneal@100
|
58 else:
|
bgneal@100
|
59 user_groups = []
|
bgneal@100
|
60 if user.is_authenticated():
|
bgneal@100
|
61 user_groups = user.groups.all()
|
bgneal@100
|
62
|
bgneal@100
|
63 qs = self.filter(Q(category__groups__isnull=True) | \
|
bgneal@100
|
64 Q(category__groups__in=user_groups))
|
bgneal@100
|
65
|
bgneal@100
|
66 return qs.select_related('category', 'last_post', 'last_post__user')
|
bgneal@100
|
67
|
bgneal@75
|
68
|
bgneal@75
|
69 class Forum(models.Model):
|
bgneal@100
|
70 """
|
bgneal@100
|
71 A forum is a collection of topics.
|
bgneal@100
|
72 """
|
bgneal@75
|
73 category = models.ForeignKey(Category, related_name='forums')
|
bgneal@75
|
74 name = models.CharField(max_length=80)
|
bgneal@75
|
75 slug = models.SlugField(max_length=80)
|
bgneal@75
|
76 description = models.TextField(blank=True, default='')
|
bgneal@75
|
77 position = models.IntegerField(blank=True, default=0)
|
bgneal@75
|
78 moderators = models.ManyToManyField(Group, blank=True, null=True)
|
bgneal@75
|
79
|
bgneal@75
|
80 # denormalized fields to reduce database hits
|
bgneal@75
|
81 topic_count = models.IntegerField(blank=True, default=0)
|
bgneal@75
|
82 post_count = models.IntegerField(blank=True, default=0)
|
bgneal@75
|
83 last_post = models.OneToOneField('Post', blank=True, null=True,
|
bgneal@75
|
84 related_name='parent_forum')
|
bgneal@75
|
85
|
bgneal@100
|
86 objects = ForumManager()
|
bgneal@100
|
87
|
bgneal@75
|
88 class Meta:
|
bgneal@75
|
89 ordering = ('position', )
|
bgneal@75
|
90
|
bgneal@75
|
91 def __unicode__(self):
|
bgneal@75
|
92 return self.name
|
bgneal@75
|
93
|
bgneal@81
|
94 @models.permalink
|
bgneal@81
|
95 def get_absolute_url(self):
|
bgneal@81
|
96 return ('forums-forum_index', [self.slug])
|
bgneal@81
|
97
|
bgneal@75
|
98 def topic_count_update(self):
|
bgneal@75
|
99 """Call to notify the forum that its topic count has been updated."""
|
bgneal@75
|
100 self.topic_count = Topic.objects.filter(forum=self).count()
|
bgneal@75
|
101
|
bgneal@75
|
102 def post_count_update(self):
|
bgneal@75
|
103 """Call to notify the forum that its post count has been updated."""
|
bgneal@75
|
104 my_posts = Post.objects.filter(topic__forum=self)
|
bgneal@75
|
105 self.post_count = my_posts.count()
|
bgneal@75
|
106 if self.post_count > 0:
|
bgneal@75
|
107 self.last_post = my_posts[self.post_count - 1]
|
bgneal@75
|
108 else:
|
bgneal@75
|
109 self.last_post = None
|
bgneal@75
|
110
|
bgneal@75
|
111
|
bgneal@75
|
112 class Topic(models.Model):
|
bgneal@100
|
113 """
|
bgneal@100
|
114 A topic is a thread of discussion, consisting of a series of posts.
|
bgneal@100
|
115 """
|
bgneal@75
|
116 forum = models.ForeignKey(Forum, related_name='topics')
|
bgneal@75
|
117 name = models.CharField(max_length=255)
|
bgneal@75
|
118 creation_date = models.DateTimeField(auto_now_add=True)
|
bgneal@75
|
119 user = models.ForeignKey(User)
|
bgneal@75
|
120 view_count = models.IntegerField(blank=True, default=0)
|
bgneal@75
|
121 sticky = models.BooleanField(blank=True, default=False)
|
bgneal@75
|
122 locked = models.BooleanField(blank=True, default=False)
|
bgneal@75
|
123
|
bgneal@75
|
124 # denormalized fields to reduce database hits
|
bgneal@75
|
125 post_count = models.IntegerField(blank=True, default=0)
|
bgneal@102
|
126 update_date = models.DateTimeField()
|
bgneal@75
|
127 last_post = models.OneToOneField('Post', blank=True, null=True,
|
bgneal@75
|
128 related_name='parent_topic')
|
bgneal@75
|
129
|
bgneal@75
|
130 class Meta:
|
bgneal@75
|
131 ordering = ('-sticky', '-update_date', )
|
bgneal@75
|
132
|
bgneal@75
|
133 def __unicode__(self):
|
bgneal@75
|
134 return self.name
|
bgneal@75
|
135
|
bgneal@82
|
136 @models.permalink
|
bgneal@82
|
137 def get_absolute_url(self):
|
bgneal@82
|
138 return ('forums-topic_index', [self.pk])
|
bgneal@82
|
139
|
bgneal@75
|
140 def post_count_update(self):
|
bgneal@75
|
141 """
|
bgneal@75
|
142 Call this function to notify the topic instance that its post count
|
bgneal@75
|
143 has changed.
|
bgneal@75
|
144 """
|
bgneal@75
|
145 my_posts = Post.objects.filter(topic=self)
|
bgneal@75
|
146 self.post_count = my_posts.count()
|
bgneal@75
|
147 if self.post_count > 0:
|
bgneal@75
|
148 self.last_post = my_posts[self.post_count - 1]
|
bgneal@75
|
149 self.update_date = self.last_post.creation_date
|
bgneal@75
|
150 else:
|
bgneal@75
|
151 self.last_post = None
|
bgneal@75
|
152 self.update_date = self.creation_date
|
bgneal@75
|
153
|
bgneal@83
|
154 def reply_count(self):
|
bgneal@83
|
155 """
|
bgneal@83
|
156 Returns the number of replies to a topic. The first post
|
bgneal@83
|
157 doesn't count as a reply.
|
bgneal@83
|
158 """
|
bgneal@83
|
159 if self.post_count > 1:
|
bgneal@83
|
160 return self.post_count - 1
|
bgneal@83
|
161 return 0
|
bgneal@83
|
162
|
bgneal@102
|
163 def save(self, *args, **kwargs):
|
bgneal@102
|
164 if not self.id:
|
bgneal@102
|
165 now = datetime.datetime.now()
|
bgneal@102
|
166 self.creation_date = now
|
bgneal@102
|
167 self.update_date = now
|
bgneal@102
|
168
|
bgneal@102
|
169 super(Topic, self).save(*args, **kwargs)
|
bgneal@102
|
170
|
bgneal@75
|
171
|
bgneal@75
|
172 class Post(models.Model):
|
bgneal@100
|
173 """
|
bgneal@100
|
174 A post is an instance of a user's single contribution to a topic.
|
bgneal@100
|
175 """
|
bgneal@75
|
176 topic = models.ForeignKey(Topic, related_name='posts')
|
bgneal@75
|
177 user = models.ForeignKey(User, related_name='posts')
|
bgneal@75
|
178 creation_date = models.DateTimeField(auto_now_add=True)
|
bgneal@75
|
179 update_date = models.DateTimeField(auto_now=True)
|
bgneal@75
|
180 body = models.TextField()
|
bgneal@75
|
181 html = models.TextField()
|
bgneal@83
|
182 user_ip = models.IPAddressField(blank=True, default='', null=True)
|
bgneal@75
|
183
|
bgneal@75
|
184 class Meta:
|
bgneal@97
|
185 ordering = ('creation_date', )
|
bgneal@75
|
186
|
bgneal@91
|
187 @models.permalink
|
bgneal@91
|
188 def get_absolute_url(self):
|
bgneal@91
|
189 return ('forums-goto_post', [self.pk])
|
bgneal@91
|
190
|
bgneal@75
|
191 def summary(self):
|
bgneal@75
|
192 LIMIT = 50
|
bgneal@75
|
193 if len(self.body) < LIMIT:
|
bgneal@75
|
194 return self.body
|
bgneal@75
|
195 return self.body[:LIMIT] + '...'
|
bgneal@75
|
196
|
bgneal@75
|
197 def __unicode__(self):
|
bgneal@75
|
198 return self.summary()
|
bgneal@75
|
199
|
bgneal@75
|
200 def save(self, *args, **kwargs):
|
bgneal@75
|
201 html = render_to_string('forums/post.html', {'data': self.body})
|
bgneal@75
|
202 self.html = html.strip()
|
bgneal@75
|
203 super(Post, self).save(*args, **kwargs)
|
bgneal@75
|
204
|
bgneal@75
|
205 def delete(self, *args, **kwargs):
|
bgneal@75
|
206 first_post_id = self.topic.posts.all()[0].id
|
bgneal@75
|
207 super(Post, self).delete(*args, **kwargs)
|
bgneal@75
|
208 if self.id == first_post_id:
|
bgneal@75
|
209 self.topic.delete()
|
bgneal@75
|
210
|
bgneal@98
|
211
|
bgneal@98
|
212 class FlaggedPost(models.Model):
|
bgneal@98
|
213 """This model represents a user flagging a post as inappropriate."""
|
bgneal@98
|
214 user = models.ForeignKey(User)
|
bgneal@98
|
215 post = models.ForeignKey(Post)
|
bgneal@98
|
216 flag_date = models.DateTimeField(auto_now_add=True)
|
bgneal@98
|
217
|
bgneal@98
|
218 def __unicode__(self):
|
bgneal@98
|
219 return u'Post ID %s flagged by %s' % (self.post.id, self.user.username)
|
bgneal@98
|
220
|
bgneal@98
|
221 class Meta:
|
bgneal@98
|
222 ordering = ('flag_date', )
|
bgneal@98
|
223
|
bgneal@98
|
224 def get_post_url(self):
|
bgneal@98
|
225 return '<a href="%s">Post</a>' % self.post.get_absolute_url()
|
bgneal@98
|
226 get_post_url.allow_tags = True
|
bgneal@98
|
227
|
bgneal@75
|
228 # TODO: A "read" table
|