
 X=M                 @   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   @/home/ubuntu/projects/ifolica/build/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 Z d d d d d e d 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/ d0   Z d1 d2   Z e d3 d4    Z  e d5 d6    Z! d7 d8   Z" d9 d:   Z# d; d<   Z$ d S)=r   NTz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  k	 r |
 |  _ 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use_required_attribute)selfr<   r=   r>   r?   r@   rA   rC   rD   rK   rL   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+   )rM   rK   rH   r   r   r   r   rJ   d   s    		zBaseForm.order_fieldsc             C   s
   |  j    S)N)as_table)rM   r   r   r   __str__z   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boundZvalid;rH   )rE   r;   boolr5   r6   joinrH   )rM   is_validr   r   r   __repr__}   s    	
zBaseForm.__repr__c             c   s!   x |  j  D] } |  | Vq
 Wd  S)N)rH   )rM   r.   r   r   r   __iter__   s    zBaseForm.__iter__c             C   s   y |  j  | } WnP t k
 rc t d | |  j j d j t d d   |  j  D   f   Yn X| |  j k r | j |  |  |  j | <n  |  j | S)z)Returns a BoundField with the given name.z,Key '%s' not found in '%s'. Choices are: %s.z, c             s   s   |  ] } | Vq d  S)Nr   ).0fr   r   r   	<genexpr>   s    z'BaseForm.__getitem__.<locals>.<genexpr>)rH   rO   r5   r6   rV   sortedrI   Zget_bound_field)rM   r.   fieldr   r   r   __getitem__   s    	1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)rM   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;   ra   )rM   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?   )rM   
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)rc   )rM   rb   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   )rZ   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.   rf   )rB   r   )rZ   e)r.   r   r   rg      s   	z class="%s"ra   labelr^   	help_texthtml_class_attrcss_classesrb   r   r   N
rn   )non_field_errorsrH   r!   rA   ra   	is_hiddenextendr#   r   	text_typerl   r   ri   r   Z	label_tagrj   Z	html_nameinsertrV   endswithlenr   )rM   
normal_row	error_row	row_enderhelp_text_htmlerrors_on_separate_rowZ
top_errorsoutputhidden_fieldsr^   rk   ZbfZ	bf_errorsrl   ri   rj   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>.rv   zW<tr%(html_class_attr)s><th>%(label)s</th><td>%(errors)s%(field)s%(help_text)s</td></tr>rw   z <tr><td colspan="2">%s</td></tr>rx   z
</td></tr>ry   z&<br /><span class="helptext">%s</span>rz   F)r}   )rM   r   r   r   rP     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>.rv   zF<li%(html_class_attr)s>%(errors)s%(label)s %(field)s%(help_text)s</li>rw   z<li>%s</li>rx   z</li>ry   z! <span class="helptext">%s</span>rz   F)r}   )rM   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.rv   z:<p%(html_class_attr)s>%(label)s %(field)s%(help_text)s</p>rw   z%srx   z</p>ry   z! <span class="helptext">%s</span>rz   T)r}   )rM   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)ra   getr   rA   )rM   r   r   r   ro   #  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!   ra   rH   
ValueErrorr5   r6   rA   rE   rq   cleaned_data)rM   r^   rf   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)ra   Zas_datacode)rM   r^   r   rf   r   r   r   	has_error[  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)rM   r   r   r   r`   d  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=   rc   r"   r   cleanr   r*   getattrr   r   )rM   r.   r^   r1   r@   rh   r   r   r   r   v  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   )rM   r   rh   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   )rM   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   )rM   r   r   r   r     s    zBaseForm.cleanc             C   s   t  |  j  S)z<
        Returns True if data differs from initial.
        )rU   changed_data)rM   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!   rc   r   r   r<   r=   Zshow_hidden_initialr@   r   callablerd   hidden_widgetZ	to_pythonr   r#   r   )	rM   r<   r.   r^   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)rM   r   r^   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)rM   r^   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   )rp   )rZ   r^   r   r   r   rg     s   	 z*BaseForm.hidden_fields.<locals>.<listcomp>r   )rM   r   r   r   r|     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   )rp   )rZ   r^   r   r   r   rg     s   	 z+BaseForm.visible_fields.<locals>.<listcomp>r   )rM   r   r   r   visible_fields  s    zBaseForm.visible_fields)%r6   r7   r8   rK   r?   rL   r
   rN   rJ   rQ   rX   rY   r_   propertyra   rW   rc   rd   r}   rP   r~   r   ro   r   r   r`   r   r   r   r   r   r   r   r   r   r|   r   r   r   r   r   r   =   sB   		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*   " 