a
    DgV)                     @   s   d dl mZmZ d dlmZ d dlmZ G dd deZeG dd deZeG dd	 d	eZ	eG d
d deZ
eG dd d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ZG dd deZdS )    )ABCabstractmethod)formatted_list_child_generator)deconstructiblec                   @   s0   e Zd Zdd Zedd Zeedd ZdS )BaseBlockOperationc                 C   s   d S N selfr   r   c/var/www/lab.imftr.de/x/nb_venv/lib/python3.9/site-packages/wagtail/blocks/migrations/operations.py__init__   s    zBaseBlockOperation.__init__c                 C   s   d S r   r   r
   block_valuer   r   r   apply
   s    zBaseBlockOperation.applyc                 C   s   d S r   r   r	   r   r   r   operation_name_fragment   s    z*BaseBlockOperation.operation_name_fragmentN)__name__
__module____qualname__r   r   r   propertyr   r   r   r   r   r      s   
r   c                       s4   e Zd ZdZ fddZdd Zedd Z  ZS )RenameStreamChildrenOperationap  Renames all StreamBlock children of the given type

    Note:
        The `block_path_str` when using this operation should point to the parent StreamBlock
        which contains the blocks to be renamed, not the block being renamed.

    Attributes:
        old_name (str): name of the child block type to be renamed
        new_name (str): new name to rename to
    c                    s   t    || _|| _d S r   superr   old_namenew_namer
   r   r   	__class__r   r   r   !   s    
z&RenameStreamChildrenOperation.__init__c                 C   sD   g }|D ]6}|d | j kr4|i |d| ji q|| q|S )Ntype)r   appendr   r
   r   mapped_block_valuechild_blockr   r   r   r   &   s    z#RenameStreamChildrenOperation.applyc                 C   s   d| j  d| j S NZrename_Z_to_r   r   r	   r   r   r   r   /   s    z5RenameStreamChildrenOperation.operation_name_fragment	r   r   r   __doc__r   r   r   r   __classcell__r   r   r   r   r      s
   	r   c                       s4   e Zd ZdZ fddZdd Zedd Z  ZS )RenameStructChildrenOperationap  Renames all StructBlock children of the given type

    Note:
        The `block_path_str` when using this operation should point to the parent StructBlock
        which contains the blocks to be renamed, not the block being renamed.

    Attributes:
        old_name (str): name of the child block type to be renamed
        new_name (str): new name to rename to
    c                    s   t    || _|| _d S r   r   r   r   r   r   r   A   s    
z&RenameStructChildrenOperation.__init__c                 C   s8   i }|  D ]&\}}|| jkr*||| j< q|||< q|S r   )itemsr   r   )r
   r   r    	child_keychild_valuer   r   r   r   F   s    

z#RenameStructChildrenOperation.applyc                 C   s   d| j  d| j S r"   r#   r	   r   r   r   r   O   s    z5RenameStructChildrenOperation.operation_name_fragmentr$   r   r   r   r   r'   4   s
   	r'   c                       s4   e Zd ZdZ fddZdd Zedd Z  ZS )RemoveStreamChildrenOperationa>  Removes all StreamBlock children of the given type

    Note:
        The `block_path_str` when using this operation should point to the parent StreamBlock
        which contains the blocks to be removed, not the block being removed.

    Attributes:
        name (str): name of the child block type to be removed
    c                    s   t    || _d S r   r   r   namer
   r-   r   r   r   r   `   s    
z&RemoveStreamChildrenOperation.__init__c                    s    fdd|D S )Nc                    s   g | ]}|d   j kr|qS )r   r-   ).0r!   r	   r   r   
<listcomp>e   s   z7RemoveStreamChildrenOperation.apply.<locals>.<listcomp>r   r   r   r	   r   r   d   s    
z#RemoveStreamChildrenOperation.applyc                 C   s   d| j  S NZremove_r/   r	   r   r   r   r   k   s    z5RemoveStreamChildrenOperation.operation_name_fragmentr$   r   r   r   r   r+   T   s
   
r+   c                       s4   e Zd ZdZ fddZdd Zedd Z  ZS )RemoveStructChildrenOperationa>  Removes all StructBlock children of the given type

    Note:
        The `block_path_str` when using this operation should point to the parent StructBlock
        which contains the blocks to be removed, not the block being removed.

    Attributes:
        name (str): name of the child block type to be removed
    c                    s   t    || _d S r   r,   r.   r   r   r   r   |   s    
z&RemoveStructChildrenOperation.__init__c                    s    fdd|  D S )Nc                    s    i | ]\}}| j kr||qS r   r/   )r0   r)   r*   r	   r   r   
<dictcomp>   s   
z7RemoveStructChildrenOperation.apply.<locals>.<dictcomp>)r(   r   r   r	   r   r      s    
z#RemoveStructChildrenOperation.applyc                 C   s   d| j  S r2   r/   r	   r   r   r   r      s    z5RemoveStructChildrenOperation.operation_name_fragmentr$   r   r   r   r   r3   p   s
   
r3   c                       s<   e Zd ZdZ fddZdd Zdd Zedd	 Z  Z	S )
"StreamChildrenToListBlockOperationa  Combines StreamBlock children of the given type into a new ListBlock

    Note:
        The `block_path_str` when using this operation should point to the parent StreamBlock
        which contains the blocks to be combined, not the child block itself.

    Attributes:
        block_name (str): name of the child block type to be combined
        list_block_name (str): name of the new ListBlock type
    c                    s   t    || _|| _d S r   )r   r   
block_namelist_block_name)r
   r6   r7   r   r   r   r      s    
z+StreamChildrenToListBlockOperation.__init__c                 C   s^   g }g }|D ](}|d | j kr*|| q|| q| |}|rZ| j|d}|| |S Nr   r   value)r6   r   map_temp_blocks_to_list_itemsr7   )r
   r   Zcandidate_blocksr    r!   
list_itemsZnew_list_blockr   r   r   r      s    

z(StreamChildrenToListBlockOperation.applyc                 C   s(   g }|D ]}| i |ddi q|S )Nr   item)r   )r
   blocksr<   blockr   r   r   r;      s    z@StreamChildrenToListBlockOperation.map_temp_blocks_to_list_itemsc                 C   s   | j  d| j S )NZ_to_list_block_)r6   r7   r	   r   r   r   r      s    z:StreamChildrenToListBlockOperation.operation_name_fragment)
r   r   r   r%   r   r   r;   r   r   r&   r   r   r   r   r5      s   r5   c                       s4   e Zd ZdZ fddZdd Zedd Z  ZS )$StreamChildrenToStreamBlockOperationa  Combines StreamBlock children of the given types into a new StreamBlock

    Note:
        The `block_path_str` when using this operation should point to the parent StreamBlock
        which contains the blocks to be combined, not the child block itself.

    Attributes:
        block_names (:obj:`list` of :obj:`str`): names of the child block types to be combined
        stream_block_name (str): name of the new StreamBlock type
    c                    s   t    || _|| _d S r   )r   r   block_namesstream_block_name)r
   rA   rB   r   r   r   r      s    
z-StreamChildrenToStreamBlockOperation.__init__c                 C   sT   g }g }|D ](}|d | j v r*|| q|| q|rP| j|d}|| |S r8   )rA   r   rB   )r
   r   r    Zstream_valuer!   Znew_stream_blockr   r   r   r      s    
z*StreamChildrenToStreamBlockOperation.applyc                 C   s   d d| jS )Nz{}_to_stream_block_)formatjoinrA   r	   r   r   r   r      s    z<StreamChildrenToStreamBlockOperation.operation_name_fragmentr$   r   r   r   r   r@      s
   r@   c                       s4   e Zd ZdZ fddZdd Zedd Z  ZS )AlterBlockValueOperationzrAlters the value of each block to the given value

    Attributes:
        new_value : new value to change to
    c                    s   t    || _d S r   )r   r   	new_value)r
   rG   r   r   r   r      s    
z!AlterBlockValueOperation.__init__c                 C   s   | j S r   )rG   r   r   r   r   r      s    zAlterBlockValueOperation.applyc                 C   s   dS )NZalter_block_valuer   r	   r   r   r   r      s    z0AlterBlockValueOperation.operation_name_fragmentr$   r   r   r   r   rF      s
   rF   c                       s4   e Zd ZdZ fddZdd Zedd Z  ZS )$StreamChildrenToStructBlockOperationa8  Move each StreamBlock child of the given type inside a new StructBlock

    A new StructBlock will be created as a child of the parent StreamBlock for each child block of
    the given type, and then that child block will be moved from the parent StreamBlocks children
    inside the new StructBlock as a child of that StructBlock.

    Example:
        Consider the following StreamField definition::

            mystream = StreamField([("char1", CharBlock()) ...], ...)

        Then the stream data would look like the following::

            [
                ...
                { "type": "char1", "value": "Value1", ... },
                { "type": "char1", "value": "Value2", ... },
                ...
            ]

        And if we define the operation like this::

            StreamChildrenToStructBlockOperation("char1", "struct1")

        Our altered stream data would look like this::

            [
                ...
                { "type": "struct1", "value": { "char1": "Value1" } },
                { "type": "struct1", "value": { "char1": "Value2" } },
                ...
            ]

    Note:
        The `block_path_str` when using this operation should point to the parent StreamBlock
        which contains the blocks to be combined, not the child block itself.

    Note:
        Block ids are not preserved here since the new blocks are structurally different than the
        previous blocks.

    Attributes:
        block_names (str): names of the child block types to be combined
        struct_block_name (str): name of the new StructBlock type
    c                    s   t    || _|| _d S r   )r   r   r6   struct_block_name)r
   r6   rI   r   r   r   r   !  s    
z-StreamChildrenToStructBlockOperation.__init__c                 C   sP   g }|D ]B}|d | j kr@|i || j| j |d id q|| q|S )Nr   r:   r9   )r6   r   rI   r   r   r   r   r   &  s    z*StreamChildrenToStructBlockOperation.applyc                 C   s   | j  d| j S )NZ_to_struct_block_)r6   rI   r	   r   r   r   r   5  s    z<StreamChildrenToStructBlockOperation.operation_name_fragmentr$   r   r   r   r   rH      s
   .rH   c                       s0   e Zd Z fddZdd Zedd Z  ZS )"ListChildrenToStructBlockOperationc                    s   t    || _d S r   )r   r   r6   )r
   r6   r   r   r   r   ;  s    
z+ListChildrenToStructBlockOperation.__init__c                 C   s6   g }t |D ]$}|i |d| j|d ii q|S )Nr:   )r   r   r6   r   r   r   r   r   ?  s    z(ListChildrenToStructBlockOperation.applyc                 C   s   d| j  S )NZlist_block_items_to_)r6   r	   r   r   r   r   J  s    z:ListChildrenToStructBlockOperation.operation_name_fragment)r   r   r   r   r   r   r   r&   r   r   r   r   rJ   :  s   rJ   N)abcr   r   Zwagtail.blocks.migrations.utilsr   Zdjango.utils.deconstructr   r   r   r'   r+   r3   r5   r@   rF   rH   rJ   r   r   r   r   <module>   s    -&H