Skip to content

Commit

Permalink
Snippet chooser Panel working for different sites. Chooser will work …
Browse files Browse the repository at this point in the history
…depending on the host (site) from which the admin is being accessed

Please enter the commit message for your changes. Lines starting
  • Loading branch information
Parbhat committed Apr 26, 2018
1 parent 42fcc1e commit 3b30633
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<div class="nice-padding">
{# Need to keep the form in the HTML, even if the snippet is not searchable #}
{# This is to allow pagination links to be generated from the form action URL #}
<form class="snippet-search search-bar" action="{% url 'wagtailsnippets:choose' model_opts.app_label model_opts.model_name site.id %}" method="GET" novalidate>
<form class="snippet-search search-bar" action="{% url 'wagtailsnippets:choose' site.id model_opts.app_label model_opts.model_name %}" method="GET" novalidate>
{% if is_searchable %}
<ul class="fields">
{% for field in search_form %}
Expand Down
10 changes: 8 additions & 2 deletions girleffect/wagtailsnippets/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,16 @@
url(r'^$', snippets.index_redirect, name='index'),
url(r'^(\d+)/$', snippets.index, name='index'),

url(r'^choose/(\d+)/$', chooser.choose, name='choose_generic'),
url(r'^choose/(\w+)/(\w+)/(\d+)/$', chooser.choose, name='choose'),
url(r'^choose/$', chooser.choose, name='choose_generic'),
url(r'^choose/(\d+)/(\w+)/(\w+)/$', chooser.choose, name='choose'),
url(r'^choose/(\w+)/(\w+)/(\d+)/(\d+)/$', chooser.chosen, name='chosen'),

url(
r'^chooser/current-site-id/$',
snippets.get_current_site_id_for_snippet_chooser,
name='current_site_id'
),

url(r'^(\w+)/(\w+)/(\d+)/$', snippets.list, name='list'),
url(r'^(\w+)/(\w+)/(\d+)/add/$', snippets.create, name='add'),
url(r'^(\w+)/(\w+)/(\d+)/(\d+)/$', snippets.edit, name='edit'),
Expand Down
2 changes: 1 addition & 1 deletion girleffect/wagtailsnippets/views/chooser.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
from .snippets import get_snippet_model_from_url_params, get_editable_sites, get_site


def choose(request, app_label, model_name, site_id):
def choose(request, site_id, app_label, model_name):
model = get_snippet_model_from_url_params(app_label, model_name)
site = get_site(get_editable_sites(request.user), site_id)
items = model.objects.filter(site=site)
Expand Down
8 changes: 7 additions & 1 deletion girleffect/wagtailsnippets/views/snippets.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

from django.apps import apps
from django.core.urlresolvers import reverse
from django.http import Http404
from django.http import Http404, JsonResponse
from django.shortcuts import get_object_or_404, redirect, render
from django.utils.text import capfirst
from django.utils.translation import ugettext as _
Expand Down Expand Up @@ -308,3 +308,9 @@ def usage(request, app_label, model_name, site_id, id):
'instance': instance,
'used_by': used_by
})


def get_current_site_id_for_snippet_chooser(request):
data = {'site_id': request.site.id}

return JsonResponse(data)
21 changes: 19 additions & 2 deletions girleffect/wagtailsnippets/wagtail_hooks.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from django.contrib.contenttypes.models import ContentType
from django.contrib.staticfiles.templatetags.staticfiles import static
from django.core import urlresolvers
from django.utils.safestring import mark_safe
from django.utils.html import format_html
from django.utils.translation import ugettext_lazy as _
from wagtail.wagtailadmin.menu import MenuItem
Expand Down Expand Up @@ -42,10 +43,26 @@ def editor_js():
return format_html(
"""
<script src="{0}"></script>
<script>window.chooserUrls.snippetChooser = '{1}';</script>
{1}
""",
static('wagtailsnippets/js/snippet-chooser.js'),
urlresolvers.reverse('wagtailsnippets:choose_generic', args=[1]) # FIXME
mark_safe(
"""
<script>
window.chooserUrls.snippetChooser = '%s';
$.ajax({
url: '%s',
dataType: 'json',
success: function (data) {
window.chooserUrls.snippetChooser += (data.site_id + '/');
}
});
</script>
""" % (
urlresolvers.reverse('wagtailsnippets:choose_generic'),
urlresolvers.reverse('wagtailsnippets:current_site_id'),
)
),
)


Expand Down

0 comments on commit 3b30633

Please sign in to comment.