Skip to content

Commit

Permalink
Improvements to draftail option handling
Browse files Browse the repository at this point in the history
  • Loading branch information
aaronhaslett committed May 30, 2021
1 parent acead70 commit f7a8e14
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 13 deletions.
5 changes: 5 additions & 0 deletions wagtailmodelchoosers/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,11 @@ def get_chooser_options(chooser):
return copy.deepcopy(chooser_options)


def get_all_chooser_options():
choosers_options = getattr(settings, "MODEL_CHOOSERS_OPTIONS", {})
return {c: get_chooser_options(c) for c in choosers_options.keys()}


def first_non_empty(data_or_instance, field_or_fields, default=None):
"""
Return the first non-empty attribute of `instance` or `default`
Expand Down
20 changes: 13 additions & 7 deletions wagtailmodelchoosers/wagtail_hooks.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from wagtail.core import hooks

from wagtailmodelchoosers.views import ModelView, RemoteResourceView
from .utils import get_all_chooser_options


@hooks.register("insert_editor_css")
Expand Down Expand Up @@ -48,12 +49,17 @@ def wagtailmodelchoosers_admin_urls():

@hooks.register("register_rich_text_features")
def register_rich_text_features(features):
entity_types = getattr(settings, "MODEL_CHOOSER_DRAFTAIL_ENTITY_TYPES", {})
for entity_type in entity_types:
type_ = entity_type.get("type", None)
if not type_:
choosers = get_all_chooser_options();
for name, chooser in choosers.items():
draftail_type = chooser.get('draftail_type', None)
if not draftail_type:
continue

features.default_features.append(type_)
feature = draftail_features.EntityFeature(entity_type)
features.register_editor_plugin("draftail", type_, feature)
# If there's no content_type then it's a remote chooser, and
# content_type means the name of the chooser instead of the model.
if 'content_type' not in chooser:
chooser['content_type'] = name

chooser['type'] = draftail_type
feature = draftail_features.EntityFeature(chooser)
features.register_editor_plugin("draftail", name, feature)
10 changes: 4 additions & 6 deletions wagtailmodelchoosers/widgets.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
from wagtail.admin.staticfiles import versioned_static
from wagtail.utils.widgets import WidgetWithScript

from .utils import first_non_empty
from .utils import first_non_empty, get_all_chooser_options


class ModelChooserWidget(WidgetWithScript, widgets.Input):
Expand Down Expand Up @@ -222,9 +222,7 @@ def media(self):

def get_context(self, *args, **kwargs):
context = super().get_context(*args, **kwargs)
entity_types = getattr(settings, "MODEL_CHOOSER_DRAFTAIL_ENTITY_TYPES", {})
field = "type"
context["entity_types"] = json.dumps(
[e[field] for e in entity_types if field in e]
)
f = "draftail_type"
met = [c[f] for c in get_all_chooser_options().values() if f in c]
context["modelchooser_entity_types"] = json.dumps(met)
return context

0 comments on commit f7a8e14

Please sign in to comment.