3
(h#                 @   s   d dl mZ 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mZmZ d dlmZ d d	lmZ dZe ZeeG dd
 d
eZdS )    )unicode_literalsN)flatattpretty_name)Textarea	TextInput)six)
force_textpython_2_unicode_compatible
smart_text)conditional_escapeformat_html	html_safe)	mark_safe)ugettext_lazy
BoundFieldc               @   s   e Zd ZdZdd Zdd Zdd Zdd	 Zd
d Ze	dd Z
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Zd/ddZe	d d! Ze	d"d# Ze	d$d% Ze	d&d' Zd0d(d)ZdS )1r   zA Field plus datac             C   sp   || _ || _|| _|j|| _|j|| _|j| j| _| jj	d krPt
|| _	n
| jj	| _	|jpbd| _t| _d S )N )formfieldnameZ
add_prefix	html_nameZadd_initial_prefixhtml_initial_nameauto_idhtml_initial_idlabelr   	help_textUNSET_initial_value)selfr   r   r    r   ;/tmp/pip-install-q3hcpn_q/Django/django/forms/boundfield.py__init__   s    
zBoundField.__init__c             C   s$   | j jr| j | jdd S | j S )z%Renders this field as an HTML widget.T)only_initial)r   Zshow_hidden_initial	as_widget	as_hidden)r   r   r   r   __str__'   s    zBoundField.__str__c             c   s\   | j jjjdp| j}|r"d|ini }| j|}x&| j jj| j| j |D ]
}|V  qJW dS )z
        Yields rendered strings that comprise all widgets in this BoundField.

        This really is only useful for RadioSelect widgets, so that you can
        iterate over individual radio buttons in a template.
        idN)	r   widgetattrsgetr   build_widget_attrsZ
subwidgetsr   value)r   id_r'   Z	subwidgetr   r   r   __iter__-   s
    
zBoundField.__iter__c             C   s   t t| j S )N)lenlistr,   )r   r   r   r   __len__:   s    zBoundField.__len__c             C   s&   t |tjtf stt| j | S )N)
isinstancer   integer_typesslice	TypeErrorr.   r,   )r   idxr   r   r   __getitem__=   s    zBoundField.__getitem__c             C   s   | j jj| j| j j S )zl
        Returns an ErrorList for this field. Returns an empty ErrorList
        if there are none.
        )r   errorsr(   r   Zerror_class)r   r   r   r   r6   D   s    zBoundField.errorsNFc             C   s   |s| j j}| j jrd|_|p i }| j||}| j}|rbd|krbd|jkrb|sX||d< n
| j|d< |sn| j}n| j	}t
|j|| j |dS )z
        Renders the field by rendering the passed widget, adding any HTML
        attributes passed as attrs.  If no widget is specified, then the
        field's default widget will be used.
        Tr%   )r'   )r   r&   ZlocalizeZis_localizedr)   r   r'   r   r   r   r   renderr*   )r   r&   r'   r!   r   r   r   r   r   r"   L   s    

zBoundField.as_widgetc             K   s   | j t |f|S )z[
        Returns a string of HTML for representing this as an <input type="text">.
        )r"   r   )r   r'   kwargsr   r   r   as_textg   s    zBoundField.as_textc             K   s   | j t |f|S )z?Returns a string of HTML for representing this as a <textarea>.)r"   r   )r   r'   r8   r   r   r   as_textaream   s    zBoundField.as_textareac             K   s   | j | jj |f|S )z]
        Returns a string of HTML for representing this as an <input type="hidden">.
        )r"   r   Zhidden_widget)r   r'   r8   r   r   r   r#   q   s    zBoundField.as_hiddenc             C   s   | j jj| jj| jj| jS )zS
        Returns 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;   w   s    zBoundField.datac             C   s>   | j js| j}n"| jj| j| j jj| j| jj}| jj|S )z
        Returns the value for this BoundField, using the initial value if
        the form is not bound or the data otherwise.
        )	r   Zis_boundinitialr   Z
bound_datar;   r(   r   Zprepare_value)r   r;   r   r   r   r*   ~   s
    zBoundField.valuec             C   s
  |p| j }|dkr.| jjdk	r&| jjn| jj}|rR|rR|d tdkrRtd||}| jj}|jjdpj| j	}|r|j
|}|rt|pi fd|i}| jjrt| jdr|pi }d|kr|d  d	| jj 7  < n| jj|d< |rt|nd
}td||}nt|}t|S )as  
        Wraps 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, uses the field's HTML-escaped label.

        If attrs are given, they're used as HTML attributes on the <label> tag.

        label_suffix allows overriding 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dictrequiredhasattrr@   r   r   r   )r   contentsr'   rD   r&   r+   rF   r   r   r   	label_tag   s*    


zBoundField.label_tagc             C   sj   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 )zQ
        Returns a string of space-separated CSS classes for this field.
        spliterror_css_classr@   rB   )rI   rL   setr6   r   addrM   r   rH   r@   join)r   Zextra_classesr   r   r   css_classes   s    
zBoundField.css_classesc             C   s
   | j jjS )z3Returns True if this BoundField's widget is hidden.)r   r&   	is_hidden)r   r   r   r   rR      s    zBoundField.is_hiddenc             C   s4   | j j}|r&dt|kr&t|| j S |r0| jS dS )z
        Calculates and returns the ID attribute for this BoundField, if the
        associated Form has specified auto_id. Returns an empty string otherwise.
        z%sr   )r   r   r
   r   )r   r   r   r   r   r      s    zBoundField.auto_idc             C   s$   | 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   rF   )r   r&   r+   r   r   r   rF      s    zBoundField.id_for_labelc             C   sj   | j jj| j| jj}t|rf| jtk	r0| j}n6| }t|t	j	t	j
fr`| jjj r`|jdd}|| _|S )Nr   )microsecond)r   r=   r(   r   r   callabler   r   r0   datetimetimer&   Zsupports_microsecondsreplace)r   r;   r   r   r   r=      s    
zBoundField.initialc             C   sL   |s| j j}t|}|j| jr8| j jr8| jjr8d|d< | j jrHd|d< |S )NTrH   disabled)r   r&   rG   Zuse_required_attributer=   rH   r   rX   )r   r'   r&   r   r   r   r)      s    zBoundField.build_widget_attrs)NNF)N)N)N)NNN)N)N)__name__
__module____qualname____doc__r    r$   r,   r/   r5   propertyr6   r"   r9   r:   r#   r;   r*   rK   rQ   rR   r   rF   r=   r)   r   r   r   r   r      s(   




%
)r   )
__future__r   rU   Zdjango.forms.utilsr   r   Zdjango.forms.widgetsr   r   Zdjango.utilsr   Zdjango.utils.encodingr   r	   r
   Zdjango.utils.htmlr   r   r   Zdjango.utils.safestringr   Zdjango.utils.translationr   rE   __all__objectr   r   r   r   r   r   <module>   s   