Esempio n. 1
0
 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
Esempio n. 2
0
 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.')
Esempio n. 3
0
 def post(self, *args, **kwargs):
     model_name = self.request.data['model_name']
     model_attrs = self.request.data['model_attrs']
     sample_text = self.request.data['text']
     try:
         model = RequestModelFactory.create(model_name, model_attrs)
     except Exception:
         model = RequestModelFactory.find(model_name)
         schema = model.schema()
         required_fields = ', '.join(
             schema['required']) if 'required' in schema else ''
         raise ValidationError(
             'The attributes does not match the model.'
             'You need to correctly specify the required fields: {}'.format(
                 required_fields))
     try:
         request = model.build()
         response = request.send(text=sample_text)
         return Response(response, status=status.HTTP_200_OK)
     except requests.exceptions.ConnectionError:
         raise URLConnectionError
     except botocore.exceptions.ClientError:
         raise AWSTokenError()
     except Exception as e:
         raise e
Esempio n. 4
0
 def validate(self, data):
     try:
         RequestModelFactory.create(data['model_name'], data['model_attrs'])
     except Exception:
         model = RequestModelFactory.find(data['model_name'])
         schema = model.schema()
         required_fields = ', '.join(schema['required']) if 'required' in schema else ''
         raise serializers.ValidationError(
             'The attributes does not match the model.'
             'You need to correctly specify the required fields: {}'.format(required_fields)
         )
     return data
Esempio n. 5
0
 def validate(self, data):
     try:
         RequestModelFactory.create(data["model_name"], data["model_attrs"])
     except Exception:
         model = RequestModelFactory.find(data["model_name"])
         schema = model.schema()
         required_fields = ", ".join(
             schema["required"]) if "required" in schema else ""
         raise serializers.ValidationError(
             "The attributes does not match the model."
             "You need to correctly specify the required fields: {}".format(
                 required_fields))
     return data
Esempio n. 6
0
 def create_model(self):
     model_name = self.request.data['model_name']
     model_attrs = self.request.data['model_attrs']
     try:
         model = RequestModelFactory.create(model_name, model_attrs)
         return model
     except Exception:
         model = RequestModelFactory.find(model_name)
         schema = model.schema()
         required_fields = ', '.join(
             schema['required']) if 'required' in schema else ''
         raise ValidationError(
             'The attributes does not match the model.'
             'You need to correctly specify the required fields: {}'.format(
                 required_fields))
Esempio n. 7
0
 def create_model(self):
     model_name = self.request.data["model_name"]
     model_attrs = self.request.data["model_attrs"]
     try:
         model = RequestModelFactory.create(model_name, model_attrs)
         return model
     except Exception:
         model = RequestModelFactory.find(model_name)
         schema = model.schema()
         required_fields = ", ".join(
             schema["required"]) if "required" in schema else ""
         raise ValidationError(
             "The attributes does not match the model."
             "You need to correctly specify the required fields: {}".format(
                 required_fields))
Esempio n. 8
0
def test_find_model():
    model = RequestModelFactory.find('Amazon Comprehend Sentiment Analysis')
    assert model == AmazonComprehendSentimentRequestModel