dosubot[bot] commented on issue #29871:
URL: https://github.com/apache/superset/issues/29871#issuecomment-2270631749
<!-- Greeting -->
Hello, @Qambitions! I'm here to help you with any bugs, questions, or
contributions you have for the repository. Let's tackle this issue together!
<!-- Answer -->
To resolve the `KeyError` for the key 'positions' when calling the
**"/api/v1/dashboard/{dashboard_id}/copy/"** API with `duplicate_slices` set to
true, you need to ensure that the `positions` key is included in the
`json_metadata` of the request payload before making the API call.
Here's how you can modify your code to include the `positions` key:
1. Fetch the `position_json` from the dashboard data.
2. Add the `position_json` to the `json_metadata`.
Here's the updated code:
```python
import json
import requests
dashboard_id = 1
headers = {
"Content-Type": "application/json",
"Authorization": f"Bearer {self.access_token}",
"X-CSRFToken": self.csrf_token
}
# Fetch the dashboard data
dashboard_response =
requests.get(f"{superset_url}/api/v1/dashboard/{dashboard_id}",
headers=headers, cookies=self.cookies)
dashboard_data = dashboard_response.json()["result"]
# Extract and update json_metadata
json_meta = dashboard_data.get("json_metadata", "")
json_meta = json.loads(json_meta)
json_meta["positions"] = dashboard_data.get("position_json", "")
# Prepare the request payload
request_payload = {
"dashboard_title": "intergrate_test",
"duplicate_slices": True,
"json_metadata": json.dumps(json_meta)
}
# Make the copy API call
r = requests.post(f'{superset_url}/api/v1/dashboard/{dashboard_id}/copy/',
json=request_payload, headers=headers,
cookies=self.cookies)
print(r.json())
```
This code ensures that the `positions` key is included in the
`json_metadata` when making the API call to
`/api/v1/dashboard/{dashboard_id}/copy/`, preventing the `KeyError`
<sup>[[1]](https://github.com/apache/superset/blob/master/superset-frontend/src/dashboard/actions/dashboardState.js)</sup>.
<!-- Dosu Comment Footer -->
*To continue talking to [Dosu](https://dosu.dev), mention @dosu.*
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]