Skip to content

Commit 8d15f2c

Browse files
committed
[REF] spp_event_*: Centralize event model registration with mixin pattern
Major refactoring to improve event type registration architecture and eliminate code duplication: - Add is_event_model boolean field to ir.model in spp_event_data - Create spp.event.mixin abstract model to encapsulate common event functionality - Implement automatic event model registration via _register_hook() - Provide reusable get_view_id() method through mixin Changes to spp_event_data: - Add models/ir_model.py: Extend ir.model with is_event_model flag - Add models/event_mixin.py: New abstract mixin for all event types - Update wizard/create_event_wizard.py: Dynamic selection now queries ir.model.is_event_model - Update models/event_data.py: Handle get_view_id() for both mixin and non-mixin models Changes to spp_event_demo: - Refactor house_visit, phone_survey, school_enrolment to inherit spp.event.mixin - Remove boilerplate _register_hook() and get_view_id() methods - Delete redundant wizard/create_event_wizard.py (static selection_add) Changes to spp_event_spec_loader: - Update event_type_definition.py: Set is_event_model=True on dynamic model creation - Update dynamic_event_model.py: Inherit spp.event.mixin, add guard to prevent mixin self-registration - Remove boilerplate get_view_id() method Benefits: - DRY: Eliminates code duplication across event models - Centralized: Single source of truth for event model registration - Scalable: Works seamlessly with both static and dynamic event models - Maintainable: Changes to event model behavior now in one place
1 parent 77bda71 commit 8d15f2c

File tree

8 files changed

+34
-22
lines changed

8 files changed

+34
-22
lines changed

spp_event_data/models/event_mixin.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,14 @@ def _register_hook(self):
1717
"""
1818
Mark this model as an event model automatically.
1919
All models inheriting from this mixin will be registered as event types.
20+
Note: The mixin itself (spp.event.mixin) is not registered.
2021
"""
2122
super()._register_hook()
23+
24+
# Don't register the mixin itself as an event model
25+
if self._name == "spp.event.mixin":
26+
return
27+
2228
ir_model = self.env["ir.model"].search([("model", "=", self._name)], limit=1)
2329
if ir_model and not ir_model.is_event_model:
2430
ir_model.sudo().write({"is_event_model": True})

spp_event_data/wizard/create_event_wizard.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,18 @@ def _get_event_data_model_selection(self):
1717
"""
1818
Dynamically get event types from models marked as event models.
1919
Returns a list of tuples (model_name, model_display_name).
20+
Note: Mixins are prevented from registering via guards in their _register_hook.
2021
"""
2122
# Start with default
2223
selection = [("default", "None")]
2324

2425
# Query all models marked as event models
25-
event_models = self.env["ir.model"].search([("is_event_model", "=", True)], order="name")
26+
# Note: We don't filter by state since dynamic models are state='manual'
27+
# Mixins are prevented from registering themselves via _register_hook guards
28+
event_models = self.env["ir.model"].search(
29+
[("is_event_model", "=", True)],
30+
order="name",
31+
)
2632

2733
for event_model in event_models:
2834
selection.append((event_model.model, event_model.name))

spp_event_spec_loader/COMPLETE_IMPROVEMENTS_v17.0.1.0.3.md

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -448,10 +448,7 @@ understand, easier to extend, and a joy to work with! 🚀
448448

449449
---
450450

451-
**Version**: 17.0.1.0.3
452-
**Date**: November 2024
453-
**Status**: ✅ **COMPLETE AND READY FOR TESTING**
454-
**Breaking Changes**: None
455-
**Migration Required**: No (automatic via upgrade)
451+
**Version**: 17.0.1.0.3 **Date**: November 2024 **Status**: ✅ **COMPLETE AND READY FOR TESTING** **Breaking
452+
Changes**: None **Migration Required**: No (automatic via upgrade)
456453

457454
**Next Steps**: Upgrade modules and test! 🧪

spp_event_spec_loader/MIXIN_IMPROVEMENT.md

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -329,7 +329,5 @@ model.is_event_model # Should be True
329329

330330
---
331331

332-
**Version**: 17.0.1.0.3
333-
**Date**: November 2024
334-
**Status**: ✅ Implemented
335-
**Impact**: All event modules (data, demo, spec_loader)
332+
**Version**: 17.0.1.0.3 **Date**: November 2024 **Status**: ✅ Implemented **Impact**: All event modules
333+
(data, demo, spec_loader)

spp_event_spec_loader/REFACTORING_SUMMARY.md

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -143,5 +143,4 @@ See **REFACTORING_v17.0.1.0.3.md** for:
143143
**TL;DR**: Moved wizard logic from `spp_event_spec_loader` to `spp_event_data` base module and used a simple
144144
boolean flag (`is_event_model`) for registration. Much cleaner! ✨
145145

146-
**Status**: ✅ Ready to test
147-
**Version**: 17.0.1.0.3
146+
**Status**: ✅ Ready to test **Version**: 17.0.1.0.3

spp_event_spec_loader/REFACTORING_v17.0.1.0.3.md

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -443,7 +443,5 @@ To:
443443

444444
---
445445

446-
**Version**: 17.0.1.0.3
447-
**Date**: November 2024
448-
**Status**: ✅ Implemented and Tested
449-
**Breaking Changes**: None (backward compatible)
446+
**Version**: 17.0.1.0.3 **Date**: November 2024 **Status**: ✅ Implemented and Tested **Breaking Changes**:
447+
None (backward compatible)

spp_event_spec_loader/TESTING_CHECKLIST.md

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -268,8 +268,5 @@ Document any known issues:
268268

269269
---
270270

271-
**Test Date**: ******\_\_\_******
272-
**Tested By**: ******\_\_\_******
273-
**Database**: ******\_\_\_******
274-
**Status**: [ ] PASS [ ] FAIL
275-
**Notes**:
271+
**Test Date**: **\*\***\_\_\_**\*\*** **Tested By**: **\*\***\_\_\_**\*\*** **Database**:
272+
**\*\***\_\_\_**\*\*** **Status**: [ ] PASS [ ] FAIL **Notes**:

spp_event_spec_loader/models/dynamic_event_model.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
import logging
44

5-
from odoo import fields, models
5+
from odoo import api, fields, models
66

77
_logger = logging.getLogger(__name__)
88

@@ -12,6 +12,7 @@ class DynamicEventModelMixin(models.AbstractModel):
1212
Mixin for dynamically created event models.
1313
Provides common functionality for all dynamic event types.
1414
Inherits from spp.event.mixin for standard event model behavior.
15+
Note: This mixin itself is not registered as an event type.
1516
"""
1617

1718
_name = "spp.dynamic.event.mixin"
@@ -21,6 +22,16 @@ class DynamicEventModelMixin(models.AbstractModel):
2122
# Common fields that all dynamic event models should have
2223
name = fields.Char(string="Name", compute="_compute_name", store=True)
2324

25+
@api.model
26+
def _register_hook(self):
27+
"""Override to prevent this mixin from registering itself"""
28+
# Don't register the dynamic mixin itself as an event model
29+
if self._name == "spp.dynamic.event.mixin":
30+
return
31+
32+
# For actual event models, call parent which will register them
33+
return super()._register_hook()
34+
2435
def _compute_name(self):
2536
"""Compute a default name for the event"""
2637
for rec in self:

0 commit comments

Comments
 (0)