0

After uploading data to GeoServer and displaying it in GeoNode, I want to to update specific parts of the metadata using the REST API. My goal is to update the groups value, which is empty (NULL) after uploading the data. I use a GET first to retrieve the metadata and then enter the new value for group. But when I try to put or patch the metadata I receive Failed to update group. Status code: 500. This is my function to adjust the metadata:

def update_layer_metadata(layer_pk, group_value):
    # Endpoint for the specific layer
    url = f"{GEONODE_API_URL}/{layer_pk}/"

    # Fetch existing metadata
    response = requests.get(url, auth=(GEONODE_USERNAME, GEONODE_PASSWORD))
    if response.status_code != 200:
        print(f"Failed to fetch existing metadata. Status code: {response.status_code}")
        return

    # Parse the metadata and update the 'group' field
    metadata = response.json()
    if "dataset" in metadata and "group" in metadata["dataset"]:
        metadata["dataset"]["group"] = group_value
    else:
        print("Key 'group' not found in metadata or invalid metadata format.")
        return

    # Send updated metadata
    update_response = requests.put(
        url,
        json=metadata,
        # headers={"Content-Type": "application/json"},
        auth=(GEONODE_USERNAME, GEONODE_PASSWORD)
    )

    if update_response.status_code in [200, 204]:
        print("Group updated successfully.")
    else:
        print(f"Failed to update group. Status code: {update_response.status_code}")

According to the Django REST framework Site, which I access by using the value of url, the allowed operations should be GET, PATCH and PUT.

Update: I read that the URL should end on "/metadata/", so I adjusted the variable url and this resulted in a new error code: Failed to update group. Status code: 405. This was the adjusted part:

 # Send updated metadata
    update_response = requests.patch(
        url+"metadata/",
        json=metadata,  # Sending only the required field
        # headers={"Content-Type": "application/json"},
        auth=(GEONODE_USERNAME, GEONODE_PASSWORD)
    )

1 Answer 1

0

Turned out you can only manipulate pkof group and not the entry as a whole. So therefore you need to know, which group has which pk and then set the value of pkof the desired layer to the pk of the desired group.

You can check the groups through http://localhost/api/v2/groups for localhost or retriev the groups through REST API:

response = requests.get(
    "http://localhost/api/v2/groups",
    auth=(GEONODE_USERNAME, GEONODE_PASSWORD)
)

And then use the desired pk for your PATCHcommand:

def update_layer_metadata(layer_pk, group_pk):
    # Endpoint for the specific layer
    url = f"http://localhost/api/v2/datasets/{layer_pk}/"

    # Fetch existing metadata
    response = requests.get(url, auth=(GEONODE_USERNAME, GEONODE_PASSWORD))
    if response.status_code != 200:
        print(f"Failed to fetch existing metadata. Status code: {response.status_code}")
        return

    # Prepare payload with only the modified field
    payload = {"group": {
            "pk": group_pk,
        }}
    update_response = requests.patch(
        url,
        json=payload,  # Sending only the required field
        auth=(GEONODE_USERNAME, GEONODE_PASSWORD)
    )

    if update_response.status_code in [200, 204]:
        print("Group updated successfully.")
    else:
        print(f"Failed to update group. Status code: {update_response.status_code} - {update_response.reason}")

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.