
    hH                         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mZ d
ZdZdZdZdZdZdZd ZdZ G d de      Ze G d d             Z e dddddddfdZ!d Z"y)    )ValidationError)Form)BooleanFieldIntegerField)	ErrorList)HiddenInputNumberInput)cached_property)	html_safe)	mark_safe)gettextngettext)BaseFormSetformset_factory	all_validTOTAL_FORMSINITIAL_FORMSMIN_NUM_FORMSMAX_NUM_FORMSORDERDELETEi  c                   "     e Zd ZdZ fdZ xZS )ManagementFormz
    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                 0   t        t              | j                  t        <   t        t              | j                  t        <   t        dt              | j                  t
        <   t        dt              | j                  t        <   t        |    |i | y )N)widgetF)requiredr   )	r   r   base_fieldsTOTAL_FORM_COUNTINITIAL_FORM_COUNTMIN_NUM_FORM_COUNTMAX_NUM_FORM_COUNTsuper__init__)selfargskwargs	__class__s      X/var/www/html/ranktracker/api/venv/lib/python3.12/site-packages/django/forms/formsets.pyr#   zManagementForm.__init__"   su    -9-M)*/;;/O+, 0<US^/_+,/;US^/_+,$)&)    )__name__
__module____qualname____doc__r#   __classcell__)r'   s   @r(   r   r      s    
* *r)   r   c                   n   e Zd ZdZeZdddddedfdZd Zd Z	d Z
d Zd	 Zed
        Zd Zd Ze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ed        Zd Zed        Z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,y)'r   z;
    A collection of instances of the same Form class.
    Nzid_%sc                     |d uxs |d u| _         |xs | j                         | _        || _        |xs i | _        |xs i | _        || _        |xs i | _        || _        d | _	        d | _
        y N)is_boundget_default_prefixprefixauto_iddatafilesinitialform_kwargserror_class_errors_non_form_errors)r$   r6   r7   r5   r4   r8   r:   r9   s           r(   r#   zBaseFormSet.__init__4   su    D(=E,=9 7 7 9JB	[b
&,"& $r)   c                 "    | j                         S r1   as_tabler$   s    r(   __str__zBaseFormSet.__str__A   s    }}r)   c                 ,    t        | j                        S )z5Yield the forms in the order they should be rendered.)iterformsr@   s    r(   __iter__zBaseFormSet.__iter__D   s    DJJr)   c                      | j                   |   S )zAReturn the form at the given index, based on the rendering order.)rD   r$   indexs     r(   __getitem__zBaseFormSet.__getitem__H   s    zz%  r)   c                 ,    t        | j                        S r1   )lenrD   r@   s    r(   __len__zBaseFormSet.__len__L   s    4::r)   c                      y)zt
        Return True since all formsets have a management form which is not
        included in the length.
        T r@   s    r(   __bool__zBaseFormSet.__bool__O   s    
 r)   c                    | j                   rTt        | j                  | j                  | j                        }|j                         st        t        d      d      |S t        | j                  | j                  t        | j                         t        | j                         t        | j                  t        | j                  i      }|S )z4Return the ManagementForm instance for this FormSet.)r5   r4   z8ManagementForm data is missing or has been tampered withmissing_management_formcode)r5   r4   r8   )r2   r   r6   r5   r4   is_validr   _r   total_form_countr   initial_form_countr    min_numr!   max_numr$   forms     r(   management_formzBaseFormSet.management_formV   s     ==!$))T\\$++VD==?%PQ2   "$,,t{{ $"7"7"9"D$;$;$="DLL"DLL	U D r)   c                 d   | j                   r1t        | j                  j                  t           | j
                        S | j                         }t        || j                        | j                  z   }|| j                  cxkD  rdk\  r|}|S  || j                  cxkD  rdk\  rn |S | j                  }|S )z1Return the total number of forms in this FormSet.r   )r2   minr\   cleaned_datar   absolute_maxrW   maxrX   extrarY   )r$   initial_formstotal_formss      r(   rV   zBaseFormSet.total_form_counti   s    ==
 t++889IJDL]L]^^ 335MmT\\:TZZGK t||0q0+ 	 1t||0q0 #llr)   c                     | j                   r| j                  j                  t           S | j                  rt        | j                        nd}|S )z=Return the number of forms that are required in this FormSet.r   )r2   r\   r_   r   r8   rK   )r$   rc   s     r(   rW   zBaseFormSet.initial_form_count|   s?    ==''445GHH 26C-1Mr)   c           
          t        | j                               D cg c]$  } | j                  |fi | j                  |      & c}S c c}w )z+Instantiate forms at first property access.)rangerV   _construct_formget_form_kwargs)r$   is     r(   rD   zBaseFormSet.forms   sO     40023
 !D  >d&:&:1&=>
 	
 
s   )Ac                 6    | 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.
        )r9   copyrG   s     r(   ri   zBaseFormSet.get_form_kwargs   s     $$&&r)   c                    | j                   | j                  |      | j                  dd}| j                  r| j                  |d<   | j
                  |d<   | j                  rd|vr	 | j                  |   |d<   || j                         k\  r|| j                  k\  rd|d<   |j                  |        | j                  di |}| j                  ||       |S # t        $ r Y iw xY w)	z;Instantiate and return the i-th form instance in a formset.F)r5   r4   r:   use_required_attributer6   r7   r8   Tempty_permittedrN   )r5   
add_prefixr:   r2   r6   r7   r8   
IndexErrorrW   rX   updater[   
add_fields)r$   rj   r&   defaultsr[   s        r(   rh   zBaseFormSet._construct_form   s     ||ooa(++ ',
 ==#yyHV $

HW<<IV3&*ll1o#
 ''))a4<<.?*.H&'tyy$8$a   s   &C 	C"!C"c                 <    | j                   d| j                          S )z7Return a list of all the initial forms in this formset.NrD   rW   r@   s    r(   rc   zBaseFormSet.initial_forms   s     zz4422455r)   c                 <    | j                   | j                         d S )z5Return a list of all the extra forms in this formset.Nrv   r@   s    r(   extra_formszBaseFormSet.extra_forms   s     zz$113455r)   c                      | j                   d| j                  | j                  d      ddd| j                  d       }| j	                  |d        |S )N
__prefix__TF)r5   r4   ro   rn   rN   )r[   r5   rp   ri   rs   rZ   s     r(   
empty_formzBaseFormSet.empty_form   sZ    tyy 
LL??<0 #(	

 ""4(
 	d#r)   c                     | j                         s"t        d| j                  j                  z        | j                  D cg c]  }|j
                   c}S c c}w )zX
        Return a list of form.cleaned_data dicts for every form in self.forms.
        z+'%s' object has no attribute 'cleaned_data')rT   AttributeErrorr'   r*   rD   r_   rZ   s     r(   r_   zBaseFormSet.cleaned_data   sI    
 }} !NQUQ_Q_QhQh!hii.2jj9d!!999s   Ac                    | j                         r| j                  sg S t        | d      sg | _        t	        d| j                               D ]b  }| j                  |   }|| j                         k\  r|j                         s6| j                  |      sH| j                  j                  |       d | j                  D cg c]  }| j                  |    c}S c c}w )z:Return a list of forms that have been marked for deletion._deleted_form_indexesr   )rT   
can_deletehasattrr   rg   rV   rD   rW   has_changed_should_delete_formappend)r$   rj   r[   s      r(   deleted_formszBaseFormSet.deleted_forms   s     }}dooI t45)+D&1d3356 9zz!}//11$:J:J:L++D1..55a89 (,'A'AB!

1BBBs   ?Cc                 |   | j                         r| j                  s"t        d| j                  j                  z        t        | d      sg | _        t        d| j                               D ]  }| j                  |   }|| j                         k\  r|j                         s6| j                  r| j                  |      rT| j                  j                  ||j                  t            f        d }| j                  j#                  |       | j                  D cg c]  }| j                  |d       c}S c c}w )z
        Return a list of form in the order specified by the incoming data.
        Raise an AttributeError if ordering is not allowed.
        z,'%s' object has no attribute 'ordered_forms'	_orderingr   c                     | d   yd| d   fS )N   )r   r   r   rN   )ks    r(   compare_ordering_keyz7BaseFormSet.ordered_forms.<locals>.compare_ordering_key  s    Q4<!1Q4y r)   )key)rT   	can_orderr}   r'   r*   r   r   rg   rV   rD   rW   r   r   r   r   r_   ORDERING_FIELD_NAMEsort)r$   rj   r[   r   s       r(   ordered_formszBaseFormSet.ordered_forms   s    }}dnn !ORVR`R`RiRi!ijj
 t[)DN1d3356 Szz!}//11$:J:J:L??t'?'?'E%%q$*;*;<O*P&QRS! NN$89 +/..9Q

1Q4 999s   D9c                      y)Nr[   rN   clss    r(   r3   zBaseFormSet.get_default_prefix
  s    r)   c                     | j                   S r1   )ordering_widgetr   s    r(   get_ordering_widgetzBaseFormSet.get_ordering_widget  s    """r)   c                 R    | j                   | j                          | j                   S )z
        Return an ErrorList of errors that aren't associated with a particular
        form -- i.e., from formset.clean(). Return an empty ErrorList if there
        are none.
        )r<   
full_cleanr@   s    r(   non_form_errorszBaseFormSet.non_form_errors  s&       (OO$$$r)   c                 R    | j                   | j                          | j                   S )z:Return a list of form.errors for every form in self.forms.)r;   r   r@   s    r(   errorszBaseFormSet.errors  s"     <<OO||r)   c                 n    t        | j                               t        d | j                  D              z   S )z<Return the number of errors across all forms in the formset.c              3   2   K   | ]  }t        |        y wr1   )rK   ).0form_errorss     r(   	<genexpr>z0BaseFormSet.total_error_count.<locals>.<genexpr>&  s     @[K @s   )rK   r   sumr   r@   s    r(   total_error_countzBaseFormSet.total_error_count#  s1    4'')*@DKK@@A 	Ar)   c                 B    |j                   j                  t        d      S )z7Return whether or not the form was marked for deletion.F)r_   getDELETION_FIELD_NAMErZ   s     r(   r   zBaseFormSet._should_delete_form(  s      $$%8%@@r)   c                     | j                   syd}| j                   t        d| j                               D ]B  }| j                  |   }| j
                  r| j                  |      r0||j                         z  }D |xr | j                          S )z1Return True if every form in self.forms is valid.FTr   )	r2   r   rg   rV   rD   r   r   rT   r   )r$   forms_validrj   r[   s       r(   rT   zBaseFormSet.is_valid,  s    }} q$//12 	+A::a=D4#;#;D#A 4==?*K	+ 94#7#7#999r)   c                 0   g | _         | j                         | _        d}| j                  syt	        d| j                               D ]~  }| j                  |   }|j                         s|| j                         k\  r|dz  }|j                  }| j                  r| j                  |      rd| j                   j                  |        	 | j                  r3| j                         t        | j                        z
  | j                   kD  s*| j"                  j$                  t&           | j(                  kD  r/t+        t-        dd| j                         | j                   z  d      | j.                  re| j                         t        | j                        z
  |z
  | j0                  k  r/t+        t-        dd| j0                        | j0                  z  d      | j3                          y# t*        $ r*}| j                  |j4                        | _        Y d}~yd}~ww xY w)	ze
        Clean all of self.data and populate self._errors and
        self._non_form_errors.
        r   Nr   z Please submit %d or fewer forms.too_many_formsrR   zPlease submit %d or more forms.too_few_forms)r;   r:   r<   r2   rg   rV   rD   r   rW   r   r   r   r   validate_maxrK   r   rY   r\   r_   r   r`   r   r   validate_minrX   clean
error_list)r$   empty_forms_countrj   r[   r   es         r(   r   zBaseFormSet.full_clean>  s   
  $ 0 0 2}}q$//12 
	-A::a=D##%!t/F/F/H*H!Q&! ++K4#;#;D#ALL,
	-	C!!))+c$2D2D.EET((556FG$J[J[[%h66'FHL'U * 
 !!))+c$2D2D.EEHYY\`\h\hh%h55t||'EGK||'T )* *
 JJL 	C$($4$4Q\\$BD!!	Cs   DG" "	H+ HHc                      y)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()
        NrN   r@   s    r(   r   zBaseFormSet.cleanh  s     	r)   c                 &    t        d | D              S )z5Return True if data in any form differs from initial.c              3   <   K   | ]  }|j                           y wr1   )r   r   r[   s     r(   r   z*BaseFormSet.has_changed.<locals>.<genexpr>s  s     7$4##%7   )anyr@   s    r(   r   zBaseFormSet.has_changedq  s    7$777r)   c                    | j                   r|N|| j                         k  r;t        t        d      |dz   d| j	                               |j
                  t        <   n6t        t        d      d| j	                               |j
                  t        <   | j                  r(t        t        d      d      |j
                  t        <   yy)	z8A hook for adding extra fields on to each form instance.NOrderr   F)labelr8   r   r   )r   r   r   Delete)r   r   )
r   rW   r   rU   r   fieldsr   r   r   r   )r$   r[   rH   s      r(   rs   zBaseFormSet.add_fieldsu  s    >> UT-D-D-F%F3?G*!AI"335	4/0 4@G*"3354/0
 ??/;!H+X]/^DKK+, r)   c                 $    | j                   d|S )N-)r4   rG   s     r(   rp   zBaseFormSet.add_prefix  s    ++u--r)   c                     | j                   r| j                   d   j                         S | j                  j                         S )zv
        Return True if the formset needs to be multipart, i.e. it
        has FileInput, or False otherwise.
        r   )rD   is_multipartr{   r@   s    r(   r   zBaseFormSet.is_multipart  s5    
 ::::a=--//??//11r)   c                 x    | j                   r| j                   d   j                  S | j                  j                  S )Nr   )rD   mediar{   r@   s    r(   r   zBaseFormSet.media  s/     ::::a=&&&??(((r)   c                 z    dj                  d | D              }t        t        | j                        dz   |z         S )zLReturn this formset rendered as HTML <tr>s -- excluding the <table></table>. c              3   <   K   | ]  }|j                           y wr1   r>   r   s     r(   r   z'BaseFormSet.as_table.<locals>.<genexpr>  s     :T:r   
joinr   strr\   r$   rD   s     r(   r?   zBaseFormSet.as_table  s8    
 :T::T112T9EABBr)   c                 z    dj                  d | D              }t        t        | j                        dz   |z         S )z*Return this formset rendered as HTML <p>s.r   c              3   <   K   | ]  }|j                           y wr1   )as_pr   s     r(   r   z#BaseFormSet.as_p.<locals>.<genexpr>  s     66r   r   r   r   s     r(   r   zBaseFormSet.as_p  s6    666T112T9EABBr)   c                 z    dj                  d | D              }t        t        | j                        dz   |z         S )z+Return this formset rendered as HTML <li>s.r   c              3   <   K   | ]  }|j                           y wr1   )as_ulr   s     r(   r   z$BaseFormSet.as_ul.<locals>.<genexpr>  s     7$7r   r   r   r   s     r(   r   zBaseFormSet.as_ul  s6    7$77T112T9EABBr)   )-r*   r+   r,   r-   r	   r   r   r#   rA   rE   rI   rL   rO   r
   r\   rV   rW   rD   ri   rh   propertyrc   rx   r{   r_   r   r   classmethodr3   r   r   r   r   r   rT   r   r   r   rs   rp   r   r   r?   r   r   rN   r)   r(   r   r   -   s    "O gd9$% !  $& 
 
'8 6 6 6 6 	 	 : : C C" #: #:J   # #%  A
A:$(CT8_(.2 ) )CC
Cr)   r   r   FNc	           
          |t         }|t        }|t        z   }	| ||||||	||d	}
t        | j                  dz   |f|
      S )z*Return a FormSet for the given form class.)	r[   rb   r   r   rX   rY   r`   r   r   FormSet)DEFAULT_MIN_NUMDEFAULT_MAX_NUMtyper*   )r[   formsetrb   r   r   rY   r   rX   r   r`   attrss              r(   r   r     sb     !! _,L $$$
E 	)G:u==r)   c                 >    d}| D ]  }||j                         z  } |S )z8Validate every formset and return True if all are valid.T)rT   )formsetsvalidr   s      r(   r   r     s.    E $!!##$Lr)   )#django.core.exceptionsr   django.formsr   django.forms.fieldsr   r   django.forms.utilsr   django.forms.widgetsr   r	   django.utils.functionalr
   django.utils.htmlr   django.utils.safestringr   django.utils.translationr   rU   r   __all__r   r   r    r!   r   r   r   r   r   r   r   r   rN   r)   r(   <module>r      s    2  : ( 9 3 ' - ;
9 ! $ $ $     *T *" AC AC ACH #.Q%$d u>4r)   