|
| 1 | +import importlib |
1 | 2 | import os |
2 | 3 | from collections import defaultdict |
3 | 4 |
|
4 | 5 | from django.apps import AppConfig, apps |
5 | 6 | from django.template import loader |
6 | 7 |
|
| 8 | +from djangocms_frontend import settings |
7 | 9 | from djangocms_frontend.component_base import CMSFrontendComponent |
8 | 10 |
|
9 | 11 |
|
| 12 | +default_field_context = { |
| 13 | + "djanghocms_text": "djangocms_text.fields.TextFormField", |
| 14 | + "djanghocms_text_ckeditor": "djangocms_text_ckeditor.fields.TextFormField", |
| 15 | + "djangocms_link": "djangocms_link.fields.LinkFormField", |
| 16 | + "djangocms_frontend.contrib.image": "djangocms_frontend.contrib.image.fields.ImageFormField", |
| 17 | + "djangocms_frontend.contrib.icon": "djangocms_frontend.contrib.icon.fields.IconPickerField", |
| 18 | +} |
| 19 | + |
| 20 | + |
| 21 | +def get_field_context() -> dict: |
| 22 | + field_context = {} |
| 23 | + default_field_context.update(settings.COMPONENT_FIELDS) |
| 24 | + for key, value in default_field_context.items(): |
| 25 | + if apps.is_installed(key) and "." in value: |
| 26 | + module, field_name = value.rsplit(".", 1) |
| 27 | + field_context[field_name] = importlib.import_module(module).__dict__[field_name] |
| 28 | + return field_context |
| 29 | + |
| 30 | + |
10 | 31 | def find_cms_component_templates() -> list[str]: |
11 | 32 | templates = [] |
12 | 33 | for app in apps.get_app_configs(): |
@@ -46,9 +67,9 @@ def scan_templates_for_component_declaration(templates: list[str]) -> list[CMSFr |
46 | 67 | from django.forms import fields |
47 | 68 |
|
48 | 69 | components = [] |
49 | | - |
| 70 | + field_context = get_field_context() |
50 | 71 | for template_name in templates: |
51 | | - context = {"_cms_components": defaultdict(list), "forms": fields, "instance": {}} |
| 72 | + context = {"_cms_components": defaultdict(list), "forms": fields, "instance": {}, **field_context} |
52 | 73 | loader.render_to_string(template_name, context) |
53 | 74 | cms_component = context["_cms_components"].get("cms_component", []) |
54 | 75 | fields = context["_cms_components"].get("fields", []) |
|
0 commit comments