

_VL                 @   sv  d  Z  d d l m Z d d l Z d d l m Z d d l m Z m 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 m Z d d
 l m Z d d l m Z 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 f Z$ Gd d   d e  Z% e e Gd d   d e&    Z' Gd d   d e j( e% e'   Z) d S)z
Form classes
    )unicode_literalsN)OrderedDict)NON_FIELD_ERRORSValidationError)
BoundField)Field	FileField)	ErrorDict	ErrorListpretty_name)MediaMediaDefiningClass)six)
force_textpython_2_unicode_compatible)cached_property)conditional_escape	html_safe)	mark_safe)ugettextBaseFormFormc                   s(   e  Z d  Z d Z   f d d   Z   S)DeclarativeFieldsMetaclasszF
    Metaclass that collects Fields declared on the base classes.
    c                sE  g  } xR t  | j    D]> \ } } t | t  r | j | | f  | j |  q q W| j d d d    t |  | d <t t	 |   j
 |  | | |  } t   } x t | j  D]p }	 t |	 d  r | j |	 j  n  xE |	 j j   D]4 \ }
 } | d  k r |
 | k r | j |
  q q Wq W| | _ | | _ | S)Nkeyc             S   s   |  d j  S)N   )Zcreation_counter)x r   4/tmp/pip-build-ghmbqnp_/Django/django/forms/forms.py<lambda>&   s    z4DeclarativeFieldsMetaclass.__new__.<locals>.<lambda>declared_fields)listitems
isinstancer   appendpopsortr   superr   __new__reversed__mro__hasattrupdater   __dict__base_fields)ZmcsnamebasesattrsZcurrent_fieldsr   value	new_classr   baseattr)	__class__r   r   r'      s&    			z"DeclarativeFieldsMetaclass.__new__)__name__
__module____qualname____doc__r'   r   r   )r5   r   r      s   r   c               @   s  e  Z d  Z d Z d Z d d d d d e d d d 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 d. d/   Z d0 d1   Z e d2 d3    Z e d4 d5    Z  d6 d7   Z! d8 d9   Z" d: d;   Z# d S)<r   Nzid_%sFc
       
      C   s   | d  k	 p | d  k	 |  _  | p$ i  |  _ | p3 i  |  _ | |  _ | d  k	 rZ | |  _ n  | pc i  |  _ | |  _ | d  k	 r | n	 t d  |  _ | |  _	 d  |  _
 t j |  j  |  _ i  |  _ |  j |	 d  k r |  j n |	  d  S)N:)is_bounddatafilesauto_idprefixinitialerror_class_label_suffixempty_permitted_errorscopydeepcopyr-   fields_bound_fields_cacheorder_fieldsfield_order)
selfr<   r=   r>   r?   r@   rA   rC   rD   rK   r   r   r   __init__H   s    		!			zBaseForm.__init__c             C   sv   | d k r d St    } x= | D]5 } y |  j j |  | | <Wq  t k
 rT Yq  Xq  W| j |  j  | |  _ d S)a  
        Rearranges the fields according to field_order.

        field_order is a list of field names specifying the order. Fields not
        included in the list are appended in the default order for backward
        compatibility with subclasses not overriding field_order. If field_order
        is None, all fields are kept in the order defined in the class.
        Unknown fields in field_order are ignored to allow disabling fields in
        form subclasses without redefining ordering.
        N)r   rH   r$   KeyErrorr+   )rL   rK   rH   r   r   r   r   rJ   a   s    		zBaseForm.order_fieldsc             C   s
   |  j    S)N)as_table)rL   r   r   r   __str__w   s    zBaseForm.__str__c             C   sj   |  j  d  k r d } n |  j o. t |  j   } d i |  j j d 6|  j d 6| d 6d j |  j  d 6S)NUnknownz?<%(cls)s bound=%(bound)s, valid=%(valid)s, fields=(%(fields)s)>clsZboundvalid;rH   )rE   r;   boolr5   r6   joinrH   )rL   is_validr   r   r   __repr__z   s    	
zBaseForm.__repr__c             c   s!   x |  j  D] } |  | Vq
 Wd  S)N)rH   )rL   r.   r   r   r   __iter__   s    zBaseForm.__iter__c             C   sx   y |  j  | } Wn. t k
 rA t d | |  j j f   Yn X| |  j k rm | j |  |  |  j | <n  |  j | S)z)Returns a BoundField with the given name.zKey %r not found in '%s')rH   rN   r5   r6   rI   Zget_bound_field)rL   r.   fieldr   r   r   __getitem__   s    zBaseForm.__getitem__c             C   s#   |  j  d k r |  j   n  |  j  S)z7Returns an ErrorDict for the data provided for the formN)rE   
full_clean)rL   r   r   r   errors   s    zBaseForm.errorsc             C   s   |  j  o |  j S)z
        Returns True if the form has no errors. Otherwise, False. If errors are
        being ignored, returns False.
        )r;   r]   )rL   r   r   r   rW      s    zBaseForm.is_validc             C   s   |  j  r d |  j  | f S| S)z
        Returns the field name with a prefix appended, if this Form has a
        prefix set.

        Subclasses may wish to override.
        z%s-%s)r?   )rL   
field_namer   r   r   
add_prefix   s    zBaseForm.add_prefixc             C   s   d |  j  |  S)zL
        Add a 'initial' prefix for checking dynamic initial values
        z
initial-%s)r_   )rL   r^   r   r   r   add_initial_prefix   s    zBaseForm.add_initial_prefixc                s  |  j    } g  g  } } x|  j j   D]\   }	 d }
 |    } |  j d d   | j D  } | j r | r | j   f d d   | D  n  | j t j	 |   q) | j
   } | r d | }
 n  | r | r | j | t |   n  | j r*t t | j   } | j |  p$d } n d } |	 j rO| t |	 j  } n d } | j | i t |  d 6t |  d 6t j	 |  d 6| d	 6|
 d
 6| d 6| j d 6 q) W| r| j d | t |   n  | rd j |  } | r{| d } | j |  sU| i d d 6d d 6d d 6d d	 6|
 d
 6d d 6d d 6} | j |  n  | d t |   | | | d <q| j |  n  t d j |   S)zIHelper function for outputting HTML. Used by as_table(), as_ul(), as_p(). c             S   s   g  |  ] } t  |   q Sr   )r   ).0errorr   r   r   
<listcomp>   s   	 z)BaseForm._html_output.<locals>.<listcomp>c                s4   g  |  ]* } t  d   i   d 6t |  d 6 q S)z!(Hidden field %(name)s) %(error)sr.   rc   )rB   r   )rb   e)r.   r   r   rd      s   	z class="%s"r]   labelrZ   	help_texthtml_class_attrcss_classesr^   r   r   N
rk   )non_field_errorsrH   r!   rA   r]   	is_hiddenextendr#   r   	text_typeri   r   rf   r   Z	label_tagrg   Z	html_nameinsertrV   endswithlenr   )rL   
normal_row	error_row	row_enderhelp_text_htmlerrors_on_separate_rowZ
top_errorsoutputhidden_fieldsrZ   rh   ZbfZ	bf_errorsri   rf   rg   Z
str_hiddenZlast_rowr   )r.   r   _html_output   sf    
			
&zBaseForm._html_outputc             C   s(   |  j  d d d d d d d d d	 d
  S)zJReturns this form rendered as HTML <tr>s -- excluding the <table></table>.rs   zW<tr%(html_class_attr)s><th>%(label)s</th><td>%(errors)s%(field)s%(help_text)s</td></tr>rt   z <tr><td colspan="2">%s</td></tr>ru   z
</td></tr>rv   z&<br /><span class="helptext">%s</span>rw   F)rz   )rL   r   r   r   rO      s    	zBaseForm.as_tablec             C   s(   |  j  d d d d d d d d d	 d
  S)zDReturns this form rendered as HTML <li>s -- excluding the <ul></ul>.rs   zF<li%(html_class_attr)s>%(errors)s%(label)s %(field)s%(help_text)s</li>rt   z<li>%s</li>ru   z</li>rv   z! <span class="helptext">%s</span>rw   F)rz   )rL   r   r   r   as_ul	  s    	zBaseForm.as_ulc             C   s(   |  j  d d d d d d d d d	 d
  S)z(Returns this form rendered as HTML <p>s.rs   z:<p%(html_class_attr)s>%(label)s %(field)s%(help_text)s</p>rt   z%sru   z</p>rv   z! <span class="helptext">%s</span>rw   T)rz   )rL   r   r   r   as_p  s    	zBaseForm.as_pc             C   s   |  j  j t |  j d d   S)z
        Returns an ErrorList of errors that aren't associated with a particular
        field -- i.e., from Form.clean(). Returns an empty ErrorList if there
        are none.
        rA   nonfield)r]   getr   rA   )rL   r   r   r   rl     s    zBaseForm.non_field_errorsc             C   sB  t  | t  s t |  } n  t | d  rT | d k	 rH t d   qj | j } n i | j | pf t 6} x | j   D] \ } } | |  j k r
| t k r | |  j	 k r t
 d |  j j | f   n  | t k r |  j d d  |  j | <q
|  j   |  j | <n  |  j | j |  | |  j k rw |  j | =qw qw Wd 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 its value is None the errors will be treated 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. What we define as
        an "error" can be either a simple string or an instance of
        ValidationError with its message attribute set and what we define as
        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'.rA   r}   )r"   r   r*   	TypeErrorr   
error_listr   r!   r]   rH   
ValueErrorr5   r6   rA   rE   rn   cleaned_data)rL   rZ   rc   r   r   r   r   	add_error#  s&    zBaseForm.add_errorc             C   s`   | d  k r | |  j  k S| |  j  k r\ x1 |  j  j   | D] } | j | k r< d Sq< Wn  d S)NTF)r]   Zas_datacode)rL   rZ   r   rc   r   r   r   	has_errorS  s    zBaseForm.has_errorc             C   s^   t    |  _ |  j s d Si  |  _ |  j r< |  j   r< d S|  j   |  j   |  j   d S)zc
        Cleans all of self.data and populates self._errors and
        self.cleaned_data.
        N)	r	   rE   r;   r   rD   has_changed_clean_fields_clean_form_post_clean)rL   r   r   r   r\   \  s    		

zBaseForm.full_cleanc             C   s<  x5|  j  j   D]$\ } } | j r@ |  j j | | j  } n' | j j |  j |  j |  j	 |   } y t
 | t  r |  j j | | j  } | j | |  } n | j |  } | |  j | <t |  d |  r t |  d |    } | |  j | <n  Wq t k
 r3} z |  j | |  WYd  d  } ~ Xq Xq Wd  S)Nzclean_%s)rH   r!   disabledr@   r~   widgetvalue_from_datadictr<   r=   r_   r"   r   cleanr   r*   getattrr   r   )rL   r.   rZ   r1   r@   re   r   r   r   r   n  s    	'zBaseForm._clean_fieldsc             C   sd   y |  j    } Wn5 t k
 rG } z |  j d  |  WYd  d  } ~ Xn X| d  k	 r` | |  _ n  d  S)N)r   r   r   r   )rL   r   re   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   )rL   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   )rL   r   r   r   r     s    zBaseForm.cleanc             C   s   t  |  j  S)z<
        Returns True if data differs from initial.
        )rU   changed_data)rL   r   r   r   r     s    zBaseForm.has_changedc       	      C   s  g  } x|  j  j   D] \ } } |  j |  } | j j |  j |  j |  } | j s |  j j	 | | j  } t
 |  r |   } q nh |  j |  } | j   } y( | j | j |  j |  j |   } Wn" t k
 r | j |  w Yn X| j | |  r | j |  q q W| S)N)rH   r!   r_   r   r   r<   r=   Zshow_hidden_initialr@   r~   callabler`   hidden_widgetZ	to_pythonr   r#   r   )	rL   r<   r.   rZ   Zprefixed_nameZ
data_valueZinitial_valueZinitial_prefixed_namer   r   r   r   r     s&    	zBaseForm.changed_datac             C   s7   t    } x' |  j j   D] } | | j j } q W| S)z`
        Provide a description of all media required to render the widgets on this form
        )r   rH   valuesr   media)rL   r   rZ   r   r   r   r     s    	zBaseForm.mediac             C   s.   x' |  j  j   D] } | j j r d Sq Wd S)zz
        Returns True if the form needs to be multipart-encoded, i.e. it has
        FileInput. Otherwise, False.
        TF)rH   r   r   Zneeds_multipart_form)rL   rZ   r   r   r   is_multipart  s    zBaseForm.is_multipartc             C   s   d d   |  D S)z
        Returns 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 Sr   )rm   )rb   rZ   r   r   r   rd     s   	 z*BaseForm.hidden_fields.<locals>.<listcomp>r   )rL   r   r   r   ry     s    zBaseForm.hidden_fieldsc             C   s   d d   |  D S)z
        Returns a list of BoundField objects that aren't hidden fields.
        The opposite of the hidden_fields() method.
        c             S   s   g  |  ] } | j  s |  q Sr   )rm   )rb   rZ   r   r   r   rd     s   	 z+BaseForm.visible_fields.<locals>.<listcomp>r   )rL   r   r   r   visible_fields  s    zBaseForm.visible_fields)$r6   r7   r8   rK   r?   r
   rM   rJ   rP   rX   rY   r[   propertyr]   rW   r_   r`   rz   rO   r{   r|   rl   r   r   r\   r   r   r   r   r   r   r   r   r   ry   r   r   r   r   r   r   >   s@   		N			0			

c               @   s   e  Z d  Z d Z d S)r   z3A collection of Fields, plus their associated data.N)r6   r7   r8   r9   r   r   r   r   r     s   )*r9   
__future__r   rF   collectionsr   Zdjango.core.exceptionsr   r   Zdjango.forms.boundfieldr   Zdjango.forms.fieldsr   r   Zdjango.forms.utilsr	   r
   r   Zdjango.forms.widgetsr   r   Zdjango.utilsr   Zdjango.utils.encodingr   r   Zdjango.utils.functionalr   Zdjango.utils.htmlr   r   Zdjango.utils.safestringr   Zdjango.utils.translationr   rB   __all__r   objectr   with_metaclassr   r   r   r   r   <module>   s*   # 