
^Q\                 @   s6  d  d l  m Z 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 m Z d d   Z e e  Z e   e d d   Z d d d d e d d d  Z d d   Z d d d d  Z d d   Z d d   Z d S)    )unicode_literalsN)settings)flatatt)Context)get_template)SimpleLazyObject)	lru_cache   )KeepContext)PY2	text_typec               C   s   t  t d d  S)NZCRISPY_TEMPLATE_PACK	bootstrap)getattrr    r   r   E/var/www/dbchiro/venv/build/django-crispy-forms/crispy_forms/utils.pyget_template_pack   s    r   c             C   s   t  d |   S)Nz%s/field.html)r   )template_packr   r   r   default_field_template   s    r   c
             K   s  |	 d k r g  n	 |	 j    } t | |  |  d k r> d St t d d  } t |  d  rx |  j | | | d | St r y7 t |  t  r |  j	 d  j
   }  n t |   }  Wq t t f k
 r t d |    Yq Xn  yC| |  } | j } | d k	 r%t | j d	 | j g  } | } t | t  rK| g t |  } n  x t t | |   D] \ } \ } } t | j d	  rd
 | k r| d
 d k r| j |  | j j | <q| j j | j j |  qad
 | k r| d
 d k r| j |  | _ qa| j j j |  qaWn  WnN t k
 rv| sOt d |    n# d } t j d |  d t j   Yn Xt | d  r|  | j k r| j j |   q| st d |    qt j d |  d t j   n  | d k rd } n| d k r2| j d k r t  |  } q>t! | j  } n t! |  } | d k	 rt | d  r~t | j" t#  r~| j" j$ |  q| g | _" n  | j i | d 6| d 6t% t | t  r| n i   d 6 |	 d k	 r| j |	  n  | j&   } | j |  } | SWd QXd 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   asciiz0Field '%s' is using forbidden unicode characters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   
isinstancer   encodedecodeUnicodeEncodeErrorUnicodeDecodeError	Exceptionr   widgetdictlen	enumeratezipZhidden_widgetr   attrsupdateKeyError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   s|    
	(" 	!	&r=   c             C   s   t  d d   |  j   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  |  ]! \ } } | | j  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   sq   d d l  m } | d k	 r. | d d  } n | d d  } t |  } | j i |  d 6| d 6 | j |  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   )CrispyFormNodeNr6   helper)Z+crispy_forms.templatetags.crispy_forms_tagsrG   r   r-   r   )r6   rH   r7   rG   nodeZnode_contextr   r   r   render_crispy_form   s    	rJ   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  |  ] } |   k r |  q Sr   r   )rA   item)list2r   r   
<listcomp>   s   	 z%list_intersection.<locals>.<listcomp>r   )Zlist1rL   r   )rL   r   list_intersection   s    rN   c             C   sP   t  |  } g  } x7 |  D]/ } | | k r | j |  | j |  q q W| S)zm
    Take the not-in-place difference of two lists (left - right), similar to sets but preserving order.
    )setr2   r4   )leftrightZblocked
differencerK   r   r   r   list_difference   s    rS   )
__future__r   r/   r1   Zdjango.confr   Zdjango.forms.utilsr   rE   Zdjango.templater   Zdjango.template.loaderr   Zdjango.utils.functionalr   Zdjango.utils.lru_cacher   baser
   Zcompatibilityr   r   r   ZTEMPLATE_PACKr   r=   rJ   rN   rS   r   r   r   r   <module>   s*   	w
