o
    {,h3                     @   s   d dl Z d dlmZ d dlmZmZ d dlmZmZm	Z	 d dl
mZ d dlmZmZ d dlmZ dZG d	d
 d
eZeG dd dZdS )    N)ValidationError)RenderableFieldMixinpretty_name)MultiWidgetTextarea	TextInput)cached_property)format_html	html_safe)gettext_lazy)
BoundFieldc                   @   s$  e Zd Z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edd Z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# Zd<d$d%Zd=d&d'Zd;d(d)Zed*d+ Zed,d- Zed.d/ Zed0d1 Zd;d2d3Zed4d5 Zed6d7 Zed8d9 Z dS )>r   zA Field plus datac                 C   sr   || _ || _|| _||| _||| _|| j| _| jj	d u r(t
|| _	n| jj	| _	|jp1d| _|j| _d S )N )formfieldname
add_prefix	html_nameadd_initial_prefixhtml_initial_nameauto_idhtml_initial_idlabelr   	help_textrenderer)selfr   r   r    r   M/var/www/html/rh/venv/lib/python3.10/site-packages/django/forms/boundfield.py__init__   s   
zBoundField.__init__c                    sX    j jjdp
 j}|rd|ini } |} fdd 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S r   )BoundWidgetr   widgetr   r   ).0r    r   r   r   
<listcomp>*   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__1   s   zBoundField.__bool__c                 C   
   t | jS N)iterr'   r"   r   r   r   __iter__5      
zBoundField.__iter__c                 C   r,   r-   )lenr'   r"   r   r   r   __len__8   r0   zBoundField.__len__c                 C   s*   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 jdS )zT
        Return an ErrorList (empty if there are no errors) for this field.
        )r   )r   errorsr%   r   error_classr   r"   r   r   r   r;   E   s   zBoundField.errorsc                 C   s   | j jp| jjjS r-   )r   template_namer   r   field_template_namer"   r   r   r   r=   N      zBoundField.template_namec                 C   s   d| iS )Nr   r   r"   r   r   r   get_contextR   s   zBoundField.get_contextNFc                 C   s   |p| j j}| j jrd|_|pi }| ||}| jr+d|jvr+|d|r'| jn| j |r@| j	| j
jv r@| j
| j  | j	}n|  }|j|rK| j	n| j||| j
jd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    localizeis_localizedr&   r   r$   
setdefaultr   r   r   data_widget_data_valuehidden_widgetr(   renderr   r   )r   r    r$   only_initialr(   r   r   r   	as_widgetU   s*   zBoundField.as_widgetc                 K      | j t |fi |S )zZ
        Return a string of HTML for representing this as an <input type="text">.
        )rI   r   r   r$   kwargsr   r   r   as_texts   s   zBoundField.as_textc                 K   rJ   )z>Return a string of HTML for representing this as a <textarea>.)rI   r   rK   r   r   r   as_textareay   s   zBoundField.as_textareac                 K   s   | j | j |fi |S )z\
        Return a string of HTML for representing this as an <input type="hidden">.
        )rI   r   rF   rK   r   r   r   	as_hidden}   s   zBoundField.as_hiddenc                 C   s   | j | jj| jS )zR
        Return the data for this BoundField, or None if it wasn't given.
        )r   rE   r   r    r   r"   r   r   r   rD      s   zBoundField.datac                 C   s*   | j }| jjr| 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   is_boundr   
bound_datarD   prepare_value)r   rD   r   r   r   r(      s   zBoundField.valuec                 C   s\   | j }|jr$| }| j|| j}z||}W n ty#   Y dS w | j}|	|| j
S r*   )r   show_hidden_initialrF   r   rE   r   	to_pythonr   rP   has_changedrD   )r   r   rF   initial_valuer   r   r   _has_changed   s   zBoundField._has_changedc           	      C   s  |p| j }|du r| jjdur| jjn| jj}|r)|r)|d tdvr)td||}| jj}|jdp5| j	}|rn|
|}|rIi |pCi d|i}| jjrnt| jdrn|pVi }d|v rh|d  d	| jj 7  < n| jj|d< | ||t||pwd
d}| j| jj|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.
        Nz:?.!z{}{}r   forrequired_css_classclass r   )r   r   r$   use_tagtag)r   r   label_suffixr   _r	   r    r$   r%   r   id_for_labelrequiredhasattrr[   boolrG   template_name_label)	r   contentsr$   r`   r_   r    r)   rb   contextr   r   r   	label_tag   s4   


zBoundField.label_tagc                 C   s   | j |||ddS )af  
        Wrap the given contents in a <legend>, 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 <legend> tag.

        label_suffix overrides the form's label_suffix.
        legendr_   )ri   )r   rg   r$   r`   r   r   r   
legend_tag   s   
zBoundField.legend_tagc                 C   sj   t |dr	| }t|pg }| jrt | jdr|| jj | jjr0t | jdr0|| jj	 d
|S )zP
        Return a string of space-separated CSS classes for this field.
        spliterror_css_classr[   r]   )rd   rm   setr;   r   addrn   r   rc   r[   join)r   extra_classesr   r   r   css_classes   s   

zBoundField.css_classesc                 C   
   | j jjS )z2Return True if this BoundField's widget is hidden.)r   r    	is_hiddenr"   r   r   r   ru      s   
zBoundField.is_hiddenc                 C   s0   | j j}|rdt|v r|| j S |r| jS d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   s$   | j j}|jdp| 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   rb   )r   r    r)   r   r   r   rb      s   
zBoundField.id_for_labelc                 C   s   | j | j| jS r-   )r   get_initial_for_fieldr   r   r"   r   r   r   rP     r?   zBoundField.initialc                 C   s   |p| j j}t|}|| jrG| j jrG| jjrGt| j drC| j jsCt	| j jt
rCt| j j|jD ]\}}|| jo=|j|jd< q1nd|d< | j jrOd|d< |jsY| jrYd|d< |dsj| jsj| j }rj||d< |S )Nrequire_all_fieldsrc   Tdisabledtruezaria-invalidaria-describedby)r   r    dictuse_required_attributerP   rc   r   rd   rx   r3   r   zipfieldswidgetsr$   ry   ru   r;   r%   use_fieldsetaria_describedby)r   r$   r    subfield	subwidgetr   r   r   r   r&   
  s8   



zBoundField.build_widget_attrsc                 C   s^   | j jjdr
d S g }| jr*| js*| jr|| j d | jr*|| j d d	|S )Nr{   	_helptext_errorr]   )
r   r    r$   r%   r   ru   r   appendr;   rq   )r   r   r   r   r   r   +  s   
zBoundField.aria_describedbyc                 C   s   t dd| jjjj S )Nzwidget$|input$r   )resubr   r    	__class__r8   lowerr"   r   r   r   widget_type8  s   zBoundField.widget_typec                 C   rt   )zV
        Return the value of this BoundField widget's use_fieldset attribute.
        )r   r    r   r"   r   r   r   r   >  s   
zBoundField.use_fieldset)NNFr-   )NNNN)NNN)!r8   
__module____qualname____doc__r   r   r'   r+   r/   r2   r:   propertyr;   r=   r@   rI   rM   rN   rO   rD   r(   rX   ri   rl   rs   ru   r   rb   rP   r&   r   r   r   r   r   r   r   r      sP    











+






!

r   c                   @   sN   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 r-   )parent_widgetrD   r   )r   r   rD   r   r   r   r   r   U  s   
zBoundWidget.__init__c                 C   s   | j ddS )NT)
wrap_labelrk   r"   r   r   r   __str__Z  s   zBoundWidget.__str__Fc                 C   s*   di | j d|ii}| j| j|| jS )Nr    r   )rD   r   _renderr=   r   )r   r   rh   r   r   r   r_   ]  s   zBoundWidget.tagc                 C   s   d| j v r
| j d S | jjS )Nr=   )rD   r   r=   r"   r   r   r   r=   a  s   

zBoundWidget.template_namec                 C   s   | j d dS )Nr$   r   )rD   r%   r"   r   r   r   rb   g  s   zBoundWidget.id_for_labelc                 C   s
   | j d S )Nr   )rD   r"   r   r   r   choice_labelk  s   
zBoundWidget.choice_labelN)F)r8   r   r   r   r   r   r_   r   r=   rb   r   r   r   r   r   r   F  s    


r   )r   django.core.exceptionsr   django.forms.utilsr   r   django.forms.widgetsr   r   r   django.utils.functionalr   django.utils.htmlr	   r
   django.utils.translationr   ra   __all__r   r   r   r   r   r   <module>   s      ;