Skip to content

Commit

Permalink
Add a max file size validator
Browse files Browse the repository at this point in the history
  • Loading branch information
thibault committed Oct 8, 2024
1 parent ddec8f1 commit 4e5acdf
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 1 deletion.
13 changes: 13 additions & 0 deletions envergo/confs/migrations/0006_merge_20241008_1517.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# Generated by Django 4.2.13 on 2024-10-08 13:17

from django.db import migrations


class Migration(migrations.Migration):

dependencies = [
("confs", "0005_alter_setting_setting"),
("confs", "0005_alter_setting_setting_hostedfile"),
]

operations = []
9 changes: 8 additions & 1 deletion envergo/confs/models.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from django.contrib.sites.models import Site
from django.core.exceptions import ValidationError
from django.db import models
from django.utils import timezone
from django.utils.translation import gettext_lazy as _
Expand Down Expand Up @@ -56,10 +57,16 @@ class Meta:
verbose_name_plural = _("Settings")


def max_file_size(value):
size_limit = 10 * 1024 * 1024 # 10 mb
if value.size > size_limit:
raise ValidationError("La taille est pour le moment limité à 10 Mo.")


class HostedFile(models.Model):
"""A single file."""

file = models.FileField(_("File"), upload_to="f")
file = models.FileField(_("File"), upload_to="f", validators=[max_file_size])
name = models.CharField(_("Name"), max_length=256)
description = models.TextField(_("Description"), blank=True)
uploaded_by = models.ForeignKey("users.User", on_delete=models.PROTECT)
Expand Down

0 comments on commit 4e5acdf

Please sign in to comment.