Skip to content

Commit

Permalink
Enables PATCHing some attributes in attribute_set of a dataset
Browse files Browse the repository at this point in the history
For now the payload is wrapped in `data` like so:

```json
{
  "data": {
     "attribute_set": [{
       "pk": 1, 
       "description": "updated description" 
     }]
  }
}
```

which skips (more or less) `rest_framework` validation.
  • Loading branch information
ridoo authored Dec 4, 2023
2 parents 8325e6e + 628e5f1 commit 7ed45ed
Showing 1 changed file with 32 additions and 1 deletion.
33 changes: 32 additions & 1 deletion geonode/layers/api/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
#
#########################################################################
from rest_framework import serializers

from rest_framework.exceptions import ValidationError, ParseError
from urllib.parse import urlparse

from django.conf import settings
Expand Down Expand Up @@ -195,6 +195,37 @@ class Meta:

featureinfo_custom_template = FeatureInfoTemplateField()

def update(self, instance, validated_data):
super().update(instance, validated_data)

# Handle updates to attribute_set
allowed_fields = ["pk", "description", "attribute_label", "visible", "display_order"]
if "blob" in validated_data and "attribute_set" in validated_data["blob"]:
attributes = validated_data["blob"]["attribute_set"]

for attribute in attributes:
for field, _ in attribute.items():
if field not in allowed_fields:
raise ValidationError(
f"{field} is not one of the fields that could be edited directly. \
Only {str(allowed_fields)} are allowed"
)

for attribute in attributes:
try:
if "pk" in attribute:
attribute_instance = Attribute.objects.get(pk=attribute["pk"], dataset=instance)
for field, value in attribute.items():
setattr(attribute_instance, field, value)
attribute_instance.save()
else:
raise Exception("Primary key of the attribute to be patched not specified")
except Exception as e:
logger.error(e)
raise ParseError(str(e))

return instance


class DatasetListSerializer(DatasetSerializer):
class Meta(DatasetSerializer.Meta):
Expand Down

0 comments on commit 7ed45ed

Please sign in to comment.