o
    uvXjRÛ  ã                   @   s  d Z ddl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mZ ddlmZmZ ddlmZ dd	lmZmZmZ dd
lmZmZ ddlmZ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ƒ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ƒZ+e)de+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-d=d,d-„Z.e)e-dddd.d'ddddd'dddddd'dfd/d0„Z/G d1d2„ d2eƒZ0G d3d4„ d4ƒZ1G d5d6„ d6eƒZ2G d7d8„ d8e2ƒZ3d9d:„ Z4dS )>z[
Helper functions for creating Form classes from Django models
and database field objects.
é    )ÚOrderedDict)Úchain)ÚNON_FIELD_ERRORSÚ
FieldErrorÚImproperlyConfiguredÚValidationError)ÚChoiceFieldÚField)ÚBaseFormÚDeclarativeFieldsMetaclass)ÚBaseFormSetÚformset_factory)Ú	ErrorList)ÚHiddenInputÚMultipleHiddenInputÚ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 ]O}|jr!t||jƒs!|j|vr"q|dur,|j|vr,q|r4|j|v r4q| 	¡ rK| |j j
j | j| j|  |j¡¡rKqt||jƒrW| |¡ q| |||j ¡ q|D ]}| |||j ¡ qc|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)Ú	django.dbr#   Ú_metaÚcleaned_dataÚfieldsÚeditableÚ
isinstanceÚ	AutoFieldÚnameÚhas_defaultÚfieldÚwidgetÚvalue_omitted_from_dataÚdataÚfilesÚ
add_prefixÚ	FileFieldÚappendÚsave_form_data)	ÚformÚinstancer'   Úexcluder#   Úoptsr&   Úfile_field_listÚf© r<   úP/var/www/html/myproject/venv/lib/python3.10/site-packages/django/forms/models.pyÚconstruct_instance    s,   

"ÿr>   c                 C   sd   | j }i }t|j|j|jƒD ]!}t|ddƒsq|r|j|vrq|r'|j|v r'q| | ¡||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(   F)r%   r   Úconcrete_fieldsÚprivate_fieldsÚmany_to_manyÚgetattrr+   Úvalue_from_object)r7   r'   r8   r9   r0   r;   r<   r<   r=   r   F   s   r   c                 C   sB   t | dƒrt | dƒr|  ¡ }|dur| j |¡| _dS dS dS dS )z=Apply limit_choices_to to the formfield's queryset if needed.ÚquerysetÚget_limit_choices_toN)ÚhasattrrE   rD   Úcomplex_filter)Ú	formfieldÚlimit_choices_tor<   r<   r=   Ú#apply_limit_choices_to_to_formfield_   s   ýrJ   T©Úapply_limit_choices_toc
                   sò  g }g ‰| j }ddlm‰  ‡ fdd„|jD ƒ}tt|j||jƒƒD ]Â}t|ddƒsF|durE|j	|v rEˆdu s;|j	ˆvrEt
d|j	| jf ƒ‚q!|durP|j	|vrPq!ˆrX|j	ˆv rXq!i }|rh|j	|v rh||j	 |d	< |tkss|rw|j	|v rwd
|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< |du r¼|jdi |¤Ž}nt|ƒsÄtdƒ‚||fi |¤Ž}|rÝ|
rÔt|ƒ | |j	|f¡ q!ˆ |j	¡ q!t|ƒ‰|r÷t‡‡‡fdd„|D ƒƒ‰ˆS )a:  
    Return an ``OrderedDict`` 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>Ž   ó    z$fields_for_model.<locals>.<listcomp>r(   FNzH'%s' cannot be specified for %s model form as it is a non-editable fieldr.   TÚlocalizeÚlabelÚ	help_textÚerror_messagesÚ
form_classz1formfield_callback must be a function or callablec                    s2   g | ]}ˆ rˆ r|ˆ vr|ˆvr|ˆ  |¡f‘qS r<   )ÚgetrM   )r8   Ú
field_dictÚignoredr<   r=   rP   »   s    ÿÿÿr<   )r%   Údjango.db.models.fieldsr	   r@   Úsortedr   r?   rA   rB   r+   r   Ú__name__r   rH   ÚcallableÚ	TypeErrorrJ   r4   r   )Úmodelr'   r8   ÚwidgetsÚformfield_callbackÚlocalized_fieldsÚlabelsÚ
help_textsrU   Úfield_classesrL   Ú
field_listr9   Úsortable_private_fieldsr;   ÚkwargsrH   r<   )rO   r8   rX   rY   r=   r   g   s`   "ÿÿÿ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 ƒ| _	d S )
Nr_   r'   r8   r`   rb   rc   rd   rU   re   )
rB   r_   r'   r8   r`   rb   rc   rd   rU   re   )Ú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                    s~  d }|D ]}t |dƒrt |jdƒr|jj} nq| d|¡}tt| ƒ | |||¡}|tfkr0|S tt	|dd ƒƒ }|_
dD ]}	t	||	ƒ}
t|
tƒrZ|
tkrZd|j|	|
dœ }t|ƒ‚q=|jr·|jd u rn|jd u rntd| ƒ‚|jtkrvd |_t|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ÚMetara   )r'   r8   rb   zP%(model)s.Meta.%(opt)s cannot be a string. Did you mean to type: ('%(value)s',)?)r_   ÚoptÚvaluez|Creating a ModelForm without either the 'fields' attribute or the 'exclude' attribute is prohibited; form %s needs updating.FrK   c                 S   s   h | ]\}}|s|’qS r<   r<   )rN   ÚkÚvr<   r<   r=   Ú	<setcomp>  s    z-ModelFormMetaclass.__new__.<locals>.<setcomp>z&Unknown field(s) (%s) specified for %sz, )"rF   rq   ra   ÚpopÚsuperrp   Ú__new__r   ri   rB   r%   r)   Ústrr   r\   r^   r_   r'   r8   r   r   r`   rb   rc   rd   rU   re   ÚitemsÚ
differenceÚdeclared_fieldsÚjoinr   ÚupdateÚbase_fields)Úmcsr+   ÚbasesÚattrsÚbase_formfield_callbackÚbra   Ú	new_classr9   rr   rs   Úmsgr'   Únone_model_fieldsÚmissing_fieldsÚmessage©Ú	__class__r<   r=   ry   Ï   sb   €

ýÿ€þÿ

û	
ÿzModelFormMetaclass.__new__)r\   rn   ro   ry   Ú__classcell__r<   r<   r‹   r=   rp   Î   s    rp   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 r| ¡ | _i }n|	| _t|	|j|jƒ}|d ur,| |¡ d| _t	ƒ j
|||||||||
|d�
 | j ¡ D ]}t|ƒ qDd S )Nz'ModelForm has no model class specified.F)Úuse_required_attributeÚrenderer)r%   r_   Ú
ValueErrorr7   r   r'   r8   r   Ú_validate_uniquerx   rl   ÚvaluesrJ   )rj   r0   r1   Úauto_idÚprefixÚinitialÚerror_classÚlabel_suffixÚempty_permittedr7   r�   r�   r9   Úobject_datarH   r‹   r<   r=   rl     s&   


ý
ÿzBaseModelForm.__init__c                 C   sÊ   g }| j jjD ][}|j}|| jvr| |j¡ q| jjr)|| jjvr)| |j¡ q| jjr:|| jjv r:| |j¡ q|| jv rF| |j¡ q| j| }| j |¡}|j	sb|j
sb||jv rb| |j¡ q|S )z‘
        For backwards-compatibility, exclude several types of fields from model
        validation. See tickets #12507, #12521, #12553.
        )r7   r%   r'   r+   r4   r8   Ú_errorsr&   rW   ÚblankÚrequiredÚempty_values)rj   r8   r;   r-   Ú
form_fieldÚfield_valuer<   r<   r=   Ú_get_validation_exclusions4  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 ]8\}}|tkr*|jr*t|jv r*|jt }n|| jv r6| j| j}nq|D ]}t|tƒrK|j	|v rK||j	 |_
q9q|  d |¡ d S )NÚ
error_dict)r%   rF   r¤   r   r{   rU   r'   r)   r   ÚcoderŠ   Ú	add_error)rj   Úerrorsr9   r¤   r-   ÚmessagesrU   rŠ   r<   r<   r=   Ú_update_errorsa  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 }~ww z
| jj|dd� W n t
ya } z|  |¡ W Y d }~nd }~ww | jrk|  ¡  d S d S )NF)r8   Úvalidate_unique)r%   r¡   r'   r{   r)   ÚInlineForeignKeyFieldr4   r>   r7   r8   r   r©   Ú
full_cleanr’   rª   )rj   r9   r8   r+   r-   Úer<   r<   r=   Ú_post_clean}  s*   	

€€ÿ€ÿÿzBaseModelForm._post_cleanc              
   C   sN   |   ¡ }z
| jj|d� W dS  ty& } z|  |¡ W Y d}~dS d}~ww )z‚
        Call the instance's validate_unique() method and update the form's
        validation errors if any were raised.
        ©r8   N)r¡   r7   rª   r   r©   )rj   r8   r­   r<   r<   r=   rª   ›  s   €ÿzBaseModelForm.validate_uniquec                 C   s€   | j }| jj}| jj}| jj}t|j|jƒD ]'}t|dƒsq|r&|j	|vr&q|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.
        r5   N)r&   r%   r8   r'   r7   r   rA   r@   rF   r+   r5   )rj   r&   r8   r'   r9   r;   r<   r<   r=   Ú	_save_m2m¦  s   

€øzBaseModelForm._save_m2mTc                 C   sZ   | j rtd| jjj| jjjrdf ƒ‚df ƒ‚|r&| 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.
        z8The %s could not be %s because the data didn't validate.ÚcreatedÚchanged)
r§   r‘   r7   r%   Úobject_nameÚ_stateÚaddingÚsaver°   Úsave_m2m)rj   Úcommitr<   r<   r=   r¶   »  s    þÿþÿ
ÿzBaseModelForm.save©T)r\   rn   ro   r   rl   r¡   r£   r©   r®   rª   r°   r¶   Úalters_datar�   r<   r<   r‹   r=   r     s    ý)
r   c                   @   s   e Zd ZdS )r   N)r\   rn   ro   r<   r<   r<   r=   r   Õ  s    r   )Ú	metaclassc                 C   s  d| i}|dur||d< |dur||d< |dur||d< |dur$||d< |dur,||d< |dur4||d< |	dur<|	|d	< |
durD|
|d
< t |dƒrM|jfnd}td||ƒ}|r\t|ƒ|_| jd }||dœ}t|ddƒdu rzt|ddƒdu rztdƒ‚t|ƒ||f|ƒS )aÿ  
    Return a ModelForm containing form fields for the given model.

    ``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.
    r_   Nr'   r8   r`   rb   rc   rd   rU   re   rq   r<   ÚForm)rq   ra   zZCalling modelform_factory without defining 'fields' or 'exclude' explicitly is prohibited.)rF   rq   ÚtypeÚstaticmethodra   r\   rB   r   )r_   r6   r'   r8   ra   r`   rb   rc   rd   rU   re   rƒ   r‚   rq   Ú
class_nameÚform_class_attrsr<   r<   r=   r!   Ù  s>   %

þÿr!   c                       sÜ   e 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d+d$d%„Zd+d&d'„Z‡ fd(d)„Z‡  ZS ),r   zO
    A ``FormSet`` for editing a queryset and/or adding new objects to it.
    NrŽ   )r–   c                   s0   || _ || _tƒ jdi ||||dœ|¥¤Ž d S )N)r0   r1   r”   r•   r<   )rD   Úinitial_extrarx   rl   )rj   r0   r1   r”   r•   rD   r–   rh   r‹   r<   r=   rl   5  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_querysetrx   Ú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<   ©Úpk©rN   Úor<   r<   r=   Ú
<dictcomp>C  ó    z5BaseModelFormSet._existing_object.<locals>.<dictcomp>)rF   rÄ   rÆ   rW   )rj   rÈ   r<   r<   r=   Ú_existing_objectA  s   
z!BaseModelFormSet._existing_objectc                 C   s$   |j dur|j  ¡ }|j dus|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_fieldÚget_related_fieldÚ	to_python©rj   r-   r<   r<   r=   Ú_get_to_pythonF  s   


ÿzBaseModelFormSet._get_to_pythonc                    s  ||   ¡ k }|rR| jrId|  |¡| jjjjf }z| j| }W n	 ty(   Y nDw |  	| jjj¡}z||ƒ}W n	 t
y@   Y n,w |  |¡|d< n#|  ¡ | |d< n| jrlz| j||   ¡   |d< W n	 tyk   Y nw tƒ j|fi |¤Ž}|r‚d|j| jjjj _|S )Nz%s-%sr7   r–   T)rÅ   rÂ   r2   r_   r%   rÈ   r+   r0   ÚKeyErrorrÒ   r   rÍ   rÄ   rÁ   Ú
IndexErrorrx   Ú_construct_formr'   r�   )rj   Úirh   Úpk_requiredÚpk_keyrÈ   rÐ   r6   r‹   r<   r=   rÕ   O  s6   ýýÿz BaseModelFormSet._construct_formc                 C   sL   t | dƒs#| jd ur| j}n| jj ¡ }|js | | jjjj	¡}|| _
| j
S )NÚ	_queryset)rF   rD   r_   Ú_default_managerrÄ   ÚorderedÚorder_byr%   rÈ   r+   rÙ   )rj   Úqsr<   r<   r=   rÄ   q  s   

zBaseModelFormSet.get_querysetTc                 C   s   |j |d�S )z8Save and return a new model instance for the given form.©r¸   ©r¶   ©rj   r6   r¸   r<   r<   r=   Úsave_new„  ó   zBaseModelFormSet.save_newc                 C   s   |j |d�S )z>Save and return an existing model instance for the given form.rÞ   rß   )rj   r6   r7   r¸   r<   r<   r=   Úsave_existingˆ  râ   zBaseModelFormSet.save_existingc                 C   s   |r|  ¡  dS dS )z#Deletes an existing model instance.N)Údelete)rj   Úobjr¸   r<   r<   r=   Údelete_existingŒ  s   ÿz BaseModelFormSet.delete_existingc                    s0   |sg ˆ _ ‡ fd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 rm   )Úsaved_formsr·   )r6   r¢   r<   r=   r·   ™  s   

ÿz'BaseModelFormSet.save.<locals>.save_m2m)rç   r·   Úsave_existing_objectsÚsave_new_objects)rj   r¸   r·   r<   r¢   r=   r¶   ‘  s
   zBaseModelFormSet.savec                 C   s   |   ¡  d S rm   )rª   r¢   r<   r<   r=   r£   ¡  ó   zBaseModelFormSet.cleanc                    sä  t ƒ }t ƒ }ˆj‰‡fdd„ˆjD ƒ}|D ]‰ ˆ  ¡ }ˆ jj|d�\}}| |¡ | |¡ qg }|D ]O\}}	t ƒ }
|D ]E‰ ‡ ‡fdd„|	D ƒ}tdd„ |D ƒƒ}|r�d |vr�||
v r|| ˆ 	|	¡¡ ˆ 
ˆ ¡ g¡ˆ jt< |	D ]}|ˆ jv r{ˆ j|= qp|
 |¡ q<q3|D ]d}t ƒ }
|\}}}}|D ]V‰ ˆ 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ã| ˆ |¡¡ ˆ 
ˆ ¡ g¡ˆ jt< ˆ j|= |
 |¡ q’q…|rðt|ƒ‚d S )Nc                    s    g | ]}|  ¡ r|ˆ vr|‘qS r<   )Úis_valid)rN   r6   )Úforms_to_deleter<   r=   rP   ©  s     z4BaseModelFormSet.validate_unique.<locals>.<listcomp>r¯   c                 3   s2   � | ]}|ˆ j v r|ˆjv r|nˆ j | V  qd S rm   )r&   Úunique_fields©rN   r-   )r6   rj   r<   r=   Ú	<genexpr>¶  s   € ÿþz3BaseModelFormSet.validate_unique.<locals>.<genexpr>c                 s   s8   � | ]}t |d ƒr| ¡ n
t|tƒrt|ƒn|V  qdS )Ú_get_pk_valN)rF   rð   r)   ÚlistÚtuple)rN   Údr<   r<   r=   rï   »  s   € ý
üÚdate)ÚsetÚdeleted_formsÚformsr¡   r7   Ú_get_unique_checksr   rò   r4   Úget_unique_error_messager—   Úget_form_errorr›   r   r&   ÚaddÚyearÚmonthÚdayrB   Úget_date_error_messager   )rj   Úall_unique_checksÚall_date_checksÚvalid_formsr8   Úunique_checksÚdate_checksr§   ÚuclassÚunique_checkÚ	seen_dataÚrow_datar-   Ú
date_checkÚlookupÚ
unique_forrô   Ú	date_datar0   r<   )r6   rì   rj   r=   rª   ¤  sf   
þü
€
€ç

€êÿz BaseModelFormSet.validate_uniquec                 C   s:   t |ƒdkrtdƒd|d i S tdƒdt|tdƒƒi 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ù   í  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_nameÚ
date_fieldr
  )r   rz   )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 ]=}|j}|jd u rq||v r0| j |¡ | j||d� q| ¡ rP| j  ||j	f¡ | | j
|||d�¡ |sP| j |¡ q|S ©NrÞ   )Úchanged_objectsÚdeleted_objectsÚinitial_formsrö   r7   rÈ   r4   ræ   Úhas_changedÚchanged_datarã   rç   )rj   r¸   Úsaved_instancesrì   r6   rå   r<   r<   r=   rè     s(   

€z&BaseModelFormSet.save_existing_objectsc                 C   sZ   g | _ | jD ]#}| ¡ sq| jr|  |¡rq| j  | j||d�¡ |s)| j |¡ q| j S r  )Únew_objectsÚextra_formsr  Ú
can_deleteÚ_should_delete_formr4   rá   rç   )rj   r¸   r6   r<   r<   r=   ré     s   
€z!BaseModelFormSet.save_new_objectsc           	         s$  ddl m‰ m}m} | jjj | _}‡ ‡fdd„‰ˆ|ƒs#|j|j	vr‰|j
r2|jjjr-dn|jj}nz|dur?|  ¡ | j}nd}W n tyM   d}Y nw t|||fƒr]|jjj ¡ }n| jj ¡ }| |jjj¡}|jjrz|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*   ÚOneToOneFieldÚ
ForeignKeyc                    s6   | j  p| jpt| ˆ ƒp| jo| jjoˆ| jjjjƒS rm   )r(   Úauto_createdr)   rÎ   Úparent_linkr_   r%   rÈ   rÇ   ©r*   Úpk_is_not_editabler<   r=   r%  6  s   ýz7BaseModelFormSet.add_fields.<locals>.pk_is_not_editableNF)r–   r�   r.   )Údjango.db.modelsr*   r   r!  r_   r%   rÈ   Ú	_pk_fieldr+   r'   rÂ   r7   r´   rµ   rÄ   rÔ   r)   rÎ   rÚ   ÚusingÚdbr`   rW   r   r   rx   Ú
add_fields)	rj   r6   Úindexr   r!  rÈ   Úpk_valuerÝ   r.   r‹   r$  r=   r*  ,  s.   €ÿzBaseModelFormSet.add_fields)NNrŽ   NNr¹   )r\   rn   ro   Ú__doc__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   ,  s6    ÿÿ	"



I



r   r  Fc                 C   sv   t |ddƒ}t |d|ƒdu rt |d|	ƒdu rtdƒ‚t| |||	||
|||||d�}t|||||||||d�	}| |_|S )z8Return a FormSet class for the given Django model class.rq   Nr'   r8   z]Calling modelformset_factory without defining 'fields' or 'exclude' explicitly is prohibited.)
r6   r'   r8   ra   r`   rb   rc   rd   rU   re   )ÚextraÚmin_numÚmax_numÚ	can_orderr  Úvalidate_minÚvalidate_max)rB   r   r!   r   r_   )r_   r6   ra   Úformsetr.  r  r1  r0  r'   r8   r`   r3  rb   rc   rd   rU   r/  r2  re   ÚmetaÚFormSetr<   r<   r=   r   X  s$   ÿ
üþr   c                       sp   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 r| jj}| jjd ur.|jdi | j j| ji¤Ž}n| 	¡ }| j jh| _
tƒ j||f||dœ|¤Ž | jjjrr| j j| jjjvrtt| jjjtƒrft| jjjƒ| jj_| jjj | j j¡ d S d S d S )N)r•   rD   r<   )ÚfkrÎ   r_   r7   Úsave_as_newrÚ   rÈ   Úfilterr+   Únonerí   rx   rl   r6   r%   r'   r)   rò   rñ   r4   )	rj   r0   r1   r7   r8  r•   rD   rh   rÝ   r‹   r<   r=   rl   w  s    ýzBaseInlineFormSet.__init__c                    s   | j rdS tƒ  ¡ S )Nr   )r8  rx   rÅ   r¢   r‹   r<   r=   rÅ   Ž  s   
z$BaseInlineFormSet.initial_form_countc                    sÈ   t ƒ j|fi |¤Ž}| jr8t|jdd ƒ}|d urd|j_d |j| | jj¡< d |j| | j	j¡< |d ur8||j_| j
j}| j	jj| j	jjjjjkrXt| j
| j	jjƒ}t|d|ƒ}t|j
| j	 ¡ |ƒ |S )NÚ_mutableTrÈ   )rx   rÕ   r8  rB   r0   r;  r2   r'  r+   r7  r7   rÈ   rÎ   r  r_   r%   ÚsetattrÚget_attname)rj   rÖ   rh   r6   ÚmutableÚfk_valuer‹   r<   r=   rÕ   “  s   z!BaseInlineFormSet._construct_formc                 C   s   | j jj| jd� dd¡S )N)r_   ú+Ú )r7  rÎ   Úget_accessor_namer_   Úreplace)Úclsr<   r<   r=   Úget_default_prefixª  s   z$BaseInlineFormSet.get_default_prefixTc                    s$   t |j| jj| jƒ tƒ j||d�S r  )r<  r7   r7  r+   rx   rá   rà   r‹   r<   r=   rá   ®  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re| d¡d urT| jj |d ¡}n| jjj}| ¡ ret| j|jd ƒ t| jfi |¤Ž|j|< d S )NÚpk_fieldTrS   Úto_field)rx   r*  r'  r7  r+   rB   r'   rW   r   Úverbose_namerÎ   r  r_   r%   rÈ   r7   r´   rµ   Ú	get_fieldr,   r<  Úattnamer«   )rj   r6   r+  r+   rh   rG  r‹   r<   r=   r*  µ  s    
ÿ

zBaseInlineFormSet.add_fieldsc                    s   ‡ fdd„|D ƒ}t ƒ  |¡S )Nc                    s   g | ]
}|ˆ j jkr|‘qS r<   )r7  r+   rî   r¢   r<   r=   rP   Õ  s    z>BaseInlineFormSet.get_unique_error_message.<locals>.<listcomp>)rx   rù   r  r‹   r¢   r=   rù   Ô  s   z*BaseInlineFormSet.get_unique_error_message)NNNFNNr¹   )r\   rn   ro   r-  rl   rÅ   rÕ   ÚclassmethodrE  rá   r*  rù   r�   r<   r<   r‹   r=   r   u  s    ÿ
r   c                    s  ddl m‰  |j}ˆrM‡fdd„|jD ƒ}t|ƒdkr?|d }t|ˆ ƒr3|jjˆkr=|jjˆj ¡ vr=t	dˆˆjj
f ƒ‚|S |sKt	d|jj
ˆf ƒ‚|S ‡ ‡fdd„|jD ƒ}t|ƒdkrd|d }|S |sv|rjd	S t	d
|jj
ˆjj
f ƒ‚t	d|jj
ˆjj
f ƒ‚)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+   rM   ©Úfk_namer<   r=   rP   å  rQ   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                    s8   g | ]}t |ˆ ƒr|jjˆks|jjˆj ¡ v r|‘qS r<   )r)   rÎ   r_   r%   Úget_parent_listrM   )r!  Úparent_modelr<   r=   rP   ô  s    þýNz'%s' has no ForeignKey to '%s'.z*'%s' has more than one ForeignKey to '%s'.)r&  r!  r%   r'   rÃ   r)   rÎ   r_   rN  r‘   rS   )rO  r_   rM  Úcan_failr9   Úfks_to_parentr7  r<   )r!  rM  rO  r=   Ú_get_foreign_keyÙ  sN   	
ÿ"áÿçÿðþÿþÿrR  r  c                 C   s¤   t | ||d�}|jrd}
i d|“d|“d|“d|“d|	“d|“d	|“d
|“d|“d|
“d|“d|“d|“d|“d|“d|“d|“d|i¥}t|fi |¤Ž}||_|S )z¤
    Return an ``InlineFormSet`` for the given kwargs.

    ``fk_name`` must be provided if ``model`` has more than one ``ForeignKey``
    to ``parent_model``.
    rL  r  r6   ra   r4  r.  r  r1  r'   r8   r/  r0  r`   r2  r3  rb   rc   rd   rU   re   )rR  Úuniquer   r7  )rO  r_   r6   r4  rM  r'   r8   r.  r1  r  r0  ra   r`   r3  rb   rc   rd   rU   r/  r2  re   r7  rh   r6  r<   r<   r=   r      sV   ÿþýüûúùø	÷
öõôóòñðïî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)rF  rG  c                   s`   || _ || _|| _| j d ur!| jrt| j | jƒ|d< n| j j|d< d|d< tƒ j|i |¤Ž d S )Nr–   Fr�   )Úparent_instancerF  rG  rB   rÈ   rx   rl   )rj   rU  rF  rG  Úargsrh   r‹   r<   r=   rl   F  s   
zInlineForeignKeyField.__init__c                 C   s`   || j v r| jr
d S | jS | jrt| j| jƒ}n| jj}t|ƒt|ƒkr-t| jd dd�‚| jS )NrT  ©r¥   )	rž   rF  rU  rG  rB   rÈ   rz   r   rU   )rj   rs   Úorigr<   r<   r=   r£   R  s   
zInlineForeignKeyField.cleanc                 C   s   dS ©NFr<   )rj   r–   r0   r<   r<   r=   r  a  s   z!InlineForeignKeyField.has_changed)r\   rn   ro   r-  r   r.   r  Údefault_error_messagesrl   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 rm   )r-   rD   rÑ   r<   r<   r=   rl   f  s   zModelChoiceIterator.__init__c                 c   sJ   � | j jd urd| j jfV  | j}|js| ¡ }|D ]}|  |¡V  qd S )NrA  )r-   Úempty_labelrD   Ú_prefetch_related_lookupsÚiteratorÚchoice)rj   rD   rå   r<   r<   r=   Ú__iter__j  s   €ÿzModelChoiceIterator.__iter__c                 C   s    | j  ¡ | jjd urd S d S )Nr  r   )rD   Úcountr-   r\  r¢   r<   r<   r=   Ú__len__t  s    zModelChoiceIterator.__len__c                 C   s   | j jd up
| j ¡ S rm   )r-   r\  rD   Úexistsr¢   r<   r<   r=   Ú__bool__z  s   zModelChoiceIterator.__bool__c                 C   s   | j  |¡| j  |¡fS rm   )r-   Úprepare_valueÚlabel_from_instance©rj   rå   r<   r<   r=   r_  }  s   zModelChoiceIterator.choiceN)r\   rn   ro   rl   r`  rb  rd  r_  r<   r<   r<   r=   r[  e  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
„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.rT  zGSelect a valid choice. That choice is not one of the available choices.z	---------TNrA  )r\  r�   r.   rS   r–   rT   Úto_field_namerI   c                K   sN   |r
|d ur
d | _ n|| _ tj| f|||||dœ|
¤Ž || _|	| _|| _d S )N)r�   r.   rS   r–   rT   )r\  r	   rl   rD   rI   rh  )rj   rD   r\  r�   r.   rS   r–   rT   rh  rI   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]   rI   r¢   r<   r<   r=   rE   ž  s   
z%ModelChoiceField.get_limit_choices_toc                    s*   t t| ƒ |¡}| jd ur| j ¡ |_|S rm   )rx   r   Ú__deepcopy__rD   Úall)rj   ÚmemoÚresultr‹   r<   r=   ri  ¨  s   
zModelChoiceField.__deepcopy__c                 C   s   | j S rm   )rÙ   r¢   r<   r<   r=   Ú_get_queryset¯  s   zModelChoiceField._get_querysetc                 C   s$   |d u rd n|  ¡ | _| j| j_d S rm   )rj  rÙ   Úchoicesr.   )rj   rD   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.
        ©rz   rg  r<   r<   r=   rf  º  s   z$ModelChoiceField.label_from_instancec                 C   s   t | dƒr| jS |  | ¡S )NÚ_choices)rF   rq  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%   )rF   rh  Úserializable_valuerÈ   rx   re  ©rj   rs   r‹   r<   r=   re  Ó  s
   
zModelChoiceField.prepare_valuec              
   C   sb   || j v rd S z| jpd}| jjdi ||i¤Ž}W |S  tt| jjjfy0   t| j	d dd�‚w )NrÈ   rT  rW  r<   )
rž   rh  rD   rW   r‘   r^   r_   ÚDoesNotExistr   rU   )rj   rs   Úkeyr<   r<   r=   rÐ   Û  s   

þÿzModelChoiceField.to_pythonc                 C   s   t  | |¡S rm   )r	   Úvalidateru  r<   r<   r=   rx  å  rê   zModelChoiceField.validatec                 C   s@   | j rdS |d ur|nd}|d ur|nd}t|  |¡ƒt|ƒkS )NFrA  )Údisabledrz   re  )rj   r–   r0   Úinitial_valueÚ
data_valuer<   r<   r=   r  è  s
   zModelChoiceField.has_changed)r\   rn   ro   r-  r  rZ  r[  r^  rl   rE   ri  rm  ro  ÚpropertyrD   rf  rr  r   Ú_set_choicesrn  re  rÐ   rx  r  r�   r<   r<   r‹   r=   r   �  s*    ÿþ


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.z"%(pk)s" is not a valid value.)rñ   rT  Úinvalid_pk_valuec                    s   t ƒ j|fdd i|¤Ž d S )Nr\  )rx   rl   )rj   rD   rh   r‹   r<   r=   rl   û  s   z!ModelMultipleChoiceField.__init__c                 C   s   |sg S t |  |¡ƒS rm   )rñ   Ú_check_valuesru  r<   r<   r=   rÐ   þ  s   z"ModelMultipleChoiceField.to_pythonc                 C   sr   |   |¡}| jr|st| jd dd�‚| js|s| j ¡ S t|ttfƒs-t| jd dd�‚|  	|¡}|  
|¡ |S )Nr�   rW  rñ   )re  r�   r   rU   rD   r:  r)   rñ   rò   r  Úrun_validators)rj   rs   rÝ   r<   r<   r=   r£     s   





zModelMultipleChoiceField.cleanc              
      sä   | j pd‰ zt|ƒ}W n ty   t| jd dd�‚w |D ]$}z| jjdi ˆ |i¤Ž W q ttfyB   t| jd dd|id�‚w | jjdi dˆ  |i¤Ž}‡ fdd„|D ƒ}|D ]}t|ƒ|vrot| 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.)
        rÈ   rñ   rW  r~  )r¥   Úparamsz%s__inc                    s   h | ]	}t t|ˆ ƒƒ’qS r<   )rz   rB   rÉ   ©rw  r<   r=   rv   ,  rQ   z9ModelMultipleChoiceField._check_values.<locals>.<setcomp>rT  rs   Nr<   )	rh  Ú	frozensetr^   r   rU   rD   r9  r‘   rz   )rj   rs   rÈ   rÝ   ÚpksÚvalr<   r‚  r=   r    s<   
þþýÿýÿz&ModelMultipleChoiceField._check_valuesc                    sD   t |dƒrt|tƒst |dƒstƒ j‰ ‡ fdd„|D ƒS tƒ  |¡S )Nr`  r%   c                    s   g | ]}ˆ |ƒ‘qS r<   r<   )rN   ru   ©re  r<   r=   rP   ;  rÌ   z:ModelMultipleChoiceField.prepare_value.<locals>.<listcomp>)rF   r)   rz   rx   re  ru  r‹   r†  r=   re  6  s   
ÿþz&ModelMultipleChoiceField.prepare_valuec                 C   s`   | j rdS |d u rg }|d u rg }t|ƒt|ƒkrdS dd„ |  |¡D ƒ}dd„ |D ƒ}||kS )NFTc                 S   ó   h | ]}t |ƒ’qS r<   rp  ©rN   rs   r<   r<   r=   rv   G  rÌ   z7ModelMultipleChoiceField.has_changed.<locals>.<setcomp>c                 S   r‡  r<   rp  rˆ  r<   r<   r=   rv   H  rÌ   )ry  rÃ   re  )rj   r–   r0   Úinitial_setÚdata_setr<   r<   r=   r  >  s   z$ModelMultipleChoiceField.has_changed)r\   rn   ro   r-  r   r.   r   Úhidden_widgetr  rZ  rl   rÐ   r£   r  re  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 rs  )rF   r%   r'   r8   )rV   r<   r<   r=   Úmodelform_defines_fieldsL  s   

þrŒ  )NN)	NNNNNNNNNrY  )5r-  Úcollectionsr   Ú	itertoolsr   Údjango.core.exceptionsr   r   r   r   Údjango.forms.fieldsr   r	   Údjango.forms.formsr
   r   Údjango.forms.formsetsr   r   Údjango.forms.utilsr   Údjango.forms.widgetsr   r   r   Údjango.utils.textr   r   Údjango.utils.translationr   r   r  r"   r   r>   r   rJ   r   ri   rp   r   r   r!   r   r   r   rR  r    r«   r[  r   r   rŒ  r<   r<   r<   r=   Ú<module>   st    

&ýýZH @
ýS  .
û
d7
ú,)o\