IV1T3 / django-middleware-fileuploadvalidation

A Django middleware to validate user file uploads and detect malicious content.
Apache License 2.0
9 stars 2 forks source link

Implement Decorators for Easier Configuration of File Upload Validation in Django Views #43

Closed IV1T3 closed 8 months ago

IV1T3 commented 9 months ago

Description

Currently, each Django view requires custom configuration for file upload validation. This process involves modifying the settings.py file to set various parameters for each upload type, like file size limits, whitelists, and others. This approach, while functional, can become cumbersome for developers, especially when dealing with multiple views and file types.

To streamline this process, I propose the implementation of decorators that can be applied directly to Django views. Decorators would allow developers to easily configure file upload validations on a per-view basis without the need to alter the settings.py file for each case. This would lead to cleaner, more maintainable code and a more straightforward implementation process.

Here's an example of how such a decorator could be used:

from django_middleware_fileuploadvalidation.decorators import file_upload_config

@file_upload_config(file_size_limit=2000000, keep_original_filename=True, whitelist=["application/pdf"])
def upload_pdf_view(request):
    # View logic for uploading PDF files
    ...

@file_upload_config(whitelist_name="IMAGES_ALL")
def upload_image_view(request):
    # View logic for uploading images
    ...

In this example, the file_upload_config decorator is used to define upload constraints directly above the view functions. This would override the default configurations set in the settings.py file for these specific views.

The benefits of this approach include:

  1. Improved clarity: Decorators make it explicit what configurations apply to which views.
  2. Enhanced flexibility: Developers can easily customize file upload constraints for individual views.
  3. Reduced boilerplate: Less need to repetitively define configurations in settings.py.

I believe this feature would greatly enhance the usability of the django-middleware-fileuploadvalidation project and I am eager to hear thoughts and suggestions from the community on this proposal.

VidyaYadavv1519 commented 9 months ago

can you please explain more ??

IV1T3 commented 9 months ago

can you please explain more ??

Sure! I updated the initial issue description.