ó
ù¢TQc           @@ sI  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 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 f Z d Z d Z d Z d Z d Z d Z  d e f d „  ƒ  YZ! e d e" f d „  ƒ  Yƒ Z# e# d e$ e$ e% d „ Z& d „  Z' d S(   i    (   t   absolute_importt   unicode_literals(   t   ValidationError(   t   Form(   t   IntegerFieldt   BooleanField(   t	   ErrorList(   t   Mediat   HiddenInput(   t   python_2_unicode_compatible(   t	   mark_safe(   t   six(   t   xrange(   t   ugettextu   BaseFormSetu	   all_validu   TOTAL_FORMSu   INITIAL_FORMSu   MAX_NUM_FORMSu   ORDERu   DELETEiè  t   ManagementFormc           B@ s   e  Z d  Z d „  Z RS(   uÏ   
    ``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         O@ se   t  d t ƒ |  j t <t  d t ƒ |  j t <t  d t d t ƒ |  j t <t t |  ƒ j	 | | Ž  d  S(   Nt   widgett   required(
   R   R   t   base_fieldst   TOTAL_FORM_COUNTt   INITIAL_FORM_COUNTt   Falset   MAX_NUM_FORM_COUNTt   superR   t   __init__(   t   selft   argst   kwargs(    (    s.   ../Django//lib/python/django/forms/formsets.pyR   !   s    (   t   __name__t
   __module__t   __doc__R   (    (    (    s.   ../Django//lib/python/django/forms/formsets.pyR      s   t   BaseFormSetc           B@ s…  e  Z d  Z d# d# d d# d# e d „ Z d „  Z d „  Z d „  Z d „  Z	 d „  Z
 d „  Z e d	 „  ƒ Z d
 „  Z d „  Z d „  Z d „  Z e d „  ƒ Z e d „  ƒ Z e d „  ƒ Z e d „  ƒ Z e d „  ƒ Z e d „  ƒ Z e d „  ƒ Z d „  Z e d „  ƒ Z d „  Z d „  Z d „  Z d „  Z d „  Z  d „  Z! d „  Z" d „  Z# e d „  ƒ Z$ d  „  Z% d! „  Z& d" „  Z' RS($   u;   
    A collection of instances of the same Form class.
    u   id_%sc         C@ s‰   | d  k	 p | d  k	 |  _ | p* |  j ƒ  |  _ | |  _ | pB i  |  _ | pQ i  |  _ | |  _ | |  _ d  |  _	 d  |  _
 |  j ƒ  d  S(   N(   t   Nonet   is_boundt   get_default_prefixt   prefixt   auto_idt   datat   filest   initialt   error_classt   _errorst   _non_form_errorst   _construct_forms(   R   R$   R%   R#   R"   R&   R'   (    (    s.   ../Django//lib/python/django/forms/formsets.pyR   ,   s    					c         C@ s
   |  j  ƒ  S(   N(   t   as_table(   R   (    (    s.   ../Django//lib/python/django/forms/formsets.pyt   __str__:   s    c         C@ s   t  |  j ƒ S(   u5   Yields the forms in the order they should be rendered(   t   itert   forms(   R   (    (    s.   ../Django//lib/python/django/forms/formsets.pyt   __iter__=   s    c         C@ s   |  j  | S(   uA   Returns the form at the given index, based on the rendering order(   R.   (   R   t   index(    (    s.   ../Django//lib/python/django/forms/formsets.pyt   __getitem__A   s    c         C@ s   t  |  j ƒ S(   N(   t   lenR.   (   R   (    (    s.   ../Django//lib/python/django/forms/formsets.pyt   __len__E   s    c         C@ s   t  S(   uG   All formsets have a management form which is not included in the length(   t   True(   R   (    (    s.   ../Django//lib/python/django/forms/formsets.pyt   __bool__H   s    c         C@ s   t  |  ƒ j |  ƒ S(   N(   t   typeR5   (   R   (    (    s.   ../Django//lib/python/django/forms/formsets.pyt   __nonzero__L   s    c      	   C@ s‘   |  j  rH t |  j d |  j d |  j ƒ} | j ƒ  s t d ƒ ‚ q nE t d |  j d |  j d i |  j ƒ  t 6|  j	 ƒ  t
 6|  j t 6ƒ } | S(   u5   Returns the ManagementForm instance for this FormSet.R#   R"   u8   ManagementForm data is missing or has been tampered withR&   (   R    R   R$   R#   R"   t   is_validR   t   total_form_countR   t   initial_form_countR   t   max_numR   (   R   t   form(    (    s.   ../Django//lib/python/django/forms/formsets.pyt   management_formO   s    	!c         C@ s‡   |  j  r |  j j t S|  j ƒ  } | |  j } | |  j k oJ d k n rX | } n+ | |  j k or d k n rƒ |  j } n  | S(   u2   Returns the total number of forms in this FormSet.i    (   R    R=   t   cleaned_dataR   R:   t   extraR;   (   R   t   initial_formst   total_forms(    (    s.   ../Django//lib/python/django/forms/formsets.pyR9   ^   s    		c         C@ sd   |  j  r |  j j t S|  j r/ t |  j ƒ p2 d } | |  j k oO d k n r` |  j } n  | S(   u>   Returns the number of forms that are required in this FormSet.i    (   R    R=   R>   R   R&   R2   R;   (   R   R@   (    (    s.   ../Django//lib/python/django/forms/formsets.pyR:   m   s    	c         C@ sO   g  |  _  x? t t |  j ƒ  |  j ƒ ƒ D] } |  j  j |  j | ƒ ƒ q( Wd  S(   N(   R.   R   t   minR9   t   absolute_maxt   appendt   _construct_form(   R   t   i(    (    s.   ../Django//lib/python/django/forms/formsets.pyR*   x   s    	%c         K@ sà   i |  j  d 6|  j | ƒ d 6|  j d 6} |  j rP |  j | d <|  j | d <n  |  j r‘ d | k r‘ y |  j | | d <Wq‘ t k
 r q‘ Xn  | |  j ƒ  k r° t	 | d <n  | j
 | ƒ |  j |   } |  j | | ƒ | S(   uO   
        Instantiates and returns the i-th form instance in a formset.
        u   auto_idu   prefixu   error_classu   datau   filesu   initialu   empty_permitted(   R#   t
   add_prefixR'   R    R$   R%   R&   t
   IndexErrorR:   R4   t   updateR<   t
   add_fields(   R   RF   R   t   defaultsR<   (    (    s.   ../Django//lib/python/django/forms/formsets.pyRE   ~   s$    
	c         C@ s   |  j  |  j ƒ   S(   u7   Return a list of all the initial forms in this formset.(   R.   R:   (   R   (    (    s.   ../Django//lib/python/django/forms/formsets.pyR@   —   s    c         C@ s   |  j  |  j ƒ  S(   u5   Return a list of all the extra forms in this formset.(   R.   R:   (   R   (    (    s.   ../Django//lib/python/django/forms/formsets.pyt   extra_formsœ   s    c         C@ s>   |  j  d |  j d |  j d ƒ d t ƒ } |  j | d  ƒ | S(   NR#   R"   u
   __prefix__t   empty_permitted(   R<   R#   RG   R4   RJ   R   (   R   R<   (    (    s.   ../Django//lib/python/django/forms/formsets.pyt
   empty_form¡   s    			c         C@ sB   |  j  ƒ  s% t d |  j j ƒ ‚ n  g  |  j D] } | j ^ q/ S(   uY   
        Returns a list of form.cleaned_data dicts for every form in self.forms.
        u+   '%s' object has no attribute 'cleaned_data'(   R8   t   AttributeErrort	   __class__R   R.   R>   (   R   R<   (    (    s.   ../Django//lib/python/django/forms/formsets.pyR>   ¬   s    c         C@ sà   |  j  ƒ  s |  j r0 t d |  j j ƒ ‚ n  t |  d ƒ s¿ g  |  _ xt t d |  j ƒ  ƒ D]Z } |  j	 | } | |  j
 ƒ  k r– | j ƒ  r– q^ n  |  j | ƒ r^ |  j j | ƒ q^ q^ Wn  g  |  j D] } |  j	 | ^ qÉ S(   uŠ   
        Returns a list of forms that have been marked for deletion. Raises an
        AttributeError if deletion is not allowed.
        u,   '%s' object has no attribute 'deleted_forms'u   _deleted_form_indexesi    (   R8   t
   can_deleteRO   RP   R   t   hasattrt   _deleted_form_indexest   rangeR9   R.   R:   t   has_changedt   _should_delete_formRD   (   R   RF   R<   (    (    s.   ../Django//lib/python/django/forms/formsets.pyt   deleted_formsµ   s    	c         C@ 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 „  } |  j j d | ƒ n  g  |  j D] } |  j	 | d ^ qþ S(   u’   
        Returns a list of form in the order specified by the incoming data.
        Raises an AttributeError if ordering is not allowed.
        u,   '%s' object has no attribute 'ordered_forms'u	   _orderingi    c         S@ s"   |  d d  k r d Sd |  d f S(   Ni   i    (   i   i    (   R   (   t   k(    (    s.   ../Django//lib/python/django/forms/formsets.pyt   compare_ordering_keyæ   s    t   key(   R8   t	   can_orderRO   RP   R   RR   t	   _orderingRT   R9   R.   R:   RU   RQ   RV   RD   R>   t   ORDERING_FIELD_NAMEt   sort(   R   RF   R<   RY   (    (    s.   ../Django//lib/python/django/forms/formsets.pyt   ordered_formsÊ   s    	!	c         C@ s   d S(   Nu   form(    (   t   cls(    (    s.   ../Django//lib/python/django/forms/formsets.pyR!   ï   s    c         C@ s    |  j  d k	 r |  j  S|  j ƒ  S(   u»   
        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(   R)   R   R'   (   R   (    (    s.   ../Django//lib/python/django/forms/formsets.pyt   non_form_errorsó   s    c         C@ s#   |  j  d k r |  j ƒ  n  |  j  S(   uM   
        Returns a list of form.errors for every form in self.forms.
        N(   R(   R   t
   full_clean(   R   (    (    s.   ../Django//lib/python/django/forms/formsets.pyt   errorsý   s    c         C@ s   | j  j t t ƒ S(   uJ   
        Returns whether or not the form was marked for deletion.
        (   R>   t   gett   DELETION_FIELD_NAMER   (   R   R<   (    (    s.   ../Django//lib/python/django/forms/formsets.pyRV     s    c         C@ s‘   |  j  s t St } |  j } x[ t d |  j ƒ  ƒ D]D } |  j | } |  j rf |  j | ƒ rf q2 qf n  | | j	 ƒ  M} q2 W| o t
 |  j ƒ  ƒ S(   uD   
        Returns True if every form in self.forms is valid.
        i    (   R    R   R4   Rc   RT   R9   R.   RQ   RV   R8   t   boolRa   (   R   t   forms_validt   errRF   R<   (    (    s.   ../Django//lib/python/django/forms/formsets.pyR8     s    				c         C@ s“   g  |  _  |  j s d Sx= t d |  j ƒ  ƒ D]& } |  j | } |  j  j | j ƒ q, Wy |  j ƒ  Wn( t k
 rŽ } |  j	 | j
 ƒ |  _ n Xd S(   uE   
        Cleans all of self.data and populates self._errors.
        Ni    (   R(   R    RT   R9   R.   RD   Rc   t   cleanR   R'   t   messagesR)   (   R   RF   R<   t   e(    (    s.   ../Django//lib/python/django/forms/formsets.pyRb      s    		c         C@ s   d S(   u  
        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 accesible
        via formset.non_form_errors()
        N(    (   R   (    (    s.   ../Django//lib/python/django/forms/formsets.pyRi   0  s    c         C@ s   t  d „  |  Dƒ ƒ S(   uH   
        Returns true if data in any form differs from initial.
        c         s@ s   |  ] } | j  ƒ  Vq d  S(   N(   RU   (   t   .0R<   (    (    s.   ../Django//lib/python/django/forms/formsets.pys	   <genexpr>=  s    (   t   any(   R   (    (    s.   ../Django//lib/python/django/forms/formsets.pyRU   9  s    c         C@ s­   |  j  r{ | d k	 rV | |  j ƒ  k  rV t d t d ƒ d | d d t ƒ | j t <q{ t d t d ƒ d t ƒ | j t <n  |  j r© t	 d t d ƒ d t ƒ | j t
 <n  d S(   u8   A hook for adding extra fields on to each form instance.t   labelu   OrderR&   i   R   u   DeleteN(   R[   R   R:   R   t   _R   t   fieldsR]   RQ   R   Re   (   R   R<   R0   (    (    s.   ../Django//lib/python/django/forms/formsets.pyRJ   ?  s    	/%	c         C@ s   d |  j  | f S(   Nu   %s-%s(   R"   (   R   R0   (    (    s.   ../Django//lib/python/django/forms/formsets.pyRG   J  s    c         C@ s+   |  j  r |  j  d j ƒ  S|  j j ƒ  Sd S(   uu   
        Returns True if the formset needs to be multipart, i.e. it
        has FileInput. Otherwise, False.
        i    N(   R.   t   is_multipartRN   (   R   (    (    s.   ../Django//lib/python/django/forms/formsets.pyRq   M  s    	c         C@ s%   |  j  r |  j  d j S|  j j Sd  S(   Ni    (   R.   t   mediaRN   (   R   (    (    s.   ../Django//lib/python/django/forms/formsets.pyRr   W  s    	c         C@ sM   d j  g  |  D] } | j ƒ  ^ q ƒ } t d j  t j |  j ƒ | g ƒ ƒ S(   uM   Returns this formset rendered as HTML <tr>s -- excluding the <table></table>.u    u   
(   t   joinR+   R
   R   t	   text_typeR=   (   R   R<   R.   (    (    s.   ../Django//lib/python/django/forms/formsets.pyR+   `  s    (c         C@ sM   d j  g  |  D] } | j ƒ  ^ q ƒ } t d j  t j |  j ƒ | g ƒ ƒ S(   u+   Returns this formset rendered as HTML <p>s.u    u   
(   Rs   t   as_pR
   R   Rt   R=   (   R   R<   R.   (    (    s.   ../Django//lib/python/django/forms/formsets.pyRu   h  s    (c         C@ sM   d j  g  |  D] } | j ƒ  ^ q ƒ } t d j  t j |  j ƒ | g ƒ ƒ S(   u,   Returns this formset rendered as HTML <li>s.u    u   
(   Rs   t   as_ulR
   R   Rt   R=   (   R   R<   R.   (    (    s.   ../Django//lib/python/django/forms/formsets.pyRv   m  s    (N((   R   R   R   R   R   R   R,   R/   R1   R3   R5   R7   t   propertyR=   R9   R:   R*   RE   R@   RL   RN   R>   RW   R_   t   classmethodR!   Ra   Rc   RV   R8   Rb   Ri   RU   RJ   RG   Rq   Rr   R+   Ru   Rv   (    (    (    s.   ../Django//lib/python/django/forms/formsets.pyR   '   sF   											%	
										
			i   c         C@ st   | d k r t } n  t t | ƒ } i |  d 6| d 6| d 6| d 6| d 6| d 6} t |  j t d ƒ | f | ƒ S(	   u*   Return a FormSet for the given form class.u   formu   extrau	   can_orderu
   can_deleteu   max_numu   absolute_maxu   FormSetN(   R   t   DEFAULT_MAX_NUMt   maxR6   R   t   str(   R<   t   formsetR?   R[   RQ   R;   RC   t   attrs(    (    s.   ../Django//lib/python/django/forms/formsets.pyt   formset_factoryr  s    	c         C@ s0   t  } x# |  D] } | j ƒ  s t } q q W| S(   u3   Returns true if every formset in formsets is valid.(   R4   R8   R   (   t   formsetst   validR|   (    (    s.   ../Django//lib/python/django/forms/formsets.pyt	   all_valid  s
    N((   t
   __future__R    R   t   django.core.exceptionsR   t   django.formsR   t   django.forms.fieldsR   R   t   django.forms.utilR   t   django.forms.widgetsR   R   t   django.utils.encodingR	   t   django.utils.safestringR
   t   django.utilsR   t   django.utils.six.movesR   t   django.utils.translationR   Ro   t   __all__R   R   R   R]   Re   Ry   R   t   objectR   R   R   R~   R   (    (    (    s.   ../Django//lib/python/django/forms/formsets.pyt   <module>   s0   ÿ K	