Skip to content

Commit

Permalink
code refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
mhmdk0 committed Dec 7, 2024
1 parent 389a510 commit 3052143
Showing 1 changed file with 16 additions and 12 deletions.
28 changes: 16 additions & 12 deletions server/dataset/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,16 +28,7 @@ class Meta:
fields = "__all__"
read_only_fields = ["owner"]

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"
)

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 @@ -52,7 +43,7 @@ def validate(self, data):
and data["state"] == "OPERATION"
and self.instance.state == "DEVELOPMENT"
):
if (
constraint = (
Dataset.objects.all()
.filter(
state="OPERATION",
Expand All @@ -61,9 +52,22 @@ def validate(self, data):
),
)
.exists()
):
)
if constraint:
raise serializers.ValidationError(
"An Operational dataset with the same "
"generated UID already exists"
)

def validate(self, data):
if self.instance.state == "OPERATION":
editable_fields = ["is_valid", "user_metadata"]
for k, v in data.items():
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 3052143

Please sign in to comment.