def _new_route(self, request, path_components):
    """
    Rewrite route method in order to handle languages fallbacks
    """
    # copied from wagtail/contrib/wagtailroutablepage/models.py mixin ##
    # Override route when Page is also RoutablePage
    if isinstance(self, RoutablePageMixin):
        if self.live:
            try:
                path = '/'
                if path_components:
                    path += '/'.join(path_components) + '/'

                view, args, kwargs = self.resolve_subpage(path)
                return RouteResult(self, args=(view, args, kwargs))
            except Http404:
                pass

    if path_components:
        # request is for a child of this page
        child_slug = path_components[0]
        remaining_components = path_components[1:]

        subpages = self.get_children()
        for page in subpages:
            if page.slug == child_slug:
                return page.specific.route(request, remaining_components)
        raise Http404

    else:
        # request is for this very page
        if self.live:
            return RouteResult(self)
        else:
            raise Http404
def _new_route(self, request, path_components):
    """
    Rewrite route method in order to handle languages fallbacks
    """
    if path_components:
        # request is for a child of this page
        child_slug = path_components[0]
        remaining_components = path_components[1:]

        # try:
        #     q = Q()
        #     for lang in settings.LANGUAGES:
        #         tr_field_name = 'slug_%s' % (lang[0])
        #         condition = {tr_field_name: child_slug}
        #         q |= Q(**condition)
        #     subpage = self.get_children().get(q)
        # except Page.DoesNotExist:
        #     raise Http404

        # return subpage.specific.route(request, remaining_components)

        subpages = self.get_children()
        for page in subpages:
            if page.specific.slug == child_slug:
                return page.specific.route(request, remaining_components)
        raise Http404

    else:
        # request is for this very page
        if self.live:
            return RouteResult(self)
        else:
            raise Http404
예제 #3
0
파일: models.py 프로젝트: xr-web-de/xr-web
    def route(self, request, path_components):
        if path_components:
            # request is for a child of this page
            child_slug = path_components[0]
            remaining_components = path_components[1:]

            try:
                subpage = self.get_children().get(slug=child_slug)
            except Page.DoesNotExist:
                try:
                    # try to find an event_page, by matching the
                    # second path_component with an event_date id
                    event_date_id = int(path_components[1])
                    event_page = EventPage.objects.get(dates__id=event_date_id)
                    return event_page.route(request,
                                            remaining_components,
                                            redirect_self=True)
                except (IndexError, ValueError, TypeError,
                        EventPage.DoesNotExist):
                    raise Http404
            return subpage.specific.route(request, remaining_components)
        else:
            # request is for this very page
            if self.live:
                return RouteResult(self)
            else:
                raise Http404
예제 #4
0
    def route(self, request, path_components):
        if path_components:
            # request is for a child of this page, probably Book or Translation
            child_slug = path_components[0]
            remaining_components = path_components[1:]

            if len(remaining_components):
                raise Http404  # subpages /w/foo/subpage are forbidden

            # is it a book?
            try:
                subpage = BookPage.objects.get(slug=child_slug)
                return subpage.specific.route(request, [])
            except BookPage.DoesNotExist:
                pass

            # is it a translation?
            try:
                subpage = TranslationPage.objects.get(slug=child_slug)
                return subpage.specific.route(request, [])
            except TranslationPage.DoesNotExist:
                pass

            raise Http404
        else:
            if self.live:
                # Return a RouteResult that will tell Wagtail to call
                # this page's serve() method
                return RouteResult(self)
            else:
                # the page matches the request, but isn't published, so 404
                raise Http404
예제 #5
0
 def route(self, request, path_components):
     # url config takes precedence over normal wagtail routing
     try:
         view, args, kwargs = self._apppage_url_resolver.resolve(
             request.path)
     except Resolver404:
         return super().route(request, path_components)
     else:
         return RouteResult(self, args=(view, args, kwargs))
예제 #6
0
    def route(site, request, path):
        """Retrieve a page from a site given a request and path.

        This method uses the standard `wagtail.core.Page.route` method to
        retrieve a page using its path from the given site root.

        If a requested page exists and is published, the result of `Page.route`
        can be returned directly.

        If the page exists but is not yet published, `Page.route` raises an
        `Http404`, which this method tries to catch and handle. `Page.route`
        raises `Http404` in two cases: if no page with the given path exists
        and if a page exists but is unpublished. This method catches both
        cases, and, if they fall into the latter category, returns the
        requested page back to the caller despite its draft status.
        """
        path_components = [
            component for component in path.split("/") if component
        ]

        try:
            return site.root_page.route(request, path_components)
        except Http404:
            exception_source = inspect.trace()[-1]
            stack_frame = exception_source[0]

            page = stack_frame.f_locals["self"]
            path_components = stack_frame.f_locals["path_components"]

            if isinstance(page, RoutablePageMixin):
                # This mimics the way that RoutablePageMixin uses the
                # RouteResult to store the page route view to call.
                path = "/"
                if path_components:
                    path += "/".join(path_components) + "/"

                view, args, kwargs = page.resolve_subpage(path)
                return RouteResult(page, args=(view, args, kwargs))
            elif path_components:
                raise

            return RouteResult(page)
예제 #7
0
파일: models.py 프로젝트: ninepints/npweb
    def route(self, request, path_components):
        """
        Kludge to insert the publication year and month into BlogPost URLs.

        If the request includes an incorrect year or month, returns a redirect to the canonical URL.
        If the request includes the correct year and month, discards them before delegating to the target post.
        If the request doesn't include a year and month but does include a valid post slug, returns a 404.
        """

        # Handle paths of the form year/month/slug/ - if there's a matching blog post we want to route to it
        if len(path_components) > 2:
            year, month, child_slug = path_components[:3]
            remaining_components = path_components[3:]

            if re.match(r'^\d+$', year) and re.match(r'^\d{2}$', month):
                try:
                    post = self.posts().get(slug=child_slug).specific
                except Page.DoesNotExist:
                    pass
                else:
                    # 404 if the post hasn't been published
                    if not post.live:
                        raise Http404

                    # Redirect to the canonical url if the request included incorrect dates
                    if str(post.pub_date_norm.year) != year or '{:02}'.format(
                            post.pub_date_norm.month) != month:
                        canonical_post_url = post.url
                        if remaining_components:
                            canonical_post_url += '/'.join(
                                remaining_components) + '/'

                        response = HttpResponsePermanentRedirect(
                            canonical_post_url)
                        return RouteResult(
                            ResponseOverrideWrapper(post, response))

                    # Otherwise, delegate routing to the post
                    return post.route(request, remaining_components)

        # Handle paths of the form slug/ - matching blog posts shouldn't be accessible this way
        if len(path_components) > 0:
            child_slug = path_components[0]

            try:
                self.posts().get(slug=child_slug)
            except Page.DoesNotExist:
                pass
            else:
                raise Http404

        # For all other paths, defer to existing routing
        return super().route(request, path_components)
예제 #8
0
파일: models.py 프로젝트: xr-web-de/xr-web
 def route(self, request, path_components, redirect_self=None):
     if path_components:
         event_date_id = None
         try:
             event_date_id = int(path_components[0])
         except (ValueError, TypeError):
             pass
         return RouteResult(
             self,
             kwargs={
                 "event_date_id": event_date_id,
                 "redirect_self": redirect_self
             },
         )
     else:
         # request is for this very page
         if self.live:
             return RouteResult(self,
                                kwargs={"redirect_self": redirect_self})
         else:
             raise Http404
예제 #9
0
파일: models.py 프로젝트: jams2/wagtail
    def route(self, request, path_components):
        """
        This hooks the subpage URLs into Wagtail's routing.
        """
        if self.live:
            try:
                path = "/"
                if path_components:
                    path += "/".join(path_components) + "/"

                view, args, kwargs = self.resolve_subpage(path)
                return RouteResult(self, args=(view, args, kwargs))
            except Http404:
                pass

        return super().route(request, path_components)
예제 #10
0
    def route(self, request, path_components):
        if path_components:
            # request is for a child of this page
            child_slug = path_components[0]

            # find a matching child or 404
            try:
                self.get_children().get(slug=child_slug)
            except Page.DoesNotExist:
                raise Http404

        if self.live:
            # Return a RouteResult that will tell Wagtail to call
            # this page's serve() method
            return RouteResult(self,
                               kwargs={'path_components': path_components})
        else:
            # the page matches the request, but isn't published, so 404
            raise Http404
예제 #11
0
def serve(request, path):
    if not request.site:
        raise Http404

    if request.method == 'GET':
        root_url = request.site.root_url
        return redirect(f'{root_url}/{path}')

    page = Page.objects.get(url_path=f'/{path}')
    if page.live:
        page, args, kwargs = RouteResult(page)
    else:
        raise Http404

    for fn in hooks.get_hooks('before_serve_page'):
        result = fn(page, request, args, kwargs)
        if isinstance(result, HttpResponse):
            return result

    return page.specific.serve(request, *args, **kwargs)
예제 #12
0
    def route(self, request, path_components):
        if path_components:
            # request is for an author
            author_id = path_components[0]

            # find a matching author or 404
            try:
                Author.objects.get(pk=author_id)
            except Page.DoesNotExist:
                raise Http404
        else:
            # the page matches the request, but has no author id
            raise Http404

        if self.live:
            # Return a RouteResult that will tell Wagtail to call
            # this page's serve() method
            return RouteResult(self)
        else:
            # the page matches the request, but isn't published, so 404
            raise Http404
예제 #13
0
    def route(self, request, path_components):
        if len(path_components) == 1 and path_components[0] == 'feed':
            return RouteResult(self, kwargs={'format': 'rss'})

        return super(FilterableFeedPageMixin,
                     self).route(request, path_components)