Example #1
0
    def process_view(self, request, view_func, view_args, view_kwargs):
        """
        Per-request mechanics for the current page object.
        """

        # Load the closest matching page by slug, and assign it to the
        # request object. If none found, skip all further processing.
        slug = path_to_slug(request.path_info)
        pages = Page.objects.with_ascendants_for_slug(
            slug, for_user=request.user, include_login_required=True)
        if pages:
            page = pages[0]
            setattr(request, "page", page)
            context_processors.page(request)
        else:
            return

        # Handle ``page.login_required``.
        if page.login_required and not request.user.is_authenticated():
            return redirect_to_login(request.get_full_path())

        # If the view isn't Mezzanine's page view, try to return the result
        # immediately. In the case of a 404 with an URL slug that matches a
        # page exactly, swallow the exception and try Mezzanine's page view.
        #
        # This allows us to set up pages with URLs that also match non-page
        # urlpatterns. For example, a page could be created with the URL
        # /blog/about/, which would match the blog urlpattern, and assuming
        # there wasn't a blog post with the slug "about", would raise a 404
        # and subsequently be rendered by Mezzanine's page view.
        if view_func != page_view:
            try:
                return view_func(request, *view_args, **view_kwargs)
            except Http404:
                if page.slug != slug:
                    raise

        # Run page processors.
        extra_context = {}
        model_processors = page_processors.processors[page.content_model]
        slug_processors = page_processors.processors["slug:%s" % page.slug]
        for (processor, exact_page) in slug_processors + model_processors:
            if exact_page and not page.is_current:
                continue
            processor_response = processor(request, page)
            if isinstance(processor_response, HttpResponse):
                return processor_response
            elif processor_response:
                try:
                    for k, v in processor_response.items():
                        if k not in extra_context:
                            extra_context[k] = v
                except (TypeError, ValueError):
                    name = "%s.%s" % (processor.__module__, processor.__name__)
                    error = ("The page processor %s returned %s but must "
                             "return HttpResponse or dict." %
                             (name, type(processor_response)))
                    raise ValueError(error)

        return page_view(request, slug, extra_context=extra_context)
Example #2
0
    def get(self, request, *args, **kwargs):
        node = self.get_object()
        page = self.get_page()

        context = {"page": page, "root_node_override": node, "_current_page": page}

        return page_view(request, page.slug, extra_context=context)
Example #3
0
    def process_view(self, request, view_func, view_args, view_kwargs):
        """
        Per-request mechanics for the current page object.
        """

        # Load the closest matching page by slug, and assign it to the
        # request object. If none found, skip all further processing.
        slug = path_to_slug(request.path_info)
        pages = Page.objects.with_ascendants_for_slug(slug,
                        for_user=request.user, include_login_required=True)
        if pages:
            page = pages[0]
            setattr(request, "page", page)
            context_processors.page(request)
        else:
            return

        # Handle ``page.login_required``.
        if page.login_required and not request.user.is_authenticated():
            return redirect_to_login(request.get_full_path())

        # If the view isn't Mezzanine's page view, try to return the result
        # immediately. In the case of a 404 with an URL slug that matches a
        # page exactly, swallow the exception and try Mezzanine's page view.
        #
        # This allows us to set up pages with URLs that also match non-page
        # urlpatterns. For example, a page could be created with the URL
        # /blog/about/, which would match the blog urlpattern, and assuming
        # there wasn't a blog post with the slug "about", would raise a 404
        # and subsequently be rendered by Mezzanine's page view.
        if view_func != page_view:
            try:
                return view_func(request, *view_args, **view_kwargs)
            except Http404:
                if page.slug != slug:
                    raise

        # Run page processors.
        extra_context = {}
        model_processors = page_processors.processors[page.content_model]
        slug_processors = page_processors.processors["slug:%s" % page.slug]
        for (processor, exact_page) in slug_processors + model_processors:
            if exact_page and not page.is_current:
                continue
            processor_response = processor(request, page)
            if isinstance(processor_response, HttpResponse):
                return processor_response
            elif processor_response:
                try:
                    for k, v in processor_response.items():
                        if k not in extra_context:
                            extra_context[k] = v
                except (TypeError, ValueError):
                    name = "%s.%s" % (processor.__module__, processor.__name__)
                    error = ("The page processor %s returned %s but must "
                             "return HttpResponse or dict." %
                             (name, type(processor_response)))
                    raise ValueError(error)

        return page_view(request, slug, extra_context=extra_context)
Example #4
0
    def form_invalid(self, form):
        root_node = self.form_node.get_root()
        page = self.get_page()

        return page_view(
            self.request,
            page.slug,
            extra_context=self.get_context_data(form=form, page=page, root_node_override=root_node),
        )
Example #5
0
    def page_view(self, request, page, context):
        # Before Mezzanine==3.1.2, the page view would check extra_context for
        # the page object, so we would pass it in then, starting with 3.1.2 and
        # the introduction of mezzanine.pages.context_processors.page,
        # Mezzanine expects the PageMiddleware to set page on the request. The
        # context_processor will add the page and _current_page values to the
        # context for us, but we are still passing it in for Mezzanine<=3.1.1.
        if hasattr(page, "page_ptr"):
            # mezzanine.pages.middleware.PageMiddleware sticks a Page object,
            # not a WidgyPage object, to the request so we also need to get the
            # raw Page object.
            page = page.page_ptr

        request.page = page

        extra_context = {"page": page, "_current_page": page}
        extra_context.update(context)

        return page_view(request, page.slug, extra_context=extra_context)
Example #6
0
    def page_view(self, request, page, context):
        # Before Mezzanine==3.1.2, the page view would check extra_context for
        # the page object, so we would pass it in then, starting with 3.1.2 and
        # the introduction of mezzanine.pages.context_processors.page,
        # Mezzanine expects the PageMiddleware to set page on the request. The
        # context_processor will add the page and _current_page values to the
        # context for us, but we are still passing it in for Mezzanine<=3.1.1.
        if hasattr(page, 'page_ptr'):
            # mezzanine.pages.middleware.PageMiddleware sticks a Page object,
            # not a WidgyPage object, to the request so we also need to get the
            # raw Page object.
            page = page.page_ptr

        request.page = page

        extra_context = {
            'page': page,
            '_current_page': page,
        }
        extra_context.update(context)

        return page_view(request, page.slug, extra_context=extra_context)
Example #7
0
    def process_view(self, request, view_func, view_args, view_kwargs):
        """
        Per-request mechanics for the current page object.
        """

        cp = "mezzanine.pages.context_processors.page"
        if cp not in settings.TEMPLATE_CONTEXT_PROCESSORS:
            raise ImproperlyConfigured("%s is missing from "
                "settings.TEMPLATE_CONTEXT_PROCESSORS" % cp)

        # Load the closest matching page by slug, and assign it to the
        # request object. If none found, skip all further processing.
        slug = path_to_slug(request.path_info)
        pages = Page.objects.with_ascendants_for_slug(slug,
                        for_user=request.user, include_login_required=True)
        if pages:
            page = pages[0]
            setattr(request, "page", page)
        else:
            return

        # Handle ``page.login_required``.
        if page.login_required and not request.user.is_authenticated():
            path = urlquote(request.get_full_path())
            bits = (settings.LOGIN_URL, REDIRECT_FIELD_NAME, path)
            return redirect("%s?%s=%s" % bits)

        # Here we do a wacky check with non-page views and 404s.
        # Basically if the view function isn't the page view and
        # raises a 404, but also matches an exact page slug, we then
        # forget about the non-page view, and run the page view
        # with the correct args.
        # This check allows us to set up pages with URLs that also
        # match non-page urlpatterns, for example a page could be
        # created with the URL /blog/about/, which would match the
        # blog urlpattern, and assuming there wasn't a blog post
        # with the slug "about", would raise a 404.
        try:
            response = view_func(request, *view_args, **view_kwargs)
        except Http404:
            if (page.slug == slug and view_func != page_view and
                    page.content_model != 'link'):
                # Matched a non-page urlpattern, but got a 404
                # for a URL that matches a valid page slug, so
                # use the page view.
                response = page_view(request, slug, **view_kwargs)
            else:
                raise

        # Run page processors.
        model_processors = page_processors.processors[page.content_model]
        slug_processors = page_processors.processors["slug:%s" % page.slug]
        for (processor, exact_page) in slug_processors + model_processors:
            if exact_page and not page.is_current:
                continue
            processor_response = processor(request, page)
            if isinstance(processor_response, HttpResponse):
                return processor_response
            elif processor_response:
                try:
                    for k in processor_response:
                        if k not in response.context_data:
                            response.context_data[k] = processor_response[k]
                except (TypeError, ValueError):
                    name = "%s.%s" % (processor.__module__, processor.__name__)
                    error = ("The page processor %s returned %s but must "
                             "return HttpResponse or dict." %
                             (name, type(processor_response)))
                    raise ValueError(error)

        return response
    def process_view(self, request, view_func, view_args, view_kwargs):
        """
        Per-request mechanics for the current page object.
        """

        cp = "mezzanine.pages.context_processors.page"
        if cp not in settings.TEMPLATE_CONTEXT_PROCESSORS:
            raise ImproperlyConfigured("%s is missing from "
                                       "settings.TEMPLATE_CONTEXT_PROCESSORS" %
                                       cp)

        # Load the closest matching page by slug, and assign it to the
        # request object. If none found, skip all further processing.
        slug = path_to_slug(request.path_info)
        pages = Page.objects.with_ascendants_for_slug(
            slug, for_user=request.user, include_login_required=True)
        if pages:
            page = pages[0]
            setattr(request, "page", page)
        else:
            return

        # Handle ``page.login_required``.
        if page.login_required and not request.user.is_authenticated():
            path = urlquote(request.get_full_path())
            bits = (settings.LOGIN_URL, REDIRECT_FIELD_NAME, path)
            return redirect("%s?%s=%s" % bits)

        # Here we do a wacky check with non-page views and 404s.
        # Basically if the view function isn't the page view and
        # raises a 404, but also matches an exact page slug, we then
        # forget about the non-page view, and run the page view
        # with the correct args.
        # This check allows us to set up pages with URLs that also
        # match non-page urlpatterns, for example a page could be
        # created with the URL /blog/about/, which would match the
        # blog urlpattern, and assuming there wasn't a blog post
        # with the slug "about", would raise a 404.
        try:
            response = view_func(request, *view_args, **view_kwargs)
        except Http404:
            if (page.slug == slug and view_func != page_view
                    and page.content_model != 'link'):
                # Matched a non-page urlpattern, but got a 404
                # for a URL that matches a valid page slug, so
                # use the page view.
                response = page_view(request, slug, **view_kwargs)
            else:
                raise

        # Run page processors.
        model_processors = page_processors.processors[page.content_model]
        slug_processors = page_processors.processors["slug:%s" % page.slug]
        for (processor, exact_page) in slug_processors + model_processors:
            if exact_page and not page.is_current:
                continue
            processor_response = processor(request, page)
            if isinstance(processor_response, HttpResponse):
                return processor_response
            elif processor_response and hasattr(response, "context_data"):
                try:
                    for k in processor_response:
                        if k not in response.context_data:
                            response.context_data[k] = processor_response[k]
                except (TypeError, ValueError):
                    name = "%s.%s" % (processor.__module__, processor.__name__)
                    error = ("The page processor %s returned %s but must "
                             "return HttpResponse or dict." %
                             (name, type(processor_response)))
                    raise ValueError(error)

        return response