gault-millau/apps/utils/validators.py

23 lines
576 B
Python

"""DB field validators"""
import xml.etree.cElementTree as et
from django.core.exceptions import ValidationError
def svg_image_validator(file: object) -> object:
"""Validate SVG file"""
tag = None
try:
for event, el in et.iterparse(file, ('start',)):
tag = el.tag
break
assert tag == '{http://www.w3.org/2000/svg}svg'
except:
raise ValidationError(
message='Invalid SVG image file',
code='invalid_svg_image',
params={'value': file},
)
else:
return file