
 XLG              
   @   s  d  d l  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  d l m Z d  d	 l m Z d  d
 l m Z d  d l m Z d  d l m Z d  d l m Z m Z d d d f Z d Z d Z d Z  d Z! d Z" d Z# d  Z$ d Z% Gd d   d e  Z& e e Gd d   d e'    Z( e( d d d d d d d d d  Z) d d   Z* d S)     )unicode_literals)ValidationError)Form)BooleanFieldIntegerField)	ErrorList)HiddenInput)six)python_2_unicode_compatible)cached_property)	html_safe)	mark_safe)range)ugettext	ungettextBaseFormSetformset_factory	all_validZTOTAL_FORMSZINITIAL_FORMSZMIN_NUM_FORMSZMAX_NUM_FORMSZORDERDELETEi  c                   s(   e  Z d  Z d Z   f d d   Z   S)ManagementFormz
    ``ManagementForm`` is used to keep track of how many form instances
    are displayed on the page. If adding new forms via javascript, you should
    increment the count field of this form as well.
    c                s   t  d t  |  j t <t  d t  |  j t <t  d d d t  |  j t <t  d d d t  |  j t <t t |   j	 | |   d  S)NZwidgetrequiredF)
r   r   Zbase_fieldsTOTAL_FORM_COUNTINITIAL_FORM_COUNTMIN_NUM_FORM_COUNTMAX_NUM_FORM_COUNTsuperr   __init__)selfargskwargs)	__class__ C/home/ubuntu/projects/ifolica/build/django/django/forms/formsets.pyr   '   s
    zManagementForm.__init__)__name__
__module____qualname____doc__r   r!   r!   )r    r"   r   !   s   r   c            	   @   s  e  Z d  Z d Z d d d d d e d d d  Z 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 e d d    Z d d   Z d d   Z e d d    Z e d  d!    Z e d" d#    Z e d$ d%    Z e d& d'    Z e d( d)    Z e d* d+    Z d, d-   Z e d. d/    Z d0 d1   Z d2 d3   Z d4 d5   Z  d6 d7   Z! d8 d9   Z" d: d;   Z# d< d=   Z$ d> d?   Z% d@ dA   Z& e dB dC    Z' dD dE   Z( dF dG   Z) dH dI   Z* d S)Jr   z;
    A collection of instances of the same Form class.
    Nzid_%sc             C   s   | d  k	 p | d  k	 |  _  | p* |  j   |  _ | |  _ | pB i  |  _ | pQ i  |  _ | |  _ | pi i  |  _ | |  _ d  |  _	 d  |  _
 d  S)N)is_boundget_default_prefixprefixauto_iddatafilesinitialform_kwargserror_class_errors_non_form_errors)r   r+   r,   r*   r)   r-   r/   r.   r!   r!   r"   r   8   s    				zBaseFormSet.__init__c             C   s
   |  j    S)N)as_table)r   r!   r!   r"   __str__E   s    zBaseFormSet.__str__c             C   s   t  |  j  S)z5Yields the forms in the order they should be rendered)iterforms)r   r!   r!   r"   __iter__H   s    zBaseFormSet.__iter__c             C   s   |  j  | S)zAReturns the form at the given index, based on the rendering order)r5   )r   indexr!   r!   r"   __getitem__L   s    zBaseFormSet.__getitem__c             C   s   t  |  j  S)N)lenr5   )r   r!   r!   r"   __len__P   s    zBaseFormSet.__len__c             C   s   d S)zGAll formsets have a management form which is not included in the lengthTr!   )r   r!   r!   r"   __bool__S   s    zBaseFormSet.__bool__c             C   s   t  |   j |   S)N)typer;   )r   r!   r!   r"   __nonzero__W   s    zBaseFormSet.__nonzero__c          	   C   s   |  j  rT t |  j d |  j d |  j } | j   s t t d  d d  q nO t d |  j d |  j d i |  j   t	 6|  j
   t 6|  j t 6|  j t 6 } | S)z5Returns the ManagementForm instance for this FormSet.r*   r)   z8ManagementForm data is missing or has been tampered withcodeZmissing_management_formr-   )r'   r   r+   r*   r)   is_validr   _total_form_countr   initial_form_countr   min_numr   max_numr   )r   formr!   r!   r"   management_formZ   s    	!
zBaseFormSet.management_formc             C   s   |  j  r# t |  j j t |  j  S|  j   } t | |  j  |  j	 } | |  j
 k ob d k n rp | } n+ | |  j
 k o d k n r |  j
 } n  | S)z2Returns the total number of forms in this FormSet.r   )r'   minrF   cleaned_datar   absolute_maxrB   maxrC   extrarD   )r   initial_formsZtotal_formsr!   r!   r"   rA   m   s    		zBaseFormSet.total_form_countc             C   s9   |  j  r |  j j t S|  j r/ t |  j  n d } | S)z>Returns the number of forms that are required in this FormSet.r   )r'   rF   rH   r   r-   r9   )r   rL   r!   r!   r"   rB      s    	zBaseFormSet.initial_form_countc                s)     f d d   t    j    D } | S)z=
        Instantiate forms at first property access.
        c                s+   g  |  ]! }   j  |   j |    q Sr!   )_construct_formget_form_kwargs).0i)r   r!   r"   
<listcomp>   s   	z%BaseFormSet.forms.<locals>.<listcomp>)r   rA   )r   r5   r!   )r   r"   r5      s    zBaseFormSet.formsc             C   s   |  j  j   S)z
        Return additional keyword arguments for each individual formset form.

        index will be None if the form being constructed is a new empty
        form.
        )r.   copy)r   r7   r!   r!   r"   rN      s    zBaseFormSet.get_form_kwargsc             K   s   i |  j  d 6|  j |  d 6|  j d 6d d 6} |  j rW |  j | d <|  j | d <n  |  j r d | k r y |  j | | d <Wq t k
 r Yq Xn  | |  j   k r | |  j	 k r d	 | d
 <n  | j
 |  |  j |   } |  j | |  | S)zO
        Instantiates and returns the i-th form instance in a formset.
        r*   r)   r/   Fuse_required_attributer+   r,   r-   Tempty_permitted)r*   
add_prefixr/   r'   r+   r,   r-   
IndexErrorrB   rC   updaterE   
add_fields)r   rP   r   defaultsrE   r!   r!   r"   rM      s&    


	!zBaseFormSet._construct_formc             C   s   |  j  d |  j    S)z7Return a list of all the initial forms in this formset.N)r5   rB   )r   r!   r!   r"   rL      s    zBaseFormSet.initial_formsc             C   s   |  j  |  j   d  S)z5Return a list of all the extra forms in this formset.N)r5   rB   )r   r!   r!   r"   extra_forms   s    zBaseFormSet.extra_formsc             C   sP   |  j  d |  j d |  j d  d d d d |  j d    } |  j | d   | S)Nr*   r)   Z
__prefix__rT   TrS   F)rE   r*   rU   rN   rX   )r   rE   r!   r!   r"   
empty_form   s    		zBaseFormSet.empty_formc             C   s9   |  j    s% t d |  j j   n  d d   |  j D S)zY
        Returns a list of form.cleaned_data dicts for every form in self.forms.
        z+'%s' object has no attribute 'cleaned_data'c             S   s   g  |  ] } | j   q Sr!   )rH   )rO   rE   r!   r!   r"   rQ      s   	 z,BaseFormSet.cleaned_data.<locals>.<listcomp>)r?   AttributeErrorr    r#   r5   )r   r!   r!   r"   rH      s    zBaseFormSet.cleaned_datac                s     j    s   j r g  St   d  s g    _ xt t d   j    D]Z }   j | } |   j   k r | j   r qI n    j	 |  rI   j j
 |  qI qI Wn    f d d     j D S)zM
        Returns a list of forms that have been marked for deletion.
        _deleted_form_indexesr   c                s   g  |  ] }   j  |  q Sr!   )r5   )rO   rP   )r   r!   r"   rQ      s   	 z-BaseFormSet.deleted_forms.<locals>.<listcomp>)r?   
can_deletehasattrr]   r   rA   r5   rB   has_changed_should_delete_formappend)r   rP   rE   r!   )r   r"   deleted_forms   s    	zBaseFormSet.deleted_formsc                s    j    s   j r0 t d   j j   n  t   d  s g    _ x t d   j    D]s }   j	 | } |   j
   k r | j   r q^ n    j r   j |  r q^ n    j j | | j t f  q^ Wd d   }   j j d |  n    f d d     j D S)	z
        Returns a list of form in the order specified by the incoming data.
        Raises an AttributeError if ordering is not allowed.
        z,'%s' object has no attribute 'ordered_forms'	_orderingr   c             S   s"   |  d d  k r d Sd |  d f S)N   r   )re   r   r!   )kr!   r!   r"   compare_ordering_key
  s    z7BaseFormSet.ordered_forms.<locals>.compare_ordering_keykeyc                s!   g  |  ] }   j  | d   q S)r   )r5   )rO   rP   )r   r!   r"   rQ     s   	 z-BaseFormSet.ordered_forms.<locals>.<listcomp>)r?   	can_orderr\   r    r#   r_   rd   r   rA   r5   rB   r`   r^   ra   rb   rH   ORDERING_FIELD_NAMEsort)r   rP   rE   rg   r!   )r   r"   ordered_forms   s    	!zBaseFormSet.ordered_formsc             C   s   d S)NrE   r!   )clsr!   r!   r"   r(     s    zBaseFormSet.get_default_prefixc             C   s#   |  j  d k r |  j   n  |  j  S)z
        Returns an ErrorList of errors that aren't associated with a particular
        form -- i.e., from formset.clean(). Returns an empty ErrorList if there
        are none.
        N)r1   
full_clean)r   r!   r!   r"   non_form_errors  s    zBaseFormSet.non_form_errorsc             C   s#   |  j  d k r |  j   n  |  j  S)zM
        Returns a list of form.errors for every form in self.forms.
        N)r0   rn   )r   r!   r!   r"   errors!  s    zBaseFormSet.errorsc             C   s*   t  |  j    t d d   |  j D  S)zO
        Returns the number of errors across all forms in the formset.
        c             s   s   |  ] } t  |  Vq d  S)N)r9   )rO   Zform_errorsr!   r!   r"   	<genexpr>/  s    z0BaseFormSet.total_error_count.<locals>.<genexpr>)r9   ro   sumrp   )r   r!   r!   r"   total_error_count*  s    zBaseFormSet.total_error_countc             C   s   | j  j t d  S)zJ
        Returns whether or not the form was marked for deletion.
        F)rH   getDELETION_FIELD_NAME)r   rE   r!   r!   r"   ra   1  s    zBaseFormSet._should_delete_formc             C   s   |  j  s d Sd } |  j x[ t d |  j    D]D } |  j | } |  j rd |  j |  rd q0 qd n  | | j   M} q0 W| o |  j   S)zD
        Returns True if every form in self.forms is valid.
        FTr   )	r'   rp   r   rA   r5   r^   ra   r?   ro   )r   Zforms_validrP   rE   r!   r!   r"   r?   7  s    			zBaseFormSet.is_validc             C   sy  g  |  _  |  j   |  _ |  j s% d Sx= t d |  j    D]& } |  j | } |  j  j | j  q; Wy |  j	 r |  j   t
 |  j  |  j k s |  j j t |  j k r t t d d |  j  |  j d d  n  |  j r-|  j   t
 |  j  |  j k  r-t t d d |  j  |  j d d  n  |  j   Wn: t k
 rt} z |  j | j  |  _ WYd d } ~ Xn Xd S)zg
        Cleans all of self.data and populates self._errors and
        self._non_form_errors.
        Nr   z Please submit %d or fewer forms.r>   Ztoo_many_formszPlease submit %d or more forms.Ztoo_few_forms)r0   r/   r1   r'   r   rA   r5   rb   rp   validate_maxr9   rc   rD   rF   rH   r   rI   r   r   validate_minrC   cleanZ
error_list)r   rP   rE   er!   r!   r"   rn   L  s0    			"	"zBaseFormSet.full_cleanc             C   s   d S)a  
        Hook for doing any extra formset-wide cleaning after Form.clean() has
        been called on every form. Any ValidationError raised by this method
        will not be associated with a particular form; it will be accessible
        via formset.non_form_errors()
        Nr!   )r   r!   r!   r"   rx   m  s    zBaseFormSet.cleanc             C   s   t  d d   |  D  S)zH
        Returns true if data in any form differs from initial.
        c             s   s   |  ] } | j    Vq d  S)N)r`   )rO   rE   r!   r!   r"   rq   z  s    z*BaseFormSet.has_changed.<locals>.<genexpr>)any)r   r!   r!   r"   r`   v  s    zBaseFormSet.has_changedc             C   s   |  j  r{ | d k	 rV | |  j   k  rV t d t d  d | d d d  | j t <q{ t d t d  d d  | j t <n  |  j r t d t d  d d  | j t <n  d S)	z8A hook for adding extra fields on to each form instance.NlabelZOrderr-   re   r   FDelete)	ri   rB   r   r@   fieldsrj   r^   r   ru   )r   rE   r7   r!   r!   r"   rX   |  s    	/%	zBaseFormSet.add_fieldsc             C   s   d |  j  | f S)Nz%s-%s)r)   )r   r7   r!   r!   r"   rU     s    zBaseFormSet.add_prefixc             C   s+   |  j  r |  j  d j   S|  j j   Sd S)zu
        Returns True if the formset needs to be multipart, i.e. it
        has FileInput. Otherwise, False.
        r   N)r5   is_multipartr[   )r   r!   r!   r"   r~     s    	zBaseFormSet.is_multipartc             C   s%   |  j  r |  j  d j S|  j j Sd  S)Nr   )r5   mediar[   )r   r!   r!   r"   r     s    	zBaseFormSet.mediac             C   sA   d j  d d   |  D  } t d j  t j |  j  | g   S)zMReturns this formset rendered as HTML <tr>s -- excluding the <table></table>. c             s   s   |  ] } | j    Vq d  S)N)r2   )rO   rE   r!   r!   r"   rq     s    z'BaseFormSet.as_table.<locals>.<genexpr>
)joinr   r	   	text_typerF   )r   r5   r!   r!   r"   r2     s    zBaseFormSet.as_tablec             C   sA   d j  d d   |  D  } t d j  t j |  j  | g   S)z+Returns this formset rendered as HTML <p>s.r   c             s   s   |  ] } | j    Vq d  S)N)as_p)rO   rE   r!   r!   r"   rq     s    z#BaseFormSet.as_p.<locals>.<genexpr>r   )r   r   r	   r   rF   )r   r5   r!   r!   r"   r     s    zBaseFormSet.as_pc             C   sA   d j  d d   |  D  } t d j  t j |  j  | g   S)z,Returns this formset rendered as HTML <li>s.r   c             s   s   |  ] } | j    Vq d  S)N)as_ul)rO   rE   r!   r!   r"   rq     s    z$BaseFormSet.as_ul.<locals>.<genexpr>r   )r   r   r	   r   rF   )r   r5   r!   r!   r"   r     s    zBaseFormSet.as_ul)+r#   r$   r%   r&   r   r   r3   r6   r8   r:   r;   r=   propertyrF   rA   rB   r   r5   rN   rM   rL   rZ   r[   rH   rc   rl   classmethodr(   ro   rp   rs   ra   r?   rn   rx   r`   rX   rU   r~   r   r2   r   r   r!   r!   r!   r"   r   2   sJ   	
		&
	!	
	re   FNc	             C   s   | d k r t  } n  | d k r* t } n  | t }	 i	 |  d 6| d 6| d 6| d 6| d 6| d 6|	 d 6| d	 6| d
 6}
 t |  j t d  | f |
  S)z*Return a FormSet for the given form class.NrE   rK   ri   r^   rC   rD   rI   rw   rv   ZFormSet)DEFAULT_MIN_NUMDEFAULT_MAX_NUMr<   r#   str)rE   formsetrK   ri   r^   rD   rv   rC   rw   rI   attrsr!   r!   r"   r     s    		

c             C   s0   d } x# |  D] } | j    s d } q q W| S)z3Returns true if every formset in formsets is valid.TF)r?   )ZformsetsZvalidr   r!   r!   r"   r     s
    )+
__future__r   Zdjango.core.exceptionsr   Zdjango.formsr   Zdjango.forms.fieldsr   r   Zdjango.forms.utilsr   Zdjango.forms.widgetsr   Zdjango.utilsr	   Zdjango.utils.encodingr
   Zdjango.utils.functionalr   Zdjango.utils.htmlr   Zdjango.utils.safestringr   Zdjango.utils.six.movesr   Zdjango.utils.translationr   r@   r   __all__r   r   r   r   rj   ru   r   r   r   objectr   r   r   r!   r!   r!   r"   <module>   s<    }		