From 0f14fd0c6275fcdbb91231088b35fa0e7511a471 Mon Sep 17 00:00:00 2001 From: Herculino Trotta Date: Mon, 20 Jan 2025 14:30:40 -0300 Subject: [PATCH] feat(import): test yaml_config before saving --- app/apps/import_app/models.py | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/app/apps/import_app/models.py b/app/apps/import_app/models.py index aca04e3..b489c43 100644 --- a/app/apps/import_app/models.py +++ b/app/apps/import_app/models.py @@ -1,13 +1,18 @@ +import yaml + +from django.core.exceptions import ValidationError from django.db import models from django.utils.translation import gettext_lazy as _ +from apps.import_app.schemas import version_1 + class ImportProfile(models.Model): class Versions(models.IntegerChoices): VERSION_1 = 1, _("Version 1") - name = models.CharField(max_length=100) - yaml_config = models.TextField(help_text=_("YAML configuration")) + name = models.CharField(max_length=100, verbose_name=_("Name")) + yaml_config = models.TextField(verbose_name=_("YAML Configuration")) version = models.IntegerField( choices=Versions, default=Versions.VERSION_1, @@ -20,6 +25,14 @@ class ImportProfile(models.Model): class Meta: ordering = ["name"] + def clean(self): + if self.version and self.version == self.Versions.VERSION_1: + try: + yaml_data = yaml.safe_load(self.yaml_config) + version_1.ImportProfileSchema(**yaml_data) + except Exception as e: + raise ValidationError({"yaml_config": _("Invalid YAML Configuration")}) + class ImportRun(models.Model): class Status(models.TextChoices):