GeoNode / geonode

GeoNode is an open source platform that facilitates the creation, sharing, and collaborative use of geospatial data.
https://geonode.org/
Other
1.44k stars 1.12k forks source link

Error when saving a new map #12594

Open etj opened 1 week ago

etj commented 1 week ago

The POST to https://development.demo.geonode.org/api/v2/maps receives a 500 response

Log says:

[pid: 1667|app: 0|req: 123/991] 172.18.0.7 () {64 vars in 1500 bytes} [Mon Sep 16 14:19:27 2024] POST /api/v2/maps => generated 292604 bytes in 330 msecs (
HTTP/1.0 500) 10 headers in 426 bytes (3 switches on core 0)
Internal Server Error: /api/v2/maps
Traceback (most recent call last):
  File "/usr/local/lib/python3.10/dist-packages/django/core/handlers/exception.py", line 55, in inner
    response = get_response(request)
  File "/usr/local/lib/python3.10/dist-packages/django/core/handlers/base.py", line 197, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "/usr/local/lib/python3.10/dist-packages/django/views/decorators/csrf.py", line 56, in wrapper_view
    return view_func(*args, **kwargs)
  File "/usr/local/lib/python3.10/dist-packages/rest_framework/viewsets.py", line 125, in view
    return self.dispatch(request, *args, **kwargs)
  File "/usr/local/lib/python3.10/dist-packages/rest_framework/views.py", line 509, in dispatch
    response = self.handle_exception(exc)
  File "/usr/local/lib/python3.10/dist-packages/rest_framework/views.py", line 469, in handle_exception
    self.raise_uncaught_exception(exc)
  File "/usr/local/lib/python3.10/dist-packages/rest_framework/views.py", line 480, in raise_uncaught_exception
    raise exc
  File "/usr/local/lib/python3.10/dist-packages/rest_framework/views.py", line 506, in dispatch
    response = handler(request, *args, **kwargs)
  File "/usr/lib/python3.10/contextlib.py", line 79, in inner
    return func(*args, **kwds)
  File "/usr/src/geonode/geonode/maps/api/views.py", line 93, in create
    return super(MapViewSet, self).create(request, *args, **kwargs)
  File "/usr/local/lib/python3.10/dist-packages/dynamic_rest/viewsets.py", line 696, in create
    return super(DynamicModelViewSet, self).create(
  File "/usr/local/lib/python3.10/dist-packages/rest_framework/mixins.py", line 18, in create
    serializer.is_valid(raise_exception=True)
  File "/usr/local/lib/python3.10/dist-packages/rest_framework/serializers.py", line 227, in is_valid
    self._validated_data = self.run_validation(self.initial_data)
  File "/usr/local/lib/python3.10/dist-packages/rest_framework/serializers.py", line 426, in run_validation
    value = self.to_internal_value(data)
  File "/usr/src/geonode/geonode/base/api/serializers.py", line 751, in to_internal_value
    data = super(ResourceBaseSerializer, self).to_internal_value(data)
  File "/usr/local/lib/python3.10/dist-packages/dynamic_rest/serializers.py", line 708, in to_internal_value
    value = super(WithDynamicSerializerMixin, self).to_internal_value(data)
  File "/usr/local/lib/python3.10/dist-packages/rest_framework/serializers.py", line 483, in to_internal_value
    validated_value = field.run_validation(primitive_value)
  File "/usr/local/lib/python3.10/dist-packages/rest_framework/fields.py", line 547, in run_validation
    value = self.to_internal_value(data)
  File "/usr/src/geonode/geonode/base/api/serializers.py", line 562, in to_internal_value
    instance = self.root.instance or ResourceBase.objects.get(pk=self.root.initial_data["pk"])
KeyError: 'pk'
mattiagiupponi commented 5 days ago

I checked a bit the issue. Since maps are the only one that in creations called the API/v2 the error is raised because it tries to calculate the permissions before save the resource. To fix this we have two options: 1) Ignore the value in creation and use the default value. Later will be the user from the metadata editor to change this

2) Move this in the save method of the ResourceBaseSerializer.

The second should work, but i notice that the client always send the information of the is_approved, is_published and featured as false (on development demo/master)

image

This is an issue since we cannot rely on the client. If the client serve the default value is_approved=True, is_published =True and featured=False

A PR with the changes is here but since the client always send the FALSE, is like to have the ADW always activated. Do you think (before opening any issue on the mapstore-client repo) that this could be a valid solution?

giohappy commented 5 days ago

This is an issue since we cannot rely on the client. If the client serve the default value is_approved=True, is_published =True and featured=False

I'm not sure what you mean here. Why can't we rely on the client? The user that creates the new map has permission to set these values, so why can't we rely on them? The user could decide to set them before saving the map for the first time.

I agree that the client should set different defaults, but that's a different story.

giohappy commented 5 days ago

As agreed @mattiagiupponi the client will not include those properties when a new map is created (https://github.com/GeoNode/geonode-mapstore-client/issues/1856). If the properties are missing the backend will assign the default values