a
    ùÜEb#  ã                   @   sÂ   d dl Z d dlZ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 d dlmZ dd	lmZ d
d„ ZeeƒZeƒ efdd„ƒZddddedfdd„Zdd„ Zddd„Zdd„ Zdd„ ZdS )é    N)Ú	lru_cache)Úsettings)Úflatatt)ÚContext©Úget_template)ÚSimpleLazyObjecté   )ÚKeepContextc                   C   s   t tddƒS )NZCRISPY_TEMPLATE_PACKZ	bootstrap)Úgetattrr   © r   r   úR/home/ja/django-apps/lartica_env/lib/python3.9/site-packages/crispy_forms/utils.pyÚget_template_pack   s    r   c                 C   s   t d|  ƒS )Nz%s/field.htmlr   ©Útemplate_packr   r   r   Údefault_field_template   s    r   c
              	   K   sÜ  |	du rg n|	  ¡ }t||ƒ��¦ | du r:W d  ƒ dS ttddƒ}t| dƒrp| j||||d�W  d  ƒ S zê||  }|j}|du�rXt|jd|jgƒ}|}t|t	ƒr¸|gt
|ƒ }tt||ƒƒD ]�\}\}}t|jdƒ�r"d|v �r|d d	k�r| |¡|jj|< n|jj| j |¡ qÆd|v �rH|d d	k�rH| |¡|_qÆ|jj |¡ qÆW nB t�yœ   |�s~td
|  ƒ‚nd}tjd
|  t ¡ d� Y n0 t|dƒ�rî| |jv�rÄ|j | ¡ n*|�sØtd|  ƒ‚ntjd|  t ¡ d� |du �rþd}n¶|du �r*|jdu �rt|ƒ}n
t|jƒ}nt|ƒ}|du�rlt|dƒ�rdt|jtƒ�rd|j |¡ n|g|_| ||tt|t	ƒ�r†|ni ƒdœ¡ |	du�r¦| |	¡ | |  ¡ ¡}|W  d  ƒ S 1 �sÎ0    Y  dS )aè  
    Renders a django-crispy-forms field

    :param field: Can be a string or a Layout object like `Row`. If it's a layout
        object, we call its render method, otherwise we instantiate a BoundField
        and render it using default template 'CRISPY_TEMPLATE_PACK/field.html'
        The field is added to a list that the form holds called `rendered_fields`
        to avoid double rendering fields.
    :param form: The form/formset to which that field belongs to.
    :param form_style: A way to pass style name to the CSS framework used.
    :template: Template used for rendering the field.
    :layout_object: If passed, it points to the Layout object that is being rendered.
        We use it to store its bound fields in a list called `layout_object.bound_fields`
    :attrs: Attributes for the field's widget
    :template_pack: Name of the template pack to be used for rendering `field`
    :extra_context: Dictionary to be added to context, added variables by the layout object
    NÚ ZCRISPY_FAIL_SILENTLYTÚrenderr   ÚwidgetsÚtypeÚhiddenz"Could not resolve form field '%s'.)Úexc_infoÚrendered_fieldsz(A field should only be rendered once: %sÚbound_fields)ÚfieldÚ
labelclassZ
flat_attrs)!Úkeysr
   r   r   Úhasattrr   r   ÚwidgetÚ
isinstanceÚdictÚlenÚ	enumerateÚzipZhidden_widgetr   ÚattrsÚupdateÚKeyErrorÚ	ExceptionÚloggingÚwarningÚsysr   r   ÚaddZcrispy_field_templater   r   r   ÚlistÚappendr   Úflatten)r   ÚformZ
form_styleÚcontextÚtemplater   Zlayout_objectr$   r   Zextra_contextÚkwargsZ
added_keysZFAIL_SILENTLYZbound_fieldZfield_instancer   Z
list_attrsÚindexr   ÚattrÚhtmlr   r   r   Úrender_field   sn    
 





ýÿ

r6   c                 C   s   t dd„ |  ¡ D ƒƒS )zÌ
    Convert a dictionary of attributes to a single string.

    Passed attributes are redirected to `django.forms.utils.flatatt()`
    with replaced "_" (underscores) by "-" (dashes) in their names.
    c                 S   s   i | ]\}}|  d d¡|“qS )Ú_ú-)Úreplace)Ú.0ÚkÚvr   r   r   Ú
<dictcomp>–   ó    zflatatt.<locals>.<dictcomp>)Ú_flatattÚitems)r$   r   r   r   r   �   s    r   c                 C   sL   ddl m} |dur |ddƒ}n
|ddƒ}t|ƒ}| | |dœ¡ | |¡S )z�
    Renders a form and returns its HTML output.

    This function wraps the template logic in a function easy to use in a Django view.
    r   )ÚCrispyFormNodeNr/   Úhelper)r/   rB   )Z+crispy_forms.templatetags.crispy_forms_tagsrA   r   r%   r   )r/   rB   r0   rA   ÚnodeZnode_contextr   r   r   Úrender_crispy_form™   s    
rD   c                    s   ‡ fdd„| D ƒS )z…
    Take the not-in-place intersection of two lists, similar to sets but preserving order.
    Does not check unicity of list1.
    c                    s   g | ]}|ˆ v r|‘qS r   r   )r:   Úitem©Úlist2r   r   Ú
<listcomp>±   r>   z%list_intersection.<locals>.<listcomp>r   )Zlist1rG   r   rF   r   Úlist_intersection¬   s    rI   c                 C   s6   t |ƒ}g }| D ] }||vr| |¡ | |¡ q|S )zm
    Take the not-in-place difference of two lists (left - right), similar to sets but preserving order.
    )Úsetr+   r-   )ÚleftÚrightZblockedÚ
differencerE   r   r   r   Úlist_difference´   s    
rN   )NN)r(   r*   Ú	functoolsr   Zdjango.confr   Zdjango.forms.utilsr   r?   Zdjango.templater   Zdjango.template.loaderr   Zdjango.utils.functionalr   Úbaser
   r   ZTEMPLATE_PACKr   r6   rD   rI   rN   r   r   r   r   Ú<module>   s.   	ö
s

