diff --git a/wagtail/wagtailsnippets/edit_handlers.py b/wagtail/wagtailsnippets/edit_handlers.py index 46f7a7eb30..155aac93b8 100644 --- a/wagtail/wagtailsnippets/edit_handlers.py +++ b/wagtail/wagtailsnippets/edit_handlers.py @@ -11,6 +11,7 @@ class BaseSnippetChooserPanel(BaseChooserPanel): object_type_name = 'item' _content_type = None + @classmethod def content_type(cls): if cls._content_type is None: @@ -38,6 +39,7 @@ class BaseSnippetChooserPanel(BaseChooserPanel): content_type.model, )) + def SnippetChooserPanel(field_name, snippet_type): return type('_SnippetChooserPanel', (BaseSnippetChooserPanel,), { 'field_name': field_name, diff --git a/wagtail/wagtailsnippets/permissions.py b/wagtail/wagtailsnippets/permissions.py index 8d81aea12a..57441f3568 100644 --- a/wagtail/wagtailsnippets/permissions.py +++ b/wagtail/wagtailsnippets/permissions.py @@ -1,6 +1,8 @@ from django.contrib.auth.models import Permission + from wagtail.wagtailsnippets.models import get_snippet_content_types + def user_can_edit_snippet_type(user, content_type): """ true if user has any permission related to this content type """ if user.is_active and user.is_superuser: diff --git a/wagtail/wagtailsnippets/urls.py b/wagtail/wagtailsnippets/urls.py index 0c7fbf89a6..326834e86e 100644 --- a/wagtail/wagtailsnippets/urls.py +++ b/wagtail/wagtailsnippets/urls.py @@ -1,7 +1,8 @@ from django.conf.urls import patterns, url -urlpatterns = patterns('wagtail.wagtailsnippets.views', +urlpatterns = patterns( + 'wagtail.wagtailsnippets.views', url(r'^$', 'snippets.index', name='wagtailsnippets_index'), url(r'^choose/(\w+)/(\w+)/$', 'chooser.choose', name='wagtailsnippets_choose'), diff --git a/wagtail/wagtailsnippets/views/chooser.py b/wagtail/wagtailsnippets/views/chooser.py index 2b34faf4a6..ba7c793713 100644 --- a/wagtail/wagtailsnippets/views/chooser.py +++ b/wagtail/wagtailsnippets/views/chooser.py @@ -1,11 +1,13 @@ +import json + from django.shortcuts import get_object_or_404 from django.contrib.auth.decorators import login_required -import json - from wagtail.wagtailadmin.modal_workflow import render_modal_workflow + from wagtail.wagtailsnippets.views.snippets import get_content_type_from_url_params, get_snippet_type_name + @login_required def choose(request, content_type_app_name, content_type_model_name): content_type = get_content_type_from_url_params(content_type_app_name, content_type_model_name) @@ -14,7 +16,8 @@ def choose(request, content_type_app_name, content_type_model_name): items = model.objects.all() - return render_modal_workflow(request, + return render_modal_workflow( + request, 'wagtailsnippets/chooser/choose.html', 'wagtailsnippets/chooser/choose.js', { 'content_type': content_type, @@ -23,6 +26,7 @@ def choose(request, content_type_app_name, content_type_model_name): } ) + @login_required def chosen(request, content_type_app_name, content_type_model_name, id): content_type = get_content_type_from_url_params(content_type_app_name, content_type_model_name) @@ -34,7 +38,8 @@ def chosen(request, content_type_app_name, content_type_model_name, id): 'string': unicode(item), }) - return render_modal_workflow(request, + return render_modal_workflow( + request, None, 'wagtailsnippets/chooser/chosen.js', { 'snippet_json': snippet_json, diff --git a/wagtail/wagtailsnippets/views/snippets.py b/wagtail/wagtailsnippets/views/snippets.py index 65fd846009..a10a16c251 100644 --- a/wagtail/wagtailsnippets/views/snippets.py +++ b/wagtail/wagtailsnippets/views/snippets.py @@ -7,12 +7,15 @@ from django.contrib import messages from django.contrib.auth.decorators import login_required from django.core.exceptions import PermissionDenied -from wagtail.wagtailsnippets.models import get_snippet_content_types -from wagtail.wagtailsnippets.permissions import user_can_edit_snippet_type from wagtail.wagtailadmin.edit_handlers import ObjectList, extract_panel_definitions_from_model_class +from wagtail.wagtailsnippets.models import get_snippet_content_types +from wagtail.wagtailsnippets.permissions import user_can_edit_snippet_type + + # == Helper functions == + def get_snippet_type_name(content_type): """ e.g. given the 'advert' content type, return ('Advert', 'Adverts') """ # why oh why is this so convoluted? @@ -22,6 +25,7 @@ def get_snippet_type_name(content_type): force_text(opts.verbose_name_plural) ) + def get_snippet_type_description(content_type): """ return the meta description of the class associated with the given content type """ opts = content_type.model_class()._meta @@ -30,6 +34,7 @@ def get_snippet_type_description(content_type): except: return '' + def get_content_type_from_url_params(app_name, model_name): """ retrieve a content type from an app_name / model_name combo. @@ -45,7 +50,10 @@ def get_content_type_from_url_params(app_name, model_name): return content_type + SNIPPET_EDIT_HANDLERS = {} + + def get_snippet_edit_handler(model): if model not in SNIPPET_EDIT_HANDLERS: panels = extract_panel_definitions_from_model_class(model) @@ -55,8 +63,10 @@ def get_snippet_edit_handler(model): return SNIPPET_EDIT_HANDLERS[model] + # == Views == + @login_required def index(request): snippet_types = [ @@ -129,6 +139,7 @@ def create(request, content_type_app_name, content_type_model_name): 'edit_handler': edit_handler, }) + @login_required def edit(request, content_type_app_name, content_type_model_name, id): content_type = get_content_type_from_url_params(content_type_app_name, content_type_model_name)