From c50800ff849ee6eced90f2e74a398f5f53b7c7fb Mon Sep 17 00:00:00 2001 From: Parbhat Puri Date: Fri, 3 Apr 2020 19:49:38 +0530 Subject: [PATCH] Show available snippet models for Link and embed only if template is present --- .../js/wagtail_draftail_snippet.js | 30 ++++++++++---- wagtail_draftail_snippet/urls.py | 5 ++- wagtail_draftail_snippet/views.py | 41 ++++++++++++------- wagtail_draftail_snippet/wagtail_hooks.py | 8 +++- 4 files changed, 58 insertions(+), 26 deletions(-) diff --git a/wagtail_draftail_snippet/static/wagtail_draftail_snippet/js/wagtail_draftail_snippet.js b/wagtail_draftail_snippet/static/wagtail_draftail_snippet/js/wagtail_draftail_snippet.js index 15af48e..c34e01c 100644 --- a/wagtail_draftail_snippet/static/wagtail_draftail_snippet/js/wagtail_draftail_snippet.js +++ b/wagtail_draftail_snippet/static/wagtail_draftail_snippet/js/wagtail_draftail_snippet.js @@ -15,15 +15,31 @@ MUTABILITY['SNIPPET'] = 'MUTABLE'; MUTABILITY['SNIPPET-EMBED'] = 'IMMUTABLE'; - const getSnippetModelChooserConfig = () => { + const getSnippetModelChooserConfig = (entityType) => { let url; let urlParams; - return { - url: global.chooserUrls.snippetModelChooser, - urlParams: {}, - onload: global.SNIPPET_MODEL_CHOOSER_MODAL_ONLOAD_HANDLERS, - }; + if (entityType.type === 'SNIPPET') { + return { + url: global.chooserUrls.snippetLinkModelChooser, + urlParams: {}, + onload: global.SNIPPET_MODEL_CHOOSER_MODAL_ONLOAD_HANDLERS, + }; + } + else if (entityType.type === 'SNIPPET-EMBED') { + return { + url: global.chooserUrls.snippetEmbedModelChooser, + urlParams: {}, + onload: global.SNIPPET_MODEL_CHOOSER_MODAL_ONLOAD_HANDLERS, + }; + } + else { + return { + url: null, + urlParams: {}, + onload: {}, + }; + } }; const getSnippetModelObjectChooserConfig = () => { @@ -62,7 +78,7 @@ componentDidMount() { const { onClose, entityType, entity, editorState } = this.props; - const { url, urlParams, onload } = getSnippetModelChooserConfig(); + const { url, urlParams, onload } = getSnippetModelChooserConfig(entityType); $(document.body).on('hidden.bs.modal', this.onClose); diff --git a/wagtail_draftail_snippet/urls.py b/wagtail_draftail_snippet/urls.py index 326fc28..4d0707d 100644 --- a/wagtail_draftail_snippet/urls.py +++ b/wagtail_draftail_snippet/urls.py @@ -1,10 +1,11 @@ from django.conf.urls import url -from .views import choose_snippet_model +from .views import choose_snippet_link_model, choose_snippet_embed_model app_name = "wagtaildraftailsnippet" urlpatterns = [ - url(r"^choose-model/$", choose_snippet_model, name="choose_snippet_model") + url(r"^choose-link-model/$", choose_snippet_link_model, name="choose_snippet_link_model"), + url(r"^choose-embed-model/$", choose_snippet_embed_model, name="choose_snippet_embed_model"), ] diff --git a/wagtail_draftail_snippet/views.py b/wagtail_draftail_snippet/views.py index f4d018c..50ec16f 100644 --- a/wagtail_draftail_snippet/views.py +++ b/wagtail_draftail_snippet/views.py @@ -6,34 +6,45 @@ from wagtail.snippets.models import get_snippet_models from .utils import get_snippet_frontend_template, get_snippet_embed_frontend_template -def choose_snippet_model(request): +def choose_snippet_link_model(request): snippet_model_opts = [] - # Only display those snippet models which have snippet frontend template + # Only display those snippet models which have snippet link frontend template for snippet_model in get_snippet_models(): - snippet_included = False snippet_frontend_template = get_snippet_frontend_template( snippet_model._meta.app_label, snippet_model._meta.model_name ) - snippet_embed_frontend_template = get_snippet_embed_frontend_template( - snippet_model._meta.app_label, snippet_model._meta.model_name - ) try: get_template(snippet_frontend_template) snippet_model_opts.append(snippet_model._meta) - snippet_included = True except TemplateDoesNotExist: pass - if not snippet_included: - try: - get_template(snippet_embed_frontend_template) - snippet_model_opts.append(snippet_model._meta) - snippet_included = True - except TemplateDoesNotExist: - pass - + return render_modal_workflow( + request, + "wagtail_draftail_snippet/choose_snippet_model.html", + None, + {"snippet_model_opts": snippet_model_opts}, + json_data={"step": "choose"}, + ) + + +def choose_snippet_embed_model(request): + snippet_model_opts = [] + + # Only display those snippet models which have snippet embed frontend template + for snippet_model in get_snippet_models(): + snippet_frontend_template = get_snippet_embed_frontend_template( + snippet_model._meta.app_label, snippet_model._meta.model_name + ) + + try: + get_template(snippet_frontend_template) + snippet_model_opts.append(snippet_model._meta) + except TemplateDoesNotExist: + pass + return render_modal_workflow( request, "wagtail_draftail_snippet/choose_snippet_model.html", diff --git a/wagtail_draftail_snippet/wagtail_hooks.py b/wagtail_draftail_snippet/wagtail_hooks.py index 4d94ff6..e6f7bed 100644 --- a/wagtail_draftail_snippet/wagtail_hooks.py +++ b/wagtail_draftail_snippet/wagtail_hooks.py @@ -67,9 +67,13 @@ def register_snippet_embed_feature(features): def editor_js(): return format_html( """ - + """, - reverse("wagtaildraftailsnippet:choose_snippet_model"), + reverse("wagtaildraftailsnippet:choose_snippet_link_model"), + reverse("wagtaildraftailsnippet:choose_snippet_embed_model"), )