core: check PropertyMapping's expression syntax before save

This commit is contained in:
Jens Langhammer 2020-02-18 15:12:05 +01:00
parent 1c1afca31f
commit f31cd7dec6
1 changed files with 8 additions and 0 deletions

View File

@ -5,6 +5,7 @@ from time import sleep
from typing import Any, Optional from typing import Any, Optional
from uuid import uuid4 from uuid import uuid4
from django.core.exceptions import ValidationError
from django.contrib.auth.models import AbstractUser from django.contrib.auth.models import AbstractUser
from django.contrib.postgres.fields import JSONField from django.contrib.postgres.fields import JSONField
from django.db import models from django.db import models
@ -314,6 +315,13 @@ class PropertyMapping(UUIDModel):
except UndefinedError as exc: except UndefinedError as exc:
raise PropertyMappingExpressionException from exc raise PropertyMappingExpressionException from exc
def save(self, *args, **kwargs):
try:
NATIVE_ENVIRONMENT.from_string(self.expression)
except TemplateSyntaxError as exc:
raise ValidationError("Expression Syntax Error") from exc
return super().save(*args, **kwargs)
def __str__(self): def __str__(self):
return f"Property Mapping {self.name}" return f"Property Mapping {self.name}"