From 78481dc84665a9b27645c1139cb90a02ff6128e1 Mon Sep 17 00:00:00 2001 From: Matt Westcott Date: Thu, 12 Jun 2014 16:48:44 +0100 Subject: [PATCH] syntactic sugar for get_page_modes: change it to a property called preview_modes and add a default_preview_mode helper Conflicts: wagtail/wagtailadmin/views/pages.py --- docs/building_your_site/djangodevelopers.rst | 2 +- docs/model_recipes.rst | 6 ++--- wagtail/wagtailadmin/views/pages.py | 23 +++++++------------- wagtail/wagtailcore/models.py | 15 ++++++++++--- wagtail/wagtailforms/models.py | 9 ++++---- 5 files changed, 28 insertions(+), 27 deletions(-) diff --git a/docs/building_your_site/djangodevelopers.rst b/docs/building_your_site/djangodevelopers.rst index aeabb264f1..e725730893 100644 --- a/docs/building_your_site/djangodevelopers.rst +++ b/docs/building_your_site/djangodevelopers.rst @@ -201,6 +201,7 @@ Properties: * status_string * subpage_types * indexed_fields +* preview_modes Methods: @@ -213,7 +214,6 @@ Methods: * get_descendants * get_siblings * search -* get_page_modes * show_as_mode diff --git a/docs/model_recipes.rst b/docs/model_recipes.rst index 6a4abed474..ae011126a7 100644 --- a/docs/model_recipes.rst +++ b/docs/model_recipes.rst @@ -190,10 +190,10 @@ Load Alternate Templates by Overriding get_template() -Page Modes ----------- +Preview Modes +------------- -get_page_modes +preview_modes show_as_mode diff --git a/wagtail/wagtailadmin/views/pages.py b/wagtail/wagtailadmin/views/pages.py index 7a2ec4f06b..e5a0c885cd 100644 --- a/wagtail/wagtailadmin/views/pages.py +++ b/wagtail/wagtailadmin/views/pages.py @@ -230,7 +230,7 @@ def create(request, content_type_app_name, content_type_model_name, parent_page_ 'page_class': page_class, 'parent_page': parent_page, 'edit_handler': edit_handler, - 'display_modes': page.get_page_modes(), + 'display_modes': page.preview_modes, 'form': form, # Used in unit tests }) @@ -361,7 +361,7 @@ def edit(request, page_id): 'page': page, 'edit_handler': edit_handler, 'errors_debug': errors_debug, - 'display_modes': page.get_page_modes(), + 'display_modes': page.preview_modes, 'form': form, # Used in unit tests }) @@ -409,12 +409,8 @@ def preview_on_edit(request, page_id): if form.is_valid(): form.save(commit=False) - try: - display_mode = request.GET['mode'] - except KeyError: - display_mode = page.get_page_modes()[0][0] - - response = page.show_as_mode(display_mode) + preview_mode = request.GET.get('mode', page.default_preview_mode) + response = page.show_as_mode(preview_mode) response['X-Wagtail-Preview'] = 'ok' return response @@ -425,7 +421,7 @@ def preview_on_edit(request, page_id): response = render(request, 'wagtailadmin/pages/edit.html', { 'page': page, 'edit_handler': edit_handler, - 'display_modes': page.get_page_modes(), + 'display_modes': page.preview_modes, }) response['X-Wagtail-Preview'] = 'error' return response @@ -458,11 +454,8 @@ def preview_on_create(request, content_type_app_name, content_type_model_name, p page.depth = parent_page.depth + 1 page.path = Page._get_children_path_interval(parent_page.path)[1] - try: - display_mode = request.GET['mode'] - except KeyError: - display_mode = page.get_page_modes()[0][0] - response = page.show_as_mode(display_mode) + preview_mode = request.GET.get('mode', page.default_preview_mode) + response = page.show_as_mode(preview_mode) response['X-Wagtail-Preview'] = 'ok' return response @@ -476,7 +469,7 @@ def preview_on_create(request, content_type_app_name, content_type_model_name, p 'page_class': page_class, 'parent_page': parent_page, 'edit_handler': edit_handler, - 'display_modes': page.get_page_modes(), + 'display_modes': page.preview_modes, }) response['X-Wagtail-Preview'] = 'error' return response diff --git a/wagtail/wagtailcore/models.py b/wagtail/wagtailcore/models.py index b980d7f54c..42e132daad 100644 --- a/wagtail/wagtailcore/models.py +++ b/wagtail/wagtailcore/models.py @@ -706,19 +706,28 @@ class Page(six.with_metaclass(PageBase, MP_Node, ClusterableModel, Indexed)): "request middleware returned a response") return request - def get_page_modes(self): + @property + def preview_modes(self): """ - Return a list of (internal_name, display_name) tuples for the modes in which + A list of (internal_name, display_name) tuples for the modes in which this page can be displayed for preview/moderation purposes. Ordinarily a page will only have one display mode, but subclasses of Page can override this - for example, a page containing a form might have a default view of the form, and a post-submission 'thankyou' page """ + return self.get_page_modes() + + def get_page_modes(self): + # Deprecated accessor for the preview_modes property return [('', 'Default')] + @property + def default_preview_mode(self): + return self.preview_modes[0][0] + def show_as_mode(self, mode_name): """ - Given an internal name from the get_page_modes() list, return an HTTP response + Given an internal name from the preview_modes list, return an HTTP response indicative of the page being viewed in that mode. By default this passes a dummy request into the serve() mechanism, ensuring that it matches the behaviour on the front-end; subclasses that define additional page modes will need to diff --git a/wagtail/wagtailforms/models.py b/wagtail/wagtailforms/models.py index 41414d3910..6bde684ace 100644 --- a/wagtail/wagtailforms/models.py +++ b/wagtail/wagtailforms/models.py @@ -170,11 +170,10 @@ class AbstractForm(Page): 'form': form, }) - def get_page_modes(self): - return [ - ('form', 'Form'), - ('landing', 'Landing page'), - ] + preview_modes = [ + ('form', 'Form'), + ('landing', 'Landing page'), + ] def show_as_mode(self, mode): if mode == 'landing':