3
g\M                 @   s   d Z ddlZddlmZmZ ddlmZm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 dd	lmZmZ dd
lmZ ddlmZ ddlmZ dZG dd deZeG dd dZG dd deedZdS )z
Form classes
    N)NON_FIELD_ERRORSValidationError)Field	FileField)	ErrorDict	ErrorList)MediaMediaDefiningClass)MultiValueDict)cached_property)conditional_escape	html_safe)	mark_safe)gettext   )get_default_rendererBaseFormFormc                   s    e Zd ZdZ fddZ  ZS )DeclarativeFieldsMetaclassz,Collect Fields declared on the base classes.c       	         s    fddt  j D  d< t j| || }i }xZt|jD ]L}t|drZ|j|j x0|j	j D ]"\}}|d krf||krf|j
| qfW q@W ||_||_|S )Nc                s&   i | ]\}}t |tr j||qS  )
isinstancer   pop).0keyvalue)attrsr   P/var/www/tester-filtro-web/env/lib/python3.6/site-packages/django/forms/forms.py
<dictcomp>   s   z6DeclarativeFieldsMetaclass.__new__.<locals>.<dictcomp>declared_fields)listitemssuper__new__reversed__mro__hasattrupdater   __dict__r   base_fields)	Zmcsnamebasesr   	new_classr   baseattrr   )	__class__)r   r   r"      s    

z"DeclarativeFieldsMetaclass.__new__)__name__
__module____qualname____doc__r"   __classcell__r   r   )r.   r   r      s   r   c               @   s.  e Zd ZdZdZdZdZdZdddddedddddfddZ	dd	 Z
d
d Zdd Zdd Zdd Zedd Zdd Zdd Zdd Zdd Zdd Zdd Zd d! Zd"d# Zd$d% Zd@d&d'Zd(d) Zd*d+ Zd,d- Zd.d/ Zd0d1 Zd2d3 Z e!d4d5 Z"ed6d7 Z#d8d9 Z$d:d; Z%d<d= Z&d>d? Z'dS )Ar   z
    The main implementation of all the Form logic. Note that this class is
    different than Form. See the comments by the Form class for more info. Any
    improvements to the form API should be made to this class, not to the Form
    class.
    NTzid_%sFc             C   s  |d k	p|d k	| _ |d kr t n|| _|d kr4t n|| _|| _|d k	rN|| _|pTi | _|| _|d k	rj|ntd| _	|| _
d | _tj| j| _i | _| j|	d kr| jn|	 |
d k	r|
| _| j
r| jrtd|d kr| jd krt }n| j}t| jtr| }|| _d S )N:zNThe empty_permitted and use_required_attribute arguments may not both be True.)is_boundr
   datafilesauto_idprefixinitialerror_class_label_suffixempty_permitted_errorscopydeepcopyr(   fields_bound_fields_cacheorder_fieldsfield_orderuse_required_attribute
ValueErrordefault_rendererr   r   typerenderer)selfr6   r7   r8   r9   r:   r;   r=   r>   rE   rF   rJ   r   r   r   __init__@   s4    


zBaseForm.__init__c             C   s^   |dkrdS i }x6|D ].}y| j j|||< W q tk
rB   Y qX qW |j| j  || _ dS )a  
        Rearrange the fields according to field_order.

        field_order is a list of field names specifying the order. Append fields
        not included in the list in the default order for backward compatibility
        with subclasses not overriding field_order. If field_order is None,
        keep all fields in the order defined in the class. Ignore unknown
        fields in field_order to allow disabling fields in form subclasses
        without redefining ordering.
        N)rB   r   KeyErrorr&   )rK   rE   rB   r   r   r   r   rD   m   s    

zBaseForm.order_fieldsc             C   s   | j  S )N)as_table)rK   r   r   r   __str__   s    zBaseForm.__str__c             C   s>   | j d krd}n| jo| j  }d| jj| j|dj| jd S )NUnknownz?<%(cls)s bound=%(bound)s, valid=%(valid)s, fields=(%(fields)s)>;)clsboundZvalidrB   )r?   r5   r.   r/   joinrB   )rK   is_validr   r   r   __repr__   s    
zBaseForm.__repr__c             c   s   x| j D ]}| | V  qW d S )N)rB   )rK   r)   r   r   r   __iter__   s    zBaseForm.__iter__c             C   sl   y| j | }W n6 tk
rD   td|| jjdjt| j f Y nX || jkrb|j| || j|< | j| S )z(Return a BoundField with the given name.z,Key '%s' not found in '%s'. Choices are: %s.z, )rB   rM   r.   r/   rT   sortedrC   Zget_bound_field)rK   r)   fieldr   r   r   __getitem__   s    
zBaseForm.__getitem__c             C   s   | j dkr| j  | j S )z7Return an ErrorDict for the data provided for the form.N)r?   
full_clean)rK   r   r   r   errors   s    
zBaseForm.errorsc             C   s   | j o| j S )z:Return True if the form has no errors, or False otherwise.)r5   r\   )rK   r   r   r   rU      s    zBaseForm.is_validc             C   s   | j rd| j |f S |S )z
        Return the field name with a prefix appended, if this Form has a
        prefix set.

        Subclasses may wish to override.
        z%s-%s)r9   )rK   
field_namer   r   r   
add_prefix   s    zBaseForm.add_prefixc             C   s   d| j | S )z<Add an 'initial' prefix for checking dynamic initial values.z
initial-%s)r^   )rK   r]   r   r   r   add_initial_prefix   s    zBaseForm.add_initial_prefixc                s  | j  j }g g  }}x| jj D ]\ }	d}
|   }| j|j}|jrt|rd|j fdd|D  |jt	| q"|j
 }|rd| }
|r|r|j|t	|  |jrt|j}|j|pd}nd}|	jr||	j }nd}|j||||||
||jd  q"W |r|jd||  |rdj|}|r|d
 }|j|sb|dddd|
ddd }|j| |dt|  | | |d< n
|j| td	j|S )z1Output HTML. Used by as_table(), as_ul(), as_p(). c                s"   g | ]}t d  t|d qS )z!(Hidden field %(name)s) %(error)s)r)   error)r<   str)r   e)r)   r   r   
<listcomp>   s   z)BaseForm._html_output.<locals>.<listcomp>z class="%s")r\   labelrY   	help_texthtml_class_attrcss_classesr]   r   r   N
rj   )non_field_errorsr@   rB   r    r;   r\   	is_hiddenextendappendrb   rh   re   r   Z	label_tagrf   Z	html_nameinsertrT   endswithlenr   )rK   
normal_row	error_row	row_enderhelp_text_htmlerrors_on_separate_rowZ
top_errorsoutputhidden_fieldsrY   rg   ZbfZ	bf_errorsrh   re   rf   Z
str_hiddenZlast_rowr   )r)   r   _html_output   sf    






 
zBaseForm._html_outputc             C   s   | j ddddddS )zIReturn this form rendered as HTML <tr>s -- excluding the <table></table>.zW<tr%(html_class_attr)s><th>%(label)s</th><td>%(errors)s%(field)s%(help_text)s</td></tr>z <tr><td colspan="2">%s</td></tr>z
</td></tr>z$<br><span class="helptext">%s</span>F)rr   rs   rt   ru   rv   )ry   )rK   r   r   r   rN     s    zBaseForm.as_tablec             C   s   | j ddddddS )zCReturn this form rendered as HTML <li>s -- excluding the <ul></ul>.zF<li%(html_class_attr)s>%(errors)s%(label)s %(field)s%(help_text)s</li>z<li>%s</li>z</li>z! <span class="helptext">%s</span>F)rr   rs   rt   ru   rv   )ry   )rK   r   r   r   as_ul  s    zBaseForm.as_ulc             C   s   | j ddddddS )z'Return this form rendered as HTML <p>s.z:<p%(html_class_attr)s>%(label)s %(field)s%(help_text)s</p>z%sz</p>z! <span class="helptext">%s</span>T)rr   rs   rt   ru   rv   )ry   )rK   r   r   r   as_p   s    zBaseForm.as_pc             C   s   | j jt| jddS )z
        Return an ErrorList of errors that aren't associated with a particular
        field -- i.e., from Form.clean(). Return an empty ErrorList if there
        are none.
        nonfield)r;   )r\   getr   r;   )rK   r   r   r   rk   *  s    zBaseForm.non_field_errorsc             C   s   t |tst|}t|dr6|dk	r.tdqD|j}n|p<t|ji}x|j D ]\}}|| jkr|tkr|| j	krt
d| jj|f |tkr| jdd| j|< n| j | j|< | j| j| || jkrN| j|= qNW dS )a%  
        Update the content of `self._errors`.

        The `field` argument is the name of the field to which the errors
        should be added. If it's None, treat the errors as NON_FIELD_ERRORS.

        The `error` argument can be a single error, a list of errors, or a
        dictionary that maps field names to lists of errors. An "error" can be
        either a simple string or an instance of ValidationError with its
        message attribute set and a "list or dictionary" can be an actual
        `list` or `dict` or an instance of ValidationError with its
        `error_list` or `error_dict` attribute set.

        If `error` is a dictionary, the `field` argument *must* be None and
        errors will be added to the fields that correspond to the keys of the
        dictionary.
        
error_dictNzbThe argument `field` must be `None` when the `error` argument contains errors for multiple fields.z'%s' has no field named '%s'.r|   )r;   )r   r   r%   	TypeErrorr~   r   
error_listr    r\   rB   rG   r.   r/   r;   r?   rm   cleaned_data)rK   rY   ra   r   r   r   r   	add_error2  s&    



zBaseForm.add_errorc                s2   || j ko0 d kp0t fdd| j j | D S )Nc             3   s   | ]}|j  kV  qd S )N)code)r   ra   )r   r   r   	<genexpr>d  s    z%BaseForm.has_error.<locals>.<genexpr>)r\   anyZas_data)rK   rY   r   r   )r   r   	has_errora  s    
zBaseForm.has_errorc             C   sH   t  | _| jsdS i | _| jr,| j  r,dS | j  | j  | j  dS )zY
        Clean all of self.data and populate self._errors and self.cleaned_data.
        N)	r   r?   r5   r   r>   has_changed_clean_fields_clean_form_post_clean)rK   r   r   r   r[   g  s    zBaseForm.full_cleanc             C   s   x| j j D ]\}}|jr(| j||}n|jj| j| j| j|}ydt	|t
rh| j||}|j||}n
|j|}|| j|< t| d| rt| d|  }|| j|< W q tk
r } z| j|| W Y d d }~X qX qW d S )Nzclean_%s)rB   r    disabledget_initial_for_fieldwidgetvalue_from_datadictr6   r7   r^   r   r   cleanr   r%   getattrr   r   )rK   r)   rY   r   r:   rc   r   r   r   r   x  s    


zBaseForm._clean_fieldsc             C   sN   y| j  }W n. tk
r: } z| jd | W Y d d }~X nX |d k	rJ|| _d S )N)r   r   r   r   )rK   r   rc   r   r   r   r     s    zBaseForm._clean_formc             C   s   dS )z
        An internal hook for performing additional cleaning after form cleaning
        is complete. Used for model validation in model forms.
        Nr   )rK   r   r   r   r     s    zBaseForm._post_cleanc             C   s   | j S )a+  
        Hook for doing any extra form-wide cleaning after Field.clean() has been
        called on every field. Any ValidationError raised by this method will
        not be associated with a particular field; it will have a special-case
        association with the field named '__all__'.
        )r   )rK   r   r   r   r     s    zBaseForm.cleanc             C   s
   t | jS )z)Return True if data differs from initial.)boolchanged_data)rK   r   r   r   r     s    zBaseForm.has_changedc       	      C   s   g }x| j j D ]\}}| j|}|jj| j| j|}|jsH| | j}nP| j	|}|j
 }y|j|j| j| j|}W n  tk
r   |j| wY nX |j||r|j| qW |S )N)rB   r    r^   r   r   r6   r7   Zshow_hidden_initialr:   r_   hidden_widgetZ	to_pythonr   rn   r   )	rK   r6   r)   rY   Zprefixed_nameZ
data_valueinitial_valueZinitial_prefixed_namer   r   r   r   r     s"    


zBaseForm.changed_datac             C   s*   t  }x| jj D ]}||jj }qW |S )z=Return all media required to render the widgets on this form.)r   rB   valuesr   media)rK   r   rY   r   r   r   r     s    zBaseForm.mediac             C   s   t dd | jj D S )z{
        Return True if the form needs to be multipart-encoded, i.e. it has
        FileInput, or False otherwise.
        c             s   s   | ]}|j jV  qd S )N)r   Zneeds_multipart_form)r   rY   r   r   r   r     s    z(BaseForm.is_multipart.<locals>.<genexpr>)r   rB   r   )rK   r   r   r   is_multipart  s    zBaseForm.is_multipartc             C   s   dd | D S )z
        Return a list of all the BoundField objects that are hidden fields.
        Useful for manual form layout in templates.
        c             S   s   g | ]}|j r|qS r   )rl   )r   rY   r   r   r   rd     s    z*BaseForm.hidden_fields.<locals>.<listcomp>r   )rK   r   r   r   rx     s    zBaseForm.hidden_fieldsc             C   s   dd | D S )z
        Return a list of BoundField objects that aren't hidden fields.
        The opposite of the hidden_fields() method.
        c             S   s   g | ]}|j s|qS r   )rl   )r   rY   r   r   r   rd     s    z+BaseForm.visible_fields.<locals>.<listcomp>r   )rK   r   r   r   visible_fields  s    zBaseForm.visible_fieldsc             C   s"   | j j||j }t|r| }|S )z
        Return initial data for field on form. Use initial data from the form
        or the field, in that order. Evaluate callable values.
        )r:   r}   callable)rK   rY   r]   r   r   r   r   r     s    zBaseForm.get_initial_for_field)N)(r/   r0   r1   r2   rH   rE   r9   rF   r   rL   rD   rO   rV   rW   rZ   propertyr\   rU   r^   r_   ry   rN   rz   r{   rk   r   r   r[   r   r   r   r   r   r   r   r   r   rx   r   r   r   r   r   r   r   3   sH   +	N


/
		c               @   s   e Zd ZdZdS )r   z3A collection of Fields, plus their associated data.N)r/   r0   r1   r2   r   r   r   r   r     s   )	metaclass)r   r   ) r2   r@   Zdjango.core.exceptionsr   r   Zdjango.forms.fieldsr   r   Zdjango.forms.utilsr   r   Zdjango.forms.widgetsr   r	   Zdjango.utils.datastructuresr
   Zdjango.utils.functionalr   Zdjango.utils.htmlr   r   Zdjango.utils.safestringr   Zdjango.utils.translationr   r<   Z	renderersr   __all__r   r   r   r   r   r   r   <module>   s$      ;