1
1
from collections .abc import Iterable
2
+ from functools import cached_property
2
3
from logging import getLogger
3
4
4
5
from django .core .exceptions import ImproperlyConfigured
@@ -19,9 +20,8 @@ class CMSCoreConfig(CMSAppConfig):
19
20
20
21
21
22
class CMSCoreExtensions (CMSAppExtension ):
22
-
23
23
def __init__ (self ):
24
- self .wizards = {}
24
+ self .lazy_wizards = []
25
25
self .toolbar_enabled_models = {}
26
26
self .model_groupers = {}
27
27
self .toolbar_mixins = []
@@ -33,17 +33,30 @@ def configure_wizards(self, cms_config):
33
33
"""
34
34
if not isinstance (cms_config .cms_wizards , Iterable ):
35
35
raise ImproperlyConfigured ("cms_wizards must be iterable" )
36
- for wizard in cms_config .cms_wizards :
37
- if not isinstance (wizard , Wizard ):
36
+
37
+ self .lazy_wizards .append (cms_config .cms_wizards )
38
+
39
+ @cached_property
40
+ def wizards (self ) -> dict [str , Wizard ]:
41
+ """
42
+ Returns a dictionary of wizard instances keyed by their unique IDs.
43
+ Iterates over all iterables in `self.lazy_wizards`, filters out objects that are instances
44
+ of the `Wizard` class, and constructs a dictionary where each key is the wizard's `id`
45
+ and the value is the corresponding `Wizard` instance.
46
+ Returns:
47
+ dict: A dictionary mapping wizard IDs to `Wizard` instances.
48
+ """
49
+
50
+ wizards = {}
51
+ for iterable in self .lazy_wizards :
52
+ new_wizards = {wizard .id : wizard for wizard in iterable }
53
+ if wizard := next ((wizard for wizard in new_wizards .values () if not isinstance (wizard , Wizard )), None ):
54
+ # If any wizard in the iterable is not an instance of Wizard, raise an exception
38
55
raise ImproperlyConfigured (
39
- "All wizards defined in cms_wizards must inherit "
40
- "from cms.wizards.wizard_base.Wizard"
56
+ f"cms_wizards must be iterable of Wizard instances, got { type (wizard )} "
41
57
)
42
- elif wizard .id in self .wizards :
43
- msg = f"Wizard for model { wizard .get_model ()} has already been registered"
44
- logger .warning (msg )
45
- else :
46
- self .wizards [wizard .id ] = wizard
58
+ wizards |= new_wizards
59
+ return wizards
47
60
48
61
def configure_toolbar_enabled_models (self , cms_config ):
49
62
if not isinstance (cms_config .cms_toolbar_enabled_models , Iterable ):
0 commit comments