def validate_content(self, content): """We only validate the content if passed. Also we use the ExperimentSpecification to check if this content was intended as an experiment. """ # content is optional if not content: return content validate_experiment_spec_config(content) return content
def post(self, request, *args, **kwargs): obj = self.get_object() auditor.record(event_type=self.event_type, instance=obj, actor_id=self.request.user.id) description = None config = None declarations = None update_code_reference = False if 'config' in request.data: spec = validate_experiment_spec_config( [obj.specification.parsed_data, request.data['config']], raise_for_rest=True) config = spec.parsed_data declarations = spec.declarations if 'update_code' in request.data: try: update_code_reference = to_bool(request.data['update_code']) except TypeError: raise ValidationError('update_code should be a boolean') if 'description' in request.data: description = request.data['description'] new_obj = self.clone(obj=obj, config=config, declarations=declarations, update_code_reference=update_code_reference, description=description) serializer = self.get_serializer(new_obj) return Response(status=status.HTTP_201_CREATED, data=serializer.data)
def create(self, validated_data): """Check the params or set the value from the specification.""" if not validated_data.get('declarations') and validated_data.get( 'config'): config = validate_experiment_spec_config(validated_data['config']) validated_data['declarations'] = config.declarations return super().create(validated_data=validated_data)
def validate_config(self, config): """We only validate the config if passed. Also we use the ExperimentSpecification to check if this config was intended as an experiment. """ # config is optional if not config: return config spec = validate_experiment_spec_config(config) if spec.is_experiment: # Resume normal creation return config # Raise an error to tell the user to use experiment creation instead raise ValidationError('Current experiment creation could not be performed.\n' 'The reason is that the specification sent correspond ' 'to a `{}`.\n'.format(spec.kind))
def validate_config(self, config): """We only validate the config if passed. Also we use the GroupSpecification to check if this config was intended as Group experiments. """ # config is optional if not config: return config spec = validate_experiment_spec_config(config) if spec.is_experiment == 1: # Resume normal creation return config # Raise an error to tell the use to use experiment creation instead raise ValidationError('Current experiment creation could not be performed.\n' 'The reason is that the specification sent correspond ' 'to a `{}`.\n' 'Please use `create group experiment endpoint`.'.format(spec.kind))
def post(self, request, *args, **kwargs): ttl = self.request.data.get(RedisTTL.TTL_KEY) if ttl: try: ttl = RedisTTL.validate_ttl(ttl) except ValueError: raise ValidationError('ttl must be an integer.') obj = self.get_object() auditor.record(event_type=self.event_type, instance=obj, actor_id=self.request.user.id, actor_name=self.request.user.username) description = None config = None declarations = None update_code_reference = False if 'config' in request.data: spec = validate_experiment_spec_config( [obj.specification.parsed_data, request.data['config']], raise_for_rest=True) config = spec.parsed_data declarations = spec.declarations if 'update_code' in request.data: update_code_reference = to_bool(request.data['update_code'], handle_none=True, exception=ValidationError) if 'description' in request.data: description = request.data['description'] new_obj = self.clone(obj=obj, config=config, declarations=declarations, update_code_reference=update_code_reference, description=description) if ttl: RedisTTL.set_for_experiment(experiment_id=new_obj.id, value=ttl) serializer = self.get_serializer(new_obj) return Response(status=status.HTTP_201_CREATED, data=serializer.data)
def create(self, validated_data): """Check the params or set the value from the specification.""" if not validated_data.get('declarations') and validated_data.get('config'): config = validate_experiment_spec_config(validated_data['config']) validated_data['declarations'] = config.declarations return super(ExperimentCreateSerializer, self).create(validated_data=validated_data)