#34704: File Size Validator
-------------------------------------+-------------------------------------
Reporter: Reza | Owner: nobody
Shakeri |
Type: New | Status: new
feature |
Component: File | Version: 4.2
uploads/storage | Keywords: file, file
Severity: Normal | validator, file validation,
| validator, file, file size
Triage Stage: | validation
Unreviewed | Has patch: 0
Needs documentation: 0 | Needs tests: 0
Patch needs improvement: 0 | Easy pickings: 0
UI/UX: 0 |
-------------------------------------+-------------------------------------
Hi
I had requested a file validator
([https://code.djangoproject.com/ticket/34700 #34700]), but you said I
couldn't add it to its core and I have a question that can I add file size
validator?
**This code has been tested**
{{{
@deconstructible
class FileSizeValidator:
"""
file size validator
"""
def __init__(
self,
max_upload_file_size: int = None,
):
"""
:type max_upload_file_size: int
:param max_upload_file_size: If you want the file size to be
checked,
the file size must be in bytes,
example: file_size=1048576 (1MB), defaults to 0, optional
:return: If everything is OK, it will return None, otherwise it
will
return a ValidationError.
"""
if max_upload_file_size is None:
raise ValueError("max_upload_file_size is None, You Must Be
Fill max_upload_file_size ")
self.max_upload_file_size = max_upload_file_size
def __call__(self, value):
current_file = value.file
file_size = current_file.size
if self.max_upload_file_size is not None and file_size >
self.max_upload_file_size:
raise ValidationError("file size not valid")
def __eq__(self, other):
return (
isinstance(other, self.__class__)
and self.max_upload_file_size == other.max_upload_file_size
)
}}}
--
Ticket URL: <https://code.djangoproject.com/ticket/34704>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.
--
You received this message because you are subscribed to the Google Groups
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/django-updates/0107018937f81b29-6fecd170-75ac-463b-8d17-7521832a8292-000000%40eu-central-1.amazonses.com.