1
Fork 0

Show available snippet models for Link and embed only if template is present

This commit is contained in:
Parbhat Puri 2020-04-03 19:49:38 +05:30
parent c605c6518b
commit c50800ff84
4 changed files with 58 additions and 26 deletions

View file

@ -15,15 +15,31 @@
MUTABILITY['SNIPPET'] = 'MUTABLE';
MUTABILITY['SNIPPET-EMBED'] = 'IMMUTABLE';
const getSnippetModelChooserConfig = () => {
const getSnippetModelChooserConfig = (entityType) => {
let url;
let urlParams;
if (entityType.type === 'SNIPPET') {
return {
url: global.chooserUrls.snippetModelChooser,
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);

View file

@ -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"),
]

View file

@ -6,31 +6,42 @@ 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

View file

@ -67,9 +67,13 @@ def register_snippet_embed_feature(features):
def editor_js():
return format_html(
"""
<script>window.chooserUrls.snippetModelChooser = '{0}';</script>
<script>
window.chooserUrls.snippetLinkModelChooser = '{0}';
window.chooserUrls.snippetEmbedModelChooser = '{1}';
</script>
""",
reverse("wagtaildraftailsnippet:choose_snippet_model"),
reverse("wagtaildraftailsnippet:choose_snippet_link_model"),
reverse("wagtaildraftailsnippet:choose_snippet_embed_model"),
)