From 9e49f56c50aea2d24bd440d05d9dfb296235e834 Mon Sep 17 00:00:00 2001 From: Mohammad Kassem <106847018+mhmdk0@users.noreply.github.com> Date: Fri, 13 Dec 2024 19:36:04 +0200 Subject: [PATCH] Fix generated_uid bug (#629) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * fix generated_uid bug * code refactoring * code refactoring - fix --------- Co-authored-by: Alejandro Aristizábal --- server/dataset/serializers.py | 31 +++++++++++++++++++++---------- 1 file changed, 21 insertions(+), 10 deletions(-) diff --git a/server/dataset/serializers.py b/server/dataset/serializers.py index aaee5aaab..8d5d86d32 100644 --- a/server/dataset/serializers.py +++ b/server/dataset/serializers.py @@ -28,7 +28,7 @@ class Meta: fields = "__all__" read_only_fields = ["owner"] - def validate_state(self, state): + def _validate_guid(self, data): # NOTE: this is checking the uniqueness of generated_uid across # operational datasets. This check relies on the fact that # such a constraint can only be violated by updating the state. @@ -38,25 +38,36 @@ def validate_state(self, state): # defined in models.py. The reason is that DRF doesn't translate # uniqueness constraint correctly, causing a 500 server error # if the check was left to be done by the database. - if state == "OPERATION" and self.instance.state == "DEVELOPMENT": + if ( + data.get("state") + and data["state"] == "OPERATION" + and self.instance.state == "DEVELOPMENT" + ): constraint = ( Dataset.objects.all() - .filter(state="OPERATION", generated_uid=self.instance.generated_uid) + .filter( + state="OPERATION", + generated_uid=data.get( + "generated_uid", self.instance.generated_uid + ), + ) .exists() ) if constraint: raise serializers.ValidationError( - "An Operational dataset with the same generated UID already exists" + "An Operational dataset with the same " + "generated UID already exists" ) - return state def validate(self, data): if self.instance.state == "OPERATION": editable_fields = ["is_valid", "user_metadata"] for k, v in data.items(): - if k not in editable_fields: - if v != getattr(self.instance, k): - raise serializers.ValidationError( - "User cannot update non editable fields in Operation mode" - ) + value_changed = v != getattr(self.instance, k) + if k not in editable_fields and value_changed: + raise serializers.ValidationError( + "User cannot update non editable fields in Operation mode" + ) + self._validate_guid(data) + return data