Skip to content

Commit

Permalink
adding a get_choices property to the Dataset model and extending the …
Browse files Browse the repository at this point in the history
…supports_time property
  • Loading branch information
Gpetrak committed Dec 6, 2024
1 parent a2ee672 commit c8d4340
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 16 deletions.
19 changes: 8 additions & 11 deletions geonode/layers/api/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -221,18 +221,15 @@ def __init__(self, *args, **kwargs):

super().__init__(*args, **kwargs)

choices = self._get_choices()
self.fields["attribute"].choices = choices
self.fields["end_attribute"].choices = choices
layer = self.context.get("layer")

@staticmethod
def _get_choices():

attributes = Attribute.objects.all()

return [(None, "-----")] + [
(_a.pk, _a.attribute) for _a in attributes if _a.attribute_type in ["xsd:dateTime", "xsd:date"]
]
if layer:
# use the get_choices method of the Dataset model
choices = [(None, "-----")] + layer.get_choices
self.fields["attribute"].choices = choices
self.fields["end_attribute"].choices = choices
else:
choices = [(None, "-----")]

has_time = serializers.BooleanField(default=False)
attribute = serializers.ChoiceField(choices=[], required=False)
Expand Down
9 changes: 5 additions & 4 deletions geonode/layers/api/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -225,13 +225,14 @@ def timeseries_info(self, request, pk, *args, **kwards):
layer = get_object_or_404(Dataset, id=pk)

if layer.supports_time is False:
return JsonResponse({"message": "The time dimension is not supported for raster data."}, status=200)
return JsonResponse({"message": "The time dimension is not supported for this dataset."}, status=200)

if request.method == "GET":

serializer = DatasetTimeSeriesSerializer
time_info = get_time_info(layer)
serialized_time_info = serializer(get_time_info(layer)).data
serializer = DatasetTimeSeriesSerializer(data=time_info, context={"layer": layer})
serializer.is_valid(raise_exception=True)
serialized_time_info = serializer.data

if layer.has_time is True and time_info is not None:
serialized_time_info["has_time"] = layer.has_time
Expand All @@ -241,7 +242,7 @@ def timeseries_info(self, request, pk, *args, **kwards):

if request.method == "PUT":

serializer = DatasetTimeSeriesSerializer(data=request.data)
serializer = DatasetTimeSeriesSerializer(data=request.data, context={"layer": layer})
serializer.is_valid(raise_exception=True)
serialized_time_info = serializer.validated_data

Expand Down
11 changes: 10 additions & 1 deletion geonode/layers/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -166,10 +166,19 @@ def is_raster(self):

@property
def supports_time(self):
if self.is_vector():
valid_attributes = self.get_choices
# check if the layer object if a vector and
# includes valid_attributes
if self.is_vector() and valid_attributes:
return True
return False

@property
def get_choices(self):

attributes = Attribute.objects.filter(dataset_id=self.pk)
return [(_a.pk, _a.attribute) for _a in attributes if _a.attribute_type in ["xsd:dateTime", "xsd:date"]]

@property
def display_type(self):
if self.subtype in ["vector", "vector_time"]:
Expand Down

0 comments on commit c8d4340

Please sign in to comment.