a
    ùÜEbº  ã                   @   s:   d dl mZ d dlmZ d dlmZmZ G dd„ dƒZdS )é    )Ú	Container)ÚDynamicError)ÚFieldsetÚ
MultiFieldc                   @   sV   e Zd ZeeefZdd„ Zdd„ Zdd„ Z	dd„ Z
d	d
„ Zdd„ Zdd„ Zdd„ ZdS )ÚLayoutSlicec                 C   s.   || _ t|tƒr$t||d dƒ| _n|| _d S )Né   )ÚlayoutÚ
isinstanceÚintÚslice)Úselfr   Úkey© r   úY/home/ja/django-apps/lartica_env/lib/python3.9/site-packages/crispy_forms/layout_slice.pyÚ__init__
   s    
zLayoutSlice.__init__c                 O   st   |rHt |tƒrt|ƒ}n|f}|| jv r2|| }n|| }||i |¤ŽS t |tƒr`||i |¤ŽS ||fi |¤ŽS dS )z|
        Returns a layout object of type `LayoutClass` with `args` and `kwargs` that
        wraps `fields` inside.
        N)r	   ÚlistÚtupleÚ
args_first)r   ÚLayoutClassÚfieldsÚargsÚkwargsÚ	argumentsr   r   r   Úwrapped_object   s    




zLayoutSlice.wrapped_objectc              	   C   sÔ   t | jtƒr8t| j t| jjƒ¡Ž D ]}|| j|ƒ q$n˜t | jtƒrÐ| jD ]„}|d }t|ƒdkrt|| j|d ƒ qJ| jj|d  }|dd… D ]}|j| }q�z|||d ƒ W qJ tyÌ   t	dƒ‚Y qJ0 qJdS )zÃ
        Iterates over layout objects pointed in `self.slice` executing `function` on them.
        It passes `function` penultimate layout object and the position where to find last one
        r   r   éÿÿÿÿzqTrying to wrap a field within an already wrapped field,                             recheck your filter or layoutN)
r	   r   ÚrangeÚindicesÚlenr   r   r   Ú
IndexErrorr   )r   ÚfunctionÚiÚpointerÚpositionÚlayout_objectr   r   r   Úpre_map(   s"    
ÿzLayoutSlice.pre_mapc                    s    ‡ ‡‡‡fdd„}ˆ  |¡ dS )z‹
        Wraps every layout object pointed in `self.slice` under a `LayoutClass` instance with
        `args` and `kwargs` passed.
        c                    s*   ˆj ˆ | j| gˆ¢R i ˆ¤Ž| j|< d S ©N)r   r   ©r#   Új©r   r   r   r   r   r   Úwrap_objectN   s    z%LayoutSlice.wrap.<locals>.wrap_objectN©r$   )r   r   r   r   r)   r   r(   r   ÚwrapH   s    zLayoutSlice.wrapc                    s    ‡ ‡‡‡fdd„}ˆ  |¡ dS )zÙ
        Wraps every layout object pointed in `self.slice` under a `LayoutClass` instance with
        `args` and `kwargs` passed, unless layout object's parent is already a subclass of
        `LayoutClass`.
        c                    s4   t | ˆ ƒs0ˆjˆ | j| gˆ¢R i ˆ¤Ž| j|< d S r%   )r	   r   r   r&   r(   r   r   Úwrap_object_onceZ   s    
z/LayoutSlice.wrap_once.<locals>.wrap_object_onceNr*   )r   r   r   r   r,   r   r(   r   Ú	wrap_onceS   s    zLayoutSlice.wrap_oncec                 O   sž   t | jtƒr†| jjdur | jjnd}| j|| jj| j g|¢R i |¤Ž| jj|< tt| j t	| jjƒ¡Ž ƒD ]}||krl| jj|= qlnt | jt
ƒrštdƒ‚dS )z“
        Wraps all layout objects pointed in `self.slice` together under a `LayoutClass`
        instance with `args` and `kwargs` passed.
        Nr   z=wrap_together doesn't work with filter, only with [] operator)r	   r   Ústartr   r   r   Úreversedr   r   r   r   r   )r   r   r   r   r.   r    r   r   r   Úwrap_together`   s    ÿÿÿ zLayoutSlice.wrap_togetherc                 C   s¶   t | jtƒr<t| j t| jjƒ¡Ž D ]}|| jj| ƒ q$nvt | jtƒr²| jD ]b}|d }| jj|d  }|dd… D ]}|}|j| }qv|jdkr¨t |t	ƒr¨||ƒ qN||ƒ qNdS )z“
        Iterates over layout objects pointed in `self.slice` executing `function` on them
        It passes `function` last layout object
        r   r   NÚupdate_attrs)
r	   r   r   r   r   r   r   r   Ú__name__Ústr)r   r   r    r!   r"   r#   Zprevious_layout_objectr   r   r   Úmapt   s    

zLayoutSlice.mapc                    s   ‡ fdd„}|   |¡ dS )z`
        Updates attributes of every layout object pointed in `self.slice` using kwargs
        c                    sb   ˆ   ¡ }t| dƒr^d|v rRd| jv rB| jd  d| d¡ 7  < n| d¡| jd< | j |¡ d S )NÚattrsZ	css_classÚclassz %s)ÚcopyÚhasattrr5   ÚpopÚupdate)r#   r   ©Úoriginal_kwargsr   r   r1   ’   s    

z3LayoutSlice.update_attributes.<locals>.update_attrsN)r4   )r   r<   r1   r   r;   r   Úupdate_attributes�   s    
zLayoutSlice.update_attributesN)r2   Ú
__module__Ú__qualname__r   r   r   r   r   r   r$   r+   r-   r0   r4   r=   r   r   r   r   r      s   
 r   N)Zcrispy_forms.bootstrapr   Zcrispy_forms.exceptionsr   Zcrispy_forms.layoutr   r   r   r   r   r   r   Ú<module>   s   