Refactor for reuse

This commit is contained in:
Chris Hill-Scott
2022-01-06 11:06:55 +00:00
parent da16ab14c3
commit 5558af2527
2 changed files with 34 additions and 13 deletions

View File

@@ -1,4 +1,5 @@
import re
from abc import ABC, abstractmethod
from notifications_utils.field import Field
from notifications_utils.formatters import formatted_list
@@ -77,18 +78,30 @@ class NoCommasInPlaceHolders:
raise ValidationError(self.message)
class NoEmbeddedImagesInSVG:
class NoElementInSVG(ABC):
def __init__(self, message='This SVG has an embedded raster image in it and will not render well'):
self.message = message
@property
@abstractmethod
def element(self):
pass
@property
@abstractmethod
def message(self):
pass
def __call__(self, form, field):
is_image_embedded = '<image' in field.data.stream.read().decode("utf-8")
svg_contents = field.data.stream.read().decode("utf-8")
field.data.stream.seek(0)
if is_image_embedded:
if f'<{self.element}' in svg_contents.lower():
raise ValidationError(self.message)
class NoEmbeddedImagesInSVG(NoElementInSVG):
element = 'image'
message = 'This SVG has an embedded raster image in it and will not render well'
class OnlySMSCharacters:
def __init__(self, *args, template_type, **kwargs):