a
    Lcòfáê  ã                   @   s8  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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mZ ddlmZ ddlmZ  dZ!dZ"d>dd„Z#d?dd„Z$dd„ Z%d@ddœdd„Z&G dd„ dƒZ'G dd„ deƒZ(G dd„ dee	ƒZ)G d d!„ d!e)e(d"�Z*e*dddddddddf
d#d$„Z+G d%d&„ d&ee	ƒZ,e*de,d'd(d(ddddd(dddddd(ddddd(fd)d*„Z-G d+d,„ d,e,ƒZ.dAd-d.„Z/e*e.dddd/d(ddddd(dddddd(ddddd(fd0d1„Z0G d2d3„ d3eƒZ1G d4d5„ d5ƒZ2G d6d7„ d7ƒZ3G d8d9„ d9eƒZ4G d:d;„ d;e4ƒZ5d<d=„ Z6dS )Bz[
Helper functions for creating Form classes from Django models
and database field objects.
é    )Úchain)ÚNON_FIELD_ERRORSÚ
FieldErrorÚImproperlyConfiguredÚValidationError)Ú
AltersData)ÚChoiceFieldÚField)ÚBaseFormÚDeclarativeFieldsMetaclass)ÚBaseFormSetÚformset_factory)Ú	ErrorList)ÚHiddenInputÚMultipleHiddenInputÚRadioSelectÚSelectMultiple)Ú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           	      C   sü   ddl m} |j}| j}g }|jD ]¸}|jr"t||jƒs"|j|vrDq"|durX|j|vrXq"|rh|j|v rhq"| 	¡ r°| |j j
j | j| j|  |j¡¡r°| |j¡| |j j
jv r°q"t||jƒrÈ| |¡ q"| |||j ¡ q"|D ]}| |||j ¡ qà|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   )ÚmodelsN)Z	django.dbr%   Ú_metaÚcleaned_dataÚfieldsÚeditableÚ
isinstanceÚ	AutoFieldÚnameÚhas_defaultÚfieldÚwidgetZvalue_omitted_from_dataÚdataÚfilesÚ
add_prefixÚgetÚempty_valuesZ	FileFieldÚappendÚsave_form_data)	ÚformÚinstancer(   Úexcluder%   Úoptsr'   Zfile_field_listÚf© r<   úP/home/httpd/docs/test/DocsMgr/lib/python3.9/site-packages/django/forms/models.pyÚconstruct_instance.   s>    
ÿ
þýÿÿþûr>   c                 C   sh   | j }i }t|j|j|jƒD ]F}t|ddƒs.q|durB|j|vrBq|rR|j|v rRq| | ¡||j< q|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)   FN)r&   r   Úconcrete_fieldsÚprivate_fieldsÚmany_to_manyÚgetattrr,   Zvalue_from_object)r8   r(   r9   r:   r0   r;   r<   r<   r=   r   ^   s    r   c                 C   s„   ddl m}m}m} t| dƒr€t| dƒr€|  ¡ }|r€|}t||ƒsP|f i |¤Ž}|||dƒd�M }| j || jj	j
 |¡ƒ¡| _dS )z=Apply limit_choices_to to the formfield's queryset if needed.r   )ÚExistsÚOuterRefÚQÚquerysetÚget_limit_choices_toÚpk©rH   N)Údjango.db.modelsrC   rD   rE   ÚhasattrrG   r*   rF   ÚfilterÚmodelZ_base_manager)Ú	formfieldrC   rD   rE   Úlimit_choices_toZcomplex_filterr<   r<   r=   Ú#apply_limit_choices_to_to_formfieldw   s    
ÿrP   T©Úapply_limit_choices_toc
                   sþ  i ‰g ‰| j }ddlm‰  ‡ fdd„|jD ƒ}tt|j||jƒƒD �]˜}t|ddƒsŽ|durB|j	|v rBˆdu sx|j	ˆvrBt
d|j	| jf ƒ‚qB|dur¢|j	|vr¢qBˆr²|j	ˆv r²qBi }|rÒ|j	|v rÒ||j	 |d	< |tksè|rð|j	|v rðd
|d< |�r|j	|v �r||j	 |d< |�r0|j	|v �r0||j	 |d< |�rP|j	|v �rP||j	 |d< |	�rp|j	|	v �rp|	|j	 |d< |du �rŒ|jf i |¤Ž}n$t|ƒ�s tdƒ‚n||fi |¤Ž}|�rÐ|
�rÄt|ƒ |ˆ|j	< qBˆ |j	¡ qB|�rú‡‡‡fdd„|D ƒ‰ˆS )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	   c                    s   g | ]}t |ˆ ƒr|‘qS r<   )r*   ©Ú.0r;   )Ú
ModelFieldr<   r=   Ú
<listcomp>º   s   z$fields_for_model.<locals>.<listcomp>r)   FNzH'%s' cannot be specified for %s model form as it is a non-editable fieldr/   TZlocalizeÚlabelÚ	help_textÚerror_messagesÚ
form_classz1formfield_callback must be a function or callablec                    s,   i | ]$}ˆ r|ˆ vr|ˆvr|ˆ  |¡“qS r<   )r3   rS   )r9   Ú
field_dictÚignoredr<   r=   Ú
<dictcomp>î   s   þz$fields_for_model.<locals>.<dictcomp>)r&   rJ   r	   r@   Úsortedr   r?   rA   rB   r,   r   Ú__name__r   rN   ÚcallableÚ	TypeErrorrP   r5   )rM   r(   r9   ÚwidgetsÚformfield_callbackÚlocalized_fieldsÚlabelsÚ
help_textsrY   Úfield_classesrR   r:   Zsortable_private_fieldsr;   ÚkwargsrN   r<   )rU   r9   r[   r\   r=   r   ˆ   sz    ,
ÿÿ
ÿþýý
ÿÿÿÿ


þr   c                   @   s   e Zd Zddd„ZdS )ÚModelFormOptionsNc                 C   s�   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 ƒ| _
d S )NrM   r(   r9   rb   rd   re   rf   rY   rg   rc   )rB   rM   r(   r9   rb   rd   re   rf   rY   rg   rc   )ÚselfÚoptionsr<   r<   r=   Ú__init__÷   s    zModelFormOptions.__init__)N)r_   Ú
__module__Ú__qualname__rl   r<   r<   r<   r=   ri   ö   s   ri   c                       s   e Zd Z‡ fdd„Z‡  ZS )ÚModelFormMetaclassc                    sD  t ƒ  | |||¡}|tfkr |S tt|dd ƒƒ }|_dD ]:}t||ƒ}t|tƒr:|tkr:d|j	||dœ }t
|ƒ‚q:|j�r4|jd u rž|jd u ržtd| ƒ‚|jtkr®d |_t|j|j|j|j|j|j|j|j|j|jdd�}	dd	„ |	 ¡ D ƒ}
|
 |j¡}|�r&d
}|d |¡|jj	f; }t|ƒ‚|	 |j¡ n|j}	|	|_|S )NÚMeta)r(   r9   rd   zP%(model)s.Meta.%(opt)s cannot be a string. Did you mean to type: ('%(value)s',)?)rM   ÚoptÚvaluez|Creating a ModelForm without either the 'fields' attribute or the 'exclude' attribute is prohibited; form %s needs updating.FrQ   c                 S   s   h | ]\}}|s|’qS r<   r<   )rT   ÚkÚvr<   r<   r=   Ú	<setcomp><  ó    z-ModelFormMetaclass.__new__.<locals>.<setcomp>z&Unknown field(s) (%s) specified for %sz, )ÚsuperÚ__new__r   ri   rB   r&   r*   Ústrr   r_   ra   rM   r(   r9   r   r   rb   rc   rd   re   rf   rY   rg   ÚitemsÚ
differenceZdeclared_fieldsÚjoinr   ÚupdateÚbase_fields)Úmcsr,   ÚbasesÚattrsÚ	new_classr:   rq   rr   Úmsgr(   Znone_model_fieldsZmissing_fieldsÚmessage©Ú	__class__r<   r=   rx     s^    

ýþÿ	
þÿ
ôzModelFormMetaclass.__new__)r_   rm   rn   rx   Ú__classcell__r<   r<   r…   r=   ro     s   ro   c                       st   e Zd Zdddddedddddf‡ fdd„	Zdd„ Zdd	„ Zd
d„ Zdd„ Zdd„ Z	dd„ Z
ddd„Zde_‡  ZS )r   Núid_%sFc                    sš   | j }|jd u rtdƒ‚|	d u r0| ¡ | _i }n|	| _t|	|j|jƒ}|d urX| |¡ d| _t	ƒ j
|||||||||
|d�
 | j ¡ D ]}t|ƒ qˆd S )Nz'ModelForm has no model class specified.F)Úuse_required_attributeÚrenderer)r&   rM   Ú
ValueErrorr8   r   r(   r9   r}   Ú_validate_uniquerw   rl   ÚvaluesrP   )rj   r0   r1   Úauto_idÚprefixÚinitialÚerror_classZlabel_suffixZempty_permittedr8   r‰   rŠ   r:   Zobject_datarN   r…   r<   r=   rl   N  s2    


özBaseModelForm.__init__c                 C   sÌ   t ƒ }| jjjD ]¶}|j}|| jvr2| |j¡ q| jjrT|| jjvrT| |j¡ q| jjrv|| jjv rv| |j¡ q|| jv rŽ| |j¡ q| j| }| j 	|¡}|j
s|js||jv r| |j¡ q|S )z‘
        For backwards-compatibility, exclude several types of fields from model
        validation. See tickets #12507, #12521, #12553.
        )Úsetr8   r&   r(   r,   Úaddr9   Ú_errorsr'   r3   ÚblankÚrequiredr4   )rj   r9   r;   r.   Z
form_fieldZfield_valuer<   r<   r=   Ú_get_validation_exclusions|  s*    

	
ÿþýz(BaseModelForm._get_validation_exclusionsc                 C   s   d| _ | jS )NT)rŒ   r'   ©rj   r<   r<   r=   Úclean©  s    zBaseModelForm.cleanc                 C   sª   | j }t|dƒr|j}nt|i}| ¡ D ]p\}}|tkrT|jrTt|jv rT|jt }n|| jv r(| j| j}nq(|D ]$}t|tƒrr|j	|v rr||j	 |_
qrq(|  d |¡ d S )NÚ
error_dict)r&   rK   rš   r   rz   rY   r(   r*   r   Úcoder„   Z	add_error)rj   Úerrorsr:   rš   r.   ÚmessagesrY   r„   r<   r<   r=   Ú_update_errors­  s,    
ÿþý
ÿþzBaseModelForm._update_errorsc              
   C   sÖ   | j }|  ¡ }| j ¡ D ]\}}t|tƒr| |¡ qzt| | j|j|j	ƒ| _W n. t
y~ } z|  |¡ W Y d }~n
d }~0 0 z| jj|dd� W n. t
yÂ } z|  |¡ W Y d }~n
d }~0 0 | jrÒ|  ¡  d S )NF)r9   Úvalidate_unique)r&   r—   r(   rz   r*   ÚInlineForeignKeyFieldr“   r>   r8   r9   r   rž   Z
full_cleanrŒ   rŸ   )rj   r:   r9   r,   r.   Úer<   r<   r=   Ú_post_cleanÎ  s"    	
ÿ
  zBaseModelForm._post_cleanc              
   C   sN   |   ¡ }z| jj|d� W n. tyH } z|  |¡ W Y d}~n
d}~0 0 dS )z‚
        Call the instance's validate_unique() method and update the form's
        validation errors if any were raised.
        )r9   N)r—   r8   rŸ   r   rž   )rj   r9   r¡   r<   r<   r=   rŸ   î  s
    zBaseModelForm.validate_uniquec                 C   s€   | j }| jj}| jj}| jj}t|j|jƒD ]N}t|dƒs<q,|rL|j	|vrLq,|r\|j	|v r\q,|j	|v r,| 
| j||j	 ¡ q,dS )zS
        Save the many-to-many fields and generic relations for this form.
        r6   N)r'   r&   r9   r(   r8   r   rA   r@   rK   r,   r6   )rj   r'   r9   r(   r:   r;   r<   r<   r=   Ú	_save_m2mù  s    

zBaseModelForm._save_m2mTc                 C   sP   | j r*td| jjj| jjjr dndf ƒ‚|rB| j ¡  |  ¡  n| 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.
        z8The %s could not be %s because the data didn't validate.ÚcreatedÚchanged)
rœ   r‹   r8   r&   Zobject_nameÚ_stateÚaddingÚsaver£   Úsave_m2m)rj   Úcommitr<   r<   r=   r¨     s    þÿÿ

zBaseModelForm.save)T)r_   rm   rn   r   rl   r—   r™   rž   r¢   rŸ   r£   r¨   Úalters_datar‡   r<   r<   r…   r=   r   M  s(   ô.-! 
r   c                   @   s   e Zd ZdS )r   N)r_   rm   rn   r<   r<   r<   r=   r   )  s   r   )Ú	metaclassc                 C   s  d| i}|dur||d< |dur(||d< |dur8||d< |durH||d< |durX||d< |durh||d< |	durx|	|d	< |
durˆ|
|d
< t |dƒrš|jfnd}td||ƒ}|r¸t|ƒ|_| jd }d|i}t|ddƒdu ròt|ddƒdu rò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.
    rM   Nr(   r9   rb   rd   re   rf   rY   rg   rp   r<   ZFormzZCalling modelform_factory without defining 'fields' or 'exclude' explicitly is prohibited.)rK   rp   ÚtypeÚstaticmethodrc   r_   rB   r   )rM   r7   r(   r9   rc   rb   rd   re   rf   rY   rg   r�   r€   rp   Ú
class_nameZform_class_attrsr<   r<   r=   r#   -  s8    0

 ÿr#   c                       sÜ   e Zd ZdZdZdZeƒ Zd+ddœ‡ fdd„Z‡ fdd	„Z	d
d„ Z
dd„ Z‡ fdd„Zdd„ Zd,dd„Zd-dd„Zd.dd„Zd/dd„Zde_dd„ Zdd„ Zdd „ Zd!d"„ Zd#d$„ Zd0d%d&„Zd1d'd(„Z‡ fd)d*„Z‡  ZS )2r   zO
    A ``FormSet`` for editing a queryset and/or adding new objects to it.
    NFrˆ   )r�   c                   s0   || _ || _tƒ jf i ||||dœ|¥¤Ž d S )N)r0   r1   rŽ   r�   )rF   Úinitial_extrarw   rl   )rj   r0   r1   rŽ   r�   rF   r�   rh   r…   r<   r=   rl   “  s    
üûÿzBaseModelFormSet.__init__c                    s   | j st|  ¡ ƒS tƒ  ¡ S )z=Return the number of forms that are required in this FormSet.)Úis_boundÚlenÚget_querysetrw   Úinitial_form_countr˜   r…   r<   r=   r´   ª  s    z#BaseModelFormSet.initial_form_countc                 C   s*   t | dƒsdd„ |  ¡ D ƒ| _| j |¡S )NÚ_object_dictc                 S   s   i | ]}|j |“qS r<   rI   ©rT   Úor<   r<   r=   r]   ²  rv   z5BaseModelFormSet._existing_object.<locals>.<dictcomp>)rK   r³   rµ   r3   )rj   rH   r<   r<   r=   Ú_existing_object°  s    
z!BaseModelFormSet._existing_objectc                 C   s   |j dur|j  ¡ }q |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.
        N)Úremote_fieldZget_related_fieldÚ	to_python©rj   r.   r<   r<   r=   Ú_get_to_pythonµ  s    
zBaseModelFormSet._get_to_pythonc                    s
  ||   ¡ k }|r¤| jr’d|  |¡| jjjjf }z| j| }W n tyP   Y q¢0 |  	| jjj¡}z||ƒ}W n t
y€   Y q¢0 |  |¡|d< qØ|  ¡ | |d< n4| jrØz| j||   ¡   |d< W n tyÖ   Y n0 tƒ j|fi |¤Ž}|�rd|j| jjjj _|S )Nz%s-%sr8   r�   T)r´   r±   r2   rM   r&   rH   r,   r0   ÚKeyErrorr¼   r   r¸   r³   r°   Ú
IndexErrorrw   Ú_construct_formr(   r–   )rj   Úirh   Zpk_requiredZpk_keyrH   rº   r7   r…   r<   r=   r¿   ¾  s0    z BaseModelFormSet._construct_formc                 C   sL   t | dƒsF| jd ur| j}n| jj ¡ }|js@| | jjjj	¡}|| _
| j
S )NÚ	_queryset)rK   rF   rM   Ú_default_managerr³   ZorderedZorder_byr&   rH   r,   rÁ   )rj   Úqsr<   r<   r=   r³   à  s    

zBaseModelFormSet.get_querysetTc                 C   s   |j |d�S )z8Save and return a new model instance for the given form.©rª   ©r¨   ©rj   r7   rª   r<   r<   r=   Úsave_newó  s    zBaseModelFormSet.save_newc                 C   s   |j |d�S )z>Save and return an existing model instance for the given form.rÄ   rÅ   )rj   r7   r8   rª   r<   r<   r=   Úsave_existing÷  s    zBaseModelFormSet.save_existingc                 C   s   |r|  ¡  dS )z#Deletes an existing model instance.N)Údelete)rj   Úobjrª   r<   r<   r=   Údelete_existingû  s    z BaseModelFormSet.delete_existingc                    sD   |sg ˆ _ ‡ fdd„}|ˆ _ˆ jr,ˆ  |¡S ˆ  |¡ˆ  |¡ S dS )zŒ
        Save model instances for every form, adding and changing instances
        as necessary, and return the list of instances.
        c                     s   ˆ j D ]} |  ¡  qd S ©N)Úsaved_formsr©   )r7   r˜   r<   r=   r©     s    
z'BaseModelFormSet.save.<locals>.save_m2mN)rÍ   r©   Ú	edit_onlyÚsave_existing_objectsÚsave_new_objects)rj   rª   r©   r<   r˜   r=   r¨      s    
zBaseModelFormSet.savec                 C   s   |   ¡  d S rÌ   )rŸ   r˜   r<   r<   r=   r™     s    zBaseModelFormSet.cleanc                    s  t ƒ }t ƒ }ˆj‰‡fdd„ˆjD ƒ}|D ]4‰ ˆ  ¡ }ˆ jj|dd�\}}| |¡ | |¡ q*g }|D ]¦\}}	t ƒ }
|D ]’‰ ‡ ‡fdd„|	D ƒ}tdd„ |D ƒƒ}|rzd |vrz||
v �r| ˆ 	|	¡¡ ˆj
ˆ ¡ gˆjd�ˆ jt< |	D ]}|ˆ jv rêˆ j|= qê|
 |¡ qzqh|D ]Ü}t ƒ }
|\}}}}|D ]¾‰ ˆ j�r.ˆ j| d u�r.ˆ j| d u�r.|d	k�r€ˆ j| }|j|j|jf}ntˆ j| |ƒf}ˆ j| f| }||
v �rà| ˆ |¡¡ ˆj
ˆ ¡ gˆjd�ˆ jt< ˆ j|= |
 |¡ �q.�q|�r t|ƒ‚d S )
Nc                    s    g | ]}|  ¡ r|ˆ vr|‘qS r<   )Zis_valid)rT   r7   )Úforms_to_deleter<   r=   rV     s   þz4BaseModelFormSet.validate_unique.<locals>.<listcomp>T)r9   Zinclude_meta_constraintsc                 3   s0   | ](}|ˆ j v r|ˆjv r|nˆ j | V  qd S rÌ   )r'   Úunique_fields©rT   r.   )r7   rj   r<   r=   Ú	<genexpr>1  s   
þz3BaseModelFormSet.validate_unique.<locals>.<genexpr>c                 s   s6   | ].}t |d ƒr| ¡ nt|tƒr*t|ƒn|V  qdS )Ú_get_pk_valN)rK   rÕ   r*   ÚlistÚtuple)rT   Údr<   r<   r=   rÔ   7  s   ý)rŠ   Údate)r’   Údeleted_formsZformsr—   r8   Z_get_unique_checksr}   r×   r5   Úget_unique_error_messager‘   Úget_form_errorrŠ   r”   r   r'   r“   ÚyearÚmonthÚdayrB   Úget_date_error_messager   )rj   Zall_unique_checksZall_date_checksZvalid_formsr9   Zunique_checksZdate_checksrœ   ZuclassÚunique_checkZ	seen_dataZrow_datar.   Ú
date_checkÚlookupZ
unique_forrÙ   Z	date_datar0   r<   )r7   rÑ   rj   r=   rŸ     sx    
þþ

þü
þ

ÿþý


þz BaseModelFormSet.validate_uniquec                 C   s>   t |ƒdkr tdƒd|d i S tdƒdt|tdƒƒi S d S )Né   z0Please correct the duplicate data for %(field)s.r.   r   zFPlease correct the duplicate data for %(field)s, which must be unique.Úand)r²   r   r   Ú_©rj   rá   r<   r<   r=   rÛ   t  s    ÿÿÿþz)BaseModelFormSet.get_unique_error_messagec                 C   s$   t dƒ|d |d t|d ƒdœ S )NzoPlease correct the duplicate data for %(field_name)s which must be unique for the %(lookup)s in %(date_field)s.é   é   rä   )Ú
field_nameZ
date_fieldrã   )r   ry   )rj   râ   r<   r<   r=   rà   €  s    ÿ
ýýz'BaseModelFormSet.get_date_error_messagec                 C   s   t dƒS )Nz*Please correct the duplicate values below.r   r˜   r<   r<   r=   rÜ   Š  s    zBaseModelFormSet.get_form_errorc                 C   s¦   g | _ g | _| jsg S g }| j}| jD ]z}|j}|jd u r<q&||v r`| j |¡ | j||d� q&| ¡ r&| j  ||j	f¡ | | j
|||d�¡ |s&| j |¡ q&|S ©NrÄ   )Zchanged_objectsZdeleted_objectsZinitial_formsrÚ   r8   rH   r5   rË   Úhas_changedZchanged_datarÈ   rÍ   )rj   rª   Zsaved_instancesrÑ   r7   rÊ   r<   r<   r=   rÏ   �  s&    

z&BaseModelFormSet.save_existing_objectsc                 C   sZ   g | _ | jD ]F}| ¡ sq| jr,|  |¡r,q| j  | j||d�¡ |s| j |¡ q| j S rë   )Znew_objectsZextra_formsrì   Ú
can_deleteZ_should_delete_formr5   rÇ   rÍ   )rj   rª   r7   r<   r<   r=   rÐ   §  s    
z!BaseModelFormSet.save_new_objectsc           	         s&  ddl m‰ m}m} | jjj | _}‡ ‡fdd„‰ˆ|ƒsH|j|j	v�r|j
rf|jjjr\dn|jj}n8z |dur€|  ¡ | j}nd}W n tyœ   d}Y n0 t|||fƒr¼|jjj ¡ }n| jj ¡ }| |jjj¡}|jjrö|jj | jjt¡}nt}t||d|d�|j	| jj< tƒ  ||¡ dS )z0Add a hidden field for the object's primary key.r   )r+   Ú
ForeignKeyÚOneToOneFieldc                    s6   | j  p4| jp4t| ˆ ƒp4| jo4| jjo4ˆ| jjjjƒS rÌ   )r)   Zauto_createdr*   r¹   Zparent_linkrM   r&   rH   rI   ©r+   Úpk_is_not_editabler<   r=   rñ   À  s    ÿÿúz7BaseModelFormSet.add_fields.<locals>.pk_is_not_editableNF)r�   r–   r/   )rJ   r+   rî   rï   rM   r&   rH   Ú	_pk_fieldr,   r(   r±   r8   r¦   r§   r³   r¾   r*   r¹   rÂ   ÚusingÚdbrb   r3   r   r   rw   Ú
add_fields)	rj   r7   Úindexrî   rï   rH   Zpk_valuerÃ   r/   r…   rð   r=   rõ   µ  s.    
ÿzBaseModelFormSet.add_fields)NNrˆ   NN)T)T)T)T)T)T)r_   rm   rn   Ú__doc__rM   rÎ   r’   rÒ   rl   r´   r¸   r¼   r¿   r³   rÇ   rÈ   rË   r¨   r«   r™   rŸ   rÛ   rà   rÜ   rÏ   rÐ   rõ   r‡   r<   r<   r…   r=   r   ˆ  s<        úø	"



]


r   rä   Fc                 C   s‚   t |ddƒ}t |d|ƒdu r4t |d|	ƒdu r4tdƒ‚t| |||	||
|||||d�}t||||||||||||d�}| |_||_|S )z8Return a FormSet class for the given Django model class.rp   Nr(   r9   z]Calling modelformset_factory without defining 'fields' or 'exclude' explicitly is prohibited.)
r7   r(   r9   rc   rb   rd   re   rf   rY   rg   )
ÚextraÚmin_numÚmax_numÚ	can_orderrí   Úvalidate_minÚvalidate_maxÚabsolute_maxÚcan_delete_extrarŠ   )rB   r   r#   r   rM   rÎ   )rM   r7   rc   Úformsetrø   rí   rû   rú   r(   r9   rb   rý   rd   re   rf   rY   rù   rü   rg   rþ   rÿ   rŠ   rÎ   ÚmetaÚFormSetr<   r<   r=   r    è  sL    ÿþÿõôr    c                       sl   e Zd ZdZd‡ fdd„	Z‡ fdd„Z‡ fdd	„Zed
d„ ƒZd‡ fdd„	Z	‡ fdd„Z
‡ fdd„Z‡  ZS )r!   z0A formset for child objects related to a parent.NFc           	         sä   |d u r| j j ¡ | _n|| _|| _|d u r4| jj}| jjd ur\|jf i | j j| ji¤Ž}n| 	¡ }| j jh| _
tƒ j||f||dœ|¤Ž | jjjrà| j j| jjjvràt| jjjtƒrÌt| jjjƒ| jj_| jjj | j j¡ d S )N)r�   rF   )Úfkr¹   rM   r8   Úsave_as_newrÂ   rH   rL   r,   ÚnonerÒ   rw   rl   r7   r&   r(   r*   r×   rÖ   r5   )	rj   r0   r1   r8   r  r�   rF   rh   rÃ   r…   r<   r=   rl   2  s    
zBaseInlineFormSet.__init__c                    s   | j r
dS tƒ  ¡ S )Nr   )r  rw   r´   r˜   r…   r<   r=   r´   Q  s    z$BaseInlineFormSet.initial_form_countc                    sÈ   t ƒ j|fi |¤Ž}| jrpt|jdd ƒ}|d ur8d|j_d |j| | jj¡< d |j| | j	j¡< |d urp||j_| j
j}| j	jj| j	jjjjjkr°t| j
| j	jjƒ}t|d|ƒ}t|j
| j	 ¡ |ƒ |S )NÚ_mutableTrH   )rw   r¿   r  rB   r0   r  r2   rò   r,   r  r8   rH   r¹   rê   rM   r&   ÚsetattrZget_attname)rj   rÀ   rh   r7   ZmutableZfk_valuer…   r<   r=   r¿   V  s    z!BaseInlineFormSet._construct_formc                 C   s   | j jj| jd� dd¡S )N)rM   ú+Ú )r  r¹   Zget_accessor_namerM   Úreplace)Úclsr<   r<   r=   Úget_default_prefixm  s    z$BaseInlineFormSet.get_default_prefixTc                    s$   t |j| jj| jƒ tƒ j||d�S rë   )r  r8   r  r,   rw   rÇ   rÆ   r…   r<   r=   rÇ   q  s    zBaseInlineFormSet.save_newc                    sæ   t ƒ  ||¡ | j| jkr,| jj}ddi}n(| jj}dt|j |¡dt| jj	ƒƒi}| jj
j| jj
jjjjkr|| jj
j|d< | jjjrÊ| d¡d ur¨| jj |d ¡}n
| jjj}| ¡ rÊt| j|jd ƒ t| jfi |¤Ž|j|< d S )NÚpk_fieldTrW   Úto_field)rw   rõ   rò   r  r,   rB   r(   r3   r   Zverbose_namer¹   rê   rM   r&   rH   r8   r¦   r§   Ú	get_fieldr-   r  Zattnamer    )rj   r7   rö   r,   rh   r  r…   r<   r=   rõ   x  s$    
ÿÿ	

zBaseInlineFormSet.add_fieldsc                    s   ‡ fdd„|D ƒ}t ƒ  |¡S )Nc                    s   g | ]}|ˆ j jkr|‘qS r<   )r  r,   rÓ   r˜   r<   r=   rV   š  rv   z>BaseInlineFormSet.get_unique_error_message.<locals>.<listcomp>)rw   rÛ   rç   r…   r˜   r=   rÛ   ™  s    z*BaseInlineFormSet.get_unique_error_message)NNNFNN)T)r_   rm   rn   r÷   rl   r´   r¿   Úclassmethodr  rÇ   rõ   rÛ   r‡   r<   r<   r…   r=   r!   /  s         ù
!r!   c                    s@  ddl m‰  |j}ˆrÂ‡fdd„|jD ƒ}t|ƒdkr¨|d }ˆj ¡ ‰t|ˆ ƒr’|jjjj	rn|jjjj
ˆvs’|jjjj	sÀ|jjˆkrÀ|jjˆvrÀtdˆˆjjf ƒ‚n|sÀtd|jjˆf ƒ‚nzˆj ¡ ‰‡ ‡‡fdd„|jD ƒ}t|ƒdkrú|d }nB|�s$|�r
d	S td
|jjˆjjf ƒ‚ntd|jjˆjjf ƒ‚|S )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   )rî   c                    s   g | ]}|j ˆ kr|‘qS r<   )r,   rS   ©Úfk_namer<   r=   rV   «  rv   z$_get_foreign_key.<locals>.<listcomp>rä   z)fk_name '%s' is not a ForeignKey to '%s'.z'%s' has no field named '%s'.c                    sN   g | ]F}t |ˆ ƒr|jjˆksF|jjˆv sF|jjjjr|jjjjˆv r|‘qS r<   )r*   r¹   rM   r&   ÚproxyÚproxy_for_modelrS   )rî   Úparent_listÚparent_modelr<   r=   rV   È  s   
øNz'%s' has no ForeignKey to '%s'.zR'%s' has more than one ForeignKey to '%s'. You must specify a 'fk_name' attribute.)rJ   rî   r&   r(   r²   Zget_parent_listr*   r¹   rM   r  r  r‹   rW   )r  rM   r  Zcan_failr:   Zfks_to_parentr  r<   )rî   r  r  r  r=   Ú_get_foreign_keyž  sh    	
ÿ
üû	
÷

ö
õ
ÿÿÿ
þ
þÿÿþþÿr  ré   c                 C   sd   t | ||d�}|jr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  rä   )r7   rc   r   rø   rí   rû   r(   r9   rù   rú   rb   rü   rý   rd   re   rf   rY   rg   rþ   rÿ   rŠ   rÎ   )r  Úuniquer    r  )r  rM   r7   r   r  r(   r9   rø   rû   rí   rú   rc   rb   rý   rd   re   rf   rY   rù   rü   rg   rþ   rÿ   rŠ   rÎ   r  rh   r  r<   r<   r=   r"   í  s:    !êr"   c                       sH   e Zd ZdZeZdedƒiZdddœ‡ fdd„
Zd	d
„ Z	dd„ Z
‡  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)r  r  c                   s`   || _ || _|| _| j d urB| jr6t| j | jƒ|d< n| j j|d< d|d< tƒ j|i |¤Ž d S )Nr�   Fr–   )Úparent_instancer  r  rB   rH   rw   rl   )rj   r  r  r  Úargsrh   r…   r<   r=   rl   =  s    
zInlineForeignKeyField.__init__c                 C   s`   || j v r| jrd S | jS | jr0t| j| jƒ}n| jj}t|ƒt|ƒkrZt| jd dd�‚| jS )Nr  ©r›   )	r4   r  r  r  rB   rH   ry   r   rY   )rj   rr   Úorigr<   r<   r=   r™   I  s    

ÿzInlineForeignKeyField.cleanc                 C   s   dS )NFr<   )rj   r�   r0   r<   r<   r=   rì   Z  s    z!InlineForeignKeyField.has_changed)r_   rm   rn   r÷   r   r/   ræ   Údefault_error_messagesrl   r™   rì   r‡   r<   r<   r…   r=   r    2  s   ÿr    c                   @   s,   e Zd Zdd„ Zdd„ Zdd„ Zdd„ Zd	S )
ÚModelChoiceIteratorValuec                 C   s   || _ || _d S rÌ   )rr   r8   )rj   rr   r8   r<   r<   r=   rl   _  s    z!ModelChoiceIteratorValue.__init__c                 C   s
   t | jƒS rÌ   )ry   rr   r˜   r<   r<   r=   Ú__str__c  s    z ModelChoiceIteratorValue.__str__c                 C   s
   t | jƒS rÌ   )Úhashrr   r˜   r<   r<   r=   Ú__hash__f  s    z!ModelChoiceIteratorValue.__hash__c                 C   s   t |tƒr|j}| j|kS rÌ   )r*   r  rr   )rj   Úotherr<   r<   r=   Ú__eq__i  s    
zModelChoiceIteratorValue.__eq__N)r_   rm   rn   rl   r   r"  r$  r<   r<   r<   r=   r  ^  s   r  c                   @   s4   e Zd Zdd„ Zdd„ Zdd„ Zdd„ Zd	d
„ ZdS )ÚModelChoiceIteratorc                 C   s   || _ |j| _d S rÌ   )r.   rF   r»   r<   r<   r=   rl   p  s    zModelChoiceIterator.__init__c                 c   sH   | j jd urd| j jfV  | j}|js.| ¡ }|D ]}|  |¡V  q2d S )Nr	  )r.   Úempty_labelrF   Z_prefetch_related_lookupsÚiteratorÚchoice)rj   rF   rÊ   r<   r<   r=   Ú__iter__t  s    zModelChoiceIterator.__iter__c                 C   s   | j  ¡ | jjd urdnd S )Nrä   r   )rF   Úcountr.   r&  r˜   r<   r<   r=   Ú__len__~  s    zModelChoiceIterator.__len__c                 C   s   | j jd up| j ¡ S rÌ   )r.   r&  rF   Úexistsr˜   r<   r<   r=   Ú__bool__„  s    zModelChoiceIterator.__bool__c                 C   s   t | j |¡|ƒ| j |¡fS rÌ   )r  r.   Úprepare_valueÚlabel_from_instance©rj   rÊ   r<   r<   r=   r(  ‡  s    
þzModelChoiceIterator.choiceN)r_   rm   rn   rl   r)  r+  r-  r(  r<   r<   r<   r=   r%  o  s
   
r%  c                
       s°   e Zd ZdZdedƒiZeZdddddddddd	œ	d
d„Zdd„ Z	‡ fdd„Z
dd„ Zdd„ ZeeeƒZdd„ Zdd„ ZeeejƒZ‡ fdd„Zdd„ Zdd„ Zdd„ Z‡  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	---------TNr	  F)	r&  r–   r/   rW   r�   rX   Úto_field_namerO   r•   c       	         K   s^   t j| f|||||dœ|¤Ž |r*|d us:t| jtƒrB|
sBd | _n|| _|| _|	| _|| _d S )N)r–   r/   rW   r�   rX   )	r	   rl   r*   r/   r   r&  rF   rO   r1  )rj   rF   r&  r–   r/   rW   r�   rX   r1  rO   r•   rh   r<   r<   r=   rl   š  s*    ÿúù	
ÿÿzModelChoiceField.__init__c                 C   s   t | jƒr|  ¡ S | jS )z�
        Return ``limit_choices_to`` for this form field.

        If it is a callable, invoke it and return the result.
        )r`   rO   r˜   r<   r<   r=   rG   ¾  s    
z%ModelChoiceField.get_limit_choices_toc                    s*   t t| ƒ |¡}| jd ur&| j ¡ |_|S rÌ   )rw   r   Ú__deepcopy__rF   Úall)rj   ÚmemoÚresultr…   r<   r=   r2  È  s    
zModelChoiceField.__deepcopy__c                 C   s   | j S rÌ   )rÁ   r˜   r<   r<   r=   Ú_get_querysetÏ  s    zModelChoiceField._get_querysetc                 C   s$   |d u rd n|  ¡ | _| j| j_d S rÌ   )r3  rÁ   Úchoicesr/   )rj   rF   r<   r<   r=   Ú_set_querysetÒ  s    zModelChoiceField._set_querysetc                 C   s   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.
        ©ry   r0  r<   r<   r=   r/  Ú  s    z$ModelChoiceField.label_from_instancec                 C   s   t | dƒr| jS |  | ¡S )NÚ_choices)rK   r:  r'  r˜   r<   r<   r=   Ú_get_choicesâ  s    
	zModelChoiceField._get_choicesc                    s.   t |dƒr"| jr| | j¡S |jS tƒ  |¡S ©Nr&   )rK   r1  Zserializable_valuerH   rw   r.  ©rj   rr   r…   r<   r=   r.  ó  s
    
zModelChoiceField.prepare_valuec              
   C   s†   || j v rd S z<| jpd}t|| jjƒr2t||ƒ}| jjf i ||i¤Ž}W n6 tt| jjj	fy€   t
| jd dd|id�‚Y n0 |S )NrH   r  rr   ©r›   Úparams)r4   r1  r*   rF   rM   rB   r3   r‹   ra   ZDoesNotExistr   rY   )rj   rr   Úkeyr<   r<   r=   rº   û  s    


ýzModelChoiceField.to_pythonc                 C   s   t  | |¡S rÌ   )r	   Úvalidater=  r<   r<   r=   rA    s    zModelChoiceField.validatec                 C   s@   | j r
dS |d ur|nd}|d ur&|nd}t|  |¡ƒt|ƒkS )NFr	  )Údisabledry   r.  )rj   r�   r0   Úinitial_valueZ
data_valuer<   r<   r=   rì     s
    zModelChoiceField.has_changed)r_   rm   rn   r÷   ræ   r  r%  r'  rl   rG   r2  r6  r8  ÚpropertyrF   r/  r;  r   Z_set_choicesr7  r.  rº   rA  rì   r‡   r<   r<   r…   r=   r   Ž  s8   ÿÿô$

r   c                       sl   e Zd ZdZeZeZedƒedƒedƒdœZ	‡ fdd„Z
dd	„ Zd
d„ Zdd„ Z‡ fdd„Zdd„ Z‡  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                    s   t ƒ j|fdd i|¤Ž d S )Nr&  )rw   rl   )rj   rF   rh   r…   r<   r=   rl   #  s    z!ModelMultipleChoiceField.__init__c                 C   s   |sg S t |  |¡ƒS rÌ   )rÖ   Ú_check_valuesr=  r<   r<   r=   rº   &  s    z"ModelMultipleChoiceField.to_pythonc                 C   st   |   |¡}| jr(|s(t| jd dd�‚n| js<|s<| j ¡ S t|ttfƒs\t| jd dd�‚|  	|¡}|  
|¡ |S )Nr–   r  rE  )r.  r–   r   rY   rF   r  r*   rÖ   r×   rG  Zrun_validators)rj   rr   rÃ   r<   r<   r=   r™   +  s    



þ

zModelMultipleChoiceField.cleanc              
      sî   | j pd‰ zt|ƒ}W n$ ty:   t| jd dd�‚Y n0 |D ]N}z| jjf i ˆ |i¤Ž W q@ ttfyŒ   t| jd dd|id�‚Y q@0 q@| jjf i dˆ  |i¤Ž}‡ fdd„|D ƒ}|D ](}t|ƒ|vrÀt| jd	 d	d
|id�‚qÀ|S )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.)
        rH   rE  r  rF  r>  z%s__inc                    s   h | ]}t t|ˆ ƒƒ’qS r<   )ry   rB   r¶   ©r@  r<   r=   ru   W  rv   z9ModelMultipleChoiceField._check_values.<locals>.<setcomp>r  rr   )	r1  Ú	frozensetra   r   rY   rF   rL   r‹   ry   )rj   rr   rH   rÃ   ZpksÚvalr<   rH  r=   rG  <  s6    
þýýz&ModelMultipleChoiceField._check_valuesc                    sD   t |dƒr8t|tƒs8t |dƒs8tƒ j‰ ‡ fdd„|D ƒS tƒ  |¡S )Nr)  r&   c                    s   g | ]}ˆ |ƒ‘qS r<   r<   )rT   rt   ©r.  r<   r=   rV   h  rv   z:ModelMultipleChoiceField.prepare_value.<locals>.<listcomp>)rK   r*   ry   rw   r.  r=  r…   rK  r=   r.  a  s    ÿþýz&ModelMultipleChoiceField.prepare_valuec                 C   s`   | j r
dS |d u rg }|d u r"g }t|ƒt|ƒkr6dS dd„ |  |¡D ƒ}dd„ |D ƒ}||kS )NFTc                 S   s   h | ]}t |ƒ’qS r<   r9  ©rT   rr   r<   r<   r=   ru   t  rv   z7ModelMultipleChoiceField.has_changed.<locals>.<setcomp>c                 S   s   h | ]}t |ƒ’qS r<   r9  rL  r<   r<   r=   ru   u  rv   )rB  r²   r.  )rj   r�   r0   Zinitial_setZdata_setr<   r<   r=   rì   k  s    z$ModelMultipleChoiceField.has_changed)r_   rm   rn   r÷   r   r/   r   Zhidden_widgetræ   r  rl   rº   r™   rG  r.  rì   r‡   r<   r<   r…   r=   r     s   ÿû%
r   c                 C   s"   t | dƒo | jjd up | jjd uS r<  )rK   r&   r(   r9   )rZ   r<   r<   r=   Úmodelform_defines_fieldsy  s    
ÿrM  )NN)NN)	NNNNNNNNN)NF)7r÷   Ú	itertoolsr   Zdjango.core.exceptionsr   r   r   r   Zdjango.db.models.utilsr   Zdjango.forms.fieldsr   r	   Zdjango.forms.formsr
   r   Zdjango.forms.formsetsr   r   Zdjango.forms.utilsr   Zdjango.forms.widgetsr   r   r   r   Zdjango.utils.textr   r   Zdjango.utils.translationr   r   ræ   r$   r   r>   r   rP   r   ri   ro   r   r   r#   r   r    r!   r  r"   r    r  r%  r   r   rM  r<   r<   r<   r=   Ú<module>   sÎ   
0
         öônI ]õ
[  dé
Go
Rç
E, 	c