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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mZ d dl m!Z! d dl"m#Z# d dl$m%Z% d dl&m'Z' ee'ƒZ(e#ƒ Z)G dd„ deƒZ*G dd„ deƒZ+G dd„ deƒZ,G dd„ de!j-ƒZ-G dd„ de!j.ƒZ.G dd„ de!j/ƒZ/G dd„ de!j0ƒZ0G d d!„ d!e!j1ƒZ1dS )"é    N)Úquote)ÚPermissionDenied)ÚHttpResponse)Úcached_property)Ú	urlencode)Úgettext)Úgettext_lazyÚngettext)Úmessages)ÚPermissionPolicyChecker)ÚBaseMediaFilterSet)ÚBulkActionsCheckboxColumnÚColumnÚ
DateColumnÚDownloadColumnÚTableÚTitleColumn)Úget_valid_next_url_from_requestÚset_query_params)Úgeneric©Úget_document_model)Úget_document_form)Úpermission_policyc                       s(   e Zd Z‡ fdd„Z‡ fdd„Z‡  ZS )ÚBulkActionsColumnc                    s   t ƒ j|ddi|¤Ž d S )NZobj_typeÚdocument)ÚsuperÚ__init__)ÚselfÚargsÚkwargs©Ú	__class__© ú`/var/www/lab.imftr.de/x/nb_venv/lib/python3.9/site-packages/wagtail/documents/views/documents.pyr   !   s    zBulkActionsColumn.__init__c                    s(   t ƒ  |¡}| d¡}|r$|j|d< |S )NÚcurrent_collectionÚparent)r   Úget_header_context_dataÚgetÚid)r   Úparent_contextÚcontextr&   r!   r#   r$   r'   $   s
    

z)BulkActionsColumn.get_header_context_data)Ú__name__Ú
__module__Ú__qualname__r   r'   Ú__classcell__r#   r#   r!   r$   r       s   r   c                       s   e Zd Z‡ fdd„Z‡  ZS )ÚDocumentTablec                    s   t ƒ  |¡}| d¡|d< |S ©Nr%   )r   Úget_context_datar(   )r   r*   r+   r!   r#   r$   r2   -   s    zDocumentTable.get_context_data)r,   r-   r.   r2   r/   r#   r#   r!   r$   r0   ,   s   r0   c                   @   s   e Zd ZeZG dd„ dƒZdS )ÚDocumentsFilterSetc                   @   s   e Zd ZeZg ZdS )zDocumentsFilterSet.MetaN)r,   r-   r.   ÚDocumentÚmodelÚfieldsr#   r#   r#   r$   ÚMeta6   s   r7   N)r,   r-   r.   r   r7   r#   r#   r#   r$   r3   3   s   r3   c                       sÎ   e Zd ZeZg d¢ZdZedƒZdZdZ	dZ
dZdZd	Zd
ZdZdZdZeZeZeƒ ZedƒZdZdd„ Zedd„ ƒZedd„ ƒZedd„ ƒZdd„ Z‡ fdd„Z ‡ fdd„Z!‡ fdd„Z"‡ fd d!„Z#‡  Z$S )"Ú	IndexView)ÚaddÚchangeÚdeleteZ	documentsZ	Documentsúdoc-full-inverseÚpé   úwagtaildocs:indexzwagtaildocs:index_resultszwagtaildocs:add_multipleúwagtaildocs:editz wagtaildocs/documents/index.htmlz(wagtaildocs/documents/index_results.htmlÚtitlezAdd a documentTc                 C   s   | j  | jjddg¡ d¡S )Nr:   r;   Ú
collection)r   Z%instances_user_has_any_permission_forÚrequestÚuserZselect_related©r   r#   r#   r$   Úget_base_querysetP   s
    ÿþzIndexView.get_base_querysetc                 C   s   | j o| j jj d¡S ©NÚcollection_id)ÚfiltersÚformZcleaned_datar(   rE   r#   r#   r$   r%   V   s    zIndexView.current_collectionc              	   C   st   t dƒtdtdƒd| jdd„ dtdtdƒd	td
tdƒd
ddg}| jrpd| jjv rp| dtdtdƒdd¡ |S )NZbulk_actionsrA   ZTitlec                 S   s   dt | jƒ› dS )NZ	document_Z_title)r   Úpk)Údocr#   r#   r$   Ú<lambda>d   ó    z#IndexView.columns.<locals>.<lambda>)ÚlabelÚsort_keyÚget_urlZget_title_idÚfilenameÚFile)rO   Z
created_atÚCreatedz16%)rO   rP   ÚwidthrH   é   rB   Ú
Collectionzcollection.name)rO   Úaccessor)	r   r   Ú_Úget_edit_urlr   r   rI   Úinsertr   )r   Úcolumnsr#   r#   r$   r\   [   s,    ûüöþzIndexView.columnsc                 C   s(   t  | jjddg¡}t|ƒdk r$d }|S )Nr9   r:   é   )r   Z'collections_user_has_any_permission_forrC   rD   Úlen)r   Úcollectionsr#   r#   r$   r_   u   s    ÿzIndexView.collectionsc                 C   s(   | j }| jj d¡}|r$|d| 7 }|S )NÚQUERY_STRINGú?)Ú	index_urlrC   ZMETAr(   )r   Únext_urlZrequest_query_stringr#   r#   r$   Úget_next_url~   s
    zIndexView.get_next_urlc                    s   t tƒ  ¡ d| jo| jjiƒS rG   )r   r   Úget_add_urlr%   rK   rE   r!   r#   r$   re   …   s    þzIndexView.get_add_urlc                    s   t tƒ  |¡d|  ¡ iƒS )NÚnext)r   r   rZ   rd   ©r   Úinstancer!   r#   r$   rZ   Œ   s    

þzIndexView.get_edit_urlc                    s   t ƒ  ¡ }| j|d< |S )NÚis_searching)r   Úget_filterset_kwargsri   ©r   r    r!   r#   r$   rj   ’   s    

zIndexView.get_filterset_kwargsc                    s    t ƒ jf i |¤Ž}| j|d< |S r1   )r   r2   r%   ©r   r    r+   r!   r#   r$   r2   —   s    
zIndexView.get_context_data)%r,   r-   r.   r   Zany_permission_requiredÚcontext_object_namer   Ú
page_titleÚheader_iconZ
page_kwargZpaginate_byÚindex_url_nameZindex_results_url_nameÚadd_url_nameÚedit_url_nameÚtemplate_nameZresults_template_nameZdefault_orderingr0   Ztable_classr3   Zfilterset_classr   r5   Zadd_item_labelZshow_other_searchesrF   r   r%   r\   r_   rd   re   rZ   rj   r2   r/   r#   r#   r!   r$   r8   ;   s>   


r8   c                       s`   e Zd ZeZdZdZdZedƒZdZ	dZ
edd„ ƒZd	d
„ Z‡ fdd„Zdd„ Zdd„ Z‡  ZS )Ú
CreateViewr?   zwagtaildocs:addr@   z0The document could not be created due to errors.zwagtaildocs/documents/add.htmlr<   c                 C   s   t ƒ S ©Nr   rE   r#   r#   r$   r5   ¦   s    zCreateView.modelc                 C   s
   t | jƒS ru   ©r   r5   rE   r#   r#   r$   Úget_form_class¬   s    zCreateView.get_form_classc                    s   t ƒ  ¡ }| jj|d< |S ©NrD   ©r   Úget_form_kwargsrC   rD   rk   r!   r#   r$   rz   ¯   s    
zCreateView.get_form_kwargsc                 C   s   | j | jjdS )N)Zuploaded_by_user)r5   rC   rD   rE   r#   r#   r$   Úget_initial_form_instance´   s    z$CreateView.get_initial_form_instancec                 C   s   t dƒd|ji S )Nz$Document '%(document_title)s' added.Údocument_title)rY   rA   rg   r#   r#   r$   Úget_success_message·   s    ÿzCreateView.get_success_message)r,   r-   r.   r   rp   rq   rr   r   Úerror_messagers   ro   r   r5   rw   rz   r{   r}   r/   r#   r#   r!   r$   rt      s   
rt   c                       sª   e Zd ZeZdZedƒZdZdZdZ	dZ
dZdZed	d
„ ƒZdd„ Zd‡ fdd„	Z‡ fdd„Zdd„ Zedd„ ƒZ‡ fdd„Z‡ fdd„Z‡ fdd„Z‡ fdd„Z‡  ZS )ÚEditViewÚdocument_idz.The document could not be saved due to errors.zwagtaildocs/documents/edit.htmlr?   r@   úwagtaildocs:deleter<   r   c                 C   s   t ƒ S ru   r   rE   r#   r#   r$   r5   È   s    zEditView.modelc                 C   s
   t | jƒS ru   rv   rE   r#   r#   r$   rw   Ì   s    zEditView.get_form_classNc                    s*   t ƒ  |¡}| j | jj| j|¡s&t‚|S ru   )r   Ú
get_objectr   Ú user_has_permission_for_instancerC   rD   Úpermission_requiredr   )r   ZquerysetÚobjr!   r#   r$   r‚   Ï   s    ÿzEditView.get_objectc                    s   t ƒ  ¡ }| jj|d< |S rx   ry   rk   r!   r#   r$   rz   ×   s    
zEditView.get_form_kwargsc                 C   s   t dƒd| jji S )Nz%Document '%(document_title)s' updatedr|   ©rY   ÚobjectrA   rE   r#   r#   r$   r}   Ü   s    ÿzEditView.get_success_messagec                 C   s
   t | jƒS ru   )r   rC   rE   r#   r#   r$   rc   á   s    zEditView.next_urlc                    s   | j ptƒ  ¡ S ru   )rc   r   Úget_success_urlrE   r!   r#   r$   rˆ   å   s    zEditView.get_success_urlc                    s*   t ƒ  ¡ }| jr&|dtd| jiƒ 7 }|S )Nra   rf   )r   Úget_delete_urlrc   r   )r   Z
delete_urlr!   r#   r$   r‰   è   s    
zEditView.get_delete_urlc                    sX   | j  ¡ rDtj | j jj¡sDtj| jt	dƒt 
|  ¡ t	dƒ¡gd tƒ j|fi |¤ŽS )NzLThe file could not be found. Please change the source or delete the documentÚDelete)Zbuttons)r‡   Zis_stored_locallyÚosÚpathÚisfileÚfiler
   ÚerrorrC   rY   Úbuttonr‰   r   Úrender_to_response)r   r+   Zresponse_kwargsr!   r#   r$   r‘   î   s    
ÿûzEditView.render_to_responsec                    s@   t ƒ jf i |¤Ž}| j ¡  ¡ |d< | j ¡ |d< | j|d< |S )NZusage_count_valÚfilesizerf   )r   r2   r‡   Ú	get_usageÚcountZget_file_sizerc   rl   r!   r#   r$   r2   ü   s
    
zEditView.get_context_data)N)r,   r-   r.   r   Úpk_url_kwargr   r~   rs   rp   rr   Údelete_url_namero   rm   r   r5   rw   r‚   rz   r}   rc   rˆ   r‰   r‘   r2   r/   r#   r#   r!   r$   r   ½   s*   

r   c                   @   sR   e Zd Zeƒ ZdZeZdZdZdZ	dZ
dZedƒZdd	„ Zed
d„ ƒZdd„ ZdS )Ú
DeleteViewr€   r;   r<   zwagtaildocs:document_usager   r?   zDelete documentc                 C   s   | j  | jj|| j¡S ru   ©r   rƒ   rC   rD   r‡   ©r   Z
permissionr#   r#   r$   Úuser_has_permission  s    ÿzDeleteView.user_has_permissionc                 C   s   t dddƒS )Nz.Are you sure you want to delete this document?z0Are you sure you want to delete these documents?é   )r	   rE   r#   r#   r$   Úconfirmation_message  s
    ýzDeleteView.confirmation_messagec                 C   s   t dƒd| jji S )Nz&Document '%(document_title)s' deleted.r|   r†   rE   r#   r#   r$   r}     s    ÿzDeleteView.get_success_messageN)r,   r-   r.   r   r5   r•   r   r„   ro   Zusage_url_namer–   rp   r   rn   rš   Úpropertyrœ   r}   r#   r#   r#   r$   r—     s   
	r—   c                   @   s:   e Zd Zeƒ ZdZeZdZdZdZ	dZ
dd„ Zdd	„ Zd
S )Ú	UsageViewr€   r:   r<   r?   r@   c                 C   s   | j  | jj|| j¡S ru   r˜   r™   r#   r#   r$   rš   -  s    ÿzUsageView.user_has_permissionc                 C   s   | j jS ru   )r‡   rA   rE   r#   r#   r$   Úget_page_subtitle2  s    zUsageView.get_page_subtitleN)r,   r-   r.   r   r5   r•   r   r„   ro   rp   rr   rš   rŸ   r#   r#   r#   r$   rž   $  s   rž   )2r‹   Zdjango.contrib.admin.utilsr   Zdjango.core.exceptionsr   Zdjango.http.responser   Zdjango.utils.functionalr   Zdjango.utils.httpr   Zdjango.utils.translationr   rY   r   r	   Zwagtail.adminr
   Zwagtail.admin.authr   Zwagtail.admin.filtersr   Zwagtail.admin.ui.tablesr   r   r   r   r   r   Zwagtail.admin.utilsr   r   Zwagtail.admin.viewsr   Zwagtail.documentsr   Zwagtail.documents.formsr   Zwagtail.documents.permissionsr   Zpermission_checkerr4   r   r0   r3   r8   rt   r   r—   rž   r#   r#   r#   r$   Ú<module>   s4    b G 