o
    hoW                     @   s  d dl Z d dlmZ d dlmZ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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!m"Z"m#Z#m$Z$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/m0Z0 d dl1m2Z2 d dl3m4Z4 d dl5m6Z6 G dd deeZ7G dd dZ8G dd de8eZ9edg dZ:G d d! d!e7eZ;dS )"    N)
namedtuple)quoteunquote)ImproperlyConfigured)models)Q)get_object_or_404redirect)reversereverse_lazy)date_format)cached_property)gettext_lazy)View)ContextMixinTemplateResponseMixin)BaseListView)ChoiceFilterDateFromToRangeFilterModelChoiceFilterModelMultipleChoiceFilterMultipleChoiceFilter)messages)
SearchForm)ColumnTable)get_valid_next_url_from_request)ButtonWithDropdown)get_search_backend)class_is_indexed)flatten_choicesc                       s   e Zd ZdZdZdZdZededdgZ		 dZ
g Zg Zdd Zd	d
 Zdd Zdd Zdd Zdd Zdd Z fddZ fddZ  ZS )WagtailAdminTemplateMixinz
    Mixin for views that render a template response using the standard Wagtail admin
    page furniture.
    Provides accessors for page title, subtitle and header icon.
     wagtailadmin_homeHome)urllabelzwagtailadmin/generic/base.htmlc                 C      | j S N)
page_titleself r,   `/var/www/html/ndineBlogger/venv/lib/python3.10/site-packages/wagtail/admin/views/generic/base.pyget_page_title;      z(WagtailAdminTemplateMixin.get_page_titlec                 C   r'   r(   )page_subtitler*   r,   r,   r-   get_page_subtitle>   r/   z+WagtailAdminTemplateMixin.get_page_subtitlec                 C   s&   |   }|  }|r| d| }|S )Nz: )r.   r1   )r+   titlesubtitler,   r,   r-   get_header_titleA   s
   z*WagtailAdminTemplateMixin.get_header_titlec                 C   r'   r(   )header_iconr*   r,   r,   r-   get_header_iconH   r/   z)WagtailAdminTemplateMixin.get_header_iconc                 C   r'   )z
        Define the current path to the view by copying the base
        :attr:`breadcrumbs_items` and appending the list.

        If breadcrumbs are not required, return an empty list.
        )breadcrumbs_itemsr*   r,   r,   r-   get_breadcrumbs_itemsK   s   z/WagtailAdminTemplateMixin.get_breadcrumbs_itemsc                 C   s8   t | j}|  }|r|t|ddtdidd |S )Nzdots-horizontalz
aria-labelActionszw-h-slim-header)buttons	icon_nameattrs	classname)sortedheader_buttonsget_header_more_buttonsappendr   _)r+   r:   more_buttonsr,   r,   r-   get_header_buttonsT   s   

z,WagtailAdminTemplateMixin.get_header_buttonsc                 C   
   t | jS r(   )r>   header_more_buttonsr*   r,   r,   r-   r@   c   s   
z1WagtailAdminTemplateMixin.get_header_more_buttonsc                    s^   t  jdi |}|  |d< |  |d< |  |d< |  |d< |  |d< |  |d< |S )Nr)   r0   r5   header_titler7   r?   r,   )superget_context_datar.   r1   r6   r4   r8   rD   )r+   kwargscontext	__class__r,   r-   rI   f   s   z*WagtailAdminTemplateMixin.get_context_datac                    s    t | jttfr| jS t  S r(   )
isinstancetemplate_namelisttuplerH   get_template_namesr*   rL   r,   r-   rR   t   s   
z,WagtailAdminTemplateMixin.get_template_names)__name__
__module____qualname____doc__r)   r0   r5   r   rB   r7   rO   r?   rF   r.   r1   r4   r6   r8   rD   r@   rI   rR   __classcell__r,   r,   rL   r-   r!   "   s&    		r!   c                       s@   e Zd ZdZdZdZ fddZdd Zdd	 Zd
d Z	  Z
S )BaseObjectMixinz2Mixin for views that make use of a model instance.Npkc                    s<   t  j|g|R i | |  | _|  | _| jj| _d S r(   )rH   setupget_pkrY   
get_objectobject_meta
model_optsr+   requestargsrJ   rL   r,   r-   rZ      s   

zBaseObjectMixin.setupc                 C   s   t t| j| j S r(   )r   strrJ   pk_url_kwargr*   r,   r,   r-   r[      s   zBaseObjectMixin.get_pkc                 C   s   | j j S r(   )model_default_managerallr*   r,   r,   r-   get_base_object_queryset   s   z(BaseObjectMixin.get_base_object_querysetc                 C   s    | j stdt|  | jdS )NztSubclasses of wagtail.admin.views.generic.base.BaseObjectMixin must provide a model attribute or a get_object method)rY   )re   r   r   rh   rY   r*   r,   r,   r-   r\      s
   zBaseObjectMixin.get_object)rS   rT   rU   rV   re   rd   rZ   r[   rh   r\   rW   r,   r,   rL   r-   rX   }   s    rX   c                       sT   e Zd ZdZdZdZdZ fddZdd Zdd	 Z	d
d Z
dd Zdd Z  ZS )BaseOperationViewzKBase view to perform an operation on a model instance using a POST request.Nr"   c                    s(   t  j|g|R i | t|| _d S r(   )rH   rZ   r   next_urlr`   rL   r,   r-   rZ      s   zBaseOperationView.setupc                 C   s   t r(   )NotImplementedErrorr*   r,   r,   r-   perform_operation   s   z#BaseOperationView.perform_operationc                 C   r'   r(   )success_messager*   r,   r,   r-   get_success_message   r/   z%BaseOperationView.get_success_messagec                 C   s(   |   }|rtj| j|| jd d S d S )N)
extra_tags)rn   r   successra   success_message_extra_tags)r+   rm   r,   r,   r-   add_success_message   s   
z%BaseOperationView.add_success_messagec                 C   s2   | j std| jr| jS t| j t| jjgdS )NzSubclasses of wagtail.admin.views.generic.base.BaseOperationView must provide a success_url_name attribute or a get_success_url method)rb   )success_url_namer   rj   r
   r   r]   rY   r*   r,   r,   r-   get_success_url   s   z!BaseOperationView.get_success_urlc                 O   s   |    |   t|  S r(   )rl   rr   r	   rt   r`   r,   r,   r-   post   s   zBaseOperationView.post)rS   rT   rU   rV   rm   rq   rs   rZ   rl   rn   rr   rt   ru   rW   r,   r,   rL   r-   ri      s    	
ri   ActiveFilter)auto_idfield_labelvalueremoved_filter_urlc                       s  e Zd ZdZdZdZeZdZe	de
ddgZdZdZdZdZd	ZdZd
ZdZdZdZ fddZdd Zdd Zedd Zedd Zedd Zdd Zedd Zedd Zdd Z dd  Z!d!d" Z"d#d$ Z#ed%d& Z$d'd( Z%ed)d* Z&d+d, Z'ed-d. Z(d/d0 Z)d1d2 Z*d3d4 Z+d5d6 Z,d7d8 Z-d9d: Z.ed;d< Z/d=d> Z0ed?d@ Z1dAdB Z2edCdD Z3 fdEdFZ4  Z5S )GBaseListingViewz!wagtailadmin/generic/listing.htmlz)wagtailadmin/generic/listing_results.htmlFN__str__Title)r&   pqdefaultc                    s.   | j rt| jttfr| jS | jgS t  S r(   )results_onlyrN   results_template_namerP   rQ   rH   rR   r*   rL   r,   r-   rR      s
   
z"BaseListingView.get_template_namesc                 C   s   | j d|  |  dg S )Nr"   )r%   r&   sublabel)r7   r.   r1   r*   r,   r,   r-   r8      s   z%BaseListingView.get_breadcrumbs_itemsc                 C   s*   | j sd S | j| jjv rt| jjS t S r(   )is_searchablesearch_kwargra   GETr   r*   r,   r,   r-   get_search_form   s
   zBaseListingView.get_search_formc                 C      |   S r(   )r   r*   r,   r,   r-   search_form      zBaseListingView.search_formc                 C   s"   | j r| j  r| j j| j S dS )Nr"   )r   is_validcleaned_datar   r*   r,   r,   r-   search_query   s   zBaseListingView.search_queryc                 C   rE   r(   )boolr   r*   r,   r,   r-   is_searching   s   
zBaseListingView.is_searchingc                 C   s   | j s|S t|jr<| jr<t| j}|j r$|j| j|| j| j	 dS t
j|j dtd |j| j|| j| j	 dS t }| jpCg D ]}|tdi |d | jiO }qD||S )N)fieldsorder_by_relevancez is defined as Indexable but does not specify any AutocompleteFields. Searches within the admin will only respond to complete words.)category__icontainsr,   )r   r   re   search_backend_namer   get_autocomplete_search_fieldsautocompleter   search_fieldsis_explicitly_orderedwarningswarnRuntimeWarningsearchr   filter)r+   querysetsearch_backendqueryfieldr,   r,   r-   search_queryset  s2   



zBaseListingView.search_querysetc                 C   s.   | j r| j di |  }|jjr|S d S d S )Nr,   )filterset_classget_filterset_kwargsformr   )r+   	filtersetr,   r,   r-   filters&  s   zBaseListingView.filtersc                 C   s   | j o| j  o| j j S r(   )r   r   r   has_changedr*   r,   r,   r-   is_filtering.  s   zBaseListingView.is_filteringc                 C   s   | j j| j dS )N)datara   ra   r   r*   r,   r,   r-   r   5  s   z$BaseListingView.get_filterset_kwargsc                 C   s    | j r| j  r| j |}|S r(   )r   r   filter_querysetr+   r   r,   r,   r-   r   ;  s   zBaseListingView.filter_querysetc                 C   st   | j dd }| jj }|| jd t|tt	fr(|D ]}||d qn||d d|d< |d |
  S )zd
        Return the index URL with the given filter parameter removed from the query string
        ?r   N   _w_filter_fragment)index_results_urlsplitra   r   copypop
page_kwargrN   rP   rQ   	urlencode)r+   parambase_url
query_dictr~   r,   r,   r-   get_url_without_filter_param@  s   z,BaseListingView.get_url_without_filter_paramc                    sb   | j dd }| jj }|| jd || fdd||D  d|d< |d |	  S )z
        Return the index URL where the filter parameter with the given value has been removed
        from the query string, preserving all other values for that parameter
        r   r   Nc                    s   g | ]
}|t  kr|qS r,   )rc   ).0vry   r,   r-   
<listcomp>X  s    zFBaseListingView.get_url_without_filter_param_value.<locals>.<listcomp>r   r   )
r   r   ra   r   r   r   r   setlistgetlistr   )r+   r   ry   r   r   r,   r   r-   "get_url_without_filter_param_valueO  s   z2BaseListingView.get_url_without_filter_param_valuec           
         s  g }| j s|S | j jjD ] | j j   }| j j  }z	| j jj  }W n	 ty,   Y qw ||jkr3qt|trU|j}|D ]}|	t
|j|j|||  |j q=qt|tr~t|jj}|D ]}|	t
|j|j|t|t||  | qbqt|tr|j}|	t
|j|j|||   qt|tr|jrt|jnd}|jrt|jnd}	|jj|	t
|j|j| d|	 |  fddjD  qt|trt|jj}|	t
|j|j|t|t||   q|	t
|j|jt||   q|S )Nr"   z - c                    s   g | ]}  |qS r,   )suffixed)r   suffix
field_namewidgetr,   r-   r     s    
z2BaseListingView.active_filters.<locals>.<listcomp>)r   r   changed_datar   KeyErrorinitialrN   r   r   rA   rv   rw   r&   label_from_instancer   rY   r   r    choicesgetrc   r   r   r   startr   stopr   suffixesr   )
r+   r   
filter_defbound_fieldry   r   itemr   start_date_displayend_date_displayr,   r   r-   active_filters]  s   




	

		zBaseListingView.active_filtersc                 C   s6   g }| j D ]}|jr||j |d|j  q|S )Nz-%s)columnssort_keyrA   )r+   	orderingscolr,   r,   r-   get_valid_orderings  s   
z#BaseListingView.get_valid_orderingsc                 C   s   d| j jv S Norderingr   r*   r,   r,   r-   r     s   z%BaseListingView.is_explicitly_orderedc                 C   s(   | j jd| j}||  vr| j}|S r   )ra   r   r   default_orderingr   )r+   r   r,   r,   r-   get_ordering  s   zBaseListingView.get_orderingc                 C   r   r(   )r   r*   r,   r,   r-   r     r   zBaseListingView.orderingc                 C   s.   | j s|S | j }t|ttfs|f}|j| S r(   )r   rN   rP   rQ   order_by)r+   r   r   r,   r,   r-   order_queryset  s   
zBaseListingView.order_querysetc                 C   sV   | j d ur| j }t|tjr| }|S | jd ur!| jj }|S tdd| jj	i )Nzj%(cls)s is missing a QuerySet. Define %(cls)s.model, %(cls)s.queryset, or override %(cls)s.get_queryset().cls)
r   rN   r   QuerySetrg   re   rf   r   rM   rS   r   r,   r,   r-   get_base_queryset  s   
	

z!BaseListingView.get_base_querysetc                 C   s*   |   }| |}| |}| |}|S r(   )r   r   r   r   r   r,   r,   r-   get_queryset  s
   


zBaseListingView.get_querysetc                 C   sF   | j |||  |  d}| jj| j}||}|||j|	 fS )N)orphansallow_empty_first_page)
get_paginatorget_paginate_orphansget_allow_emptyra   r   r   r   get_pageobject_listhas_other_pages)r+   r   	page_size	paginatorpage_numberpager,   r,   r-   paginate_queryset  s   
z!BaseListingView.paginate_querysetc                 C   s   | j | j| jdS )N)r   r=   r   )r   table_classname	index_urlr*   r,   r,   r-   get_table_kwargs   s   z BaseListingView.get_table_kwargsc                 C   s   | j | j|fi |  S r(   )table_classr   r   )r+   r   r,   r,   r-   	get_table  s   zBaseListingView.get_tablec                 C   r   r(   )get_index_urlr*   r,   r,   r-   r     r   zBaseListingView.index_urlc                 C      | j rt| j S d S r(   )index_url_namer
   r*   r,   r,   r-   r        
zBaseListingView.get_index_urlc                 C   r   r(   )get_index_results_urlr*   r,   r,   r-   r     r   z!BaseListingView.index_results_urlc                 C   r   r(   )index_results_url_namer
   r*   r,   r,   r-   r     r   z%BaseListingView.get_index_results_urlc                 C   s>   | j stdS | js| jrtdd| j i S tdd| j i S )NzThere are no results.z#No %(model_name)s match your query.
model_namez'There are no %(model_name)s to display.)verbose_name_pluralrB   r   r   r*   r,   r,   r-   no_results_message  s   z"BaseListingView.no_results_messagec                    s:  t  j|i |}| |d }| j|d< | j|d< | j|d< | j|d< | j|d< ||d< |j|d< |d	 d u|d
< |d
 rF|d j	|d< nt
|d |d< | jrf| j|d< | j|d< |d  | jjj7  < | jr| j|d< | j|d< | j|d< |d  | jj7  < | jjdo| jo| j|d< |do| j|d< |S )Nr   r   r   r   r   r   tablemediapage_objis_paginatedr   items_countr   r   r   r   query_stringr   render_filters_fragmentr?   render_buttons_fragment)rH   rI   r   r   r   r   r   r   r  countlenr   r   r   r   r   r   ra   r   r   r   )r+   rb   rJ   rK   r   rL   r,   r-   rI   ,  s<   










z BaseListingView.get_context_data)6rS   rT   rU   rO   r   r   r   r   r   r   rB   r   r   r   r   r   r   r   r   r   r   r   rR   r8   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   rI   rW   r,   r,   rL   r-   r{      st    		


$


Z

	


r{   )<r   collectionsr   django.contrib.admin.utilsr   r   django.core.exceptionsr   	django.dbr   django.db.modelsr   django.shortcutsr   r	   django.urlsr
   r   django.utils.formatsr   django.utils.functionalr   django.utils.translationr   rB   django.viewsr   django.views.generic.baser   r   django.views.generic.listr   django_filters.filtersr   r   r   r   r   wagtail.adminr   wagtail.admin.forms.searchr   wagtail.admin.ui.tablesr   r   wagtail.admin.utilsr   wagtail.admin.widgets.buttonr   wagtail.search.backendsr   wagtail.search.indexr   wagtail.utils.utilsr    r!   rX   ri   rv   r{   r,   r,   r,   r-   <module>   s<    [+