updated validate_file_extension() to look for defined VALID_EXTENTIONS in settings.py so its configurable

This commit is contained in:
Thomas Wheeler 2021-12-31 14:59:37 -08:00
parent 94902ec44f
commit 345a713777

View File

@ -2,14 +2,22 @@
#
# validators for file uploads, etc.
from django.conf import settings
def validate_file_extension(value):
import os
from django.core.exceptions import ValidationError
ext = os.path.splitext(value.name)[1] # [0] returns path+filename
valid_extensions = ['.txt', '.pdf', '.doc', '.docx', '.odt', '.jpg', '.png']
# TODO: we might improve this with more thorough checks of file types
# rather than just the extensions.
if not ext.lower() in valid_extensions:
# check if VALID_EXTENSTIONS is defined in settings.py
# if not use defaults
if settings.VALID_EXTENSTIONS:
valid_extenstions = settings.VALID_EXTENSTIONS
else:
valid_extenstions = ['.txt', '.pdf', '.doc', '.docx', '.odt', '.jpg', '.png']
if not ext.lower() in valid_extenstions:
raise ValidationError('Unsupported file extension.')