o
    e                     @   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
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)
SafeString   )KeepContextc                   C   s
   t tdS )NCRISPY_TEMPLATE_PACK)getattrr    r   r   S/var/www/html/humari/django-venv/lib/python3.10/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||
I | du r tdW  d   S ttdd}t| dr:| j|||dW  d   S zo||  }|j}|durt|jd|jg}|}t	|t
r]|gt| }tt||D ]C\}\}}t|jdrd|v r|d d	kr|||jj|< qd|jj| j| qdd|v r|d d	kr|||_qd|jj| qdW n ty   |std
|  d}tjd
|  t d Y nw t|dr| |jvr|j|  n|std|  tjd|  t d |du rtd}n[|du r|jdu rt|}n
t|j}nt|}|dur,t|dr(t	|jtr(|j| n|g|_|||t t	|t
r9|ni d |durI|| ||! }|W  d   S 1 s]w   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.
    :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 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
labelclass
flat_attrs)"keysr   r	   r   r   hasattrr   r   widget
isinstancedictlen	enumerateziphidden_widgetr   attrsupdateKeyError	Exceptionloggingwarningsysr   r   addcrispy_field_templater   r   r   listappendr   flatten)r   formcontexttemplater   layout_objectr)   r   extra_contextkwargs
added_keysFAIL_SILENTLYbound_fieldfield_instancer   
list_attrsindexr"   attrhtmlr   r   r   render_field   sx   
	








&rC   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>   s    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   )CrispyFormNodeNr5   helper)r5   rN   )+crispy_forms.templatetags.crispy_forms_tagsrM   r   r*   r   )r5   rN   r6   rM   nodenode_contextr   r   r   render_crispy_form   s   

rR   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   )rG   itemlist2r   r   
<listcomp>   s    z%list_intersection.<locals>.<listcomp>r   )list1rU   r   rT   r   list_intersection   s   rX   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.
    )setr0   r3   )leftrightblocked
differencerS   r   r   r   list_difference   s   

r^   )NN)r-   r/   	functoolsr   django.confr   django.forms.utilsr   rK   django.templater   django.template.loaderr   django.utils.functionalr   django.utils.safestringr	   baser   r   TEMPLATE_PACKr   rC   rR   rX   r^   r   r   r   r   <module>   s2    
q

