comparison forums/models.py @ 1206:02181fa5ac9d modernize tip

Update to Django 1.9.
author Brian Neal <bgneal@gmail.com>
date Wed, 22 Jan 2025 17:58:16 -0600
parents eeaf387803c6
children
comparison
equal deleted inserted replaced
1205:510ef3cbf3e6 1206:02181fa5ac9d
80 80
81 class Forum(models.Model): 81 class Forum(models.Model):
82 """ 82 """
83 A forum is a collection of topics. 83 A forum is a collection of topics.
84 """ 84 """
85 category = models.ForeignKey(Category, related_name='forums') 85 category = models.ForeignKey(Category, related_name='forums',
86 on_delete=models.CASCADE)
86 name = models.CharField(max_length=80) 87 name = models.CharField(max_length=80)
87 slug = models.SlugField(max_length=80) 88 slug = models.SlugField(max_length=80)
88 description = models.TextField(blank=True, default='') 89 description = models.TextField(blank=True, default='')
89 position = models.IntegerField(blank=True, default=0) 90 position = models.IntegerField(blank=True, default=0)
90 moderators = models.ManyToManyField(Group, blank=True) 91 moderators = models.ManyToManyField(Group, blank=True)
91 92
92 # denormalized fields to reduce database hits 93 # denormalized fields to reduce database hits
93 topic_count = models.IntegerField(blank=True, default=0) 94 topic_count = models.IntegerField(blank=True, default=0)
94 post_count = models.IntegerField(blank=True, default=0) 95 post_count = models.IntegerField(blank=True, default=0)
95 last_post = models.OneToOneField('Post', blank=True, null=True, 96 last_post = models.OneToOneField(
96 related_name='parent_forum') 97 'Post', blank=True, null=True, related_name='parent_forum',
98 on_delete=models.CASCADE)
97 99
98 objects = ForumManager() 100 objects = ForumManager()
99 101
100 class Meta: 102 class Meta:
101 ordering = ('position', ) 103 ordering = ('position', )
168 170
169 class Topic(models.Model): 171 class Topic(models.Model):
170 """ 172 """
171 A topic is a thread of discussion, consisting of a series of posts. 173 A topic is a thread of discussion, consisting of a series of posts.
172 """ 174 """
173 forum = models.ForeignKey(Forum, related_name='topics') 175 forum = models.ForeignKey(Forum, related_name='topics',
176 on_delete=models.CASCADE)
174 name = models.CharField(max_length=255) 177 name = models.CharField(max_length=255)
175 creation_date = models.DateTimeField(db_index=True) 178 creation_date = models.DateTimeField(db_index=True)
176 user = models.ForeignKey(User) 179 user = models.ForeignKey(User, on_delete=models.CASCADE)
177 view_count = models.IntegerField(blank=True, default=0) 180 view_count = models.IntegerField(blank=True, default=0)
178 sticky = models.BooleanField(blank=True, default=False) 181 sticky = models.BooleanField(blank=True, default=False)
179 locked = models.BooleanField(blank=True, default=False) 182 locked = models.BooleanField(blank=True, default=False)
180 subscribers = models.ManyToManyField(User, related_name='subscriptions', 183 subscribers = models.ManyToManyField(User, related_name='subscriptions',
181 verbose_name='subscribers', blank=True) 184 verbose_name='subscribers', blank=True)
183 verbose_name='bookmarkers', blank=True) 186 verbose_name='bookmarkers', blank=True)
184 187
185 # denormalized fields to reduce database hits 188 # denormalized fields to reduce database hits
186 post_count = models.IntegerField(blank=True, default=0) 189 post_count = models.IntegerField(blank=True, default=0)
187 update_date = models.DateTimeField(db_index=True) 190 update_date = models.DateTimeField(db_index=True)
188 last_post = models.OneToOneField('Post', blank=True, null=True, 191 last_post = models.OneToOneField(
189 related_name='parent_topic') 192 'Post', blank=True, null=True, related_name='parent_topic',
193 on_delete=models.CASCADE)
190 194
191 class Meta: 195 class Meta:
192 ordering = ('-sticky', '-update_date', ) 196 ordering = ('-sticky', '-update_date', )
193 197
194 def __unicode__(self): 198 def __unicode__(self):
274 278
275 class Post(models.Model): 279 class Post(models.Model):
276 """ 280 """
277 A post is an instance of a user's single contribution to a topic. 281 A post is an instance of a user's single contribution to a topic.
278 """ 282 """
279 topic = models.ForeignKey(Topic, related_name='posts') 283 topic = models.ForeignKey(Topic, related_name='posts',
280 user = models.ForeignKey(User, related_name='posts') 284 on_delete=models.CASCADE)
285 user = models.ForeignKey(User, related_name='posts',
286 on_delete=models.CASCADE)
281 creation_date = models.DateTimeField(db_index=True) 287 creation_date = models.DateTimeField(db_index=True)
282 update_date = models.DateTimeField(db_index=True) 288 update_date = models.DateTimeField(db_index=True)
283 body = models.TextField() 289 body = models.TextField()
284 html = models.TextField() 290 html = models.TextField()
285 user_ip = models.GenericIPAddressField(blank=True, default='', null=True) 291 user_ip = models.GenericIPAddressField(blank=True, default='', null=True)
334 return self.body 340 return self.body
335 341
336 342
337 class FlaggedPost(models.Model): 343 class FlaggedPost(models.Model):
338 """This model represents a user flagging a post as inappropriate.""" 344 """This model represents a user flagging a post as inappropriate."""
339 user = models.ForeignKey(User) 345 user = models.ForeignKey(User, on_delete=models.CASCADE)
340 post = models.ForeignKey(Post) 346 post = models.ForeignKey(Post, on_delete=models.CASCADE)
341 flag_date = models.DateTimeField(auto_now_add=True) 347 flag_date = models.DateTimeField(auto_now_add=True)
342 348
343 def __unicode__(self): 349 def __unicode__(self):
344 return u'Post ID %s flagged by %s' % (self.post.id, self.user.username) 350 return u'Post ID %s flagged by %s' % (self.post.id, self.user.username)
345 351
358 We keep track of a window of time, delimited by begin_date and end_date. 364 We keep track of a window of time, delimited by begin_date and end_date.
359 Topics updated within this window are tracked, and may have TopicLastVisit 365 Topics updated within this window are tracked, and may have TopicLastVisit
360 objects. 366 objects.
361 Marking a forum as all read sets the begin_date equal to the end_date. 367 Marking a forum as all read sets the begin_date equal to the end_date.
362 """ 368 """
363 user = models.ForeignKey(User) 369 user = models.ForeignKey(User, on_delete=models.CASCADE)
364 forum = models.ForeignKey(Forum) 370 forum = models.ForeignKey(Forum, on_delete=models.CASCADE)
365 begin_date = models.DateTimeField() 371 begin_date = models.DateTimeField()
366 end_date = models.DateTimeField() 372 end_date = models.DateTimeField()
367 373
368 class Meta: 374 class Meta:
369 unique_together = ('user', 'forum') 375 unique_together = ('user', 'forum')
381 """ 387 """
382 This model records the last time a user read a topic. 388 This model records the last time a user read a topic.
383 Objects of this class exist for the window specified in the 389 Objects of this class exist for the window specified in the
384 corresponding ForumLastVisit object. 390 corresponding ForumLastVisit object.
385 """ 391 """
386 user = models.ForeignKey(User) 392 user = models.ForeignKey(User, on_delete=models.CASCADE)
387 topic = models.ForeignKey(Topic) 393 topic = models.ForeignKey(Topic, on_delete=models.CASCADE)
388 last_visit = models.DateTimeField(db_index=True) 394 last_visit = models.DateTimeField(db_index=True)
389 395
390 class Meta: 396 class Meta:
391 unique_together = ('user', 'topic') 397 unique_together = ('user', 'topic')
392 ordering = ('-last_visit', ) 398 ordering = ('-last_visit', )
407 class Attachment(models.Model): 413 class Attachment(models.Model):
408 """ 414 """
409 This model is a "through" table for the M2M relationship between forum 415 This model is a "through" table for the M2M relationship between forum
410 posts and Oembed objects. 416 posts and Oembed objects.
411 """ 417 """
412 post = models.ForeignKey(Post) 418 post = models.ForeignKey(Post, on_delete=models.CASCADE)
413 embed = models.ForeignKey(Oembed) 419 embed = models.ForeignKey(Oembed, on_delete=models.CASCADE)
414 order = models.IntegerField() 420 order = models.IntegerField()
415 421
416 class Meta: 422 class Meta:
417 ordering = ('order', ) 423 ordering = ('order', )
418 424