This commit is contained in:
relikd
2024-06-27 21:13:47 +02:00
commit d44d74eb2c
72 changed files with 2444 additions and 0 deletions

View File

@@ -0,0 +1,23 @@
from django.core.validators import BaseValidator
from django.utils.deconstruct import deconstructible
from django.utils.translation import gettext_lazy as _
from django.core.files.uploadedfile import TemporaryUploadedFile
UNITS = {'k': 1000, 'm': 1000_000, 'g': 1000_000_000}
def readableToInt(limit: str) -> int:
x = limit.lower().rstrip(' ib') # KiB & KB -> k
multiply = UNITS.get(x[-1], 1)
value = float(x.rstrip(' _kmg').replace(',', '.'))
return int(value * multiply)
@deconstructible
class MaxFilesizeValidator(BaseValidator):
message = _('File size too large (max. %(limit_value)s).')
code = 'max_filesize'
def compare(self, a: TemporaryUploadedFile, limit: str):
return a.size > readableToInt(limit)