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mZmZmZ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mZ d dlm Z 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l0m1Z1 d dl2m3Z3 d dl4m5Z5 d dl6m7Z8 d dl6m9Z9 d dl:m;Z; d dl<m=Z= d dl>m?Z? d dl@mAZA d dlBmCZCmDZDmEZEmFZF d dlGmHZH d dlImJZJ d dlKmLZL d d lKmMZN d d!lOmPZPmQZQmRZR d"d#lmSZS zd d$lmTZT W n eUy   d d%lmVZT Y nw h d&ZWG d'd( d(e;ZXG d)d* d*eXe=ZYG d+d, d,eXZZG d-d. d.eJeXZ[G d/d0 d0eYZ\G d1d2 d2eYeZZ]G d3d4 d4eXZ^G d5d6 d6eZZ_G d7d8 d8eZZ`G d9d: d:e?eHeZZadS );    N)OrderedDict)VERSION)forms)settings)FieldListFilter)IncorrectLookupParameters)get_fields_from_pathlabel_for_fieldlookup_fieldprepare_lookup_valuequoteunquote)login_required)FieldDoesNotExistImproperlyConfiguredObjectDoesNotExistPermissionDeniedSuspiciousOperation)InvalidPage	Paginator)modelstransaction)ManyToManyFieldOneToOneRel)get_object_or_404redirect)filesizeformat)method_decorator)	force_str)cached_property)format_html)	urlencode)capfirst)gettext)gettext_lazy)TemplateView)FormView)MultipleObjectMixin)messages)Column
DateColumnTable
UserColumn)WagtailAdminTemplateMixin)SpreadsheetExportMixin)log)registry)LocaleRevisionMixinTranslatableMixin   )ParentChooserForm)lookup_spawns_duplicates)lookup_needs_distinct>   gtinltdaygteltehouryearexactmonthrangeregexiexactiregexisnullminutesearchsecondcontainsendswithweek_day	icontains	iendswith
startswithistartswithc                       s   e Zd ZdZdZdZdZdZdd Zdd Z	e
e fdd	Zed
d Zedd Zdd Zdd Zedd Zedd ZdddZ fddZ  ZS )WMABaseViewzA
    Groups together common functionality for all app views.
    N c                 C   sx   || _ |j| _| jj| _t| jj| _t| jj| _t| jj| _t| jj| _| jj	j
| _|j| _|j| _|j| _d S N)model_adminmodel_metaoptsr   	app_label
model_nameverbose_nameverbose_name_pluralpkattname
pk_attnameis_pagemodelpermission_helper
url_helper)selfrT    rc   X/var/www/html/ndineBlogger/venv/lib/python3.10/site-packages/wagtail_modeladmin/views.py__init__c   s   
zWMABaseView.__init__c                 C   s   dS )NTrc   rb   userrc   rc   rd   check_action_permittedp   s   z"WMABaseView.check_action_permittedc                    s@   |  |jst| j }|| || _t j|g|R i |S rS   )rh   rg   r   rT   get_button_helper_classbutton_helpersuperdispatch)rb   requestargskwargsbutton_helper_class	__class__rc   rd   rl   s   s
   
zWMABaseView.dispatchc                 C   
   | j  S rS   )rT   get_menu_iconrb   rc   rc   rd   	menu_icon{   s   
zWMABaseView.menu_iconc                 C      | j S rS   )rv   ru   rc   rc   rd   header_icon   s   zWMABaseView.header_iconc                 C   s   | j pt| jjS rS   )
page_titler"   rW   r[   ru   rc   rc   rd   get_page_title      zWMABaseView.get_page_titlec                 C   s   | j p|  S rS   )
meta_titlerz   ru   rc   rc   rd   get_meta_title      zWMABaseView.get_meta_titlec                 C      | j jS rS   )ra   	index_urlru   rc   rc   rd   r         zWMABaseView.index_urlc                 C   r   rS   )ra   
create_urlru   rc   rc   rd   r      r   zWMABaseView.create_urlc                 C   s   | j |p| jS rS   )rT   get_querysetrm   rb   rm   rc   rc   rd   get_base_queryset   r{   zWMABaseView.get_base_querysetc                    s(   | | j d}|| t jdi |S )N)viewrT   rc   )rT   updaterk   get_context_datarb   ro   contextrq   rc   rd   r      s
   
zWMABaseView.get_context_datarS   )__name__
__module____qualname____doc__rT   r|   ry   page_subtitlere   rh   r   r   rl   r   rv   rx   rz   r}   r   r   r   r   __classcell__rc   rc   rq   rd   rQ   Y   s,    




rQ   c                       s   e Zd Z fddZ fddZdd Zdd Zd	d
 Zdd Z fddZ	e
dd Zd  fdd	Zdd Zdd Zdd Zdd Zdd Zdd Z  ZS )!ModelFormViewc                    s(   t  j|g|R i | |  | _d S rS   )rk   setupget_edit_handleredit_handlerrb   rm   rn   ro   rq   rc   rd   r      s   zModelFormView.setupc                    s   t   }|S rS   )rk   get_formrb   formrq   rc   rd   r      s   
zModelFormView.get_formc                 C   s   | j  }|| j jS rS   )rT   r   bind_to_modelrU   )rb   r   rc   rc   rd   r      s   
zModelFormView.get_edit_handlerc                 C   rs   rS   )r   get_form_classru   rc   rc   rd   r         
zModelFormView.get_form_classc                 C   rw   rS   )r   ru   rc   rc   rd   get_success_url      zModelFormView.get_success_urlc                 C   s   t | dd p	|  S )Ninstance)getattrrU   ru   rc   rc   rd   get_instance   s   zModelFormView.get_instancec                    s&   t   }||  | jjd |S )N)r   for_user)rk   get_form_kwargsr   r   rm   rg   )rb   ro   rq   rc   rd   r      s   
zModelFormView.get_form_kwargsc                 C      t jd| j i| j dS Nallcssjs)r   MediarT   get_form_view_extra_cssget_form_view_extra_jsru   rc   rc   rd   media      zModelFormView.mediaNc                    sn   |d u r|   }| jj||j| jd}| |}| |||| j|j |j d}|| t	 j
di |S )N)r   r   rm   )is_multipartr   r   prepopulated_fieldsr   rc   )r   r   get_bound_panelr   rm   get_prepopulated_fieldsr   r   r   rk   r   )rb   r   ro   bound_panelr   r   rq   rc   rd   r      s   


zModelFormView.get_context_datac              	      s   g }| j | j D ]J\}} fdd|D }t|dkr?d|}t|dkr+dnd}tjd| d	| d
| dtd q| j	v rU|
 |  fdd|D d q|S )Nc                    s"   g | ]}| j vrd | d qS )')fields.0fr   rc   rd   
<listcomp>   s    z9ModelFormView.get_prepopulated_fields.<locals>.<listcomp>r   , r4   dependencies
dependencyzMissing  z for prepopulated_field 'z''.)categoryc                    s   g | ]} | qS rc   rc   r   r   rc   rd   r          )fieldr   )rT   r   rm   itemslenjoinwarningswarnRuntimeWarningr   append)rb   r   r   
field_namer   missing_dependenciesmissing_deps_stringdependency_stringrc   r   rd   r      s2   



z%ModelFormView.get_prepopulated_fieldsc                 C   s   t dt| jj|d S )Nz$%(model_name)s '%(object)s' created.rY   object)_r"   rW   rZ   rb   r   rc   rc   rd   get_success_message   s   
z!ModelFormView.get_success_messagec                 C   s&   | j dt|j}t|tdgS )NeditEdit)ra   get_action_urlr   r\   r(   buttonr   )rb   r   
button_urlrc   rc   rd   get_success_message_buttons      z)ModelFormView.get_success_message_buttonsc                 C      | j }tdd|i S )Nz2The %(object)s could not be created due to errors.r   rZ   r   )rb   rY   rc   rc   rd   get_error_message   s   zModelFormView.get_error_messagec                 C   s8   |  | _tj| j| | j| | jd t|  S )N)buttons)	saver   r(   successrm   r   r   r   r   r   rc   rc   rd   
form_valid   s   


zModelFormView.form_validc                 C   s&   t | j|  | | | j|dS Nr   )r(   validation_errorrm   r   render_to_responser   r   rc   rc   rd   form_invalid  r   zModelFormView.form_invalidrS   )r   r   r   r   r   r   r   r   r   r   propertyr   r   r   r   r   r   r   r   r   rc   rc   rq   rd   r      s"    
	r   c                       sX   e Zd ZdZdZdZdZ fddZdd Ze	dd Z
e	dd	 Z fd
dZ  ZS )InstanceSpecificViewNc                    s~   t  | t|| _t| j| _| j| ji}|jj	 j
di |}t|| _ttddr;t|jtr=| jj| _d S d S d S )NWAGTAIL_I18N_ENABLEDFrc   )rk   re   r   instance_pkr   	pk_quotedr^   rU   _default_managerr   filterr   r   r   r   
issubclassr3   locale)rb   rT   r   filter_kwargs	object_qsrq   rc   rd   re     s   

zInstanceSpecificView.__init__c                 C   rw   rS   )r   ru   rc   rc   rd   get_page_subtitle  r   z&InstanceSpecificView.get_page_subtitlec                 C      | j d| jS )Nr   ra   r   r   ru   rc   rc   rd   edit_url"     zInstanceSpecificView.edit_urlc                 C   r   Ndeleter   ru   rc   rc   rd   
delete_url&  r   zInstanceSpecificView.delete_urlc                    s&   d| j i}|| t jdi |S )Nr   rc   )r   r   rk   r   r   rq   rc   rd   r   *  s   

z%InstanceSpecificView.get_context_data)r   r   r   r   r   r   r   re   r   r   r   r   r   r   rc   rc   rq   rd   r   
  s    

r   c                       s   e Zd ZdZdZdZdZdZdZeeeefZ	dZ
dZee fd	d
Z fddZdd Zdd Zedd Zdd Zdd Zd1ddZdd Zd2ddZdd Zdd  Zd!d" Zd#d$ Zd%d& Zd1d'd(Zd)d* Zd+d, Z  fd-d.Z!d/d0 Z"  Z#S )3	IndexViewootpqeexportNFc                    sl  | j |js	t| j|| _| j|| _| j	|| _
| j|| _| jj| _| jj| _| j|| j| _|j| j| _zt|j| jd| _W n tyY   d| _Y nw tdkre|j | _n|j | _| j| jv rv| j| j= | j| jv r| j| j= | j| jv r| j| j= |j| j d| _!| "|| _#| j| j$v r| %| j#| jS t& j'|g|R i |S )Nr      r   rR   )(r`   user_can_listrg   r   rT   get_list_exportlist_exportget_list_displaylist_displayget_list_filterlist_filterget_search_fieldssearch_fieldslist_per_pageitems_per_pagelist_select_relatedselect_relatedget_search_handlersearch_handlerGETget
EXPORT_VARr   intPAGE_VARpage_num
ValueErrorDJANGO_VERSIONcopyparamsdict
ERROR_FLAG
SEARCH_VARqueryr   querysetFORMATSas_spreadsheetrk   rl   r   rq   rc   rd   rl   A  s>   





zIndexView.dispatchc                    s   t | jdt  S )z8Get filename for exported spreadsheet, without extensionexport_filename)r   rT   rk   get_filenameru   rq   rc   rd   r   m  s   zIndexView.get_filenamec                 C   s2   | j |}|rt|S tt|| j| jd S )zCGet headings for exported spreadsheet column for the relevant field)rU   rT   )export_headingsr  r   r	   rU   rT   title)rb   r  r   heading_overriderc   rc   rd   get_headingq  s   
zIndexView.get_headingc                 C   sJ   t  }| jD ]}t||| j\}}}|st|d| j|}|||< q|S )zmReturns an OrderedDict (in the order given by list_export) of the exportable information for a model instanceempty_value_display)r   r  r
   rT   r   get_empty_value_display)rb   itemrow_dictr   r   attrvaluerc   rc   rd   to_row_dict|  s   


zIndexView.to_row_dictc                 C   s$   t jd| j dg i| j dS )Nr   z%wagtail_modeladmin/css/modeladmin.cssr   )r   r   rT   get_index_view_extra_cssget_index_view_extra_jsru   rc   rc   rd   r     s   
zIndexView.mediac                 C   s   | j j|dgdS )Nzbutton-small)classnames_add)rj   get_buttons_for_obj)rb   objrc   rc   rd   r/    s   zIndexView.get_buttons_for_objc                 C   s4   | j ||}| j|jv |d< | jj||fi |S )Npreserve_order)rT   get_extra_search_kwargs	ORDER_VARr  r  search_queryset)rb   rm   r  search_termro   rc   rc   rd   get_search_results  s   zIndexView.get_search_resultsc                 C   s0   |s| j }| }| jD ]	}||v r||= q|S )z:
        Returns all params except IGNORED_PARAMS
        )r  r  IGNORED_PARAMS)rb   r  lookup_paramsignoredrc   rc   rd   get_filters_params  s   
zIndexView.get_filters_paramsc              
   C   s$  |   }d}g }| jra| jD ]R}t|r|||| j| j}n7d }t|ttfr,|\}}	n|}tj	}	t|t
jsA|}t| j|d }|	|||| j| j|d}|pTt| j|}|r`| r`|| qz!| D ]\}
}t|
|||
< |pxt| j|
}qf|t|||fW S  ty } zt|d }~ww )NF)
field_path)r:  r  callablerU   rT   
isinstancetuplelistr   creater   Fieldr   r6   rW   
has_outputr   r   r   boolr   r   )rb   rm   r8  use_distinctfilter_specsr  specr<  r   field_list_filter_classkeyr*  r   rc   rc   rd   get_filters  sP   



zIndexView.get_filtersc                 C   s   |d u ri }|d u rg }| j  }|D ]}t|D ]
}||r#||= qq| D ]\}}|d u r9||v r8||= q)|||< q)dtt|  S )Nz?%s)r  r  r@  rO   r   r!   sorted)rb   
new_paramsremover   rkvrc   rc   rd   get_query_string  s$   


zIndexView.get_query_stringc                 C   s,   g }| j jr| j j}|S | jjr| jj}|S rS   )rT   orderingrW   )rb   rR  rc   rc   rd   _get_default_ordering  s   zIndexView._get_default_orderingc                 C   s,   | j |r| j |S | jjr| jjS dS )Nrc   )rT   get_orderingrW   rR  r   rc   rc   rd   get_default_ordering   s
   zIndexView.get_default_orderingc                 C   sh   z
| j |}|jW S  ty3   t|r|}nt| j|r%t| j|}nt| j|}t|dd Y S w )ak  
        Returns the proper model field name corresponding to the given
        field_name to use for ordering. field_name may either be the name of a
        proper model field or the name of a method (on the admin or model) or a
        callable with the 'admin_order_field' attribute. Returns None if no
        proper model field name can be matched.
        admin_order_fieldN)	rW   	get_fieldnamer   r=  hasattrrT   r   rU   )rb   r   r   r)  rc   rc   rd   get_ordering_field  s   zIndexView.get_ordering_fieldc              
   C   s   | j }t| |}| j|v r]g }|| j d}|D ]A}z4|d\}}}	| jt|	 }
| |
}|s6W q|	drI|dkrI|
|dd  n|
||  W q ttfy\   Y qw ||jj | jjj}t|dd|d| h@ s||
d|  |S )a  
        Returns the list of ordering fields for the change list.
        First we check the get_ordering() method in model admin, then we check
        the object's default ordering. Then, any manually-specified ordering
        from the query string overrides anything. Finally, a deterministic
        order is guaranteed by ensuring the primary key is used as the last
        ordering field.
        .-r4   Nr\   z-pk)r  r@  rU  r3  split
rpartitionr  r  rZ  rO   r   
IndexErrorr  extendr  order_byrW   r\   rX  set)rb   rm   r  r  rR  order_paramsr   nonepfxidxr   order_fieldpk_namerc   rc   rd   rT    s0   	


zIndexView.get_orderingc              	   C   s   |   }t }| j| jvr;|D ])}|dr|dd }d}nd}t| jD ]\}}| ||kr7|||<  nq&q|S | j| j dD ]$}|	d\}}	}
zt
|
}
W n	 ty]   Y qDw |	dkrddnd||
< qD|S )zV
        Returns an OrderedDict of ordering field column numbers and asc/desc
        r\  r4   Ndescascr[  )rS  r   r3  r  rO   	enumerater  rZ  r]  r^  r  r  )rb   rR  ordering_fieldsr   
order_typeindexr)  r   rd  re  rf  rc   rc   rd   get_ordering_field_columnsI  s0   
z$IndexView.get_ordering_field_columnsc           
   
   C   s   |p| j }| |\| _| _}}| |}| jD ]}|||}|d ur&|}qztdkr:ddlm} |	||}n|j	di |}W n t
tfyM     ty\ } zt|d }~ww |jjsf| |}| ||}	|j|	 }|rw| }| ||| jS )Nr   r   )%build_q_object_from_lookup_parametersrc   )rm   rJ  rF  has_filtersr   r  r  django.contrib.admin.utilsrp  r   r   r   	Exceptionr   r  r  apply_select_relatedrT  ra  distinctr6  )
rb   rm   remaining_lookup_paramsfilters_use_distinctqsfilter_specnew_qsrp  r   rR  rc   rc   rd   r   j  sD   




zIndexView.get_querysetc                 C   sB   | j du r	|  S | j du r|  r|  S | j r|j | j  S |S NTF)r  !has_related_field_in_list_display)rb   rx  rc   rc   rd   rt    s   

zIndexView.apply_select_relatedc              	   C   sF   | j D ]}z| j|}W n	 ty   Y qw t|tjr  dS qdS r{  )r  rW   rW  r   r>  r   ManyToOneRelrb   r   r   rc   rc   rd   r|    s   
z+IndexView.has_related_field_in_list_displayc              	      s   | j j}|   }|  }| }t|| j}z
|| jd }W n t	y0   |d}Y nw | |||||j
| j|| jjd}| jre| j }	dd |	D }
| j|}| }|| |
d || t jdi |S )Nr4   )r   	all_countresult_count	paginatorpage_objobject_listuser_can_createshow_searchc                 S   s   g | ]}|j jqS rc   )rV   rZ   )r   mrc   rc   rd   r     r   z.IndexView.get_context_data.<locals>.<listcomp>)no_valid_parentsrequired_parent_typesrc   )rm   rg   r   countr   r   r	  pager  r   r  r`   r  r  show_search_formr_   rU   allowed_parent_page_modelsget_valid_parent_pagesr   rk   r   )rb   ro   rg   r  r  r  r  r  r   r   allowed_parent_typesvalid_parentsvalid_parent_countrq   rc   rd   r     s>   


zIndexView.get_context_datac                 C   rs   rS   )rT   get_index_templateru   rc   rc   rd   get_template_names  r   zIndexView.get_template_namesrS   )NN)$r   r   r   r3  ORDER_TYPE_VARr  r  r  r  r7  sortable_by
add_facetsr   r   rl   r   r$  r+  r   r   r/  r6  r:  rJ  rQ  rS  rU  rZ  rT  ro  r   rt  r|  r   r  r   rc   rc   rq   rd   r   0  s@    +
	

9,
!:&r   c                       s\   e Zd ZedZdd Z fddZ fddZdd	 Zd
d Z	dd Z
 fddZ  ZS )
CreateViewNewc                 C   s   | j |S rS   )r`   r  rf   rc   rc   rd   rh     s   z!CreateView.check_action_permittedc                    s    j r2|j} j|}| }|dkr*| }t|j}t j	
d j j|S t j	
dS ttddrit jtri jjd}	|	rNtt|	d}
nt }
||
 fdd	tj j|
jd
D d t j|g|R i |S )Nr4   addchoose_parentr   Fr   language_codec                    s"   g | ]}| j d  |j dqS )?locale=r   url)r   r  )r   r   ru   rc   rd   r     s    z'CreateView.dispatch.<locals>.<listcomp>)idr   translations)r_   rg   r`   r  r  r  r   r\   r   ra   r   rX   rY   r   r   r   rU   r3   rm   r  r   r1   get_defaultr   objectsr   excluder  rk   rl   )rb   rm   rn   ro   rg   parentsparent_countparent	parent_pkselected_localer   rq   ru   rd   rl     s8   

zCreateView.dispatchc                    sD   t  |}d }t| jtr| jj| jjd}t| jd|dd |S )N)rg   zwagtail.createTr   actionrevisioncontent_changed)	rk   r   r>  r   r2   save_revisionrm   rg   r/   rb   r   responser  rq   rc   rd   r     s   zCreateView.form_validc                 C      t dd| ji S )NzCreate new %(object)sr   r   rZ   ru   rc   rc   rd   r}   (  r{   zCreateView.get_meta_titlec                 C   
   t | jS rS   )r"   rZ   ru   rc   rc   rd   r   +  r   zCreateView.get_page_subtitlec                 C   rs   rS   )rT   get_create_templateru   rc   rc   rd   r  .  r   zCreateView.get_template_namesc                    sJ   t   }ttddr#t| jtr#| jj	d}|r#t
t|d|d _|S )Nr   Fr   r  r   )rk   r   r   r   r   rU   r3   rm   r  r  r   r1   r   )rb   ro   r  rq   rc   rd   r   1  s   
zCreateView.get_form_kwargs)r   r   r   r$   ry   rh   rl   r   r}   r   r  r   r   rc   rc   rq   rd   r    s    -r  c                       sl   e Zd ZedZdd Zee fddZdd Z	dd	 Z
 fd
dZdd Zdd Z fddZ  ZS )EditViewEditingc                 C      | j || jS rS   )r`   user_can_edit_objr   rf   rc   rc   rd   rh   C     zEditView.check_action_permittedc                    s   | j rt| jd| jS ttddrIt| jt	rIg }| j
 dD ]}|j}| jd|jd |j }|||d q#|rI|| j|d t j|g|R i |S )Nr   r   Fr   r  r  r  )r_   r   ra   r   r   r   r   r   rU   r3   r   get_translationsr  r   r\   r  r   r   rk   rl   rb   rm   rn   ro   r  translationr   r  rq   rc   rd   rl   F  s0   
zEditView.dispatchc                 C   r  )NzEditing %(object)sr   r  ru   rc   rc   rd   r}   d  r{   zEditView.get_meta_titlec                 C   s   t dt| j|d S )Nz$%(model_name)s '%(object)s' updated.r   )r   r"   rZ   r   rc   rc   rd   r   g  s   zEditView.get_success_messagec                    s|   d| j | jj| ji}|| | jjr-t	| j
 |d< | jdt| jj|d< nd |d< d |d< t jdi |S )Nuser_can_deletelatest_log_entryhistoryhistory_urlrc   )r`   user_can_delete_objrm   rg   r   r   rT   history_view_enabledlog_registryget_logs_for_instancefirstra   r   r   r\   rk   r   r   rq   rc   rd   r   m  s"   


zEditView.get_context_datac                 C   r   )Nz0The %(object)s could not be saved due to errors.r   r   )rb   rX  rc   rc   rd   r     s   zEditView.get_error_messagec                 C   rs   rS   )rT   get_edit_templateru   rc   rc   rd   r    r   zEditView.get_template_namesc                    sT   t  |}d }| | _t| jtr| jj| jj	| jd}t
| jd|| jd |S )N)rg   changedzwagtail.editr  )rk   r   has_changedhas_content_changesr>  r   r2   r  rm   rg   r/   r  rq   rc   rd   r     s   
zEditView.form_valid)r   r   r   r$   ry   rh   r   r   rl   r}   r   r   r   r  r   r   rc   rc   rq   rd   r  @  s    r  c                       sT   e Zd Z fddZdd Zdd Zdd Zd	d
 Zdd Zdd Z	dd Z
  ZS )ChooseParentViewc                    s,   | j |js	tt j|g|R i |S rS   )r`   r  rg   r   rk   rl   r   rq   rc   rd   rl     s   zChooseParentView.dispatchc                 C   r  )NzAdd %(object)sr   r  ru   rc   rc   rd   rz     r{   zChooseParentView.get_page_titlec                 C   s   | j |j}t||jpd S rS   )r`   r  rg   r5   POST)rb   rm   r  rc   rc   rd   r     s   zChooseParentView.get_formc                 O   s    |  |}| j|d}| |S r   )r   r   r   )rb   rm   rn   ro   r   r   rc   rc   rd   r    s   

zChooseParentView.getc                 O   s&   |  |}| r| |S | |S rS   )r   is_validr   r   )rb   rm   rn   kargsr   rc   rc   rd   post  s   


zChooseParentView.postc                 C   s*   t |jd j}t| jd| j| j|S )Nparent_pager  )r   cleaned_datar\   r   ra   r   rX   rY   )rb   r   r  rc   rc   rd   r     s   zChooseParentView.form_validc                 C   s   | j |d}| |S r   )r   r   )rb   r   r   rc   rc   rd   r     s   
zChooseParentView.form_invalidc                 C   rs   rS   )rT   get_choose_parent_templateru   rc   rc   rd   r    r   z#ChooseParentView.get_template_names)r   r   r   rl   rz   r   r  r  r   r   r  r   rc   rc   rq   rd   r    s    r  c                       s\   e Zd ZedZdd Zee fddZdd Z	dd	 Z
d
d Zdd Zdd Z  ZS )
DeleteViewDeletec                 C   r  rS   )r`   r  r   rf   rc   rc   rd   rh     r  z!DeleteView.check_action_permittedc                    sD   |  |jst| jrt| jd| jS t j	|g|R i |S r   )
rh   rg   r   r_   r   ra   r   r   rk   rl   r   rq   rc   rd   rl     s
   zDeleteView.dispatchc                 C   r  )NzConfirm deletion of %(object)sr   r  ru   rc   rc   rd   r}     r{   zDeleteView.get_meta_titlec                 C   r  )Nz{Are you sure you want to delete this %(object)s? If other things in your site are related to it, they may also be affected.r   r  ru   rc   rc   rd   confirmation_message  s
   zDeleteView.confirmation_messagec                 C   s   | j   d S rS   )r   r   ru   rc   rc   rd   delete_instance  r~   zDeleteView.delete_instancec                 O   s6  z6t d| j| jd }t  t| jdd |   W d    n1 s&w   Y  t|| t	| j
W S  tjy   g }| jjj }dd |D }|D ];}|jtjkrt|trwz
t| j| }W n	 typ   Y qPw || qPt| j| }	|	 D ]}|| qqP| jd|d}
| |
 Y S w )	Nz$%(model_name)s '%(object)s' deleted.r   zwagtail.delete)r   r  c                 s   s     | ]}t |jts|V  qd S rS   )r>  r   r   r   r0  rc   rc   rd   	<genexpr>  s    
z"DeleteView.post.<locals>.<genexpr>T)protected_errorlinked_objects)r   rZ   r   r   atomicr/   r  r(   r   r   r   r   ProtectedErrorrU   rV   
fields_mapvalues	on_deletePROTECTr>  r   r   get_accessor_namer   r   r   r   r   )rb   rm   rn   ro   msgr  r   relr0  rx  r   rc   rc   rd   r    sF   


zDeleteView.postc                 C   rs   rS   )rT   get_delete_templateru   rc   rc   rd   r    r   zDeleteView.get_template_names)r   r   r   r$   ry   rh   r   r   rl   r}   r  r  r  r  r   rc   rc   rq   rd   r    s    #r  c                       s   e Zd ZedZdd Z fddZedd Zdd	 Z	dddZ
dddZdd Zdd Zdd Zdd Z fddZdd Z  ZS )InspectView
Inspectingc                 C   r  rS   )r`   user_can_inspect_objr   rf   rc   rc   rd   rh   
  r  z"InspectView.check_action_permittedc                    s   t tddr=t| jjtr=g }| j dD ]}|j	}| j
d|jd |j }|||d q|r=|| j	|d t j|g|R i |S )Nr   Fr   inspectr  r  r  )r   r   r   rT   rU   r3   r   r  r  r   ra   r   r\   r  r   r   rk   rl   r  rq   rc   rd   rl     s,   
zInspectView.dispatchc                 C   r   r   )r   r   rT   get_inspect_view_extra_cssget_inspect_view_extra_jsru   rc   rc   rd   r   '  r   zInspectView.mediac                 C   r  )NzInspecting %(object)sr   r  ru   rc   rc   rd   r}   .  r{   zInspectView.get_meta_titleNc                 C   s   t || jdS )z%Return a label to display for a field)rU   )r	   rU   r~  rc   rc   rd   get_field_label1  s   zInspectView.get_field_labelc                 C   s  t | jd| d}|durt|r| S |S t | j|}t|tjr&| }t|tjr@| r:d	dd |D S | j
|S zddlm} t||rS| ||W S W n	 ty]   Y nw zddlm} t||rq| ||W S W n	 ty{   Y nw |s|d	u r|S | j
|S )
z,Return a display value for a field/attributezget_%s_displayNr   c                 S   s   g | ]}d | qS )z%src   r  rc   rc   rd   r   I  r   z7InspectView.get_field_display_value.<locals>.<listcomp>r   )AbstractImage)AbstractDocumentF)r   r   r=  r>  r   Managerr   QuerySetexistsr   rT   r&  wagtail.images.modelsr  get_image_field_displayRuntimeErrorwagtail.documents.modelsr  get_document_field_display)rb   r   r   	val_functvalr  r  rc   rc   rd   get_field_display_value5  s>   

z#InspectView.get_field_display_valuec                 C   s4   ddl m} t| j|}|r||djS | j|S )zRender an imager   )get_rendition_or_not_foundzmax-400x400)wagtail.images.shortcutsr  r   r   img_tagrT   r&  )rb   r   r   r  imagerc   rc   rd   r  e  s
   z#InspectView.get_image_field_displayc                 C   s>   t | j|}|rtd|j|j|j t|jj	S | j
|S )zRender a link to a documentz6<a href="{}">{} <span class="meta">({}, {})</span></a>)r   r   r    r  r"  file_extensionupperr   filesizerT   r&  )rb   r   r   documentrc   rc   rd   r  n  s   
z&InspectView.get_document_field_displayc                 C   sD   z	| j j|}W n ty   d}Y nw | ||| ||dS )zk
        Return a dictionary containing `label` and `value` values to display
        for a field.
        N)labelr*  )rU   rV   rW  r   r  r  r~  rc   rc   rd   get_dict_for_field{  s   

zInspectView.get_dict_for_fieldc                 C   s(   g }| j  D ]
}|| | q|S )z
        Return a list of `label`/`value` dictionaries to represent the
        fields named by the model_admin class's `get_inspect_view_fields` method
        )rT   get_inspect_view_fieldsr   r  )rb   r   r   rc   rc   rd   get_fields_dict  s   zInspectView.get_fields_dictc                    s:   |   | jj| jdgdd}|| t jdi |S )Nr  )r  )r   r   rc   )r  rj   r/  r   r   rk   r   r   rq   rc   rd   r     s   
zInspectView.get_context_datac                 C   rs   rS   )rT   get_inspect_templateru   rc   rc   rd   r    r   zInspectView.get_template_namesrS   )r   r   r   r$   ry   rh   rl   r   r   r}   r  r  r  r  r  r  r   r  r   rc   rc   rq   rd   r    s    


0	

r  c                       sj   e Zd ZedZdZededdeddded	ed
dgZ	dd Z
dd Zdd Z fddZ  ZS )HistoryViewHistory2   messageAction)r  rg   system)blank_display_name	timestampDatec                 C   r  rS   )strr   ru   rc   rc   rd   r     r   zHistoryView.get_page_subtitlec                 C   rs   rS   )rT   get_history_templateru   rc   rc   rd   r    r   zHistoryView.get_template_namesc                 C   s   t | jdS )Nuser__wagtail_userprofile)r  r  r   prefetch_relatedru   rc   rc   rd   r     s   zHistoryView.get_querysetc                    sr   |   | _t jd	i |}| jdt| jj}t	| j
|d ||  d}||d< |j|d< ||d< d|d< |S )
Nr  r  )base_urlrR  tabler   r   Tis_paginatedrc   )r   r  rk   r   ra   r   r   r   r\   r+   columnsrT  r   )rb   ro   r   r   r  rq   rc   rd   r     s   

zHistoryView.get_context_data)r   r   r   r$   ry   paginate_byr)   r,   r*   r  r   r  r   r   r   rc   rc   rq   rd   r    s    
r  )br   collectionsr   djangor   r  r   django.confr   django.contrib.adminr   django.contrib.admin.optionsr   rr  r   r	   r
   r   r   r   django.contrib.auth.decoratorsr   django.core.exceptionsr   r   r   r   r   django.core.paginatorr   r   	django.dbr   r   django.db.models.fields.relatedr   r   django.shortcutsr   r   django.template.defaultfiltersr   django.utils.decoratorsr   django.utils.encodingr   django.utils.functionalr   django.utils.htmlr    django.utils.httpr!   django.utils.textr"   django.utils.translationr#   r   r$   django.views.genericr%   django.views.generic.editr&   django.views.generic.listr'   wagtail.adminr(   wagtail.admin.ui.tablesr)   r*   r+   r,    wagtail.admin.views.generic.baser-   wagtail.admin.views.mixinsr.   wagtail.log_actionsr/   r0   r  wagtail.modelsr1   r2   r3   r5   r6   ImportErrorr7   QUERY_TERMSrQ   r   r   r   r  r  r  r  r  r  rc   rc   rc   rd   <module>   sl     Dm&   8[^(A 