o
    h                     @   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__)	view_funcfuncr   pattern d/var/www/html/ndineBlogger/venv/lib/python3.10/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 ])}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   s    z4RoutablePageMixin.get_subpage_urls.<locals>.<lambda>)keyc                 s   s    | ]}|d  V  qdS )r   Nr   ).0r'   r   r   r   	<genexpr>R   s    z5RoutablePageMixin.get_subpage_urls.<locals>.<genexpr>)__mro____dict__valuesr   extendr   sorttuple)clsroutesklass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-   r7   r   r	   r8   )r2   subpage_urlsr   r   r   get_resolverV   s   
zRoutablePageMixin.get_resolverc                    s$   t  jdi |}||   |S )Nr   )supercheckr/   _check_path_with_regex)r2   r%   errors	__class__r   r   r<   `   s   zRoutablePageMixin.checkc              	   C   sr   |   }g }|D ].}t|jtr6|jj}d|v s"|ds"|dr6|td|j	p,|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)r7   
isinstancer   r
   _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.
        )r:   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.
        )r:   resolver   __get__type)r"   r   resolver_matchr   r   r   resolve_subpage   s   z!RoutablePageMixin.resolve_subpagec                    sp   | j r1z$d}|r|d|d 7 }| |}||_|\}}}t| |||fdW S  ty0   Y nw t ||S )zE
        This hooks the subpage URLs into Wagtail's routing.
        /)r$   )livejoinrQ   routable_resolver_matchr   r   r;   r'   )r"   r#   path_componentsr   rP   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 )N)r;   serve)r"   r#   rW   r$   r%   r?   r   r   rX      s   zRoutablePageMixin.serve)templatecontext_overridesc                O   sR   |du r| j |g|R i |}| j|g|R i |}||p!i  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#   rY   rZ   r$   r%   contextr   r   r   render   s
   #zRoutablePageMixin.renderc                 C   s&   |  d\}}}||g|R i |S )NrR   )rQ   )r"   r#   	mode_namerW   r$   r%   r   r   r   serve_preview   s   zRoutablePageMixin.serve_previewNN)NNN)r   
__module____qualname____doc__r   r&   classmethodr7   r:   r<   r=   rL   rQ   r'   rX   r]   r_   __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   ra   rb   abstractr   r   r   r   Meta   s    rh   N)r   ra   rb   rc   rh   r   r   r   r   rf      s    rf   r`   )logging	functoolsr   django.core.checksr   django.httpr   django.template.responser   django.urlsr   r   	path_funcr   re_path_funcdjango.urls.resolversr	   r
   wagtail.modelsr   wagtail.url_routingr   r   	getLoggerloggerr   r'   r   rf   r   r   r   r   <module>   s(    

 -