U
    {h                     @   s   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	 G dd de
ZG dd	 d	eZG d
d deZG dd dZG dd dZdS )    )PermissionDenied)transaction)find_available_slug)_copy)copy_for_translation_donec                   @   s   e Zd ZdZdS )ParentNotTranslatedErrorz
    Raised when a call to Page.copy_for_translation is made but the
    parent page is not translated and copy_parents is False.
    N__name__
__module____qualname____doc__ r   r   l/var/www/dating/data/www/fatepal.com/env/lib/python3.8/site-packages/wagtail/actions/copy_for_translation.pyr   	   s   r   c                   @   s   e Zd ZdZdS )!CopyForTranslationPermissionErrorzf
    Raised when the object translation copy cannot be performed due to insufficient permissions.
    Nr   r   r   r   r   r      s   r   c                   @   s   e Zd ZdS )%CopyPageForTranslationPermissionErrorN)r	   r
   r   r   r   r   r   r      s   r   c                   @   sD   e Zd ZdZdddZdddZdd	 Zejd
d Z	dddZ
dS )CopyPageForTranslationActional  
    Creates a copy of this page in the specified locale.

    The new page will be created in draft as a child of this page's translated
    parent.

    For example, if you are translating a blog post from English into French,
    this method will look for the French version of the blog index and create
    the French translation of the blog post under that.

    If this page's parent is not translated into the locale, then a ``ParentNotTranslatedError``
    is raised. You can circumvent this error by passing ``copy_parents=True`` which
    copies any parents that are not translated yet.

    The ``exclude_fields`` parameter can be used to set any fields to a blank value
    in the copy.

    Note that this method calls the ``.copy()`` method internally so any fields that
    are excluded in ``.exclude_fields_in_copy`` will be excluded from the translation.
    FNc                 C   s.   || _ || _|| _|| _|| _|| _|| _d S N)pagelocalecopy_parentsaliasexclude_fieldsuserinclude_subtree)selfr   r   r   r   r   r   r   r   r   r   __init__4   s    
z%CopyPageForTranslationAction.__init__c                 C   s$   | j r |s | j dgs tdd S )N%simple_translation.submit_translationzAYou do not have permission to submit a translation for this page.)r   	has_permsr   r   skip_permission_checksr   r   r   checkF   s    z"CopyPageForTranslationAction.checkc                 C   sX   |  D ]J}| |jr|n| | j| j| j| j}tj	| j
|j|d | | qd S )NZsenderZ
source_objZ
target_obj)Zget_children_copy_for_translationliveget_latest_revision_as_objectr   r   r   r   r   send	__class__specificwalk)r   Zcurrent_pageZ
child_pagetranslated_pager   r   r   r(   Q   s     
z!CopyPageForTranslationAction.walkc           
   
      s   |  j}|j}| s^z| }W qp |jjk
rZ   |sFtd|j ddd}Y qpX n|}|d j	 7 }t
||}|r|j|| ddS  fdd}	|j| |d	ddd|	|d
dS d S )NzParent page is not translated.T)r   r   -F)parentZupdate_slugZupdate_localereset_translation_keyc                    s    ddl m} t||r |_d S )Nr   )TranslatableMixin)wagtail.modelsr-   
isinstancer   )Zoriginal_pageZ	page_copyZchild_relationchild_objectr-   r   r   r   process_child_object   s    
zPCopyPageForTranslationAction._copy_for_translation.<locals>.process_child_object)r   slugzwagtail.copy_for_translation)toZupdate_attrsZcopy_revisionsZ	keep_liver,   r2   r   Z
log_action)Z
get_parentr'   r3   is_rootZget_translationr&   ZDoesNotExistr   Zcopy_for_translationZlanguage_coder   Zcreate_aliascopy)
r   r   r   r   r   r   r+   r3   Ztranslated_parentr2   r   r1   r   r"   e   sH    
  
	z2CopyPageForTranslationAction._copy_for_translationc                 C   sd   | j |d | | jjr| jn| j | j| j| j| j}t	j
| j| j|d | jr`| | j |S )Nr   r!   )r    r"   r   r#   r$   r   r   r   r   r   r%   r&   r   r(   )r   r   r)   r   r   r   execute   s     z$CopyPageForTranslationAction.execute)FFNNF)F)F)r	   r
   r   r   r   r    r(   r   atomicr"   r8   r   r   r   r   r      s        


:r   c                   @   s>   e Zd ZdZdddZdddZejddd	Zdd
dZ	dS )CopyForTranslationActionz
    Creates a copy of this object in the specified locale.

    The ``exclude_fields`` parameter can be used to set any fields to a blank value
    in the copy.
    Nc                 C   s   || _ || _|| _|| _d S r   )objectr   r   r   )r   r;   r   r   r   r   r   r   r      s    z!CopyForTranslationAction.__init__Fc                 C   s$   | j r |s | j dgs tdd S )Nr   zCYou do not have permission to submit a translation for this object.)r   r   r   r   r   r   r   r       s    zCopyForTranslationAction.checkc                 C   s   ddl m}m} t||r"| }t|dg t|dg  |p>g  }t||d\}}||_| D ]\\}}	}
t|
|r`||
_q`|S )Nr   )DraftStateMixinr-   Zdefault_exclude_fields_in_copyZexclude_fields_in_copy)r   )	r.   r<   r-   r/   r$   getattrr   r   items)r   r;   r   r   r<   r-   Z
translatedZchild_object_mapZ_child_relationZ_old_pkr0   r   r   r   r"      s    



z.CopyForTranslationAction._copy_for_translationc                 C   s$   | j |d | | j| j| j}|S )Nr7   )r    r"   r;   r   r   )r   r   Ztranslated_objectr   r   r   r8      s      z CopyForTranslationAction.execute)NN)F)N)F)
r	   r
   r   r   r   r    r   r9   r"   r8   r   r   r   r   r:      s     

r:   N)Zdjango.core.exceptionsr   Z	django.dbr   Zwagtail.coreutilsr   Zwagtail.models.copyingr   Zwagtail.signalsr   	Exceptionr   r   r   r   r:   r   r   r   r   <module>   s   	 