def _store_obj(self, obj): key = self._generate_key(obj) document = self._resource_schema().dump(obj) for field in self._unique_values: # Check if a document already exists with a duplicate unique value if document[field] in [o[field] for o in self.objects.values()]: value = document[field] if not value: continue msg = f"A duplicate value '{value}' exists for field '{field}'." error = types.DuplicateFieldError( message=msg, field=field, duplicate_value=value, conflicting_resource=None, ) serialized_errors = [DuplicateFieldErrorSchema().dump(error)] raise CommercetoolsError( msg, errors=serialized_errors, response=types.ErrorResponse( status_code=400, message=msg, error=error.code, errors=serialized_errors, ), ) self.objects[key] = document return document
def post_load(self, data): del data["code"] return types.DuplicateFieldError(**data)