FrancescoCastaldi opened a new pull request, #44358:
URL: https://github.com/apache/superset/pull/44358
### SUMMARY
Fixes #44305: when adding a new chart to a dashboard (either during chart
creation via `CreateChartCommand` or when updating a chart's dashboard
associations via `UpdateChartCommand`), the associated dashboard's `changed_on`
and `changed_by` audit attributes were not updated. Consequently, the
Dashboards list view continued to display a stale "Last modified" timestamp and
editor.
#### Root Cause
In `CreateChartCommand.run()`, `self._properties["dashboards"]` are linked
to the newly created chart model via `ChartDAO.create()`, and in
`UpdateChartCommand.run()`, newly linked dashboards are associated via
`ChartDAO.update()`. In neither case were the dashboard models' audit fields
touched. Because `AuditMixin` only auto-updates audit fields on direct model
mutations, the attached `Dashboard` records remained unmodified, preserving
their previous `changed_on` and `changed_by`.
#### Solution
1. Introduced `touch_dashboards(dashboards: list[Any] | None)` in
`superset/commands/chart/utils.py` that sets `dashboard.changed_on =
datetime.now()` and `dashboard.changed_by = g.user` (if present) for each
provided dashboard.
2. In `CreateChartCommand.run()`, invoked
`touch_dashboards(self._properties.get("dashboards"))`.
3. In `UpdateChartCommand.run()`, identified newly attached dashboards (`[d
for d in requested_dashboards if d.id not in existing_dash_ids]`) and invoked
`touch_dashboards(new_dashboards)` without affecting existing or unchanged
dashboards.
4. Added extensive unit test coverage in
`tests/unit_tests/commands/chart/create_test.py` and
`tests/unit_tests/commands/chart/update_test.py` testing:
- Reproduction of #44305 (verifying `changed_on` and `changed_by` get
updated upon chart creation).
- Linking multiple dashboards upon creation.
- Creating standalone charts with no dashboards.
- Graceful handling when `g.user` is not set.
- Updating charts with newly linked dashboards.
- Updating charts without altering dashboard links (ensuring no spurious
audit touch).
- Removing dashboards from a chart (ensuring remaining dashboards remain
untouched).
### BEFORE/AFTER SCREENSHOTS OR ANIMATED GIF
N/A (Backend command and audit metadata synchronization).
### TESTING INSTRUCTIONS
1. Create or open an existing dashboard. Note its "Last modified" timestamp.
2. Create a new chart and add it to the dashboard, or edit an existing chart
and add the dashboard to it.
3. Return to the Dashboards list view: the dashboard's "Last modified"
timestamp is now updated to the current time and the modified-by user is
updated to the active user.
4. Run unit tests:
```bash
pytest tests/unit_tests/commands/chart/create_test.py
tests/unit_tests/commands/chart/update_test.py
```
### CHECKLIST
- [x] Has associated issue: fixes #44305
- [x] Required documentation is changed or added
- [x] Unit tests have been added/updated
- [x] All automated tests are passing
- [x] Lint and formatting checks pass (`ruff`)
--
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]