Add attachment validator when uploading attachment to tickets

This commit is contained in:
Garret Wassermann
2021-10-05 06:25:42 -04:00
parent a5f801bb52
commit aff67184d4
10 changed files with 81 additions and 4 deletions

15
helpdesk/validators.py Normal file
View File

@ -0,0 +1,15 @@
# validators.py
#
# validators for file uploads, etc.
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:
raise ValidationError('Unsupported file extension.')