o
    ˜¨Êh¸ë  ã                   @   s:  d Z ddlmZ ddlmZmZmZmZ ddlm	Z	m
Z
 ddlmZmZ ddlmZmZ ddlmZ ddlmZmZmZmZ dd	lmZ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ƒ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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dddd'fd/d0„Z.G d1d2„ d2e
ƒZ/G d3d4„ d4ƒZ0G d5d6„ d6ƒZ1G d7d8„ d8e	ƒZ2G d9d:„ d:e2ƒZ3d;d<„ Z4dS )@z[
Helper functions for creating Form classes from Django models
and database field objects.
é    )Úchain)ÚNON_FIELD_ERRORSÚ
FieldErrorÚImproperlyConfiguredÚValidationError)Ú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r"q|dur,|j|vr,q|r4|j|v r4q| 	¡ rX| |j j
j | j| j|  |j¡¡rX| |j¡| |j j
jv rXqt||jƒrd| |¡ q| |||j ¡ q|D ]}| |||j ¡ qp|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ÚgetÚempty_valuesÚ	FileFieldÚappendÚsave_form_data)	ÚformÚinstancer(   Úexcluder$   Úoptsr'   Úfile_field_listÚf© r?   úE/var/www/html/env/lib/python3.10/site-packages/django/forms/models.pyÚconstruct_instance-   s:   
ÿ
þ
ÿÿþrA   c                 C   sh   | j }i }t|j|j|jƒD ]#}t|ddƒsq|du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)   FN)r&   r   Úconcrete_fieldsÚprivate_fieldsÚmany_to_manyÚgetattrr,   Úvalue_from_object)r:   r(   r;   r<   r1   r>   r?   r?   r@   r   ]   s   r   c                 C   s�   ddl m}m}m} t| dƒrBt| dƒrD|  ¡ }|rF|}t||ƒs(|di |¤Ž}|||dƒd�M }| j || jj	j
 |¡ƒ¡| _dS dS dS dS )	z=Apply limit_choices_to to the formfield's queryset if needed.r   )ÚExistsÚOuterRefÚQÚquerysetÚget_limit_choices_toÚpk©rL   Nr?   )Údjango.db.modelsrG   rH   rI   ÚhasattrrK   r*   rJ   ÚfilterÚmodelÚ_base_manager)Ú	formfieldrG   rH   rI   Úlimit_choices_toÚcomplex_filterr?   r?   r@   Ú#apply_limit_choices_to_to_formfieldv   s   

ÿørV   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ƒ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	< q!ˆ |j	¡ q!|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/   TÚ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?   )r4   rY   )r;   Ú
field_dictÚignoredr?   r@   Ú
<dictcomp>í   s    ý
þz$fields_for_model.<locals>.<dictcomp>r?   )r&   rN   r   rC   Úsortedr   rB   rD   rE   r,   r   Ú__name__r   rS   ÚcallableÚ	TypeErrorrV   r7   )rQ   r(   r;   ÚwidgetsÚformfield_callbackÚlocalized_fieldsÚlabelsÚ
help_textsr`   Úfield_classesrX   r<   Úsortable_private_fieldsr>   ÚkwargsrS   r?   )r[   r;   rb   rc   r@   r   ‡   sn   ,
ÿÿ

ÿÿÿ
þ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 )
NrQ   r(   r;   ri   rk   rl   rm   r`   rn   )
rE   rQ   r(   r;   ri   rk   rl   rm   r`   rn   )ÚselfÚoptionsr?   r?   r@   Ú__init__ö   s   zModelFormOptions.__init__©N)rf   Ú
__module__Ú__qualname__rt   r?   r?   r?   r@   rq   õ   s    rq   c                       s   e Zd Z‡ fdd„Z‡  ZS )ÚModelFormMetaclassc                    sz  d }|D ]}t |dƒrt |jdƒr|jj} nq| d|¡}tƒ  | |||¡}|tfkr.|S tt|dd ƒƒ }|_	dD ]}	t||	ƒ}
t
|
tƒrX|
tkrXd|j|	|
dœ }t|ƒ‚q;|jrµ|jd u rl|jd u rltd| ƒ‚|jtkrtd |_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ÚMetarj   )r(   r;   rk   zP%(model)s.Meta.%(opt)s cannot be a string. Did you mean to type: ('%(value)s',)?)rQ   ÚoptÚvaluez|Creating a ModelForm without either the 'fields' attribute or the 'exclude' attribute is prohibited; form %s needs updating.FrW   c                 S   s   h | ]\}}|s|’qS r?   r?   )rZ   ÚkÚvr?   r?   r@   Ú	<setcomp>B  s    z-ModelFormMetaclass.__new__.<locals>.<setcomp>z&Unknown field(s) (%s) specified for %sz, )!rO   ry   rj   ÚpopÚsuperÚ__new__r   rq   rE   r&   r*   Ústrr   rf   rh   rQ   r(   r;   r   r   ri   rk   rl   rm   r`   rn   ÚitemsÚ
differenceÚdeclared_fieldsÚjoinr   ÚupdateÚbase_fields)Úmcsr,   ÚbasesÚattrsÚbase_formfield_callbackÚbrj   Ú	new_classr<   rz   r{   Úmsgr(   Únone_model_fieldsÚmissing_fieldsÚmessage©Ú	__class__r?   r@   r�     sn   €

ýþÿ	€þÿ
ôzModelFormMetaclass.__new__)rf   rv   rw   r�   Ú__classcell__r?   r?   r“   r@   rx     s    rx   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&   rQ   Ú
ValueErrorr:   r   r(   r;   r‡   Ú_validate_uniquer€   rt   ÚvaluesrV   )rr   r1   r2   Úauto_idÚprefixÚinitialÚerror_classÚlabel_suffixÚempty_permittedr:   r—   r˜   r<   Úobject_datarS   r“   r?   r@   rt   T  s4   


ö
ÿzBaseModelForm.__init__c                 C   sÌ   t ƒ }| 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 rG| |j¡ q| j| }| j 	|¡}|j
sc|jsc||jv rc| |j¡ q|S )z‘
        For backwards-compatibility, exclude several types of fields from model
        validation. See tickets #12507, #12521, #12553.
        )Úsetr:   r&   r(   r,   Úaddr;   Ú_errorsr'   r4   ÚblankÚrequiredr5   )rr   r;   r>   r.   Ú
form_fieldÚfield_valuer?   r?   r@   Ú_get_validation_exclusions‚  s*   


	ÿþ
€z(BaseModelForm._get_validation_exclusionsc                 C   s   d| _ | jS )NT)rš   r'   ©rr   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&   rO   r­   r   rƒ   r`   r(   r*   r   Úcoder’   Ú	add_error)rr   Úerrorsr<   r­   r.   Úmessagesr`   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 }~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)r;   Úvalidate_unique)r&   rª   r(   rƒ   r*   ÚInlineForeignKeyFieldr¤   rA   r:   r;   r   r²   Ú
full_cleanrš   r³   )rr   r<   r;   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.
        )r;   N)rª   r:   r³   r   r²   )rr   r;   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.
        r8   N)r'   r&   r;   r(   r:   r   rD   rC   rO   r,   r8   )rr   r'   r;   r(   r<   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™   r:   r&   Úobject_nameÚ_stateÚaddingÚsaver¸   Úsave_m2m)rr   Úcommitr?   r?   r@   r¾     s$   þÿÿþÿÿ
ÿzBaseModelForm.save©T)rf   rv   rw   r   rt   rª   r¬   r²   r·   r³   r¸   r¾   Úalters_datar•   r?   r?   r“   r@   r   S  s*    ô.-! 
r   c                   @   s   e Zd ZdS )r   N)rf   rv   rw   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 )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.
    rQ   Nr(   r;   ri   rk   rl   rm   r`   rn   ry   r?   ÚForm)ry   rj   zZCalling modelform_factory without defining 'fields' or 'exclude' explicitly is prohibited.)rO   ry   ÚtypeÚstaticmethodrj   rf   rE   r   )rQ   r9   r(   r;   rj   ri   rk   rl   rm   r`   rn   r‹   rŠ   ry   Ú
class_nameÚform_class_attrsr?   r?   r@   r"   3  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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.
    NFr–   )rž   c                   s0   || _ || _tƒ jdi ||||dœ|¥¤Ž d S )N)r1   r2   rœ   r�   r?   )rJ   Úinitial_extrar€   rt   )rr   r1   r2   rœ   r�   rJ   rž   rp   r“   r?   r@   rt   ™  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_querysetr€   Ú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?   rM   ©rZ   Úor?   r?   r@   rd   ¸  ó    z5BaseModelFormSet._existing_object.<locals>.<dictcomp>)rO   rÌ   rÎ   r4   )rr   rL   r?   r?   r@   Ú_existing_object¶  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©rr   r.   r?   r?   r@   Ú_get_to_python»  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-%sr:   rž   T)rÍ   rÊ   r3   rQ   r&   rL   r,   r1   ÚKeyErrorr×   r   rÒ   rÌ   rÉ   Ú
IndexErrorr€   Ú_construct_formr(   r§   )rr   Úirp   Úpk_requiredÚpk_keyrL   rÕ   r9   r“   r?   r@   rÚ   Ä  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)rO   rJ   rQ   Ú_default_managerrÌ   ÚorderedÚorder_byr&   rL   r,   rÞ   )rr   Ú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¾   ©rr   r9   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ä   )rr   r9   r:   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)rr   ÚobjrÀ   r?   r?   r@   Údelete_existing  s   ÿz BaseModelFormSet.delete_existingc                    s@   |sg ˆ _ ‡ fdd„}|ˆ _ˆ jrˆ  |¡S ˆ  |¡ˆ  |¡ 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 ru   )Úsaved_formsr¿   )r9   r«   r?   r@   r¿     s   

ÿz'BaseModelFormSet.save.<locals>.save_m2m)rì   r¿   Ú	edit_onlyÚsave_existing_objectsÚsave_new_objects)rr   rÀ   r¿   r?   r«   r@   r¾     s   
zBaseModelFormSet.savec                 C   s   |   ¡  d S ru   )r³   r«   r?   r?   r@   r¬     ó   zBaseModelFormSet.cleanc                    sò  t ƒ }t ƒ }ˆj‰‡fdd„ˆjD ƒ}|D ]‰ ˆ  ¡ }ˆ jj|dd�\}}| |¡ | |¡ qg }|D ]R\}}	t ƒ }
|D ]H‰ ‡ ‡fdd„|	D ƒ}tdd„ |D ƒƒ}|r…d |vr…||
v r€| ˆ 	|	¡¡ ˆj
ˆ ¡ gˆjd�ˆ jt< |	D ]}|ˆ jv rˆ j|= qt|
 |¡ q=q4|D ]g}t ƒ }
|\}}}}|D ]Y‰ ˆ 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?   )Úis_valid)rZ   r9   )Úforms_to_deleter?   r@   r\   "  s    ýþz4BaseModelFormSet.validate_unique.<locals>.<listcomp>T)r;   Úinclude_meta_constraintsc                 3   s2   � | ]}|ˆ j v r|ˆjv r|nˆ j | V  qd S ru   )r'   Úunique_fields©rZ   r.   )r9   rr   r?   r@   Ú	<genexpr>7  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)rO   r÷   r*   ÚlistÚtuple)rZ   Údr?   r?   r@   rö   =  s   € ý
ý)r˜   Údate)r£   Údeleted_formsÚformsrª   r:   Ú_get_unique_checksr‡   rù   r7   Úget_unique_error_messagerŸ   Úget_form_errorr˜   r¥   r   r'   r¤   ÚyearÚmonthÚdayrE   Úget_date_error_messager   )rr   Úall_unique_checksÚall_date_checksÚvalid_formsr;   Úunique_checksÚdate_checksr°   ÚuclassÚunique_checkÚ	seen_dataÚrow_datar.   Ú
date_checkÚlookupÚ
unique_forrû   Ú	date_datar1   r?   )r9   rò   rr   r@   r³     s€   
þ
þ
þüþ
€
€á!ÿ
þ
€ãÿ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   Ú_©rr   r  r?   r?   r@   rÿ   z  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   r‚   )rr   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ü   r:   rL   r7   rë   Úhas_changedÚchanged_datarè   rì   )rr   rÀ   Úsaved_instancesrò   r9   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_formr7   ræ   rì   )rr   rÀ   r9   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+   Ú
ForeignKeyÚOneToOneFieldc                    s6   | j  p| jpt| ˆ ƒp| jo| jjoˆ| jjjjƒS ru   )r)   Úauto_createdr*   rÓ   Úparent_linkrQ   r&   rL   rM   ©r+   Úpk_is_not_editabler?   r@   r*  Æ  s   ÿÿúz7BaseModelFormSet.add_fields.<locals>.pk_is_not_editableNF)rž   r§   r/   )rN   r+   r%  r&  rQ   r&   rL   Ú	_pk_fieldr,   r(   rÊ   r:   r¼   r½   rÌ   rÙ   r*   rÓ   rß   ÚusingÚdbri   r4   r   r   r€   Ú
add_fields)	rr   r9   Úindexr%  r&  rL   Úpk_valuerâ   r/   r“   r)  r@   r.  »  s2   €ÿÿzBaseModelFormSet.add_fields)NNr–   NNrÁ   )rf   rv   rw   Ú__doc__rQ   rí   r£   rô   rt   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 rt |d|	ƒdu rtdƒ‚t| |||	||
|||||d�}t||||||||||||d�}| |_||_|S )z8Return a FormSet class for the given Django model class.ry   Nr(   r;   z]Calling modelformset_factory without defining 'fields' or 'exclude' explicitly is prohibited.)
r9   r(   r;   rj   ri   rk   rl   rm   r`   rn   )
ÚextraÚmin_numÚmax_numÚ	can_orderr#  Úvalidate_minÚvalidate_maxÚabsolute_maxÚcan_delete_extrar˜   )rE   r   r"   r   rQ   rí   )rQ   r9   rj   Úformsetr2  r#  r5  r4  r(   r;   ri   r7  rk   rl   rm   r`   r3  r6  rn   r8  r9  r˜   rí   ÚmetaÚFormSetr?   r?   r@   r   î  sH   ÿõôr   c                       sx   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�   rJ   r?   )ÚfkrÓ   rQ   r:   Úsave_as_newrß   rL   rP   r,   Únonerô   r€   rt   r9   r&   r(   r*   rù   rø   r7   )	rr   r1   r2   r:   r>  r�   rJ   rp   râ   r“   r?   r@   rt   8  s    
ýzBaseInlineFormSet.__init__c                    s   | j rdS tƒ  ¡ S )Nr   )r>  r€   rÍ   r«   r“   r?   r@   rÍ   W  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Ú_mutableTrL   )r€   rÚ   r>  rE   r1   r@  r3   r+  r,   r=  r:   rL   rÓ   r  rQ   r&   ÚsetattrÚget_attname)rr   rÛ   rp   r9   ÚmutableÚfk_valuer“   r?   r@   rÚ   \  s   z!BaseInlineFormSet._construct_formc                 C   s   | j jj| jd� dd¡S )N)rQ   ú+Ú )r=  rÓ   Úget_accessor_namerQ   Úreplace)Úclsr?   r?   r@   Úget_default_prefixs  s   z$BaseInlineFormSet.get_default_prefixTc                    s$   t |j| jj| jƒ tƒ j||d�S r  )rA  r:   r=  r,   r€   ræ   rå   r“   r?   r@   ræ   w  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_fieldTr^   Úto_field)r€   r.  r+  r=  r,   rE   r(   r4   r   Úverbose_namerÓ   r  rQ   r&   rL   r:   r¼   r½   Ú	get_fieldr-   rA  Úattnamer´   )rr   r9   r/  r,   rp   rL  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?   )r=  r,   rõ   r«   r?   r@   r\      s    z>BaseInlineFormSet.get_unique_error_message.<locals>.<listcomp>)r€   rÿ   r  r“   r«   r@   rÿ   Ÿ  s   z*BaseInlineFormSet.get_unique_error_message)NNNFNNrÁ   )rf   rv   rw   r1  rt   rÍ   rÚ   ÚclassmethodrJ  ræ   r.  rÿ   r•   r?   r?   r“   r@   r    5  s     ù
!r    c                    s<  ddl m‰  |j}ˆrc‡fdd„|jD ƒ}t|ƒdkrU|d }ˆj ¡ ‰t|ˆ ƒrI|jjjj	r7|jjjj
ˆvsI|jjjj	sS|jjˆkrS|jjˆvrStdˆˆjjf ƒ‚|S |satd|jjˆf ƒ‚|S ˆj ¡ ‰‡ ‡‡fdd„|jD ƒ}t|ƒdkr€|d }|S |s’|r†d	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,   rY   ©Úfk_namer?   r@   r\   ±  ó    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 | ]#}t |ˆ ƒr%|jjˆks#|jjˆv s#|jjjjr%|jjjjˆv r|‘qS r?   )r*   rÓ   rQ   r&   ÚproxyÚproxy_for_modelrY   )r%  Úparent_listÚparent_modelr?   r@   r\   Î  s    ý
þøNz'%s' has no ForeignKey to '%s'.zR'%s' has more than one ForeignKey to '%s'. You must specify a 'fk_name' attribute.)rN   r%  r&   r(   rË   Úget_parent_listr*   rÓ   rQ   rT  rU  r™   r^   )rW  rQ   rR  Úcan_failr<   Úfks_to_parentr=  r?   )r%  rR  rV  rW  r@   Ú_get_foreign_key¤  sf   	
ÿ
ü
ü
ÿÿ-×ÿ(
Ýþíþÿÿþþÿr[  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œ¥}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``.
    rQ  r  r9   rj   r:  r2  r#  r5  r(   r;   r3  r4  ri   r6  r7  rk   rl   rm   r`   )rn   r8  r9  r˜   rí   )r[  Úuniquer   r=  )rW  rQ   r9   r:  rR  r(   r;   r2  r5  r#  r4  rj   ri   r7  rk   rl   rm   r`   r3  r6  rn   r8  r9  r˜   rí   r=  rp   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)rK  rL  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_instancerK  rL  rE   rL   r€   rt   )rr   r^  rK  rL  Úargsrp   r“   r?   r@   rt   C  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 )Nr]  ©r®   )	r5   rK  r^  rL  rE   rL   r‚   r   r`   )rr   r{   Úorigr?   r?   r@   r¬   O  s   

ÿzInlineForeignKeyField.cleanc                 C   s   dS ©NFr?   )rr   rž   r1   r?   r?   r@   r  `  s   z!InlineForeignKeyField.has_changed)rf   rv   rw   r1  r   r/   r  Údefault_error_messagesrt   r¬   r  r•   r?   r?   r“   r@   r´   8  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 ru   )r{   r:   )rr   r{   r:   r?   r?   r@   rt   e  s   
z!ModelChoiceIteratorValue.__init__c                 C   ó
   t | jƒS ru   )r‚   r{   r«   r?   r?   r@   Ú__str__i  ó   
z ModelChoiceIteratorValue.__str__c                 C   re  ru   )Úhashr{   r«   r?   r?   r@   Ú__hash__l  rg  z!ModelChoiceIteratorValue.__hash__c                 C   s   t |tƒr|j}| j|kS ru   )r*   rd  r{   )rr   Úotherr?   r?   r@   Ú__eq__o  s   

zModelChoiceIteratorValue.__eq__N)rf   rv   rw   rt   rf  ri  rk  r?   r?   r?   r@   rd  d  s
    rd  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 ru   )r.   rJ   rÖ   r?   r?   r@   rt   v  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 )NrF  )r.   Úempty_labelrJ   Ú_prefetch_related_lookupsÚiteratorÚchoice)rr   rJ   rê   r?   r?   r@   Ú__iter__z  s   €ÿzModelChoiceIterator.__iter__c                 C   s    | j  ¡ | jjd urd S d S )Nr  r   )rJ   Úcountr.   rm  r«   r?   r?   r@   Ú__len__„  s    zModelChoiceIterator.__len__c                 C   s   | j jd up
| j ¡ S ru   )r.   rm  rJ   Úexistsr«   r?   r?   r@   Ú__bool__Š  s   zModelChoiceIterator.__bool__c                 C   s   t | j |¡|ƒ| j |¡fS ru   )rd  r.   Úprepare_valueÚlabel_from_instance©rr   rê   r?   r?   r@   rp  �  s   
þzModelChoiceIterator.choiceN)rf   rv   rw   rt   rq  rs  ru  rp  r?   r?   r?   r@   rl  u  s    
rl  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	---------TNrF  F)	rm  r§   r/   r^   rž   r_   Úto_field_namerT   r¦   c       	         K   s^   t j| f|||||dœ|¤Ž |r|d ust| jtƒr!|
s!d | _n|| _|| _|	| _|| _d S )N)r§   r/   r^   rž   r_   )	r   rt   r*   r/   r   rm  rJ   rT   ry  )rr   rJ   rm  r§   r/   r^   rž   r_   ry  rT   r¦   rp   r?   r?   r@   rt      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.
        )rg   rT   r«   r?   r?   r@   rK   Ä  s   
z%ModelChoiceField.get_limit_choices_toc                    s*   t t| ƒ |¡}| jd ur| j ¡ |_|S ru   )r€   r   Ú__deepcopy__rJ   Úall)rr   ÚmemoÚresultr“   r?   r@   rz  Î  s   
zModelChoiceField.__deepcopy__c                 C   s   | j S ru   )rÞ   r«   r?   r?   r@   Ú_get_querysetÕ  s   zModelChoiceField._get_querysetc                 C   s$   |d u rd n|  ¡ | _| j| j_d S ru   )r{  rÞ   Úchoicesr/   )rr   rJ   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.
        ©r‚   rx  r?   r?   r@   rw  à  s   z$ModelChoiceField.label_from_instancec                 C   s   t | dƒr| jS |  | ¡S )NÚ_choices)rO   r‚  ro  r«   r?   r?   r@   Ú_get_choicesè  s   

	zModelChoiceField._get_choicesc                    s.   t |dƒr| jr| | j¡S |jS tƒ  |¡S ©Nr&   )rO   ry  Úserializable_valuerL   r€   rv  ©rr   r{   r“   r?   r@   rv  ù  s
   
zModelChoiceField.prepare_valuec              
   C   s€   || j v rd S z| jpd}t|| jjƒrt||ƒ}| jjdi ||i¤Ž}W |S  tt| jjj	fy?   t
| jd dd|id�‚w )NrL   r]  r{   ©r®   Úparamsr?   )r5   ry  r*   rJ   rQ   rE   r4   r™   rh   ÚDoesNotExistr   r`   )rr   r{   Úkeyr?   r?   r@   rÕ     s   


úýÿzModelChoiceField.to_pythonc                 C   s   t  | |¡S ru   )r   Úvalidater†  r?   r?   r@   r‹    rð   zModelChoiceField.validatec                 C   s@   | j rdS |d ur|nd}|d ur|nd}t|  |¡ƒt|ƒkS )NFrF  )Údisabledr‚   rv  )rr   rž   r1   Úinitial_valueÚ
data_valuer?   r?   r@   r    s
   zModelChoiceField.has_changed)rf   rv   rw   r1  r  rc  rl  ro  rt   rK   rz  r~  r€  ÚpropertyrJ   rw  rƒ  r   Ú_set_choicesr  rv  rÕ   r‹  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.u"   â€œ%(pk)sâ€� is not a valid value.)Úinvalid_listr]  Úinvalid_pk_valuec                    s   t ƒ j|fdd i|¤Ž d S )Nrm  )r€   rt   )rr   rJ   rp   r“   r?   r@   rt   )  s   z!ModelMultipleChoiceField.__init__c                 C   s   |sg S t |  |¡ƒS ru   )rø   Ú_check_valuesr†  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§   r`  r‘  )rv  r§   r   r`   rJ   r?  r*   rø   rù   r“  Úrun_validators)rr   r{   râ   r?   r?   r@   r¬   1  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.)
        rL   r‘  r`  r’  r‡  z%s__inc                    s   h | ]	}t t|ˆ ƒƒ’qS r?   )r‚   rE   rÏ   ©rŠ  r?   r@   r~   ]  rS  z9ModelMultipleChoiceField._check_values.<locals>.<setcomp>r]  r{   Nr?   )	ry  Ú	frozensetrh   r   r`   rJ   rP   r™   r‚   )rr   r{   rL   râ   ÚpksÚvalr?   r•  r@   r“  B  s<   
þþýÿýÿz&ModelMultipleChoiceField._check_valuesc                    sD   t |dƒrt|tƒst |dƒstƒ j‰ ‡ fdd„|D ƒS tƒ  |¡S )Nrq  r&   c                    s   g | ]}ˆ |ƒ‘qS r?   r?   )rZ   r}   ©rv  r?   r@   r\   n  rÑ   z:ModelMultipleChoiceField.prepare_value.<locals>.<listcomp>)rO   r*   r‚   r€   rv  r†  r“   r™  r@   rv  g  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?   r�  ©rZ   r{   r?   r?   r@   r~   z  rÑ   z7ModelMultipleChoiceField.has_changed.<locals>.<setcomp>c                 S   rš  r?   r�  r›  r?   r?   r@   r~   {  rÑ   )rŒ  rË   rv  )rr   rž   r1   Úinitial_setÚdata_setr?   r?   r@   r  q  s   z$ModelMultipleChoiceField.has_changed)rf   rv   rw   r1  r   r/   r   Úhidden_widgetr  rc  rt   rÕ   r¬   r“  rv  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„  )rO   r&   r(   r;   )ra   r?   r?   r@   Úmodelform_defines_fields  s   
ÿrŸ  )NN)	NNNNNNNNNrb  )5r1  Ú	itertoolsr   Údjango.core.exceptionsr   r   r   r   Údjango.forms.fieldsr   r   Údjango.forms.formsr	   r
   Údjango.forms.formsetsr   r   Údjango.forms.utilsr   Údjango.forms.widgetsr   r   r   r   Údjango.utils.textr   r   Údjango.utils.translationr   r   r  r#   r   rA   r   rV   r   rq   rx   r   r   r"   r   r   r    r[  r!   r´   rd  rl  r   r   rŸ  r?   r?   r?   r@   Ú<module>   sÎ    

0öônQ ]
õ[  d
éG
oR
çE, 	c