Add thread pruning for threads that pass the maximum page threshhold
Each board is allowed a number of threads per page, and a maximum number of pages (default 10 for both). If a thread falls off the last page, it is deleted. Signed-off-by: Alek Ratzloff <alekratz@gmail.com>
This commit is contained in:
@@ -39,21 +39,30 @@ class Board(models.Model):
|
||||
url = models.CharField(max_length=255, null=False, blank=False, unique=True)
|
||||
# Human-readable name for the board
|
||||
name = models.CharField(max_length=255, null=False, blank=False)
|
||||
# Max pages
|
||||
max_pages = models.IntegerField(default=10)
|
||||
# Threads per page
|
||||
threads_per_page = models.IntegerField(default=10)
|
||||
|
||||
@property
|
||||
def threads(self):
|
||||
return Post.objects.filter(board=self, op=None)
|
||||
|
||||
@property
|
||||
def max_threads(self):
|
||||
return self.max_pages * self.threads_per_page
|
||||
|
||||
def prune_threads(self):
|
||||
to_remove = self.threads.order_by("-last_bump")[self.max_threads :]
|
||||
for thread in to_remove:
|
||||
thread.delete()
|
||||
|
||||
|
||||
class Post(models.Model):
|
||||
# Board that this post was made on
|
||||
board = models.ForeignKey("Board", on_delete=models.CASCADE)
|
||||
# Thread that this is a part of
|
||||
op = models.ForeignKey(
|
||||
"self", null=True, on_delete=models.CASCADE, related_name="all_replies"
|
||||
)
|
||||
# Post that this is replying to
|
||||
reply = models.ForeignKey(
|
||||
"self", null=True, on_delete=models.CASCADE, related_name="replies"
|
||||
)
|
||||
# User's supplied name for this post
|
||||
@@ -139,9 +148,14 @@ class Post(models.Model):
|
||||
|
||||
@receiver(signals.post_save, sender=Post)
|
||||
def post_created(sender, instance, created, **kwargs):
|
||||
if created and instance.op:
|
||||
instance.op.last_bump = timezone.now()
|
||||
instance.op.save()
|
||||
if created:
|
||||
if instance.op:
|
||||
# Update the bump
|
||||
instance.op.last_bump = timezone.now()
|
||||
instance.op.save()
|
||||
else:
|
||||
# Prune threads for the board
|
||||
instance.board.prune_threads()
|
||||
|
||||
|
||||
@receiver(signals.post_delete, sender=Post)
|
||||
|
||||
Reference in New Issue
Block a user