
    h3                     \   d Z ddlZddlmZ ddlmZmZmZmZ ddl	m
Z
mZ ddlmZmZ ddlmZmZ ddlmZ dd	lmZmZmZmZ dd
lmZ ddlmZmZ ddlmZm Z! dZ"dZ#d4dZ$d4dZ%d Z&	 	 	 	 d5dddZ' G d d      Z( G d de      Z) G d de      Z* G d de*e)      Z+e+dddddddddf
dZ, G d d e      Z-e+de-d!d"d"ddddd"dddddd"dfd#Z. G d$ d%e-      Z/d6d&Z0e+e/dddd'd"ddddd"dddddd"dfd(Z1 G d) d*e      Z2 G d+ d,      Z3 G d- d.      Z4 G d/ d0e
      Z5 G d1 d2e5      Z6d3 Z7y)7z[
Helper functions for creating Form classes from Django models
and database field objects.
    N)chain)NON_FIELD_ERRORS
FieldErrorImproperlyConfiguredValidationError)ChoiceFieldField)BaseFormDeclarativeFieldsMetaclass)BaseFormSetformset_factory)	ErrorList)HiddenInputMultipleHiddenInputRadioSelectSelectMultiple)RemovedInDjango40Warning)capfirstget_text_list)gettextgettext_lazy)	ModelFormBaseModelFormmodel_to_dictfields_for_modelModelChoiceFieldModelMultipleChoiceField
ALL_FIELDSBaseModelFormSetmodelformset_factoryBaseInlineFormSetinlineformset_factorymodelform_factory__all__c                 P   ddl m} |j                  }| j                  }g }|j                  D ]O  }|j
                  r$t        ||j                        s|j                  |vr5||j                  |vrF|r|j                  |v rW|j                         r| |j                     j                  j                  j                  | j                  | j                  | j                  |j                              r@|j!                  |j                        | |j                     j                  j"                  v rt        ||j$                        r|j'                  |       1|j)                  |||j                            R |D ]!  }|j)                  |||j                            # |S )z
    Construct and return a model instance from the bound ``form``'s
    ``cleaned_data``, but do not save the returned instance to the database.
    r   )models)	django.dbr&   _metacleaned_datafieldseditable
isinstance	AutoFieldnamehas_defaultfieldwidgetvalue_omitted_from_datadatafiles
add_prefixgetempty_values	FileFieldappendsave_form_data)	forminstancer*   excluder&   optsr)   file_field_listfs	            V/var/www/html/ranktracker/api/venv/lib/python3.12/site-packages/django/forms/models.pyconstruct_instancerB       sa   
 !>>D$$LO[[ =zzZ6+;+;<66-!&&"6qvv( MMOL%%==diiUYUdUdefekekUlmQVV$QVV(:(:(G(GG a))*""1%X|AFF';<+=.  9	<#789 O    c                    | j                   }i }t        |j                  |j                  |j                        D ]P  }t        |dd      s||j                  |vr"|r|j                  |v r3|j                  |       ||j                  <   R |S )a  
    Return a dict containing the data in ``instance`` suitable for passing as
    a Form's ``initial`` keyword argument.

    ``fields`` is an optional list of field names. If provided, return only the
    named.

    ``exclude`` is an optional list of field names. If provided, exclude the
    named from the returned dict, even if they are listed in the ``fields``
    argument.
    r+   F)r(   r   concrete_fieldsprivate_fieldsmany_to_manygetattrr.   value_from_object)r<   r*   r=   r>   r3   r@   s         rA   r   r   I   s     >>DD4'')<)<d>O>OP 5q*e,!&&"6qvv(**84QVV5 KrC   c                     t        | d      rAt        | d      r4| j                         }|!| j                  j                  |      | _        yyyy)z=Apply limit_choices_to to the formfield's queryset if needed.querysetget_limit_choices_toN)hasattrrL   rK   complex_filter)	formfieldlimit_choices_tos     rA   #apply_limit_choices_to_to_formfieldrQ   b   sS    y*%')=S*T$99;'!*!3!3!B!BCS!TI ( +U%rC   Tapply_limit_choices_toc
                   i }g }| j                   }ddlm} |j                  D cg c]  }t	        ||      s| }}t        t        |j                  ||j                              D ]  }t        |dd      sG|D|j                  |v r6||j                  |vr&t        d|j                  d| j                  d      X||j                  |vri|r|j                  |v rzi }|r |j                  |v r||j                     |d<   |t        k(  s|r|j                  |v rd	|d
<   |r |j                  |v r||j                     |d<   |r |j                  |v r||j                     |d<   |r |j                  |v r||j                     |d<   |	r |j                  |	v r|	|j                     |d<   | |j                  di |}nt        |      st!        d       ||fi |}|r|
rt#        |       |||j                  <   |j%                  |j                          |r)|D ci c]  }|r||vr||vr||j'                  |        }}|S c c}w c c}w )a4  
    Return a dictionary containing form fields for the given model.

    ``fields`` is an optional list of field names. If provided, return only the
    named fields.

    ``exclude`` is an optional list of field names. If provided, exclude the
    named fields from the returned fields, even if they are listed in the
    ``fields`` argument.

    ``widgets`` is a dictionary of model field names mapped to a widget.

    ``formfield_callback`` is a callable that takes a model field and returns
    a form field.

    ``localized_fields`` is a list of names of fields which should be localized.

    ``labels`` is a dictionary of model field names mapped to a label.

    ``help_texts`` is a dictionary of model field names mapped to a help text.

    ``error_messages`` is a dictionary of model field names mapped to a
    dictionary of error messages.

    ``field_classes`` is a dictionary of model field names mapped to a form
    field class.

    ``apply_limit_choices_to`` is a boolean indicating if limit_choices_to
    should be applied to a field's queryset.
    r   )r	   r+   F'z' cannot be specified for z) model form as it is a non-editable fieldr1   Tlocalizelabel	help_texterror_messages
form_classz1formfield_callback must be a function or callable )r(   django.db.modelsr	   rF   r,   sortedr   rE   rG   rH   r.   r   __name__r   rO   callable	TypeErrorrQ   r9   r6   )modelr*   r=   widgetsformfield_callbacklocalized_fieldslabels
help_textsrY   field_classesrS   
field_dictignoredr>   
ModelFieldr@   sortable_private_fieldskwargsrO   s                      rA   r   r   j   sh   D JG;;D4*.*=*=[QAzAZq[[E$..0GIZIZ[\ (#q*e,"qvv'7_g(= 0  !&&"6qvv(qvv(&qvvF8z).>166M]C]!%F:aff&$QVVnF7O!&&J.",QVV"4F;aff6'5aff'=F#$QVV}4#0#8F< %#-f-I,-OPP*177I%3I>!*JqvvNN166"Q(#R *0
%&q/Qg5E z~~a  

 
 _ \V
s   I
I
##Ic                       e Zd ZddZy)ModelFormOptionsNc                 H   t        |dd       | _        t        |dd       | _        t        |dd       | _        t        |dd       | _        t        |dd       | _        t        |dd       | _        t        |dd       | _        t        |dd       | _        t        |d	d       | _	        y )
Nra   r*   r=   rb   rd   re   rf   rY   rg   )
rH   ra   r*   r=   rb   rd   re   rf   rY   rg   )selfoptionss     rA   __init__zModelFormOptions.__init__   s    Wgt4
gx6w	48w	48 '1CT Jgx6!'<>%g/?F$WotDrC   N)r^   
__module____qualname__rr   r[   rC   rA   rn   rn      s    	ErC   rn   c                        e Zd Z fdZ xZS )ModelFormMetaclassc                    d }|D ]=  }t        |d      st        |j                  d      s'|j                  j                  } n |j                  d|      }t        |   | |||      }|t        fk(  r|S t        t        |dd             x}|_	        dD ]F  }	t        ||	      }
t        |
t              s |
t        k7  s*d|j                  |	|
dz  }t        |       |j                  rE|j                   |j"                  t%        d|z        |j                   t        k(  rd |_        t'        |j                  |j                   |j"                  |j(                  ||j*                  |j,                  |j.                  |j0                  |j2                  d      }|j5                         D ch c]
  \  }}|r	| }}}|j7                  |j8                        }|r7d	}|d
j;                  |      |j                  j                  fz  }t=        |      |j?                  |j8                         n|j8                  }||_         |S c c}}w )NMetarc   )r*   r=   rd   zP%(model)s.Meta.%(opt)s cannot be a string. Did you mean to type: ('%(value)s',)?)ra   optvaluez|Creating a ModelForm without either the 'fields' attribute or the 'exclude' attribute is prohibited; form %s needs updating.FrR   z&Unknown field(s) (%s) specified for %sz, )!rM   ry   rc   popsuper__new__r   rn   rH   r(   r,   strr   r^   r`   ra   r*   r=   r   r   rb   rd   re   rf   rY   rg   items
differencedeclared_fieldsjoinr   updatebase_fields)mcsr.   basesattrsbase_formfield_callbackbrc   	new_classr>   rz   r{   msgr*   kvnone_model_fieldsmissing_fieldsmessage	__class__s                     rA   r~   zModelFormMetaclass.__new__   s2   "& 	Aq&!gaff6J&K*+&&*C*C'	
 #YY';=TUGOCue<	]$$!1')VT2R!SSy
 = 		%CD#&E%%%:*=?$-$6$6"%$)B  n$		% ::{{"t||';*&(,-  {{j( #%

DKKt||"D$9$94;;!4!4d6H6H',F 06||~ Gtq!Q G G.99):S:STNB!TYY~%>%)ZZ%8%8%: : )) MM)334..F &	 !Hs   .
I9I)r^   rt   ru   r~   __classcell__r   s   @rA   rw   rw      s    D DrC   rw   c                   p     e Zd Zdddddedddddf fd	Zd Zd Zd Zd Zd	 Z	d
 Z
ddZde_         xZS )r   Nid_%sFc                    | j                   }|j                  t        d      |	|j                         | _        i }n(|	| _        t	        |	|j
                  |j                        }||j                  |       d| _        t        | )  |||||||||
|
       | j
                  j                         D ]  }t        |        y )Nz'ModelForm has no model class specified.F)use_required_attributerenderer)r(   ra   
ValueErrorr<   r   r*   r=   r   _validate_uniquer}   rr   valuesrQ   )rp   r3   r4   auto_idprefixinitialerror_classlabel_suffixempty_permittedr<   r   r   r>   object_datarO   r   s                  rA   rr   zBaseModelForm.__init__  s     zz::FGG JJLDMK$DM'$++t||LKw' !&%&+{/BX 	 	

 ++- 	;I/	:	;rC   c                 "   g }| j                   j                  j                  D ]h  }|j                  }|| j                  vr|j	                  |j                         :| j                  j                  r4|| j                  j                  vr|j	                  |j                         | j                  j
                  r4|| j                  j
                  v r|j	                  |j                         || j                  v r|j	                  |j                         | j                  |   }| j                  j                  |      }|j                  r0|j                  r>||j                  v sN|j	                  |j                         k |S )z
        For backwards-compatibility, exclude several types of fields from model
        validation. See tickets #12507, #12521, #12553.
        )r<   r(   r*   r.   r9   r=   _errorsr)   r6   blankrequiredr7   )rp   r=   r@   r0   
form_fieldfield_values         rA   _get_validation_exclusionsz(BaseModelForm._get_validation_exclusions6  s&   
  $$++ 	+AFFE DKK'qvv&
 ""uDJJ4E4E'Eqvv&##1C1C(Cqvv& $,,&qvv& "[[/
"//33E:wwz':':{jNeNe?eNN166*=	+> rC   c                 (    d| _         | j                  S )NT)r   r)   rp   s    rA   cleanzBaseModelForm.clean_  s     $   rC   c                    | j                   }t        |d      r|j                  }nt        |i}|j	                         D ]  \  }}|t        k(  r2|j
                  r&t        |j
                  v r|j
                  t           }n)|| j                  v r| j                  |   j
                  }nj|D ]6  }t        |t              s|j                  |v s#||j                     |_
        8  | j                  d |       y )N
error_dict)r(   rM   r   r   r   rY   r*   r,   r   coder   	add_error)rp   errorsr>   r   r0   messagesrY   r   s           rA   _update_errorszBaseModelForm._update_errorsc  s     zz 6<(**J*F3J)//1 	COE8))d.A.A$(;(;;!%!4!45E!F$++%!%U!3!B!B# Cw86&4W\\&BGOC	C 	tV$rC   c                 :   | j                   }| j                         }| j                  j                         D ]'  \  }}t	        |t
              s|j                  |       ) 	 t        | | j                  |j                  |j                        | _        	 | j                  j                  |d       | j                  r| j                          y y # t        $ r}| j                  |       Y d }~[d }~ww xY w# t        $ r}| j                  |       Y d }~dd }~ww xY w)NF)r=   validate_unique)r(   r   r*   r   r,   InlineForeignKeyFieldr9   rB   r<   r=   r   r   
full_cleanr   r   )rp   r>   r=   r.   r0   es         rA   _post_cleanzBaseModelForm._post_clean  s    zz113  ;;,,. 	%KD%%!67t$	%	#.tT]]DKKQUQ]Q]^DM	#MM$$We$L
     " !  	#""	#
  	#""	#s0   "1C C6 	C3C..C36	D?DDc                     | j                         }	 | j                  j                  |       y# t        $ r}| j	                  |       Y d}~yd}~ww xY w)z
        Call the instance's validate_unique() method and update the form's
        validation errors if any were raised.
        r=   N)r   r<   r   r   r   )rp   r=   r   s      rA   r   zBaseModelForm.validate_unique  sM    
 113	#MM))'): 	#""	#s   / 	AAAc                    | j                   }| j                  j                  }| j                  j                  }| j                  j                  }t        |j                  |j                        D ]i  }t        |d      s|r|j                  |vr!|r|j                  |v r2|j                  |v sA|j                  | j                  ||j                            k y)zS
        Save the many-to-many fields and generic relations for this form.
        r:   N)r)   r(   r=   r*   r<   r   rG   rF   rM   r.   r:   )rp   r)   r=   r*   r>   r@   s         rA   	_save_m2mzBaseModelForm._save_m2m  s     ((**$$""}}"" t(($*=*=> 	FA1./!&&.166W,vv%  QVV0DE	FrC   Tc                 h   | j                   rRt        d| j                  j                  j                  d| j                  j
                  j                  rdndd      |r6| j                  j                          | j                          | j                  S | j                  | _	        | j                  S )z
        Save this form's self.instance object if commit=True. Otherwise, add
        a save_m2m() method to the form which can be called after the instance
        is saved manually at a later time. Return the model instance.
        zThe z could not be createdchangedz" because the data didn't validate.)
r   r   r<   r(   object_name_stateaddingsaver   save_m2m)rp   commits     rA   r   zBaseModelForm.save  s     ;;MM''33!%!5!5!<!<I)K  MM NN
 }} !NNDM}}rC   T)r^   rt   ru   r   rr   r   r   r   r   r   r   r   alters_datar   r   s   @rA   r   r     sT     gd94!&d;:'R!%8#<	#F*. DrC   r   c                       e Zd Zy)r   N)r^   rt   ru   r[   rC   rA   r   r     s    rC   r   )	metaclassc                    d| i}|||d<   |||d<   |||d<   |||d<   |||d<   |||d<   |	|	|d	<   |
|
|d
<   t        |d      r|j                  fnd}t        d||      }|rt        |      |_        | j
                  dz   }||d}t        |dd      t        |dd      t        d       t        |      ||f|      S )al  
    Return a ModelForm containing form fields for the given model. You can
    optionally pass a `form` argument to use as a starting point for
    constructing the ModelForm.

    ``fields`` is an optional list of field names. If provided, include only
    the named fields in the returned fields. If omitted or '__all__', use all
    fields.

    ``exclude`` is an optional list of field names. If provided, exclude the
    named fields from the returned fields, even if they are listed in the
    ``fields`` argument.

    ``widgets`` is a dictionary of model field names mapped to a widget.

    ``localized_fields`` is a list of names of fields which should be localized.

    ``formfield_callback`` is a callable that takes a model field and returns
    a form field.

    ``labels`` is a dictionary of model field names mapped to a label.

    ``help_texts`` is a dictionary of model field names mapped to a help text.

    ``error_messages`` is a dictionary of model field names mapped to a
    dictionary of error messages.

    ``field_classes`` is a dictionary of model field names mapped to a form
    field class.
    ra   Nr*   r=   rb   rd   re   rf   rY   rg   ry   r[   Form)ry   rc   zZCalling modelform_factory without defining 'fields' or 'exclude' explicitly is prohibited.)rM   ry   typestaticmethodrc   r^   rH   r   )ra   r;   r*   r=   rc   rb   rd   re   rf   rY   rg   r   r   ry   
class_nameform_class_attrss                   rA   r#   r#     s0   N eE h"i"i#$4 ! h(l!"0 !.o $D&1TYYLrEu%D"./A"B&(J 0
 	h%-D)T*2"2
 	
 4:j4'+;<<rC   c                        e Zd ZdZdZ e       Z	 	 ddd fdZ fdZd Z	d Z
 fdZd	 ZddZddZddZddZd
e_        d Zd Zd Zd Zd ZddZddZ fdZ xZS )r   zO
    A ``FormSet`` for editing a queryset and/or adding new objects to it.
    N)r   c          	      N    || _         || _        t        |   di ||||d| y )N)r3   r4   r   r   r[   )rK   initial_extrar}   rr   )	rp   r3   r4   r   r   rK   r   rl   r   s	           rA   rr   zBaseModelFormSet.__init__9  s2     $jD5WX^ibhijrC   c                 j    | j                   st        | j                               S t        |          S )z=Return the number of forms that are required in this FormSet.)is_boundlenget_querysetr}   initial_form_countrp   r   s    rA   r   z#BaseModelFormSet.initial_form_count?  s,    }}t((*++w)++rC   c                     t        | d      s.| j                         D ci c]  }|j                  | c}| _        | j                  j	                  |      S c c}w )N_object_dict)rM   r   pkr   r6   )rp   r   os      rA   _existing_objectz!BaseModelFormSet._existing_objectE  sM    t^,262C2C2E FQq FD  $$R(( !Gs   Ac                     |j                   '|j                   j                         }|j                   '|j                  S )z
        If the field is a related field, fetch the concrete field's (that
        is, the ultimate pointed-to field's) to_python.
        )remote_fieldget_related_field	to_pythonrp   r0   s     rA   _get_to_pythonzBaseModelFormSet._get_to_pythonJ  s<    
   ,&&88:E   ,rC   c                    || j                         k  }|r| j                  r| j                  |      d| j                  j                  j
                  j                  }	 | j                  |   }| j                  | j                  j                  j
                        }	  ||      }| j                  |      |d<   nG| j                         |   |d<   n0| j                  r$	 | j                  || j                         z
     |d<   t        | @  |fi |}|r<d|j"                  | j                  j                  j
                  j                     _        |S # t        $ r Y [w xY w# t        $ r Y jw xY w# t        $ r Y yw xY w)N-r<   r   T)r   r   r5   ra   r(   r   r.   r3   r   r   r   KeyErrorr   r   
IndexErrorr}   _construct_formr*   r   )	rp   irl   pk_requiredpk_keyr   r   r;   r   s	           rA   r   z BaseModelFormSet._construct_formS  sq   $1133}}$(OOA$6

8H8H8K8K8P8PQG6*B !% 3 3DJJ4D4D4G4G HIG&r] .2-B-B2-Fz*%)%6%6%8%;z"$($6$6q4;R;R;T7T$Uy! w&q3F3=ADKK

((++001:# +      (  s6   "E$ !E "#E3 	E! E!$	E0/E03	E?>E?c                 D   t        | d      s| j                  | j                  }n$| j                  j                  j	                         }|j
                  s9|j                  | j                  j                  j                  j                        }|| _
        | j                  S )N	_queryset)rM   rK   ra   _default_managerr   orderedorder_byr(   r   r.   r   )rp   qss     rA   r   zBaseModelFormSet.get_querysetu  sw    t[)}}(]]ZZ00==?
 ::[[!1!1!4!4!9!9:
  DN~~rC   Tc                 &    |j                  |      S )z8Save and return a new model instance for the given form.r   r   )rp   r;   r   s      rA   save_newzBaseModelFormSet.save_new      yyy''rC   c                 &    |j                  |      S )z>Save and return an existing model instance for the given form.r   r   )rp   r;   r<   r   s       rA   save_existingzBaseModelFormSet.save_existing  r   rC   c                 *    |r|j                          yy)z#Deletes an existing model instance.N)delete)rp   objr   s      rA   delete_existingz BaseModelFormSet.delete_existing  s    JJL rC   c                 t     |sg  _          fd}| _         j                  |       j                  |      z   S )z
        Save model instances for every form, adding and changing instances
        as necessary, and return the list of instances.
        c                  H    j                   D ]  } | j                           y rs   )saved_formsr   )r;   rp   s    rA   r   z'BaseModelFormSet.save.<locals>.save_m2m  s!     ,, $DMMO$rC   )r  r   save_existing_objectssave_new_objects)rp   r   r   s   `  rA   r   zBaseModelFormSet.save  s?    
 !D$ %DM))&1D4I4I&4QQQrC   c                 $    | j                          y rs   )r   r   s    rA   r   zBaseModelFormSet.clean  s    rC   c                 b    t               }t               } j                  } j                  D cg c]  }|j                         s||vs| }}|D ]S  j	                         }j
                  j                  |      \  }}|j                  |       |j                  |       U g }	|D ]  \  }
}t               }|D ]   fd|D        }t        d |D              }|s$d |vs)||v rt|	j                   j                  |              j                   j                         g      j                  t        <   |D ]  }|j                  v sj                  |=   |j!                  |         |D ]"  }t               }|\  }
}}}|D ]  j                  sj                  |   !j                  |   1|dk(  r3j                  |   }|j"                  |j$                  |j&                  f}nt)        j                  |   |      f}j                  |   f|z   }||v r^|	j                   j+                  |              j                   j                         g      j                  t        <   j                  |= |j!                  |        % |	rt-        |	      y c c}w )Nr   c              3   x   K   | ]1  }|j                   v r!|j                  v r|nj                   |    3 y wrs   )r)   unique_fields).0r0   r;   rp   s     rA   	<genexpr>z3BaseModelFormSet.validate_unique.<locals>.<genexpr>  sD      $:K:K1K #d&8&88Ed>O>OPU>VVs   7:c              3      K   | ]=  }t        |d       r|j                         nt        |t              rt	        |      n| ? yw)_get_pk_valN)rM   r  r,   listtuple)r  ds     rA   r	  z3BaseModelFormSet.validate_unique.<locals>.<genexpr>  sA      !  ! (/q-'@AMMO%/4%8q!s   AAdate)setdeleted_formsformsis_validr   r<   _get_unique_checksr   r  r9   get_unique_error_messager   get_form_errorr   r   r)   addyearmonthdayrH   get_date_error_messager   )rp   all_unique_checksall_date_checksforms_to_deleter;   valid_formsr=   unique_checksdate_checksr   uclassunique_check	seen_datarow_datar0   
date_checklookup
unique_forr  	date_datar3   s   `   `                rA   r   z BaseModelFormSet.validate_unique  s   E%,,(,

fdmmo$VeJetff 	0D557G)-)I)IRY)I)Z&M;$$]3"";/		0 $5 	, FLI# ,!-
 ! ! %-	!  H 49, d&C&CL&QR9=9I9I4K^K^K`Ja9b%56%1 =E$(9(99$($5$5e$<= MM(+3,	,: * 	(JI0:-FFE:# (%%$*;*;E*B*N))*5A'#00<%)YY

DHH$E	 &-T->->z-JF%S$U	 --e46BDy( d&A&A*&MN9=9I9I4K^K^K`Ja9b%56 --e4MM$'-(	(6 !&)) C gs   J,J,J,c                     t        |      dk(  rt        d      d|d   iz  S t        d      dt        |t        d            iz  S )N   z0Please correct the duplicate data for %(field)s.r0   r   zFPlease correct the duplicate data for %(field)s, which must be unique.and)r   r   r   _)rp   r#  s     rA   r  z)BaseModelFormSet.get_unique_error_message  s\    |!MNaR   cd|QuX>h  rC   c                 J    t        d      |d   |d   t        |d         dz  S )NzoPlease correct the duplicate data for %(field_name)s which must be unique for the %(lookup)s in %(date_field)s.      r+  )
field_name
date_fieldr'  )r   r   )rp   r&  s     rA   r  z'BaseModelFormSet.get_date_error_message  s;    I
 %Q-$Q-*Q-(

 	
rC   c                     t        d      S )Nz*Please correct the duplicate values below.)r   r   s    rA   r  zBaseModelFormSet.get_form_error  s    CDDrC   c                    g | _         g | _        | j                  sg S g }| j                  }| j                  D ]  }|j                  }|j
                  ||v r/| j                  j                  |       | j                  ||       O|j                         s`| j                   j                  ||j                  f       |j                  | j                  |||             |r| j                  j                  |        |S Nr   )changed_objectsdeleted_objectsinitial_formsr  r<   r   r9   r   has_changedchanged_datar   r  )rp   r   saved_instancesr  r;   r   s         rA   r  z&BaseModelFormSet.save_existing_objects  s    !!!!I,,&& 	2D--C
 vv~&$$++C0$$S$8!!#$$++S$2C2C,DE&&t'9'9$F'9'ST$$++D1	2  rC   c                 <   g | _         | j                  D ]{  }|j                         s| j                  r| j	                  |      r2| j                   j                  | j                  ||             |ra| j                  j                  |       } | j                   S r5  )new_objectsextra_formsr9  
can_delete_should_delete_formr9   r   r  )rp   r   r;   s      rA   r  z!BaseModelFormSet.save_new_objects"  s    $$ 		.D##% 4#;#;D#A##DMM$vM$FG  ''-		. rC   c                   	
 ddl m	m}m} | j                  j
                  j                  x| _        }	
fd
 
|      s|j                  |j                  vr|j                  r9|j                  j                  j                  rdn|j                  j                  }n#	 || j                         |   j                  }nd}t!        |||f      r/|j"                  j                  j$                  j                         }n$| j                  j$                  j                         }|j'                  |j                  j                  j(                        }|j
                  j*                  r?|j
                  j*                  j-                  | j                  j                  t.              }nt.        }t1        ||d|      |j                  | j                  j                  <   t2        | i  ||       y# t        $ r d}Y :w xY w)z0Add a hidden field for the object's primary key.r   )r-   
ForeignKeyOneToOneFieldc                    | j                    xsr | j                  xs t        |       xsV | j                  xrH | j                  j                  xr0  | j                  j
                  j                  j                        S rs   )r+   auto_createdr,   r   parent_linkra   r(   r   )r   r-   pk_is_not_editables    rA   rG  z7BaseModelFormSet.add_fields.<locals>.pk_is_not_editable:  sj    [[ boo&RB	9R OO G(C(C G&r'<'<'B'B'E'EFrC   NF)r   r   r1   )r\   r-   rB  rC  ra   r(   r   	_pk_fieldr.   r*   r   r<   r   r   r   r   r,   r   r   usingdbrb   r6   r   r   r}   
add_fields)rp   r;   indexrB  rC  r   pk_valuer   r1   r-   rG  r   s            @@rA   rK  zBaseModelFormSet.add_fields0  s   II"jj..111	 b!RWWDKK%?}} $(==#7#7#>#>4DMMDTDT$(#'#4#4#6u#=#@#@#' "z=9:__**;;HHJZZ00==?$--..112Bzz!!++//0C0C[Q$/?H_dms/tDKK++,4' " $#H$s   ""G0 0G?>G?)NNr   NNr   )r^   rt   ru   __doc__ra   r  r  rr   r   r   r   r   r   r   r   r   r   r   r   r   r  r  r  r  r  rK  r   r   s   @rA   r   r   0  s     E EMFJk+/k,)
 D&((
R DG*R
E4 )( )(rC   r   r+  Fc                     t        |dd      }t        |d|      t        |d|	      t        d      t        | |||	||
|||||      }t        |||||||||	      }| |_        |S )z8Return a FormSet class for the given Django model class.ry   Nr*   r=   z]Calling modelformset_factory without defining 'fields' or 'exclude' explicitly is prohibited.)
r;   r*   r=   rc   rb   rd   re   rf   rY   rg   )extramin_nummax_num	can_orderr?  validate_minvalidate_max)rH   r   r#   r   ra   )ra   r;   rc   formsetrP  r?  rS  rR  r*   r=   rb   rU  rd   re   rf   rY   rQ  rT  rg   metaFormSets                        rA   r    r    \  s     4&Dh'/D)W-5"2
 	

 Ufg0B%,?O$*z,:-	YD
 dG5'SZ(1j+7lTG GMNrC   c                   l     e Zd ZdZ	 	 d	 fd	Z fdZ fdZed        Zd
 fd	Z	 fdZ
 fdZ xZS )r!   z0A formset for child objects related to a parent.c                    |*| j                   j                  j                         | _        n|| _        || _        || j                  j
                  }| j                  j                  3 |j                  di | j                   j                  | j                  i}n|j                         }| j                   j                  h| _
        t        	| 0  ||f||d| | j                  j                  j                  r| j                   j                  | j                  j                  j                  vrt!        | j                  j                  j                  t"              rBt%        | j                  j                  j                        | j                  j                  _        | j                  j                  j                  j'                  | j                   j                         y y y )N)r   rK   r[   )fkr   ra   r<   save_as_newr   r   filterr.   noner  r}   rr   r;   r(   r*   r,   r  r  r9   )
rp   r3   r4   r<   r\  r   rK   rl   r   r   s
            rA   rr   zBaseInlineFormSet.__init__{  sK    GG00668DM$DM&zz22H==' ADGGLL$--#@ABB"ggll^uKVbKFK 99??!!dggll$))//:P:P&P$))//00%8)-diioo.D.D)E		&IIOO""))$'',,7 'Q!rC   c                 :    | j                   ryt        | 	         S )Nr   )r\  r}   r   r   s    rA   r   z$BaseInlineFormSet.initial_form_count  s    w)++rC   c                 L   t        |   |fi |}| j                  rt        |j                  dd       }|d|j                  _        d |j                  |j                  | j                  j                        <   d |j                  |j                  | j                  j                        <   |||j                  _        | j                  j                  }| j                  j                  j                  | j                  j                  j                  j                  j                  j                  k7  rAt        | j                  | j                  j                  j                        }t        |d|      }t!        |j                  | j                  j#                         |       |S )N_mutableTr   )r}   r   r\  rH   r3   ra  r5   rH  r.   r[  r<   r   r   r1  ra   r(   setattrget_attname)rp   r   rl   r;   mutablefk_valuer   s         rA   r   z!BaseInlineFormSet._construct_form  s4   w&q3F3diiT:G"%)		" ?CDIIdoodnn&9&9:;7;DIIdoodggll34"%,		" ==##77**dgg.B.B.H.H.N.N.Q.Q.V.VVt}}dgg.B.B.M.MNHxx8Htww224h?rC   c                     | j                   j                  j                  | j                        j	                  dd      S )N)ra   + )r[  r   get_accessor_namera   replace)clss    rA   get_default_prefixz$BaseInlineFormSet.get_default_prefix  s2    vv""443994EMMcSUVVrC   c                     t        |j                  | j                  j                  | j                         t        |   ||      S r5  )rb  r<   r[  r.   r}   r   )rp   r;   r   r   s      rA   r   zBaseInlineFormSet.save_new  s6     	tww||T]];wV44rC   c                    t         |   ||       | j                  | j                  k(  r| j                  j                  }ddi}n[| j                  j                  }dt        |j                  j                  |      dt        | j                  j                              i}| j                  j                  j                  | j                  j                  j                  j                  j                  j                  k7  r#| j                  j                  j                  |d<   | j                  j                   j"                  r|j                  d      )| j                  j                  j%                  |d         }n | j                  j                  j                  }|j'                         r!t)        | j                  |j*                  d        t-        | j                  fi ||j                  |<   y )Npk_fieldTrW   to_field)r}   rK  rH  r[  r.   rH   r*   r6   r   verbose_namer   r1  ra   r(   r   r<   r   r   	get_fieldr/   rb  attnamer   )rp   r;   rL  r.   rl   rp  r   s         rA   rK  zBaseInlineFormSet.add_fields  sg   4'>>TWW$>>&&D $'F 77<<D!6$''J^J^A_`F 77**dgg.B.B.H.H.N.N.Q.Q.V.VV!%!5!5!@!@F: ==&&zz*%1==..88
9KL==..11##%x'7'7>1$--J6JDrC   c                 ~    |D cg c]  }|| j                   j                  k7  s|  }}t        |   |      S c c}w rs   )r[  r.   r}   r  )rp   r#  r0   r   s      rA   r  z*BaseInlineFormSet.get_unique_error_message  s;    +7Q%5DGGLL;PQQw/== Rs   ::)NNNFNNr   )r^   rt   ru   rN  rr   r   r   classmethodrl  r   rK  r  r   r   s   @rA   r!   r!   y  sF    :7;:>8.,
. W W5K>> >rC   r!   c                    ddl m} |j                  }|r|j                  D cg c]  }|j                  |k(  s| }}t        |      dk(  r|d   }t        ||      rI|j                  j                  | k7  r~|j                  j                  | j                  j                         vrNt        d|d| j                  j                  d      |s&t        d|j                  j                  d|d      S |j                  D cg c]Y  }t        ||      rK|j                  j                  | k(  s0|j                  j                  | j                  j                         v r|[ }}t        |      dk(  r|d   }|S |s=|ry	t        d|j                  j                  d
| j                  j                  d      t        d|j                  j                  d| j                  j                  d      c c}w c c}w )aD  
    Find and return the ForeignKey from model to parent if there is one
    (return None if can_fail is True and no such field exists). If fk_name is
    provided, assume it is the name of the ForeignKey field. Unless can_fail is
    True, raise an exception if there isn't a ForeignKey from model to
    parent_model.
    r   )rB  r+  z	fk_name 'z' is not a ForeignKey to 'z'.rU   z' has no field named 'Nz' has no ForeignKey to 'z#' has more than one ForeignKey to 'z*'. You must specify a 'fk_name' attribute.)r\   rB  r(   r*   r.   r   r,   r   ra   get_parent_listr   rW   )	parent_modelra   fk_namecan_failrB  r>   r@   fks_to_parentr[  s	            rA   _get_foreign_keyr|    s    ,;;D$(KKEq166W3DEE}"q!Bb*-__**l:__**,2D2D2T2T2VV CJLL^L^LdLde  38;;3D3DgN > I3 {{
!Z($$4$$(:(:(J(J(LL 
 
 }"q!B$ I# KK%% &&,,   KK%% &&,, E F
s   G2G2AG7r0  c                     t        | ||      }|j                  rd}
i d|d|d|d|d|	d|d	|d
|d|d|
d|d|d|d|d|d|d|d|i}t        |fi |}||_        |S )z
    Return an ``InlineFormSet`` for the given kwargs.

    ``fk_name`` must be provided if ``model`` has more than one ``ForeignKey``
    to ``parent_model``.
    )ry  r+  r;   rc   rV  rP  r?  rS  r*   r=   rQ  rR  rb   rT  rU  rd   re   rf   rY   rg   )r|  uniquer    r[  )rx  ra   r;   rV  ry  r*   r=   rP  rS  r?  rR  rc   rb   rU  rd   re   rf   rY   rQ  rT  rg   r[  rl   rX  s                           rA   r"   r"     s    
,w	?B	yy0 	7 		
 	j 	Y 	& 	7 	7 	7 	7 	 	 	, 	&  	j!" 	.#$ 	%F( #53F3GGJNrC   c                   N     e Zd ZdZeZd ed      iZddd fd
Zd Z	d	 Z
 xZS )
r   zw
    A basic integer field that deals with validating the given value to a
    given parent instance in an inline.
    invalid_choicez3The inline value did not match the parent instance.FN)ro  rp  c                   || _         || _        || _        | j                   I| j                  r$t        | j                   | j                        |d<   n| j                   j                  |d<   d|d<   t        |   |i | y )Nr   Fr   )parent_instancero  rp  rH   r   r}   rr   )rp   r  ro  rp  argsrl   r   s         rA   rr   zInlineForeignKeyField.__init__K  sy    .  +}}$+D,@,@$--$Py!$($8$8$;$;y!"z$)&)rC   c                 P   || j                   v r| j                  ry | j                  S | j                  r!t	        | j                  | j                        }n| j                  j
                  }t        |      t        |      k7  rt        | j                  d   d      | j                  S )Nr  r   )	r7   ro  r  rp  rH   r   r   r   rY   )rp   r{   origs      rA   r   zInlineForeignKeyField.cleanW  s    D%%%}}'''==4//?D''**Du:T"!$"5"56F"GN^__###rC   c                      yNFr[   )rp   r   r3   s      rA   r9  z!InlineForeignKeyField.has_changedf  s    rC   )r^   rt   ru   rN  r   r1   r-  default_error_messagesrr   r   r9  r   r   s   @rA   r   r   A  s8     F!QR 9> 
*$rC   r   c                       e Zd Zd Zd Zd Zy)ModelChoiceIteratorValuec                      || _         || _        y rs   )r{   r<   )rp   r{   r<   s      rA   rr   z!ModelChoiceIteratorValue.__init__k  s    
 rC   c                 ,    t        | j                        S rs   )r   r{   r   s    rA   __str__z ModelChoiceIteratorValue.__str__o  s    4::rC   c                 X    t        |t              r|j                  }| j                  |k(  S rs   )r,   r  r{   )rp   others     rA   __eq__zModelChoiceIteratorValue.__eq__r  s%    e56KKEzzU""rC   N)r^   rt   ru   rr   r  r  r[   rC   rA   r  r  j  s    !#rC   r  c                   *    e Zd Zd Zd Zd Zd Zd Zy)ModelChoiceIteratorc                 4    || _         |j                  | _        y rs   )r0   rK   r   s     rA   rr   zModelChoiceIterator.__init__y  s    
rC   c              #      K   | j                   j                  d| j                   j                  f | j                  }|j                  s|j	                         }|D ]  }| j                  |        y w)Nrh  )r0   empty_labelrK   _prefetch_related_lookupsiteratorchoice)rp   rK   r   s      rA   __iter__zModelChoiceIterator.__iter__}  si     ::!!-tzz--..==11((*H 	#C++c""	#s   A4A6c                 p    | j                   j                         | j                  j                  dz   S dz   S )Nr+  r   )rK   countr0   r  r   s    rA   __len__zModelChoiceIterator.__len__  s4     }}""$TZZ-C-C-OWWUVWWrC   c                 j    | j                   j                  d uxs | j                  j                         S rs   )r0   r  rK   existsr   s    rA   __bool__zModelChoiceIterator.__bool__  s)    zz%%T1KT]]5I5I5KKrC   c                     t        | j                  j                  |      |      | j                  j                  |      fS rs   )r  r0   prepare_valuelabel_from_instancerp   r   s     rA   r  zModelChoiceIterator.choice  s7    $TZZ%=%=c%BCHJJ**3/
 	
rC   N)r^   rt   ru   rr   r  r  r  r  r[   rC   rA   r  r  x  s    '#XL
rC   r  c            
            e Zd ZdZd ed      iZeZdddddddddd		d
Zd Z	 fdZ
d Zd Z eee      Zd Zd Z eeej$                        Z fdZd Zd Zd Z xZS )r   z1A ChoiceField whose choices are a model QuerySet.r  zGSelect a valid choice. That choice is not one of the available choices.z	---------TNrh  F)	r  r   r1   rW   r   rX   to_field_namerP   r   c       	   	          t        j                  | f|||||d| |r|t        | j                  t              r
|
sd | _        n|| _        || _        |	| _        || _        y )N)r   r1   rW   r   rX   )	r	   rr   r,   r1   r   r  rK   rP   r  )rp   rK   r  r   r1   rW   r   rX   r  rP   r   rl   s               rA   rr   zModelChoiceField.__init__  sm     		
#F%y	
4:	

 '-[1%#D*D  0*rC   c                 d    t        | j                        r| j                         S | j                  S )z
        Return ``limit_choices_to`` for this form field.

        If it is a callable, invoke it and return the result.
        )r_   rP   r   s    rA   rL   z%ModelChoiceField.get_limit_choices_to  s-     D))*((**$$$rC   c                     t         t        |   |      }| j                  | j                  j	                         |_        |S rs   )r}   r   __deepcopy__rK   all)rp   memoresultr   s      rA   r  zModelChoiceField.__deepcopy__  s7    {D6t<==$"mm//1FOrC   c                     | j                   S rs   )r   r   s    rA   _get_querysetzModelChoiceField._get_queryset  s    ~~rC   c                 l    |d n|j                         | _        | j                  | j                  _        y rs   )r  r   choicesr1   )rp   rK   s     rA   _set_querysetzModelChoiceField._set_queryset  s&    !)!1x||~"llrC   c                     t        |      S )z
        Convert objects into strings and generate the labels for the choices
        presented by this object. Subclasses can override this method to
        customize the display of the choices.
        )r   r  s     rA   r  z$ModelChoiceField.label_from_instance  s     3xrC   c                 T    t        | d      r| j                  S | j                  |       S )N_choices)rM   r  r  r   s    rA   _get_choiceszModelChoiceField._get_choices  s)     4$==  }}T""rC   c                     t        |d      r3| j                  r|j                  | j                        S |j                  S t        |   |      S Nr(   )rM   r  serializable_valuer   r}   r  )rp   r{   r   s     rA   r  zModelChoiceField.prepare_value  sF    5'"!!//0B0BCCxxw$U++rC   c                 r   || j                   v ry 	 | j                  xs d}t        || j                  j                        rt        ||      } | j                  j                  di ||i}|S # t        t        | j                  j                  j                  f$ r t        | j                  d   d      w xY w)Nr   r  r  r[   )r7   r  r,   rK   ra   rH   r6   r   r`   DoesNotExistr   rY   )rp   r{   keys      rA   r   zModelChoiceField.to_python  s    D%%%	`$$,C%!4!45s+%DMM%%5e5E  It}}':':'G'GH 	`!$"5"56F"GN^__	`s   AA- -A	B6c                 .    t        j                  | |      S rs   )r	   validaterp   r{   s     rA   r  zModelChoiceField.validate  s    ~~dE**rC   c                     | j                   ry||nd}||nd}t        | j                  |            t        |      k7  S )NFrh  )disabledr   r  )rp   r   r3   initial_value
data_values        rA   r9  zModelChoiceField.has_changed  sC    ==#*#6B!-T2
4%%m45ZHHrC   )r^   rt   ru   rN  r-  r  r  r  rr   rL   r  r  r  propertyrK   r  r  r   _set_choicesr  r  r   r  r9  r   r   s   @rA   r   r     s    ; 	! 6 7 #H0;t4TD+*%+ }5H# |[%=%=>G,
+IrC   r   c                   |     e Zd ZdZeZeZ ed       ed       ed      dZ	 fdZ
d Zd Zd	 Z fd
Zd Z xZS )r   z9A MultipleChoiceField whose choices are a model QuerySet.zEnter a list of values.zESelect a valid choice. %(value)s is not one of the available choices.u"   “%(pk)s” is not a valid value.)invalid_listr  invalid_pk_valuec                     t        |   |fdd i| | j                  j                  d      9t	        j
                  dt        d       | j                  d   | j                  d<   y y )Nr  r  zFThe 'list' error message key is deprecated in favor of 'invalid_list'.r/  )
stacklevelr  )r}   rr   rY   r6   warningswarnr   )rp   rK   rl   r   s      rA   rr   z!ModelMultipleChoiceField.__init__  si    >t>v>""6*6MM"(Q
 372E2Ef2MD/ 7rC   c                 >    |sg S t        | j                  |            S rs   )r  _check_valuesr  s     rA   r   z"ModelMultipleChoiceField.to_python  s     ID&&u-..rC   c                 l   | j                  |      }| j                  r|st        | j                  d   d      | j                  s|s| j                  j                         S t        |t        t        f      st        | j                  d   d      | j                  |      }| j                  |       |S )Nr   r  r  )r  r   r   rY   rK   r^  r,   r  r  r  run_validators)rp   r{   r   s      rA   r   zModelMultipleChoiceField.clean$  s    ""5)==!$"5"5j"A
SSu==%%''%$/!##N3#  & 	E"	rC   c           	      *   | j                   xs d}	 t        |      }|D ]!  }	  | j
                  j                  d	i ||i #  | j
                  j                  d	i d|z  |i}|D ch c]  }t        t        ||             }}|D ],  }t        |      |vst        | j                  d   dd|i       |S # t        $ r t        | j                  d   d      w xY w# t        t        f$ r t        | j                  d   dd|i      w xY wc c}w )
z
        Given a list of possible PK values, return a QuerySet of the
        corresponding objects. Raise a ValidationError if a given value is
        invalid (not a valid PK, not in the queryset, etc.)
        r   r  r  r  )r   paramsz%s__inr  r{   r[   )
r  	frozensetr`   r   rY   rK   r]  r   r   rH   )rp   r{   r  r   r   r   pksvals           rA   r  z&ModelMultipleChoiceField._check_values5  sL      (D	e$E  	B$$$1Ry1	 "T]]!!<X^U$;<-/0s71c?#00 	C3xs"%''(89)#S> 	 	1  	!##N3# 	 	* %''(:;+ ":  1s   B9 C (D9$C -Dc                     t        |d      r<t        |t              s,t        |d      s t        |   }|D cg c]
  } ||       c}S t        |   |      S c c}w )Nr  r(   )rM   r,   r   r}   r  )rp   r{   r  r   r   s       rA   r  z&ModelMultipleChoiceField.prepare_valueZ  sV    E:&uc*E7+!G1M.34M!$44w$U++ 5s   Ac                     | j                   ry|g }|g }t        |      t        |      k7  ry| j                  |      D ch c]  }t        |       }}|D ch c]  }t        |       }}||k7  S c c}w c c}w )NFT)r  r   r  r   )rp   r   r3   r{   initial_setdata_sets         rA   r9  z$ModelMultipleChoiceField.has_changedb  s    ==?G<Dw<3t9$/3/A/A'/JKes5zKK,015CJ11;&& L1s   A2A7)r^   rt   ru   rN  r   r1   r   hidden_widgetr-  r  rr   r   r   r  r  r9  r   r   s   @rA   r   r   
  sW    CF'M34 2 3BC	N/
"#J,'rC   r   c                     t        | d      xr2 | j                  j                  d uxs | j                  j                  d uS r  )rM   r(   r*   r=   )rZ   s    rA   modelform_defines_fieldsr  p  sC    :w' t+ 	-  ,rC   )NN)	NNNNNNNNNr  )8rN  r  	itertoolsr   django.core.exceptionsr   r   r   r   django.forms.fieldsr   r	   django.forms.formsr
   r   django.forms.formsetsr   r   django.forms.utilsr   django.forms.widgetsr   r   r   r   django.utils.deprecationr   django.utils.textr   r   django.utils.translationr   r   r-  r$   r   rB   r   rQ   r   rn   rw   r   r   r#   r   r    r!   r|  r"   r   r  r  r   r   r  r[   rC   rA   <module>r     s      3 C > (  > 5 ? 
$R2U @D?CBF#'V DHVr
E 
EE3 EP|H |~	); 	 #,D$)-td!d4$(P=ji({ i(X	 &/4!1u#($tT!%ED $d!%E:a>( a>H5p 5>"3T!%t1%)4D"&UT!%$t"&U$'X&E &R# #
 
>pI{ pIfc'/ c'LrC   