a
    Dg                     @   s   d dl Z d dlmZ d dlmZ d dlmZ d dlmZ d dl	m
Z
 d dl	mZ d dl	mZ d d	lmZmZ d d
lmZ d dlmZ d ae dZdddZeeedZeeedZeZG dd dZG dd deeZdS )    N)partial)Warning)Http404)TemplateResponse)URLResolver)path)re_path)RegexPatternRoutePattern)Page)RouteResultzwagtail.routablepagec                    s    fdd}|S )Nc                    s<   t d7 a t| dsg | _| j | p,| jdt f | S )N   _routablepage_routes)name)_creation_counterhasattrr   append__name__)Z	view_funcfuncr   pattern c/var/www/lab.imftr.de/x/nb_venv/lib/python3.9/site-packages/wagtail/contrib/routable_page/models.py	decorator   s    
z_path.<locals>.decoratorr   )r   r   r   r   r   r   r   _path   s    r   )r   c                       s   e Zd ZdZeddd Zedd Zedd Ze fd	d
Z	edd Z
dddZdd Z fddZd fdd	ZdddddZdd Z  ZS )RoutablePageMixinzb
    This class can be mixed in to a Page model, allowing extra routes to be
    added to it.
     c                 O   sB   t |dd|_t|| j|g|R i || j|g|R i |S )N
is_previewF)getattrr   r   get_templateget_context)selfrequestargskwargsr   r   r   index_route7   s    zRoutablePageMixin.index_routec                 C   sf   g }| j D ]R}g }|j D ]}t|dr||j q|jdd d |dd |D  q
t|S )Nr   c                 S   s   | d S )Nr   r   )router   r   r   <lambda>P       z4RoutablePageMixin.get_subpage_urls.<locals>.<lambda>)keyc                 s   s   | ]}|d  V  qdS )r   Nr   ).0r&   r   r   r   	<genexpr>R   r(   z5RoutablePageMixin.get_subpage_urls.<locals>.<genexpr>)__mro____dict__valuesr   extendr   sorttuple)clsroutesklassZroutes_for_classvalr   r   r   get_subpage_urlsA   s    

z"RoutablePageMixin.get_subpage_urlsc                 C   s(   d| j vr"|  }ttd|| _| jS )N_routablepage_urlresolverz^/)r-   r6   r   r	   r7   )r2   Zsubpage_urlsr   r   r   get_resolverV   s    
zRoutablePageMixin.get_resolverc                    s$   t  jf i |}||   |S N)supercheckr/   _check_path_with_regex)r2   r$   errors	__class__r   r   r;   `   s    zRoutablePageMixin.checkc              	   C   sr   |   }g }|D ]\}t|jtr|jj}d|v sD|dsD|dr|td|j	pX|j
j dd| dd q|S )	Nz(?P<^$zYour URL pattern zI has a route that contains '(?P<', begins with a '^', or ends with a '$'.z:Decorate your view with re_path if you want to use regexp.zwagtailroutablepage.W001)hintobjid)r6   
isinstancer   r
   Z_route
startswithendswithr   r   r   callbackr   )r2   r3   r=   r&   r   r   r   r   r<   f   s(    z(RoutablePageMixin._check_path_with_regexNc                 C   s,   |pg }|pi }|   j|g|R i |S )zR
        This method takes a route name/arguments and returns a URL path.
        )r8   reverse)r!   r   r#   r$   r   r   r   reverse_subpage   s    z!RoutablePageMixin.reverse_subpagec                 C   s&   |   |}|j| t| |_|S )zJ
        This method takes a URL path and finds the view to call.
        )r8   resolver   __get__type)r!   r   resolver_matchr   r   r   resolve_subpage   s    z!RoutablePageMixin.resolve_subpagec                    sp   | j rbzHd}|r"|d|d 7 }| |}||_|\}}}t| |||fdW S  ty`   Y n0 t ||S )zE
        This hooks the subpage URLs into Wagtail's routing.
        /)r#   )ZlivejoinrO   Zroutable_resolver_matchr   r   r:   r&   )r!   r"   Zpath_componentsr   rN   viewr#   r$   r>   r   r   r&      s    

zRoutablePageMixin.routec                    sP   |d u rg }|d u ri }|d u r:t  j|g|R i |S ||g|R i |S r9   )r:   serve)r!   r"   rR   r#   r$   r>   r   r   rS      s    zRoutablePageMixin.serve)templatecontext_overridesc                O   sR   |du r | j |g|R i |}| j|g|R i |}||pBi  t|||S )aN  
        This method replicates what ``Page.serve()`` usually does when ``RoutablePageMixin``
        is not used. By default, ``Page.get_template()`` is called to derive the template
        to use for rendering, and ``Page.get_context()`` is always called to gather the
        data to be included in the context.

        You can use the ``context_overrides`` keyword argument as a shortcut to override or
        add new values to the context. For example:

        .. code-block:: python

            @path('') # override the default route
            def upcoming_events(self, request):
                return self.render(request, context_overrides={
                    'title': "Current events",
                    'events': EventPage.objects.live().future(),
                })

        You can also use the ``template`` argument to specify an alternative
        template to use for rendering. For example:

        .. code-block:: python

            @path('past/')
            def past_events(self, request):
                return self.render(
                    request,
                    context_overrides={
                        'title': "Past events",
                        'events': EventPage.objects.live().past(),
                    },
                    template="events/event_index_historical.html",
                )
        N)r   r    updater   )r!   r"   rT   rU   r#   r$   contextr   r   r   render   s
    #zRoutablePageMixin.renderc                 C   s&   |  d\}}}||g|R i |S )NrP   )rO   )r!   r"   Z	mode_namerR   r#   r$   r   r   r   serve_preview   s    zRoutablePageMixin.serve_preview)NN)NNN)r   
__module____qualname____doc__r   r%   classmethodr6   r8   r;   r<   rJ   rO   r&   rS   rX   rY   __classcell__r   r   r>   r   r   1   s"   
	

	

		)r   c                   @   s   e Zd ZdZG dd dZdS )RoutablePagezd
    This class extends Page by adding methods which allows extra routes to be
    added to it.
    c                   @   s   e Zd ZdZdS )zRoutablePage.MetaTN)r   rZ   r[   Zabstractr   r   r   r   Meta   s   r`   N)r   rZ   r[   r\   r`   r   r   r   r   r_      s   r_   )NN)logging	functoolsr   Zdjango.core.checksr   Zdjango.httpr   Zdjango.template.responser   Zdjango.urlsr   r   Z	path_funcr   Zre_path_funcZdjango.urls.resolversr	   r
   Zwagtail.modelsr   Zwagtail.url_routingr   r   	getLoggerloggerr   r&   r   r_   r   r   r   r   <module>   s&   

 -