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)
|
||||
|
||||
@@ -3,7 +3,6 @@ from django.http import Http404, HttpResponseRedirect
|
||||
from django.shortcuts import render, get_object_or_404
|
||||
from django.views.generic import DetailView
|
||||
from django.views.generic.edit import CreateView
|
||||
|
||||
from board.models import Post, Board
|
||||
|
||||
__all__ = ("BoardView", "PostView")
|
||||
@@ -29,7 +28,6 @@ class BoardView(CreateView):
|
||||
fields = ["subject", "name", "text", "image"]
|
||||
slug_field = "url"
|
||||
slug_url_kwarg = "url"
|
||||
|
||||
template_name = "board/board_detail.html"
|
||||
|
||||
def get_context_data(self, **kwargs):
|
||||
@@ -38,13 +36,11 @@ class BoardView(CreateView):
|
||||
kwargs["board"] = board
|
||||
page = self.kwargs.get("page", 1)
|
||||
|
||||
# TODO - allow max number of pages to be specified per-board
|
||||
if page not in range(1, 10 + 1):
|
||||
if page not in range(1, board.max_pages + 1):
|
||||
raise Http404()
|
||||
|
||||
PER_PAGE = 10
|
||||
start = (page - 1) * PER_PAGE
|
||||
end = start + PER_PAGE
|
||||
start = (page - 1) * board.threads_per_page
|
||||
end = start + board.threads_per_page
|
||||
kwargs["threads"] = board.threads.order_by("-last_bump")[start:end]
|
||||
kwargs["page"] = page
|
||||
kwargs["max_upload_size"] = settings.MAX_UPLOAD_SIZE
|
||||
|
||||
Reference in New Issue
Block a user