B
    CVvg                 @   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   J/tmp/pip-install-o3oxmrkh/django-crispy-forms/crispy_forms/layout_slice.py__init__
   s    
zLayoutSlice.__init__c             O   sh   |rDt |trt|}n|f}|| jkr2|| }n|| }|||S t |trX|||S ||f|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r<xt| jt| jj D ]}|| j| q&W nt | jtrx| jD ]}|j}t|dkrx|| j|d  qP| jj|d  }x|dd D ]}|j| }qW y|||d  W qP t	k
r   t
dY qPX qPW d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   	positions
IndexErrorr   )r   functionipointerr   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| f| 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   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                s.   t |  s*j | j| f| j|< d S )N)r	   r   r   )r#   r%   )r   r   r   r   r   r   wrap_object_onceZ   s    
z/LayoutSlice.wrap_once.<locals>.wrap_object_onceN)r$   )r   r   r   r   r(   r   )r   r   r   r   r   	wrap_onceS   s    zLayoutSlice.wrap_oncec             O   s   t | jtr| jjdk	r | jjnd}| j|| jj| j f||| jj|< xLtt| jt	| jj D ]}||krh| jj|= qhW nt | jt
rt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@xt| jt| jj D ]}|| jj|  q&W n|t | jtrxn| jD ]d}|j}| jj|d  }x"|dd D ]}|}|j| }q|W |j	dkrt |t
r|| qT|| qTW d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   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|krRd| jkrB| jd  d|d 7  < n|d| jd< | j| d S )NattrsZ	css_classclassz %s)copyhasattrr1   popupdate)r#   r   )original_kwargsr   r   r-      s    

z3LayoutSlice.update_attributes.<locals>.update_attrsN)r0   )r   r7   r-   r   )r7   r   update_attributes   s    
zLayoutSlice.update_attributesN)r.   
__module____qualname__r   r   r   r   r   r   r$   r'   r)   r,   r0   r8   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   