a
    Dgq7                     @   s   d dl Z d dl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 e dZG d	d
 d
eZG dd deZG dd dZdS )    N)PermissionDenied)get_all_child_relations)log)_copy_copy_m2m_relations)TranslatableMixin)page_publishedZwagtailc                   @   s   e Zd ZdZdS )CopyPageIntegrityErrorzS
    Raised when the page copy cannot be performed for data integrity reasons.
    N__name__
__module____qualname____doc__ r   r   X/var/www/lab.imftr.de/x/nb_venv/lib/python3.9/site-packages/wagtail/actions/copy_page.pyr	      s   r	   c                   @   s   e Zd ZdZdS )CopyPagePermissionErrorzX
    Raised when the page copy cannot be performed due to insufficient permissions.
    Nr
   r   r   r   r   r      s   r   c                
   @   s@   e Zd ZdZdddZdd	 Zdd
dZdddZdddZdS )CopyPageActionz&
    Copies pages and page trees.
    NFTwagtail.copyc                 C   sL   || _ || _|| _|| _|| _|| _|| _|| _|	| _|
| _	|| _
i | _d S )N)pagetoupdate_attrsexclude_fields	recursivecopy_revisions	keep_liveuserprocess_child_object
log_actionreset_translation_key_uuid_mapping)selfr   r   r   r   r   r   r   r   r   r   r   r   r   r   __init__$   s    zCopyPageAction.__init__c                 C   s"   || j vrt | j |< | j | S )z
        Generates a new UUID if it isn't already being used.
        Otherwise it will return the same UUID if it's already in use.
        )r   uuiduuid4)r    Zold_uuidr   r   r   generate_translation_keyA   s    
z'CopyPageAction.generate_translation_keyc                 C   s   | j jjrtd| jrD| jrD| jj| j jks<| j| j rDtd| jr|s| j}|d u rf| j 	 }| j 
| j|| jstd| jr| j
| j}| stdd S )Nz%Page.copy() called on an unsaved pagez5You cannot copy a tree branch recursively into itselfz,You do not have permission to copy this pagez?You do not have permission to publish a page at the destination)r   _stateZaddingr	   r   r   idZis_descendant_ofr   
get_parentZpermissions_for_userZcan_copy_tor   r   Zcan_publish_subpage)r    skip_permission_checksr   Zdestination_permsr   r   r   checkK   s:    


zCopyPageAction.checkc           "      C   s  |j }|j|j |pg  }| jr*dd i}nddd d d d d}| jrL| j|d< | jr^t |d< |rl|| t	|||d\}}	|	
 D ]@\\}
}}| jr| |||
| | jrt|tr| |j|_q|r|d |_|d	 |_|jdd
 n,|r|j|d}n|j|d}|j|jf}t||||d | jrh|j D ](}|j|jk}d |_d |_|j|_|j}|j|d< t|D ]}
|
  }z|| }W n t!y   Y qzY n0 |D ]^}|j||
j"j#< |	$|
|d f}|r|jnd |d< | jrd|v r| |d |d< qqz|jD ],}||v rt%||rt&||d ||< q||_|  |r<||_'q<|( }|r|
 D ]\}}t)||| q~|j*| jddd}||_'| jr||_+|j,|_-|j,|_.|jdg dd |j/rt0j1|j2||d | j3r|4 }t5|| j3| j|j|6 |j7|j8j9dd|rH|j|j:6 dnd |rb|j|j:6 dnd |j/op| j|j7|j8j9ddd |j/r| jrt5|d| j|d t;<d|j=|j|j ddl>m?}m@} | jArNd}|B   C D ]@}|d	 d	 }|D|d |||f}|d	7 }| jE|||d q|dkrN||_F|jddgd |r^|G }n| jH4 G }|I s| jHjJ D ]8} || jK| jL|d}!|!j| jd |!jMN| jM  q|S )Nalias_ofFT)liveZhas_unpublished_changeslive_revisionfirst_published_atlast_published_atr*   ownertranslation_key)r   r   r      )clean)instancepk)r   changedr2   )r,   r.   r-   )r2   Zupdate_fields)Zsenderr3   revision)r&   language_code)r&   titlelocale)r&   r8   )r   sourceZdestinationr   Zsource_locale)r3   actionr   datazwagtail.publish)r3   r;   r   r6   zPage copied: "%s" id=%d from=%d)PagePageViewRestriction)r   _mpnode_attrsnumchild)restriction_typepasswordr   )r   )OZspecificZdefault_exclude_fields_in_copyZexclude_fields_in_copyr   r   r   r"   r#   updater   itemsr   
isinstancer   r$   r0   pathdepthsaveZ	add_childZadd_siblingr   r   Z	revisionsallr4   Zlatest_revision_idZapproved_go_live_atr&   Z	object_idcontentr   Zget_accessor_nameKeyErrorfieldnamegethasattrgetattrlatest_revisionZget_latest_revision_as_objectsetattrZsave_revisionr,   Z
created_atr.   r-   r+   r   sendZspecific_classr   r'   r   Zget_admin_display_titleZ	locale_idr9   r7   Zspecific_deferredloggerinfor8   Zwagtail.modelsr=   r>   r   Zget_childreniterator	_get_path
_copy_pager@   Zget_view_restrictionsr   existsZview_restrictionsrA   rB   groupsset)"r    r   r   r   r   r?   Zspecific_pageZbase_update_attrsZ	page_copyZchild_object_mapZchild_relationZold_pkZchild_objectr6   Zuse_as_latest_revisionZrevision_contentZaccessor_nameZchild_objectsZcopied_child_objectZexclude_fieldrQ   rL   valueZ latest_revision_as_page_revisionparentr=   r>   r@   Z
child_pageZnewdepthZchild_mpnode_attrsZparent_page_restrictionZview_restrictionZview_restriction_copyr   r   r   rX   n   sn   	









	 



zCopyPageAction._copy_pagec                 C   s&   | j |d | j| j| j| j| jdS )N)r(   )r   r   r   )r)   rX   r   r   r   r   )r    r(   r   r   r   executep  s    zCopyPageAction.execute)
NNNFTTNNr   T)F)NNNN)F)	r   r   r   r   r!   r$   r)   rX   r^   r   r   r   r   r      s&             


$ 
  r   )loggingr"   Zdjango.core.exceptionsr   Zmodelcluster.modelsr   Zwagtail.log_actionsr   Zwagtail.models.copyingr   r   Zwagtail.models.i18nr   Zwagtail.signalsr   	getLoggerrT   RuntimeErrorr	   r   r   r   r   r   r   <module>   s   
