[geo] keep floorplan org in sync with location#1210
Conversation
WalkthroughThis PR fixes a bug where changing a location's organization does not synchronize the organization assignment to related floorplan objects. The solution adds a Sequence DiagramsequenceDiagram
participant Admin as Admin/User
participant Location as Location Model
participant Floorplan as Floorplan Model
participant DB as Database
Admin->>Location: Change organization_id
Location->>Location: save()
Note over Location: Detect org_id changed?
alt Organization Changed
Location->>DB: Fetch related floorplans
DB-->>Location: Floorplans data
Location->>Floorplan: Update organization_id on all<br/>related floorplans
Floorplan->>DB: Save changes
else Organization Unchanged
Location->>DB: Save location normally
end
Admin->>Floorplan: Access/Validate floorplan
Floorplan->>Floorplan: clean()
Floorplan->>Location: Verify org via location
Location-->>Floorplan: organization_id
Floorplan->>Floorplan: Sync org context
Floorplan->>DB: Validated and synced
Estimated code review effort🎯 2 (Simple) | ⏱️ ~10 minutes 🚥 Pre-merge checks | ✅ 4✅ Passed checks (4 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing touches
🧪 Generate unit tests (beta)
🧹 Recent nitpick comments
📜 Recent review detailsConfiguration used: Organization UI Review profile: CHILL Plan: Pro 📒 Files selected for processing (2)
🧰 Additional context used🧠 Learnings (4)📚 Learning: 2026-01-12T22:27:48.342ZApplied to files:
📚 Learning: 2026-01-15T15:05:49.557ZApplied to files:
📚 Learning: 2026-01-15T15:07:17.354ZApplied to files:
📚 Learning: 2026-01-15T14:06:53.460ZApplied to files:
🧬 Code graph analysis (1)openwisp_controller/geo/tests/test_models.py (3)
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (9)
🔇 Additional comments (3)
✏️ Tip: You can disable this entire section by setting 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. Comment |
nemesifier
left a comment
There was a problem hiding this comment.
Thanks @xoaryaa, this is a reasonable fix for #385 and doing it in Location.save() (rather than only in the admin) is the right call, since it keeps floorplans consistent for API and programmatic changes too. Tests are included, good.
A few notes:
- Use the project's model loader. You added
from django.apps import appsand doapp_label, model_name = get_model_name(...).split("."); apps.get_model(...). The codebase convention isswapper.load_model("geo", "FloorPlan"), which is cleaner and handles swapping consistently. Please switch to that. Also fix the import ordering. - Extra query per save. When
self.pkis set you do a.values_list(...)query on every save to detect an org change. That is acceptable, but you could avoid it by tracking the original org on load, or only running it whenorganization_idis actually in the changed fields. Not blocking. .update()bypasses validation/signals on FloorPlan. That is fine here because you are propagating a known-consistent org value, just be aware no FloorPlan save signals fire. If anything downstream depends on FloorPlanpost_savefor org changes, it will not run. Worth a moment's thought.- Confirm the test asserts the floorplan org actually changes after the location org changes.
Solid fix. Address point 1 and confirm the test, and this is good to merge.
Checklist
Reference to Existing Issue
Closes #385
Description of Changes
FloorPlan.organizationcould remain stale after changing the relatedLocation.organization.Screenshot
no UI changes