
    g                        d 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 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 ddl m!Z!m"Z" ddl#m$Z$ ddl#m%Z& dZ'dZ(d8dZ)d8dZ*d Z+	 	 	 	 	 	 	 	 	 d9ddddZ, G d d      Z- G d de      Z. G d dee      Z/ G d d e/e.!      Z0e0dddddddddf
d"Z1 G d# d$ee      Z2e0de2d%d&d&ddddd&dddddd&ddddd&fd'Z3 G d( d)e2      Z4d:d*Z5e0e4dddd+d&ddddd&dddddd&ddddd&fd,Z6 G d- d.e      Z7 G d/ d0      Z8 G d1 d2e      Z9 G d3 d4e      Z: G d5 d6e:      Z;d7 Z<y);z[
Helper functions for creating Form classes from Django models
and database field objects.
    )chain)NON_FIELD_ERRORS
FieldErrorImproperlyConfiguredValidationErrorProhibitNullCharactersValidator)
AltersData)ChoiceFieldField)BaseFormDeclarativeFieldsMetaclass)BaseFormSetformset_factory)	ErrorList)HiddenInputMultipleHiddenInputRadioSelectSelectMultiple)BaseChoiceIterator)make_hashable)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__Nc                 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	            k/var/www/python.vincentserveurtest.ovh/public_html/venv/lib/python3.12/site-packages/django/forms/models.pyconstruct_instancerG   2   sW   
 !>>D$$LO[[

!V--.vv\)!&&"6qvv( MMOQVV""))AA		4::tqvv'>   (DL,>,>,K,KK a))*""1%X|AFF';<5 8 	<#78  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.
    r0   F)r-   r   concrete_fieldsprivate_fieldsmany_to_manygetattrr3   value_from_object)rA   r/   rB   rC   r8   rE   s         rF   r   r   b   s     >>DD4'')<)<d>O>OPq*e,!&&"6qvv(**84QVV Q KrH   c                 h   ddl m}m}m} t	        | d      rt	        | d      r| j                         }|r||}t        ||      s |di |}| | |d            z  }| j                  j                   || j                  j                  j                  j                  |                  | _        yyyy)	z=Apply limit_choices_to to the formfield's queryset if needed.r   )ExistsOuterRefQquerysetget_limit_choices_topk)rU   N )django.db.modelsrP   rQ   rR   hasattrrT   r1   rS   filtermodel_base_manager)	formfieldrP   rQ   rR   limit_choices_tocomplex_filters         rF   #apply_limit_choices_to_to_formfieldr_   {   s    44y*%')=S*T$99;-Nna0!"!6%5!6a8D>22N!*!3!3!:!:y))//==DD^TU"I  +U%rH   Tapply_limit_choices_toform_declared_fieldsc
                
   |xs i }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z|j                  |v r||j                     ||j                  <   i }|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 )a  
    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.

    ``form_declared_fields`` is a dictionary of form fields created directly on
    a form.
    r   )r   r0   F'z' cannot be specified for z) model form as it is a non-editable fieldr6   Tlocalizelabel	help_texterror_messages
form_classz1formfield_callback must be a function or callablerV   )r-   rW   r   rK   r1   sortedr   rJ   rL   rM   r3   r   __name__r#   r\   callable	TypeErrorr_   r>   r;   )rZ   r/   rB   widgetsformfield_callbacklocalized_fieldslabels
help_textsrh   field_classesra   rb   
field_dictignoredrC   
ModelFieldrE   sortable_private_fieldskwargsr\   s                       rF   r    r       s   ` 052JG;;D4 &&&a*Q
*C&   d""$;T=N=NO q*e,"FFf$_g(= ,-FFENND  !&&"6qvv(66))!5aff!=Jqvvqvv(&qvvF8z)+;!;!%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"ef  
q/Qg5E z~~a   	 

 yn
s   I;I;#J c                       e Zd ZddZy)ModelFormOptionsNc                 l   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       | _	        t        |d
d       | _
        y )NrZ   r/   rB   rn   rp   rq   rr   rh   rs   ro   )rM   rZ   r/   rB   rn   rp   rq   rr   rh   rs   ro   )selfoptionss     rF   __init__zModelFormOptions.__init__  s    Wgt4
gx6w	48w	48 '1CT Jgx6!'<>%g/?F$WotD")'3G"NrH   N)rk   
__module____qualname__r~   rV   rH   rF   rz   rz     s    
OrH   rz   c                        e Zd Z fdZ xZS )ModelFormMetaclassc                    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                  rZ|j                  |j                  t        d|z        |j                  t        k(  rd |_        t        |j                  |j                  |j                  |j                   |j"                  |j$                  |j&                  |j(                  |j*                  |j,                  d|j.                        }	|	j1                         D 
ch c]
  \  }
}|r	|
 }}
}|j3                  |j.                        }|r7d}|d	j5                  |      |j                  j                  fz  }t7        |      |	j9                  |j.                         n|j.                  }	|	|_        |S c c}}
w )
NMeta)r/   rB   rp   zP%(model)s.Meta.%(opt)s cannot be a string. Did you mean to type: ('%(value)s',)?)rZ   optvaluez|Creating a ModelForm without either the 'fields' attribute or the 'exclude' attribute is prohibited; form %s needs updating.Fr`   z&Unknown field(s) (%s) specified for %sz, )super__new__r   rz   rM   r-   r1   strr#   rk   rm   rZ   r/   rB   r   r    rn   ro   rp   rq   rr   rh   rs   declared_fieldsitems
differencejoinr   updatebase_fields)mcsr3   basesattrs	new_classrC   r   r   msgr/   kvnone_model_fieldsmissing_fieldsmessage	__class__s                  rF   r   zModelFormMetaclass.__new__  s   GOCue<	]$$!1')VT2R!SSy
 =CD#&E%%%:*=< "+!3!3"!&   n$ = ::{{"t||';*&(,-  {{j( #%

''%%##""',%.%>%>F" 06||~ G~tq!Q~ G.99):S:STNBDIIn5tzz7J7JKK ))MM)334..F &	 !Hs   -
H8H)rk   r   r   r   __classcell__r   s   @rF   r   r     s    E ErH   r   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-   rZ   
ValueErrorrA   r   r/   rB   r   _validate_uniquer   r~   valuesr_   )r|   r8   r9   auto_idprefixinitialerror_classlabel_suffixempty_permittedrA   r   r   rC   object_datar\   r   s                  rF   r~   zBaseModelForm.__init__Z  s     zz::FGG JJLDMK$DM'$++t||LKw' !&#9 	 	
 ++-I/	: .rH   c                 2   t               }| 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.
        )setrA   r-   r/   r3   addrB   _errorsr.   r;   blankrequiredr<   )r|   rB   rE   r5   
form_fieldfield_values         rF   _get_validation_exclusionsz(BaseModelForm._get_validation_exclusions  s'   
 % $$++AFFE DKK'AFF#
 ""uDJJ4E4E'EAFF###1C1C(CAFF# $,,&AFF# "[[/
"//33E:&//#z'>'>>KK'E ,F rH   c                 (    d| _         | j                  S )NT)r   r.   r|   s    rF   cleanzBaseModelForm.clean  s     $   rH   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-   rX   r   r   r   rh   r/   r1   r   coder   	add_error)r|   errorsrC   r   r5   messagesrh   r   s           rF   _update_errorszBaseModelForm._update_errors  s     zz 6<(**J*F3J)//1OE8))''$(;(;;!%!4!45E!F$++%!%U!3!B!B#w86&4W\\&BGO $  2& 	tV$rH   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)rB   validate_unique)r-   r   r/   r   r1   InlineForeignKeyFieldr   rG   rA   rB   r   r   
full_cleanr   r   )r|   rC   rB   r3   r5   es         rF   _post_cleanzBaseModelForm._post_clean  s    zz113  ;;,,.KD%%!67D! /	#.dmmT[[$,,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.
        )rB   N)r   rA   r   r   r   )r|   rB   r   s      rF   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-   rB   r/   rA   r   rL   rK   rX   r3   r?   )r|   r.   rB   r/   rC   rE   s         rF   	_save_m2mzBaseModelForm._save_m2m  s     ((**$$""}}"" t(($*=*=>A1./!&&.166W,vv%  QVV0DE ?rH   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   rA   r-   object_name_stateaddingsaver   save_m2m)r|   commits     rF   r   zBaseModelForm.save  s     ;; MM''33!%!5!5!<!<I)K  MM NN
 }} !NNDM}}rH   T)rk   r   r   r   r~   r   r   r   r   r   r   r   alters_datar   r   s   @rF   r   r   Y  s`     #,;\+Z!%B#@	#F*0 DrH   r   c                       e Zd Zy)r   N)rk   r   r   rV   rH   rF   r   r   5  s    rH   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|i}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.
    rZ   Nr/   rB   rn   rp   rq   rr   rh   rs   r   rV   FormzZCalling modelform_factory without defining 'fields' or 'exclude' explicitly is prohibited.)rX   r   typestaticmethodro   rk   rM   r   )rZ   r@   r/   rB   ro   rn   rp   rq   rr   rh   rs   r   r   r   
class_nameform_class_attrss                   rF   r(   r(   9  s)   ` eE h"i"i#$4 ! h(l!"0 !.o $D&1TYYLrEu%D"./A"B&(J ~tXt$,y$1O1W"2
 	
 4:j4'+;<<rH   c                        e Zd 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.
    NF)r   c          	      N    || _         || _        t        |   di ||||d| y )N)r8   r9   r   r   rV   )rS   initial_extrar   r~   )	r|   r8   r9   r   r   rS   r   rx   r   s	           rF   r~   zBaseModelFormSet.__init__  sA     !$ 	
" 	
 	
rH   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r|   r   s    rF   r   z#BaseModelFormSet.initial_form_count  s,    }}t((*++w)++rH   c                     t        | d      s.| j                         D ci c]  }|j                  | c}| _        | j                  j	                  |      S c c}w )N_object_dict)rX   r   rU   r   r;   )r|   rU   os      rF   _existing_objectz!BaseModelFormSet._existing_object  sS    t^,262C2C2E F2EQq2E 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r|   r5   s     rF   _get_to_pythonzBaseModelFormSet._get_to_python  s<    
   ,&&88:E   ,rH   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-rA   r   T)r   r   r:   rZ   r-   rU   r3   r8   r   r   r   KeyErrorr   r   
IndexErrorr   _construct_formr/   r   )	r|   irx   pk_requiredpk_keyrU   r   r@   r   s	           rF   r   z BaseModelFormSet._construct_form  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)rX   rS   rZ   _default_managerr   orderedorder_byr-   rU   r3   r   )r|   qss     rF   r   zBaseModelFormSet.get_queryset  sw    t[)}}(]]ZZ00==?
 ::[[!1!1!4!4!9!9:
  DN~~rH   Tc                 &    |j                  |      S )z8Save and return a new model instance for the given form.r   r   )r|   r@   r   s      rF   save_newzBaseModelFormSet.save_new      yyy''rH   c                 &    |j                  |      S )z>Save and return an existing model instance for the given form.r   r   )r|   r@   objr   s       rF   save_existingzBaseModelFormSet.save_existing  r  rH   c                 *    |r|j                          yy)z#Deletes an existing model instance.N)delete)r|   r  r   s      rF   delete_existingz BaseModelFormSet.delete_existing  s    JJL rH   c                      |sg  _          fd}| _         j                  r j                  |      S  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 r   )saved_formsr   )r@   r|   s    rF   r   z'BaseModelFormSet.save.<locals>.save_m2m  s     ,,DMMO -rH   )r
  r   	edit_onlysave_existing_objectssave_new_objects)r|   r   r   s   `  rF   r   zBaseModelFormSet.save  sW    
 !D$ %DM>>--f55--f58M8Mf8UUUrH   c                 $    | j                          y r   )r   r   s    rF   r   zBaseModelFormSet.clean   s    rH   c                     t               }t               } j                  } j                  D cg c]  }|j                         r||vr| }}|D ]T  j	                         }j
                  j                  |d      \  }}|j                  |       |j                  |       V g }	|D ]  \  }
}t               }|D ]   fd|D        }t        d |D              }|s$d |vs)||v r|	j                   j                  |              j                   j                         g j                        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 rj|	j                   j-                  |              j                   j                         g j                        j                  t        <   j                   |= |j#                  |        1 |	rt/        |	      y c c}w )NT)rB   include_meta_constraintsc              3   x   K   | ]1  }|j                   v r!|j                  v r|nj                   |    3 y wr   )r.   unique_fields).0r5   r@   r|   s     rF   	<genexpr>z3BaseModelFormSet.validate_unique.<locals>.<genexpr>=  sE      !- 1 11 #d&8&88Ed>O>OPU>VV!-s   7:c              3   j   K   | ]+  }t        |d       r|j                         n
t        |       - yw)_get_pk_valN)rX   r  r   )r  ds     rF   r  z3BaseModelFormSet.validate_unique.<locals>.<genexpr>C  s=      ! &	 #1m4  +1-.
 &s   13)r   date)r   deleted_formsformsis_validr   rA   _get_unique_checksr   tupler>   get_unique_error_messager   get_form_errorr   r   r   r.   r   yearmonthdayrM   get_date_error_messager   )r|   all_unique_checksall_date_checksforms_to_deleter@   valid_formsrB   unique_checksdate_checksr   uclassunique_check	seen_datarow_datar5   
date_checklookup
unique_forr  	date_datar8   s   `   `                rF   r   z BaseModelFormSet.validate_unique#  s   E%,, 


"}}4#> " 	 

  D557G)-)I)I)- *J *&M; $$]3"";/   $5 FLI#!- ! ! &!  H 49, d&C&CL&QR9=9I9I!0023%)]] :J :%56 &2E$(9(99$($5$5e$< &2 MM(+E $ %6L *JI0:-FFE:# %%))%0<))*5A '#00<%)YY

DHH$E	 &-T->->z-JF%S$U	 --e46BDy( d&A&A*&MN9=9I9I!0023%)]] :J :%56 !--e4MM$'; $ *D !&)) q
s   K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.r5   r   zFPlease correct the duplicate data for %(field)s, which must be unique.and)r   r   r   _)r|   r+  s     rF   r  z)BaseModelFormSet.get_unique_error_message  s_    |!MNaR   X |QuX> rH   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.      r3  )
field_name
date_fieldr/  )r   r   )r|   r.  s     rF   r#  z'BaseModelFormSet.get_date_error_message  s;    I
 %Q-$Q-*Q-(

 	
rH   c                     t        d      S )Nz*Please correct the duplicate values below.r   r   s    rF   r  zBaseModelFormSet.get_form_error  s    CDDrH   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  rA   rU   r>   r  has_changedchanged_datar  r
  )r|   r   saved_instancesr&  r@   r  s         rF   r  z&BaseModelFormSet.save_existing_objects  s    !!!!I,,&&D--C
 vv~&$$++C0$$S$8!!#$$++S$2C2C,DE&&t'9'9$F'9'ST$$++D1 '  rH   c                 <   g | _         | j                  D ]{  }|j                         s| j                  r| j	                  |      r2| j                   j                  | j                  ||             |ra| j                  j                  |       } | j                   S r=  )new_objectsextra_formsrA  
can_delete_should_delete_formr>   r   r
  )r|   r   r@   s      rF   r  z!BaseModelFormSet.save_new_objects  s    $$D##% 4#;#;D#A##DMM$vM$FG  ''- % rH   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   )r2   
ForeignKeyOneToOneFieldc                    | j                    xsr | j                  xs t        |       xsV | j                  xrH | j                  j                  xr0  | j                  j
                  j                  j                        S r   )r0   auto_createdr1   r   parent_linkrZ   r-   rU   )rU   r2   pk_is_not_editables    rF   rO  z7BaseModelFormSet.add_fields.<locals>.pk_is_not_editable  sn    [[ OO@z"i'@ OO K33K*2??+@+@+F+F+I+IJrH   NF)r   r   r6   )rW   r2   rJ  rK  rZ   r-   rU   	_pk_fieldr3   r/   r   rA   r   r   r   r   r1   r   r   usingdbrn   r;   r   r!   r   
add_fields)r|   r@   indexrJ  rK  rU   pk_valuer   r6   r2   rO  r   s            @@rF   rS  zBaseModelFormSet.add_fields  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$/?HuV0DKK++, 	4' " $#H$s   ""G0 0G?>G?)NNr   NNr   )rk   r   r   __doc__rZ   r  r   r  r~   r   r   r   r   r   r   r  r  r   r   r   r   r  r#  r  r  r  rS  r   r   s   @rF   r$   r$     s     EI EM 
 
.,)
 D&((
V$ D^*@

E4 0( 0(rH   r$   r3  Fc                     t        |dd      }t        |d|      t        |d|	      t        d      t        | |||	||
|||||      }t        ||||||||||||      }| |_        ||_        |S )z8Return a FormSet class for the given Django model class.r   Nr/   rB   z]Calling modelformset_factory without defining 'fields' or 'exclude' explicitly is prohibited.)
r@   r/   rB   ro   rn   rp   rq   rr   rh   rs   )
extramin_nummax_num	can_orderrG  validate_minvalidate_maxabsolute_maxcan_delete_extrar   )rM   r   r(   r   rZ   r  )rZ   r@   ro   formsetrX  rG  r[  rZ  r/   rB   rn   r]  rp   rq   rr   rh   rY  r\  rs   r^  r_  r   r  metaFormSets                            rF   r%   r%     s    4 4&Dh'/D)W-5"2
 	

 -)%#D !!!)G GM!GNrH   c                   t     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   rS   rV   )fkr   rZ   rA   save_as_newr   rU   rY   r3   noner  r   r~   r@   r-   r/   r1   r  listr>   )
r|   r8   r9   rA   rf  r   rS   rx   r   r   s
            rF   r~   zBaseInlineFormSet.__init__A  sM     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!rH   c                 :    | j                   ryt        | 	         S )Nr   )rf  r   r   r   s    rF   r   z$BaseInlineFormSet.initial_form_count`  s    w)++rH   c                 D   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_mutableTrU   )r   r   rf  rM   r8   rk  r:   rP  r3   re  rA   rU   r   r9  rZ   r-   setattrattname)r|   r   rx   r@   mutablefk_valuer   s         rF   r   z!BaseInlineFormSet._construct_forme  s/   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9rH   c                     | j                   j                  j                  | j                        j	                  dd      S )N)rZ   + )re  r   get_accessor_namerZ   replace)clss    rF   get_default_prefixz$BaseInlineFormSet.get_default_prefix|  s2    vv""443994EMMcSUVVrH   c                     t        |j                  | j                  j                  | j                         t        |   ||      S r=  )rl  rA   re  r3   r   r   )r|   r@   r   r   s      rF   r   zBaseInlineFormSet.save_new  s6     	tww||T]];wV44rH   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'                         rx|j(                  | j                  j                  j                  j                  j                  j                  k(  s|j*                  s!t-        | j                  |j(                  d        t/        | j                  fi ||j                  |<   y )Npk_fieldTrf   to_field)r   rS  rP  re  r3   rM   r/   r;   r   verbose_namer   r9  rZ   r-   rU   rA   r   r   	get_fieldr4   rm  r8   rl  r   )r|   r@   rT  r3   rx   rz  r   s         rF   rS  zBaseInlineFormSet.add_fields  s   4'>>TWW$>>&&D $'F 77<<DKKOOD)7HTWW=Q=Q4R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##%   DGG$8$8$>$>$D$D$G$G$L$LLyyx'7'7>1$--J6JDrH   c                 ~    |D cg c]  }|| j                   j                  k7  s|  }}t        |   |      S c c}w r   )re  r3   r   r  )r|   r+  r5   r   s      rF   r  z*BaseInlineFormSet.get_unique_error_message  s<    +7Q<%5DGGLL;P<Qw/== Rs   ::)NNNFNNr   )rk   r   r   rV  r~   r   r   classmethodrv  r   rS  r  r   r   s   @rF   r&   r&   >  sU    : 8>,
. W W5%KN> >rH   r&   c                    ddl m} |j                  }|re|j                  D cg c]  }|j                  |k(  s| }}t        |      dk(  r|d   }g | j                  j                  | }	t        ||      r|j                  j                  j                  j                  r,|j                  j                  j                  j                  |	vs[|j                  j                  j                  j                  s|j                  j                  | k7  rf|j                  j                  |	vrNt        d|d| j                  j                  d      |s&t        d|j                  j                  d|d      S g | j                  j                  | }	|j                  D cg c]  }t        ||      r|j                  j                  | k(  sn|j                  j                  |	v sV|j                  j                  j                  j                  r.|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   )rJ  r3  z	fk_name 'z' is not a ForeignKey to 'z'.rd   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.)rW   rJ  r-   r/   r3   r   all_parentsr1   r   rZ   proxyproxy_for_modelr   rf   )
parent_modelrZ   fk_namecan_failrJ  rC   rE   fks_to_parentre  r  s
             rF   _get_foreign_keyr    s~    ,;;D$(KKEKq166W3DKE}"q!BIL..::ILIKr:. OO))//55--33CC;V --3399--=--[@ ! 2 2 8 8:  38;;3D3DgN P IG F**66EE [[
 !Z($$4>>'';6NN((..44,,22BBkQ   	 
 }"q!B( I'  KK%% &&,,   KK%% &&,,	 o F:
s   KK$BKr8  c                     t        | ||      }|j                  rd}
i d|d|d|d|d|	d|d	|d
|d|d|
d|d|d|d|d|d|d||||||d}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``.
    )r  r3  r@   ro   r`  rX  rG  r[  r/   rB   rY  rZ  rn   r\  r]  rp   rq   rr   rh   )rs   r^  r_  r   r  )r  uniquer%   re  )r  rZ   r@   r`  r  r/   rB   rX  r[  rG  rZ  ro   rn   r]  rp   rq   rr   rh   rY  r\  rs   r^  r_  r   r  re  rx   rb  s                               rF   r'   r'     s#   B 
,w	?B	yy0 	7 		
 	j 	Y 	& 	7 	7 	7 	7 	 	 	, 	&  	j!" 	.#$ '$,-F0 #53F3GGJNrH   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)ry  rz  c                   || _         || _        || _        | j                   I| j                  r$t        | j                   | j                        |d<   n| j                   j                  |d<   d|d<   t        |   |i | y )Nr   Fr   )parent_instancery  rz  rM   rU   r   r~   )r|   r  ry  rz  argsrx   r   s         rF   r~   zInlineForeignKeyField.__init__R  sy    .  +}}$+D,@,@$--$Py!$($8$8$;$;y!"z$)&)rH   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   )	r<   ry  r  rz  rM   rU   r   r   rh   )r|   r   origs      rF   r   zInlineForeignKeyField.clean^  s    D%%%}}'''==4//?D''**Du:T"!##$45<L  ###rH   c                      yNFrV   )r|   r   r8   s      rF   rA  z!InlineForeignKeyField.has_changedo  s    rH   )rk   r   r   rV  r   r6   r5  default_error_messagesr~   r   rA  r   r   s   @rF   r   r   G  s8    
 F!QR 9> 
*$"rH   r   c                   $    e Zd Zd Zd Zd Zd Zy)ModelChoiceIteratorValuec                      || _         || _        y r   )r   rA   )r|   r   rA   s      rF   r~   z!ModelChoiceIteratorValue.__init__t  s    
 rH   c                 ,    t        | j                        S r   )r   r   r   s    rF   __str__z ModelChoiceIteratorValue.__str__x  s    4::rH   c                 ,    t        | j                        S r   )hashr   r   s    rF   __hash__z!ModelChoiceIteratorValue.__hash__{  s    DJJrH   c                 X    t        |t              r|j                  }| j                  |k(  S r   )r1   r  r   )r|   others     rF   __eq__zModelChoiceIteratorValue.__eq__~  s%    e56KKEzzU""rH   N)rk   r   r   r~   r  r  r  rV   rH   rF   r  r  s  s    ! #rH   r  c                   *    e Zd Zd Zd Zd Zd Zd Zy)ModelChoiceIteratorc                 4    || _         |j                  | _        y r   )r5   rS   r   s     rF   r~   zModelChoiceIterator.__init__  s    
rH   c              #      K   | j                   j                  d| j                   j                  f | j                  }|j                  s|j	                         }|D ]  }| j                  |        y w)Nrr  )r5   empty_labelrS   _prefetch_related_lookupsiteratorchoice)r|   rS   r  s      rF   __iter__zModelChoiceIterator.__iter__  sd     ::!!-tzz--..==11((*HC++c"" s   A4A6c                 p    | j                   j                         | j                  j                  dz   S dz   S )Nr3  r   )rS   countr5   r  r   s    rF   __len__zModelChoiceIterator.__len__  s4     }}""$TZZ-C-C-OWWUVWWrH   c                 j    | j                   j                  d uxs | j                  j                         S r   )r5   r  rS   existsr   s    rF   __bool__zModelChoiceIterator.__bool__  s)    zz%%T1KT]]5I5I5KKrH   c                     t        | j                  j                  |      |      | j                  j                  |      fS r   )r  r5   prepare_valuelabel_from_instancer|   r  s     rF   r  zModelChoiceIterator.choice  s7    $TZZ%=%=c%BCHJJ**3/
 	
rH   N)rk   r   r   r~   r  r  r  r  rV   rH   rF   r  r    s    '#XL
rH   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	d Z
 fdZd Zd Z eee      Zd Zd Z eeej&                  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	---------TNrr  F)	r  r   r6   rf   r   rg   to_field_namer]   r   c       	   	          t        j                  | f|||||d| |r|t        | j                  t              r
|
sd | _        n|| _        || _        |	| _        || _        y )N)r   r6   rf   r   rg   )	r   r~   r1   r6   r   r  rS   r]   r  )r|   rS   r  r   r6   rf   r   rg   r  r]   r   rx   s               rF   r~   zModelChoiceField.__init__  sr    " 		
	
 	
 ,t{{K0#D*D  0*rH   c                 &    t               } ||      S r   r   )r|   r   non_null_character_validators      rF   validate_no_null_charactersz,ModelChoiceField.validate_no_null_characters  s    'F'H$+E22rH   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.
        )rl   r]   r   s    rF   rT   z%ModelChoiceField.get_limit_choices_to  s-     D))*((**$$$rH   c                     t         t        |   |      }| j                  | j                  j	                         |_        |S r   )r   r   __deepcopy__rS   all)r|   memoresultr   s      rF   r  zModelChoiceField.__deepcopy__  s7    {D6t<==$"mm//1FOrH   c                     | j                   S r   )r   r   s    rF   _get_querysetzModelChoiceField._get_queryset  s    ~~rH   c                 l    |d n|j                         | _        | j                  | j                  _        y r   )r  r   choicesr6   )r|   rS   s     rF   _set_querysetzModelChoiceField._set_queryset  s&    !)!1x||~"llrH   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     rF   r  z$ModelChoiceField.label_from_instance  s     3xrH   c                 T    t        | d      r| j                  S | j                  |       S )N_choices)rX   r  r  r   s    rF   _get_choiceszModelChoiceField._get_choices  s)     4$==  }}T""rH   c                     t        |d      r3| j                  r|j                  | j                        S |j                  S t        |   |      S Nr-   )rX   r  serializable_valuerU   r   r  )r|   r   r   s     rF   r  zModelChoiceField.prepare_value  sF    5'"!!//0B0BCCxxw$U++rH   c                    || j                   v ry | j                  |       	 | 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d|i      w xY w)NrU   r  r   r   paramsrV   )r<   r  r  r1   rS   rZ   rM   r;   r   rm   DoesNotExistr   rh   )r|   r   keys      rF   r   zModelChoiceField.to_python  s    D%%%((/
	$$,C%!4!45s+%DMM%%5e5E  It}}':':'G'GH 	!##$45%' 	s   AA> >AC
c                 .    t        j                  | |      S r   )r   validater|   r   s     rF   r  zModelChoiceField.validate%  s    ~~dE**rH   c                     | j                   ry||nd}||nd}t        | j                  |            t        |      k7  S )NFrr  )disabledr   r  )r|   r   r8   initial_value
data_values        rF   rA  zModelChoiceField.has_changed(  sC    ==#*#6B!-T2
4%%m45ZHHrH   )rk   r   r   rV  r5  r  r  r  r~   r  rT   r  r  r  propertyrS   r  r  r   r  fsetr  r   r  rA  r   r   s   @rF   r!   r!     s    ;
 	!U

 #H  "+H3%+ }5H# |[%8%8%=%=>G,"+IrH   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| y )Nr  )r   r~   )r|   rS   rx   r   s      rF   r~   z!ModelMultipleChoiceField.__init__=  s    >t>v>rH   c                 >    |sg S t        | j                  |            S r   )rh  _check_valuesr  s     rF   r   z"ModelMultipleChoiceField.to_python@  s     ID&&u-..rH   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   rh   rS   rg  r1   rh  r  r  run_validators)r|   r   r   s      rF   r   zModelMultipleChoiceField.cleanE  s    ""5)==!$"5"5j"A
SSu==%%''%$/!##N3#  & 	E"	rH   c           	      L   | j                   xs d}	 t        |      }|D ]2  }| j                  |       	  | j                  j                  d	i ||i 4  | 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.)
        rU   r  r  r  r  z%s__inr  r   rV   )r  	frozensetrm   r   rh   r  rS   rY   r   r   rM   )r|   r   r  rU   r   r   pksvals           rF   r  z&ModelMultipleChoiceField._check_valuesV  sQ      (D	e$E B,,R0$$$1Ry1  "T]]!!<X^U$;<-/0Rs71c?#R0C3xs"%''(89)#S>   	3  	!##N3# 	 	* %''(:;+ ":  1s   C
 C19D!
$C.1-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-   )rX   r1   r   r   r  )r|   r   r  r   r   s       rF   r  z&ModelMultipleChoiceField.prepare_value|  sZ    E:&uc*E7+!G1M.34eM!$e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   )r|   r   r8   r   initial_setdata_sets         rF   rA  z$ModelMultipleChoiceField.has_changed  s    ==?G<Dw<3t9$/3/A/A'/JK/Jes5z/JK,01D5CJD1;&& L1s   A2A7)rk   r   r   rV  r   r6   r   hidden_widgetr5  r  r~   r   r   r  r  rA  r   r   s   @rF   r"   r"   0  sV    CF'M34S
 BC?/
"$L,'rH   r"   c                     t        | d      xr2 | j                  j                  d uxs | j                  j                  d uS r  )rX   r-   r/   rB   )ri   s    rF   modelform_defines_fieldsr    s@    :w' t+Sz/?/?/G/Gt/SrH   )NN)	NNNNNNNNNr  )=rV  	itertoolsr   django.core.exceptionsr   r   r   r   django.core.validatorsr	   django.db.models.utilsr
   django.forms.fieldsr   r   django.forms.formsr   r   django.forms.formsetsr   r   django.forms.utilsr   django.forms.widgetsr   r   r   r   django.utils.choicesr   django.utils.hashabler   django.utils.textr   r   django.utils.translationr   r   r5  r)   r#   rG   r   r_   r    rz   r   r   r   r(   r$   r%   r&   r  r'   r   r  r  r!   r"   r  rV   rH   rF   <module>r     s  
   C - 2 C > (  4 / 5 , 6 
*`2& s  slO OF3 FRYHj Yx	); 	 
U=v`({J `(J 

/ANr>( r>jLd 

3?J)E )X# #"
, 
>JI{ JIZa'/ a'HrH   