You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

38 lines
1.4 KiB

  1. from auto_labeling_pipeline.models import RequestModelFactory
  2. from rest_framework import serializers
  3. from .models import AutoLabelingConfig
  4. class AutoLabelingConfigSerializer(serializers.ModelSerializer):
  5. class Meta:
  6. model = AutoLabelingConfig
  7. fields = ('id', 'model_name', 'model_attrs', 'template', 'label_mapping', 'default', 'task_type')
  8. read_only_fields = ('created_at', 'updated_at')
  9. def validate_model_name(self, value):
  10. try:
  11. RequestModelFactory.find(value)
  12. except NameError:
  13. raise serializers.ValidationError(f'The specified model name {value} does not exist.')
  14. return value
  15. def valid_label_mapping(self, value):
  16. if isinstance(value, dict):
  17. return value
  18. else:
  19. raise serializers.ValidationError(f'The {value} is not a dictionary. Please specify it as a dictionary.')
  20. def validate(self, data):
  21. try:
  22. RequestModelFactory.create(data['model_name'], data['model_attrs'])
  23. except Exception:
  24. model = RequestModelFactory.find(data['model_name'])
  25. schema = model.schema()
  26. required_fields = ', '.join(schema['required']) if 'required' in schema else ''
  27. raise serializers.ValidationError(
  28. 'The attributes does not match the model.'
  29. 'You need to correctly specify the required fields: {}'.format(required_fields)
  30. )
  31. return data