-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathcomponent_base.py
233 lines (202 loc) · 7.96 KB
/
component_base.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
import importlib
import typing
from django import forms
from django.apps import apps
from django.utils.encoding import force_str
from django.utils.translation import gettext_lazy as _
from entangled.forms import EntangledModelForm
def _import_or_empty(module, name):
try:
return importlib.import_module(module).__dict__[name]
except (ImportError, KeyError):
return type(name, (), {})
def _get_mixin_classes(mixins: list, suffix: str = "") -> list[type]:
"""Find and import mixin classes from a list of mixin strings"""
mixins = [
(mixin.rsplit(".")[0], f"{mixin.rsplit('.')[-1]}{suffix}Mixin")
if "." in mixin
else ("djangocms_frontend.common", f"{mixin}{suffix}Mixin")
for mixin in reversed(mixins)
]
return [_import_or_empty(module, name) for module, name in mixins]
class classproperty:
def __init__(self, fget):
self.fget = fget
def __get__(self, obj, owner):
return self.fget(owner)
class Slot:
"""Slat class as syntactic surgar to more easily define slot plugins"""
def __init__(self, name, verbose_name, **kwargs):
self.name = name
self.verbose_name = verbose_name
self.kwargs = kwargs
class CMSFrontendComponent(forms.Form):
"""Base class for frontend components:"""
slot_template = "djangocms_frontend/slot.html"
_base_form = EntangledModelForm
_plugin_mixins = []
_model_mixins = []
_admin_form = None
_model = None
_plugin = None
META_FIELDS = [
"is_local",
"disable_edit",
"disable_child_plugins",
"show_add_form",
"frontend_editable_fields",
"link_fieldset_position",
"require_parent",
"parent_classes",
]
@classmethod
def admin_form_factory(cls, **kwargs) -> type:
if cls._admin_form is None:
from djangocms_frontend.models import FrontendUIItem
mixins = getattr(cls._component_meta, "mixins", [])
mixins = _get_mixin_classes(mixins, "Form")
cls._admin_form = type(
f"{cls.__name__}Form",
(
*mixins,
cls,
cls._base_form,
),
{
**kwargs,
"Meta": type(
"Meta",
(),
{
"model": FrontendUIItem,
"entangled_fields": {
"config": list(cls.declared_fields.keys()),
},
},
),
},
)
return cls._admin_form
@classmethod
def get_slot_plugins(cls) -> dict[str:str]:
slots: list[Slot] = [
slot if isinstance(slot, Slot) else Slot(*slot) for slot in getattr(cls._component_meta, "slots", [])
]
return {f"{cls.__name__}{slot.name.capitalize()}Plugin": slot for slot in slots}
@classmethod
def plugin_model_factory(cls) -> type:
if cls._model is None:
from djangocms_frontend.models import FrontendUIItem
app_config = apps.get_containing_app_config(cls.__module__)
if app_config is None: # pragma: no cover
raise ValueError(f"Cannot find app_config for {cls.__module__}")
cls._model = type(
cls.__name__,
(
*cls._model_mixins,
FrontendUIItem,
),
{
"Meta": type(
"Meta",
(),
{
"app_label": app_config.label,
"proxy": True,
"managed": False,
"verbose_name": getattr(cls._component_meta, "name", cls.__name__),
},
),
"get_short_description": cls.get_short_description,
"__module__": cls.__module__,
},
)
return cls._model
@classmethod
def plugin_factory(cls) -> type:
from .ui_plugin_base import CMSUIComponent
if cls._plugin is None:
mixins = getattr(cls._component_meta, "mixins", [])
slots = cls.get_slot_plugins()
mixins = _get_mixin_classes(mixins)
cls._plugin = type(
cls.__name__ + "Plugin",
(
*mixins,
*cls._plugin_mixins,
CMSUIComponent,
),
{
"name": getattr(cls._component_meta, "name", cls.__name__),
"module": getattr(cls._component_meta, "module", _("Components")),
"model": cls.plugin_model_factory(),
"form": cls.admin_form_factory(),
"allow_children": slots or getattr(cls._component_meta, "allow_children", False),
"child_classes": getattr(cls._component_meta, "child_classes", []) + list(slots.keys()),
"render_template": getattr(cls._component_meta, "render_template", CMSUIComponent.render_template),
"fieldsets": getattr(cls, "fieldsets", cls._generate_fieldset()),
"change_form_template": "djangocms_frontend/admin/base.html",
"slots": slots,
"save_model": cls.save_model,
**{
field: getattr(cls._component_meta, field)
for field in cls.META_FIELDS
if hasattr(cls._component_meta, field)
},
**(
{
"get_render_template": cls.get_render_template,
"TEMPLATES": cls.TEMPLATES,
}
if hasattr(cls, "get_render_template")
else {}
),
"__module__": cls.__module__,
},
)
return cls._plugin
@classmethod
def slot_plugin_factory(cls) -> list[type]:
from cms.plugin_base import CMSPluginBase
slots = cls.get_slot_plugins()
return [
type(
name,
(CMSPluginBase,),
{
"name": force_str(slot.verbose_name),
"module": getattr(cls._component_meta, "module", _("Component")),
"allow_children": True,
"edit_disabled": True,
"is_local": False,
"show_add_form": False,
"parent_classes": [cls.__name__ + "Plugin"],
"render_template": cls.slot_template,
**slot.kwargs,
},
)
for name, slot in slots.items()
]
@classmethod
def get_registration(cls) -> tuple[type, type, list[type]]:
return (
cls.plugin_model_factory(),
cls.plugin_factory(),
cls.slot_plugin_factory(),
)
@classproperty
def _component_meta(cls) -> typing.Optional[type]:
return getattr(cls, "Meta", None)
@classmethod
def _generate_fieldset(cls) -> list[tuple[typing.Optional[str], dict]]:
return [(None, {"fields": cls.declared_fields.keys()})]
def get_short_description(self) -> str:
return self.config.get("title", "")
def save_model(self, request, obj, form: forms.Form, change: bool) -> None:
"""Auto-creates slot plugins upon creation of component plugin instance"""
from cms.api import add_plugin
from .ui_plugin_base import CMSUIComponent
super(CMSUIComponent, self).save_model(request, obj, form, change)
if not change:
for slot in self.slots.keys():
add_plugin(obj.placeholder, slot, obj.language, target=obj)