From 8a64eb7a13ef16b6ca61f95ad9ec9c88444fe9f5 Mon Sep 17 00:00:00 2001 From: Hironsan Date: Tue, 2 Feb 2021 10:53:30 +0900 Subject: [PATCH] Add AutoLabelingConfig validation --- app/api/models.py | 10 ++++++++++ app/api/serializers.py | 21 +++++++++++++++++++++ 2 files changed, 31 insertions(+) diff --git a/app/api/models.py b/app/api/models.py index a29933f4..ac994f7e 100644 --- a/app/api/models.py +++ b/app/api/models.py @@ -1,5 +1,6 @@ import string +from auto_labeling_pipeline.models import RequestModelFactory from django.db import models from django.dispatch import receiver from django.db.models.signals import post_save, pre_delete @@ -357,3 +358,12 @@ class AutoLabelingConfig(models.Model): def __str__(self): return self.model_name + + def clean_fields(self, exclude=None): + super().clean_fields(exclude=exclude) + try: + RequestModelFactory.find(self.model_name) + except NameError: + raise ValidationError(f'The specified model name {self.model_name} does not exist.') + except Exception: + raise ValidationError('The attributes does not match the model.') diff --git a/app/api/serializers.py b/app/api/serializers.py index f652b086..05056031 100644 --- a/app/api/serializers.py +++ b/app/api/serializers.py @@ -1,3 +1,4 @@ +from auto_labeling_pipeline.models import RequestModelFactory from django.conf import settings from django.contrib.auth import get_user_model from django.shortcuts import get_object_or_404 @@ -249,3 +250,23 @@ class AutoLabelingConfigSerializer(serializers.ModelSerializer): model = AutoLabelingConfig fields = ('id', 'model_name', 'model_attrs', 'template', 'label_mapping', 'default') read_only_fields = ('created_at', 'updated_at') + + def validate_model_name(self, value): + try: + RequestModelFactory.find(value) + except NameError: + raise serializers.ValidationError(f'The specified model name {value} does not exist.') + return value + + def valid_label_mapping(self, value): + if isinstance(value, dict): + return value + else: + raise serializers.ValidationError(f'The {value} is not a dictionary. Please specify it as a dictionary.') + + def validate(self, data): + try: + RequestModelFactory.create(data['model_name'], data['model_attrs']) + except Exception: + raise serializers.ValidationError('The attributes does not match the model.') + return data