Skip to content

Commit

Permalink
Fix generated_uid bug (#629)
Browse files Browse the repository at this point in the history
* fix generated_uid bug

* code refactoring

* code refactoring - fix

---------

Co-authored-by: Alejandro Aristizábal <[email protected]>
  • Loading branch information
mhmdk0 and aristizabal95 authored Dec 13, 2024
1 parent 848bb72 commit 9e49f56
Showing 1 changed file with 21 additions and 10 deletions.
31 changes: 21 additions & 10 deletions server/dataset/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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

0 comments on commit 9e49f56

Please sign in to comment.