
v^w'                 @   s   d  d l  Z  d  d l m Z m Z d  d l m Z m Z d  d l m Z d  d l	 m
 Z
 m Z m Z d  d l m Z d  d l m Z d Z e Gd	 d   d   Z e Gd
 d   d   Z d S)    N)flatattpretty_name)Textarea	TextInput)cached_property)conditional_escapeformat_html	html_safe)	mark_safe)gettext_lazy
BoundFieldc               @   s]  e  Z d  Z d Z d d   Z d d   Z e d d    Z d d	   Z d
 d   Z	 d d   Z
 d d   Z e d d    Z d d d d d  Z d d d  Z d d d  Z d d d  Z e d d    Z d d   Z d d d d  d!  Z d d" d#  Z e d$ d%    Z e d& d'    Z e d( d)    Z e d* d+    Z d d, d-  Z d S).r   zA Field plus datac             C   s   | |  _  | |  _ | |  _ | j |  |  _ | j |  |  _ | j |  j  |  _ |  j j	 d  k rx t
 |  |  _	 n |  j j	 |  _	 | j p d |  _ d  S)N )formfieldnameZ
add_prefix	html_nameZadd_initial_prefixhtml_initial_nameauto_idhtml_initial_idlabelr   	help_text)selfr   r   r    r   9/tmp/pip-build-8lau8j11/django/django/forms/boundfield.py__init__   s    			zBoundField.__init__c             C   s0   |  j  j r& |  j   |  j d d  S|  j   S)z$Render this field as an HTML widget.only_initialT)r   Zshow_hidden_initial	as_widget	as_hidden)r   r   r   r   __str__   s    zBoundField.__str__c                s     j  j j j d  p   j } | r3 d | i n i  }   j |  }   f d d     j  j j   j   j   d | D S)a  
        Most widgets yield a single subwidget, but others like RadioSelect and
        CheckboxSelectMultiple produce one subwidget for each choice.

        This property is cached so that only one database query occurs when
        rendering ModelChoiceFields.
        idc                s.   g  |  ]$ } t    j j |   j j   q Sr   )BoundWidgetr   widgetr   renderer).0r!   )r   r   r   
<listcomp>0   s   	z)BoundField.subwidgets.<locals>.<listcomp>attrs)	r   r!   r%   getr   build_widget_attrs
subwidgetsr   value)r   id_r%   r   )r   r   r(   #   s
    	!zBoundField.subwidgetsc             C   s   d S)NTr   )r   r   r   r   __bool__4   s    zBoundField.__bool__c             C   s   t  |  j  S)N)iterr(   )r   r   r   r   __iter__8   s    zBoundField.__iter__c             C   s   t  |  j  S)N)lenr(   )r   r   r   r   __len__;   s    zBoundField.__len__c             C   s9   t  | t t f  s. t d t |  j   |  j | S)Nz6BoundField indices must be integers or slices, not %s.)
isinstanceintslice	TypeErrortype__name__r(   )r   idxr   r   r   __getitem__>   s
    zBoundField.__getitem__c             C   s"   |  j  j j |  j |  j  j    S)zT
        Return an ErrorList (empty if there are no errors) for this field.
        )r   errorsr&   r   Zerror_class)r   r   r   r   r8   H   s    zBoundField.errorsNFc          	   C   s   | p |  j  j } |  j  j r' d | _ | p0 i  } |  j | |  } |  j r d | j k r | j d | ru |  j n |  j  | j	 d | r |  j
 n |  j d |  j   d | d |  j j  S)z
        Render the field by rendering the passed widget, adding any HTML
        attributes passed as attrs. If a widget isn't specified, use the
        field's default widget.
        Tr   r   r)   r%   r"   )r   r!   ZlocalizeZis_localizedr'   r   r%   
setdefaultr   renderr   r   r)   r   r"   )r   r!   r%   r   r   r   r   r   O   s    	"	zBoundField.as_widgetc             K   s   |  j  t   | |  S)zZ
        Return a string of HTML for representing this as an <input type="text">.
        )r   r   )r   r%   kwargsr   r   r   as_textc   s    zBoundField.as_textc             K   s   |  j  t   | |  S)z>Return a string of HTML for representing this as a <textarea>.)r   r   )r   r%   r;   r   r   r   as_textareai   s    zBoundField.as_textareac             K   s   |  j  |  j j   | |  S)z\
        Return a string of HTML for representing this as an <input type="hidden">.
        )r   r   Zhidden_widget)r   r%   r;   r   r   r   r   m   s    zBoundField.as_hiddenc             C   s(   |  j  j j |  j j |  j j |  j  S)zR
        Return the data for this BoundField, or None if it wasn't given.
        )r   r!   Zvalue_from_datadictr   datafilesr   )r   r   r   r   r>   s   s    zBoundField.datac             C   s=   |  j  } |  j j r- |  j j |  j |  } |  j j |  S)z
        Return the value for this BoundField, using the initial value if
        the form is not bound or the data otherwise.
        )initialr   Zis_boundr   Z
bound_datar>   Zprepare_value)r   r>   r   r   r   r)   z   s    	zBoundField.valuec             C   sw  | p |  j  } | d k rE |  j j d k	 r9 |  j j n	 |  j j } | ry | ry | d t d  k ry t d | |  } |  j j } | j j d  p |  j	 } | ra| j
 |  } | r | p i  d | i  } |  j j r4t |  j d  r4| p i  } d | k r$| d d	 |  j j 7<n |  j j | d <| rFt |  n d
 } t d | |  } n t |  } t |  S)ad  
        Wrap the given contents in a <label>, if the field has an ID attribute.
        contents should be mark_safe'd to avoid HTML escaping. If contents
        aren't given, use the field's HTML-escaped label.

        If attrs are given, use them as HTML attributes on the <label> tag.

        label_suffix overrides the form's label_suffix.
        N   z:?.!z{}{}r   forrequired_css_classclass r   z<label{}>{}</label>)r   r   label_suffixr   _r   r!   r%   r&   r   id_for_labelrequiredhasattrrC   r   r   r
   )r   contentsr%   rG   r!   r*   rI   r   r   r   	label_tag   s*    
"zBoundField.label_tagc             C   s   t  | d  r | j   } t | p' g   } |  j r[ t  |  j d  r[ | j |  j j  |  j j r t  |  j d  r | j |  j j	  d j
 |  S)zP
        Return a string of space-separated CSS classes for this field.
        spliterror_css_classrC   rE   )rK   rN   setr8   r   addrO   r   rJ   rC   join)r   Zextra_classesr   r   r   css_classes   s    zBoundField.css_classesc             C   s   |  j  j j S)z2Return True if this BoundField's widget is hidden.)r   r!   	is_hidden)r   r   r   r   rT      s    zBoundField.is_hiddenc             C   s@   |  j  j } | r/ d t |  k r/ | |  j S| r< |  j Sd S)z
        Calculate and return the ID attribute for this BoundField, if the
        associated Form has specified auto_id. Return an empty string otherwise.
        z%sr   )r   r   strr   )r   r   r   r   r   r      s    zBoundField.auto_idc             C   s4   |  j  j } | j j d  p$ |  j } | j |  S)z
        Wrapper around the field widget's `id_for_label` method.
        Useful, for example, for focusing on this field regardless of whether
        it has a single widget or a MultiWidget.
        r   )r   r!   r%   r&   r   rI   )r   r!   r*   r   r   r   rI      s    zBoundField.id_for_labelc             C   s\   |  j  j |  j |  j  } t | t j t j f  rX |  j j j rX | j	 d d  } | S)Nmicrosecondr   )
r   Zget_initial_for_fieldr   r   r0   datetimetimer!   Zsupports_microsecondsreplace)r   r>   r   r   r   r@      s
    zBoundField.initialc             C   sl   | p |  j  j } t |  } | j |  j  rR |  j  j rR |  j j rR d | d <|  j  j rh d | d <| S)NTrJ   disabled)r   r!   dictZuse_required_attributer@   rJ   r   rZ   )r   r%   r!   r   r   r   r'      s    *

zBoundField.build_widget_attrs)r5   
__module____qualname____doc__r   r   r   r(   r+   r-   r/   r7   propertyr8   r   r<   r=   r   r>   r)   rM   rS   rT   r   rI   r@   r'   r   r   r   r   r      s,   

%
c               @   ss   e  Z d  Z d Z d d   Z d d   Z d d d  Z e d	 d
    Z e d d    Z	 e d d    Z
 d S)r    ap  
    A container class used for iterating over widgets. This is useful for
    widgets that have choices. For example, the following can be used in a
    template:

    {% for radio in myform.beatles %}
      <label for="{{ radio.id_for_label }}">
        {{ radio.choice_label }}
        <span class="radio">{{ radio.tag }}</span>
      </label>
    {% endfor %}
    c             C   s   | |  _  | |  _ | |  _ d  S)N)parent_widgetr>   r"   )r   r`   r>   r"   r   r   r   r      s    		zBoundWidget.__init__c             C   s   |  j  d d  S)N
wrap_labelT)tag)r   r   r   r   r      s    zBoundWidget.__str__Fc             C   s7   d |  j  d | i  i } |  j j |  j | |  j  S)Nr!   ra   )r>   r`   Z_rendertemplate_namer"   )r   ra   contextr   r   r   rb      s    zBoundWidget.tagc             C   s$   d |  j  k r |  j  d S|  j j S)Nrc   )r>   r`   rc   )r   r   r   r   rc     s    zBoundWidget.template_namec             C   s   d |  j  d |  j  d f S)Nzid_%s_%sr   index)r>   )r   r   r   r   rI     s    zBoundWidget.id_for_labelc             C   s   |  j  d S)Nr   )r>   )r   r   r   r   choice_label  s    zBoundWidget.choice_labelN)r5   r\   r]   r^   r   r   rb   r_   rc   rI   rf   r   r   r   r   r       s   r    )z
BoundField)rW   Zdjango.forms.utilsr   r   Zdjango.forms.widgetsr   r   Zdjango.utils.functionalr   Zdjango.utils.htmlr   r   r	   Zdjango.utils.safestringr
   Zdjango.utils.translationr   rH   __all__r   r    r   r   r   r   <module>   s   