Add board locking, update tests, and update clean() methods

* Boards can be locked from allowing posts - this is can be useful for
  things like archived boards or locking down in the event of an
  emergency
* Some validation checks for new posts are from the reply/thread form to
  the Post model's clean() method
* Add some new tests, I've really been falling behind on those

Signed-off-by: Alek Ratzloff <alekratz@gmail.com>
This commit is contained in:
2022-07-12 18:30:16 -07:00
parent 250ebe2b84
commit d3e1f5a978
3 changed files with 70 additions and 6 deletions

View File

@@ -58,6 +58,8 @@ class Board(models.Model):
# Auto-sink threshhold. This is the number of replies that a thread can have
# before it stops being bumped.
autosink = models.IntegerField(default=300)
# Whether this board is read-only or not.
readonly = models.BooleanField(default=False)
@property
def threads(self):
@@ -115,10 +117,10 @@ class Post(models.Model):
# Thumbnail
thumbnail = models.ImageField(upload_to=thumbs_upload, editable=False, null=True)
# Original image name
original_image_name = models.CharField(max_length=255, null=True)
original_image_name = models.CharField(max_length=255, null=True, blank=True)
# Image width and height
image_width = models.IntegerField(null=True)
image_height = models.IntegerField(null=True)
image_width = models.IntegerField(null=True, blank=True)
image_height = models.IntegerField(null=True, blank=True)
class Meta:
permissions = [
@@ -183,6 +185,15 @@ class Post(models.Model):
_("Image supplied is too large. Maximum image size is %(max)s"),
params={"max": settings.MAX_UPLOAD_SIZE},
)
# Check if board is readonly
if self.board.readonly:
raise ValidationError(
_("This board is in readonly mode, you cannot create new posts.")
)
# Check if OP is locked
if self.op and self.op.lock:
raise ValidationError(_("This thread is locked, you cannot reply to it"))
# Rate limiting for posts
# TODO BUG: if a user's last post is deleted, and it is their only post,
# they will not hit rate limit. This could probably be abused