[Django] #34704: File Size Validator

5 views
Skip to first unread message

Django

unread,
Jul 8, 2023, 8:05:58 PM7/8/23
to django-...@googlegroups.com
#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.

Django

unread,
Jul 9, 2023, 6:24:27 AM7/9/23
to django-...@googlegroups.com
#34704: File Size Validator
-------------------------------------+-------------------------------------
Reporter: Reza Shakeri | Owner: nobody
Type: New feature | Status: closed

Component: File | Version: 4.2
uploads/storage |
Severity: Normal | Resolution: duplicate
Keywords: file, file | Triage Stage:
validator, file validation, | Unreviewed
validator, file, file size |
validation |

Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Mariusz Felisiak):

* status: new => closed
* resolution: => duplicate


Comment:

> 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**

For me, this is another (4th) duplicate of #34263. You've already
mentioned the size validator there. Please write to the
DevelopersMailingList if you want other opinions, and
[https://docs.djangoproject.com/en/stable/internals/contributing/triaging-
tickets/#closing-tickets follow the triaging guidelines with regards to
wontfix tickets]. **Don't** create more duplicates of the same ticket.
I'll close all of them immediately.

--
Ticket URL: <https://code.djangoproject.com/ticket/34704#comment:1>

Reply all
Reply to author
Forward
0 new messages