o
    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                   @      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   d/var/www/html/ndineBlogger/venv/lib/python3.10/site-packages/wagtail/actions/copy_for_translation.pyr   	   s    r   c                   @   r   )!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                   @   sN   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   ,   | j r|s| j dgstdd S d S 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      z"CopyPageForTranslationAction.checkc                 C   sX   |  D ]%}| |jr|n| | j| j| j| j}tj	| j
|j|d | | qd S )Nsender
source_obj
target_obj)get_children_copy_for_translationliveget_latest_revision_as_objectr   r   r   r   r   send	__class__specificwalk)r   current_page
child_pagetranslated_pager   r   r   r/   Q   s"   
z!CopyPageForTranslationAction.walkc           
   
      s   |  j}|j}| s-z| }W n" |jjy,   |s"td|j ddd}Y n
w |}|d j	 7 }t
||}|rF|j|| ddS  fdd}	|j| |d	ddd|	|d
dS )NzParent page is not translated.T)r   r   -F)parentupdate_slugupdate_localereset_translation_keyc                    s$   ddl m} t||r |_d S d S )Nr   )TranslatableMixin)wagtail.modelsr8   
isinstancer   )original_page	page_copychild_relationchild_objectr8   r   r   r   process_child_object   s   

zPCopyPageForTranslationAction._copy_for_translation.<locals>.process_child_object)r   slugzwagtail.copy_for_translation)toupdate_attrscopy_revisions	keep_liver7   r@   r   
log_action)
get_parentr.   rA   is_rootget_translationr-   DoesNotExistr   copy_for_translationlanguage_coder   create_aliascopy)
r   r   r   r   r   r   r4   rA   translated_parentr@   r   r?   r   r)   e   sF   

	
	z2CopyPageForTranslationAction._copy_for_translationc                 C   sd   | j |d | | jjr| jn| j | j| j| j| j}t	j
| j| j|d | jr0| | j |S N)r!   r$   )r"   r)   r   r*   r+   r   r   r   r   r   r,   r-   r   r/   )r   r!   r2   r   r   r   execute   s    z$CopyPageForTranslationAction.execute)FFNNFF)r
   r   r   r   r   r"   r/   r   atomicr)   rQ   r   r   r   r   r      s    


:r   c                   @   sB   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   rU   r   r   r   r   r   r   r      s   
z!CopyForTranslationAction.__init__Fc                 C   r   )Nr   zCYou do not have permission to submit a translation for this object.)r   r   r   r    r   r   r   r"      r#   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>||
_q0|S )Nr   )DraftStateMixinr8   default_exclude_fields_in_copyexclude_fields_in_copy)r   )	r9   rV   r8   r:   r+   getattrr   r   items)r   rU   r   r   rV   r8   
translatedchild_object_map_child_relation_old_pkr>   r   r   r   r)      s    



z.CopyForTranslationAction._copy_for_translationc                 C   s8   | j |d | | j| j| j}tj| j| j|d |S rP   )r"   r)   rU   r   r   r   r,   r-   )r   r!   translated_objectr   r   r   rQ      s   z CopyForTranslationAction.execute)NNrR   r   )
r
   r   r   r   r   r"   r   rS   r)   rQ   r   r   r   r   rT      s    

rT   N)django.core.exceptionsr   	django.dbr   wagtail.coreutilsr   wagtail.models.copyingr   wagtail.signalsr   	Exceptionr   r   r   r   rT   r   r   r   r   <module>   s    	 