Skip to content

[fix] Reassign shared default templates on org change #1334#1427

Draft
omlahore wants to merge 1 commit into
openwisp:masterfrom
omlahore:issues/1334-reassign-shared-default-templates-on-org-change
Draft

[fix] Reassign shared default templates on org change #1334#1427
omlahore wants to merge 1 commit into
openwisp:masterfrom
omlahore:issues/1334-reassign-shared-default-templates-on-org-change

Conversation

@omlahore

@omlahore omlahore commented Jul 7, 2026

Copy link
Copy Markdown

Fixes #1334

Problem

When a device's organization is changed via the REST API, DeviceDetailSerializer.update() clears the config templates and re-adds only the required templates of the new organization. The default templates (shared defaults and the new organization's own defaults) are not re-assigned. This is inconsistent with device registration, where add_default_templates() is applied, so a device moved to a new organization ends up missing that organization's default templates.

Change

Add instance.config.add_default_templates() in the organization-change branch, right after the required templates are enforced. The organization is already set on the config before the templates are cleared, so the default queryset resolves against the new organization. add() is idempotent, so there is no duplication with the required templates that were just re-added.

Tests

Adds test_device_change_organization_default_templates, covering a shared default plus per-organization defaults across an organization change, and confirming the previous organization's default is removed. The existing test_device_change_organization_required_templates still passes.

Note

Opening this as a draft to confirm the intended behavior: changing a device's organization should re-assign the new organization's default templates, matching what happens on registration. If the current required-only behavior is intentional, I am happy to close this. The change is scoped to the REST API path only; the admin path is left unchanged.

@coderabbitai

coderabbitai Bot commented Jul 7, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

📝 Walkthrough

Walkthrough

The change updates DeviceDetailSerializer.update so that when a device's organization is changed without new config data, the serializer re-adds default templates for the new organization, including shared defaults, via instance.config.add_default_templates(). This restores default templates that were previously removed but not automatically re-added after an organization change. A corresponding API test validates that shared and organization-specific default templates are correctly reassigned during organization change.

Estimated code review effort: 1 (Trivial) | ~5 minutes

Sequence Diagram(s)

sequenceDiagram
  participant Client
  participant DeviceDetailSerializer
  participant Config
  Client->>DeviceDetailSerializer: PATCH device organization
  DeviceDetailSerializer->>Config: clear existing templates
  DeviceDetailSerializer->>Config: enforce required templates
  DeviceDetailSerializer->>Config: add_default_templates()
  Config-->>DeviceDetailSerializer: templates updated for new organization
  DeviceDetailSerializer-->>Client: updated device response
Loading

Related issues: #1334 — Shared default templates not re-assigned after device organization change

Suggested labels: bug, backport-needed

Suggested reviewers: nemesifier, pandafy

Poem:
A rabbit hops from org to org anew,
Its templates scattered, only a few remained true.
Now with a hop and a call so bright,
Shared defaults return, setting things right.
Config restored, the burrow complete —
This rabbit's fix makes the roundtrip sweet! 🐰


Caution

Pre-merge checks failed

Please resolve all errors before merging. Addressing warnings is optional.

  • Ignore

❌ Failed checks (2 errors)

Check name Status Explanation Resolution
Linked Issues check ❌ Error The API fix reassigns defaults on org change, but the linked bug also affects the admin flow, which this PR leaves unchanged. Apply the same default-template reassignment in the admin org-change path or move it into shared logic used by both flows.
General Rules ❌ Error Linked issue #1334 has no labels or project assignment, so it doesn't meet the maintainer-validation rule for non-members. Have a maintainer add a valid label and assign the issue to one approved OpenWISP project before merging.
✅ Passed checks (6 passed)
Check name Status Explanation
Title check ✅ Passed The title is descriptive, matches the fix, and references the linked issue.
Description check ✅ Passed It covers the problem, change, tests, and issue reference, with only non-critical template items omitted.
Out of Scope Changes check ✅ Passed Only the serializer fix and a focused regression test were added, and both align with the stated bug fix.
Bug Fixes ✅ Passed Serializer now re-adds defaults on org change; the new API regression test reproduces the bug and would fail without add_default_templates().
Features ✅ Passed Bugfix, not a feature request; a new API test was added and docs already cover default templates. No UI changes, so screenshots aren’t needed.
Changes ✅ Passed PASS: Added API test covers org-change defaults; no UI changes; docs already describe default templates for existing/current devices, and the API fix doesn’t break public APIs.
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[bug] Shared default templates not re-assigned after device organization change

1 participant