o
    ÕèFh¢í  ã                   @   sZ  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 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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.ƒZ0dAd.d/„Z1e,e0dddd0d)ddddd)dddddd)ddddd)fd1d2„Z2G d3d4„ d4eƒZ3G d5d6„ d6ƒZ4G d7d8„ d8eƒZ5G d9d:„ d:eƒZ6G d;d<„ d<e6ƒZ7d=d>„ Z8d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)ÚBaseChoiceIterator)Ú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© rA   úL/var/www/html/loop/nvenv/lib/python3.10/site-packages/django/forms/models.pyÚconstruct_instance0   s:   
ÿ
þ
ÿÿþrC   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>   r3   r@   rA   rA   rB   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©rN   NrA   )Údjango.db.modelsrI   rJ   rK   ÚhasattrrM   r,   rL   ÚfilterÚmodelÚ_base_manager)Ú	formfieldrI   rJ   rK   Úlimit_choices_toÚcomplex_filterrA   rA   rB   Ú#apply_limit_choices_to_to_formfieldy   s   

ÿørX   T©Úapply_limit_choices_toÚform_declared_fieldsc
                   s  |pi }i ‰g ‰| j }ddlm‰  ‡ fdd„|jD ƒ}tt|j||jƒƒD ]Í}t|ddƒsJ|durI|j	|v rIˆdu s?|j	ˆvrIt
d|j	| jf ƒ‚q%|durT|j	|vrTq%ˆr\|j	ˆv r\q%|j	|v rj||j	 ˆ|j	< q%i }|rz|j	|v rz||j	 |d	< |tks…|r‰|j	|v r‰d
|d< |r—|j	|v r—||j	 |d< |r¥|j	|v r¥||j	 |d< |r³|j	|v r³||j	 |d< |	rÁ|j	|	v rÁ|	|j	 |d< |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 )a‘  
    Return a dictionary containing form fields for the given model.

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

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

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

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

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

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

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

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

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

    ``apply_limit_choices_to`` is a boolean indicating if limit_choices_to
    should be applied to a field's queryset.

    ``form_declared_fields`` is a dictionary of form fields created directly on
    a form.
    r   )r	   c                    s   g | ]	}t |ˆ ƒr|‘qS rA   )r,   ©Ú.0r@   )Ú
ModelFieldrA   rB   Ú
<listcomp>Á   s
    
ÿÿz$fields_for_model.<locals>.<listcomp>r+   FNzH'%s' cannot be specified for %s model form as it is a non-editable fieldr1   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 rA   )r6   r\   )r=   Ú
field_dictÚignoredrA   rB   Ú
<dictcomp>ø   s    ý
þz$fields_for_model.<locals>.<dictcomp>rA   )r(   rP   r	   rE   Úsortedr   rD   rF   rG   r.   r   Ú__name__r   rU   ÚcallableÚ	TypeErrorrX   r9   )rS   r*   r=   ÚwidgetsÚformfield_callbackÚlocalized_fieldsÚlabelsÚ
help_textsrc   Úfield_classesrZ   r[   r>   Úsortable_private_fieldsr@   ÚkwargsrU   rA   )r^   r=   re   rf   rB   r   Š   sv   0
ÿÿ

ÿÿ
ÿ
þ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 )NrS   r*   r=   rl   rn   ro   rp   rc   rq   rm   )rG   rS   r*   r=   rl   rn   ro   rp   rc   rq   rm   )ÚselfÚoptionsrA   rA   rB   Ú__init__  s   zModelFormOptions.__init__©N)ri   Ú
__module__Ú__qualname__rw   rA   rA   rA   rB   rt      s    rt   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rš|jd u rN|jd u rNtd| ƒ‚|jtkrVd |_t|j|j|j|j|j|j|j|j|j|jd|jd�}	dd	„ |	 ¡ D ƒ}
|
 |j¡}|r“d
}|d |¡|jj	f; }t|ƒ‚|	 |j¡ n|j}	|	|_|S )NÚMeta)r*   r=   rn   zP%(model)s.Meta.%(opt)s cannot be a string. Did you mean to type: ('%(value)s',)?)rS   ÚoptÚvaluez|Creating a ModelForm without either the 'fields' attribute or the 'exclude' attribute is prohibited; form %s needs updating.FrY   c                 S   s   h | ]\}}|s|’qS rA   rA   )r]   ÚkÚvrA   rA   rB   Ú	<setcomp>G  s    z-ModelFormMetaclass.__new__.<locals>.<setcomp>z&Unknown field(s) (%s) specified for %sz, )ÚsuperÚ__new__r   rt   rG   r(   r,   Ústrr   ri   rk   rS   r*   r=   r   r   rl   rm   rn   ro   rp   rc   rq   Údeclared_fieldsÚitemsÚ
differenceÚjoinr   ÚupdateÚbase_fields)Úmcsr.   ÚbasesÚattrsÚ	new_classr>   r}   r~   Úmsgr*   Únone_model_fieldsÚmissing_fieldsÚmessage©Ú	__class__rA   rB   rƒ     sb   

ýþÿ	€þÿ
ózModelFormMetaclass.__new__)ri   ry   rz   rƒ   Ú__classcell__rA   rA   r“   rB   r{     s    r{   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(   rS   Ú
ValueErrorr<   r   r*   r=   r‰   Ú_validate_uniquer‚   rw   ÚvaluesrX   )ru   r3   r4   Úauto_idÚprefixÚinitialÚerror_classÚlabel_suffixÚempty_permittedr<   r—   r˜   r>   Úobject_datarU   r“   rA   rB   rw   X  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)   r6   ÚblankÚrequiredr7   )ru   r=   r@   r0   Ú
form_fieldÚfield_valuerA   rA   rB   Ú_get_validation_exclusions†  s*   


	ÿþ
€z(BaseModelForm._get_validation_exclusionsc                 C   s   d| _ | jS )NT)rš   r)   ©ru   rA   rA   rB   Ú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(   rQ   r­   r   r†   rc   r*   r,   r   Úcoder’   Ú	add_error)ru   Úerrorsr>   r­   r0   Úmessagesrc   r’   rA   rA   rB   Ú_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¤   rC   r<   r=   r   r²   Ú
full_cleanrš   r³   )ru   r>   r=   r.   r0   ÚerA   rA   rB   Ú_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²   )ru   r=   r¶   rA   rA   rB   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.
        r:   N)r)   r(   r=   r*   r<   r   rF   rE   rQ   r.   r:   )ru   r)   r=   r*   r>   r@   rA   rA   rB   Ú	_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)ru   ÚcommitrA   rA   rB   r¾     s$   þÿÿþÿÿ
ÿzBaseModelForm.save©T)ri   ry   rz   r   rw   rª   r¬   r²   r·   r³   r¸   r¾   Úalters_datar•   rA   rA   r“   rB   r   W  s*    ô.-! 
r   c                   @   s   e Zd ZdS )r   N)ri   ry   rz   rA   rA   rA   rB   r   3  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|i}t|ddƒdu ryt|ddƒdu ryt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.
    rS   Nr*   r=   rl   rn   ro   rp   rc   rq   r|   rA   ÚFormzZCalling modelform_factory without defining 'fields' or 'exclude' explicitly is prohibited.)rQ   r|   ÚtypeÚstaticmethodrm   ri   rG   r   )rS   r;   r*   r=   rm   rl   rn   ro   rp   rc   rq   r�   rŒ   r|   Ú
class_nameÚform_class_attrsrA   rA   rB   r$   7  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)r3   r4   rœ   r�   rA   )rL   Úinitial_extrar‚   rw   )ru   r3   r4   rœ   r�   rL   rž   rs   r“   rA   rB   rw   �  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“   rA   rB   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 rA   rO   ©r]   ÚorA   rA   rB   rg   ¼  ó    z5BaseModelFormSet._existing_object.<locals>.<dictcomp>)rQ   rÌ   rÎ   r6   )ru   rN   rA   rA   rB   Ú_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©ru   r0   rA   rA   rB   Ú_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Ê   r5   rS   r(   rN   r.   r3   ÚKeyErrorr×   r   rÒ   rÌ   rÉ   Ú
IndexErrorr‚   Ú_construct_formr*   r§   )ru   Úirs   Úpk_requiredÚpk_keyrN   rÕ   r;   r“   rA   rB   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)rQ   rL   rS   Ú_default_managerrÌ   ÚorderedÚorder_byr(   rN   r.   rÞ   )ru   ÚqsrA   rA   rB   rÌ   ê  s   

zBaseModelFormSet.get_querysetTc                 C   s   |j |d�S )z8Save and return a new model instance for the given form.©rÀ   ©r¾   ©ru   r;   rÀ   rA   rA   rB   Ú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ä   )ru   r;   ÚobjrÀ   rA   rA   rB   Úsave_existing  rç   zBaseModelFormSet.save_existingc                 C   s   |r|  ¡  dS dS )z#Deletes an existing model instance.N)Údelete)ru   rè   rÀ   rA   rA   rB   Ú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 rx   )Úsaved_formsr¿   )r;   r«   rA   rB   r¿     s   

ÿz'BaseModelFormSet.save.<locals>.save_m2m)rì   r¿   Ú	edit_onlyÚsave_existing_objectsÚsave_new_objects)ru   rÀ   r¿   rA   r«   rB   r¾   
  s   
zBaseModelFormSet.savec                 C   s   |   ¡  d S rx   )r³   r«   rA   rA   rB   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 rA   )Úis_valid)r]   r;   )Úforms_to_deleterA   rB   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 rx   )r)   Úunique_fields©r]   r0   )r;   ru   rA   rB   Ú	<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)rQ   r÷   r,   ÚlistÚtuple)r]   ÚdrA   rA   rB   rö   A  s   € ü
ÿ
û)r˜   Údate)r£   Údeleted_formsÚformsrª   r<   Ú_get_unique_checksr‰   rù   r9   Úget_unique_error_messagerŸ   Úget_form_errorr˜   r¥   r   r)   r¤   ÚyearÚmonthÚdayrG   Úget_date_error_messager   )ru   Úall_unique_checksÚall_date_checksÚvalid_formsr=   Úunique_checksÚdate_checksr°   ÚuclassÚunique_checkÚ	seen_dataÚrow_datar0   Ú
date_checkÚlookupÚ
unique_forrû   Ú	date_datar3   rA   )r;   rò   ru   rB   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.r0   r   zFPlease correct the duplicate data for %(field)s, which must be unique.Úand)rË   r   r   Ú_©ru   r  rA   rA   rB   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   r„   )ru   r  rA   rA   rB   r  �  s   ÿ
ýýz'BaseModelFormSet.get_date_error_messagec                 C   s   t dƒS )Nz*Please correct the duplicate values below.r   r«   rA   rA   rB   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<   rN   r9   rë   Úhas_changedÚchanged_dataré   rì   )ru   rÀ   Úsaved_instancesrò   r;   rè   rA   rA   rB   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_formr9   ræ   rì   )ru   rÀ   r;   rA   rA   rB   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 rx   )r+   Úauto_createdr,   rÓ   Úparent_linkrS   r(   rN   rO   ©r-   Úpk_is_not_editablerA   rB   r*  Í  s   ÿÿúz7BaseModelFormSet.add_fields.<locals>.pk_is_not_editableNF)rž   r§   r1   )rP   r-   r%  r&  rS   r(   rN   Ú	_pk_fieldr.   r*   rÊ   r<   r¼   r½   rÌ   rÙ   r,   rÓ   rß   ÚusingÚdbrl   r6   r   r   r‚   Ú
add_fields)	ru   r;   Úindexr%  r&  rN   Úpk_valuerâ   r1   r“   r)  rB   r.  Â  s2   €ÿÿzBaseModelFormSet.add_fields)NNr–   NNrÁ   )ri   ry   rz   Ú__doc__rS   rí   r£   rô   rw   rÍ   rÒ   r×   rÚ   rÌ   ræ   ré   rë   r¾   rÂ   r¬   r³   rÿ   r  r   rî   rï   r.  r•   rA   rA   r“   rB   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.r|   Nr*   r=   z]Calling modelformset_factory without defining 'fields' or 'exclude' explicitly is prohibited.)
r;   r*   r=   rm   rl   rn   ro   rp   rc   rq   )
ÚextraÚmin_numÚmax_numÚ	can_orderr#  Úvalidate_minÚvalidate_maxÚabsolute_maxÚcan_delete_extrar˜   )rG   r   r$   r   rS   rí   )rS   r;   rm   Úformsetr2  r#  r5  r4  r*   r=   rl   r7  rn   ro   rp   rc   r3  r6  rq   r8  r9  r˜   rí   ÚmetaÚFormSetrA   rA   rB   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�   rL   rA   )ÚfkrÓ   rS   r<   Úsave_as_newrß   rN   rR   r.   Únonerô   r‚   rw   r;   r(   r*   r,   rù   rø   r9   )	ru   r3   r4   r<   r>  r�   rL   rs   râ   r“   rA   rB   rw   ?  s    
ýzBaseInlineFormSet.__init__c                    s   | j rdS tƒ  ¡ S )Nr   )r>  r‚   rÍ   r«   r“   rA   rB   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Ú_mutableTrN   )r‚   rÚ   r>  rG   r3   r@  r5   r+  r.   r=  r<   rN   rÓ   r  rS   r(   ÚsetattrÚget_attname)ru   rÛ   rs   r;   ÚmutableÚfk_valuer“   rA   rB   rÚ   c  s   z!BaseInlineFormSet._construct_formc                 C   s   | j jj| jd� dd¡S )N)rS   ú+Ú )r=  rÓ   Úget_accessor_namerS   Úreplace)ÚclsrA   rA   rB   Úget_default_prefixz  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“   rA   rB   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rs| d¡d urT| jj |d ¡}n| jjj}| ¡ rs|j| jj
jjjjksk|jsst| j|jd ƒ t| jfi |¤Ž|j|< d S )NÚpk_fieldTra   Úto_field)r‚   r.  r+  r=  r.   rG   r*   r6   r   Úverbose_namerÓ   r  rS   r(   rN   r<   r¼   r½   Ú	get_fieldr/   Úattnamer3   rA  r´   )ru   r;   r/  r.   rs   rL  r“   rA   rB   r.  …  s*   
ÿÿ	

ÿzBaseInlineFormSet.add_fieldsc                    s   ‡ fdd„|D ƒ}t ƒ  |¡S )Nc                    s   g | ]
}|ˆ j jkr|‘qS rA   )r=  r.   rõ   r«   rA   rB   r_   ­  s    z>BaseInlineFormSet.get_unique_error_message.<locals>.<listcomp>)r‚   rÿ   r  r“   r«   rB   rÿ   ¬  s   z*BaseInlineFormSet.get_unique_error_message)NNNFNNrÁ   )ri   ry   rz   r1  rw   rÍ   rÚ   ÚclassmethodrJ  ræ   r.  rÿ   r•   rA   rA   r“   rB   r"   <  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 rA   )r.   r\   ©Úfk_namerA   rB   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 rA   )r,   rÓ   rS   r(   ÚproxyÚproxy_for_modelr\   )r%  Úparent_listÚparent_modelrA   rB   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.)rP   r%  r(   r*   rË   Úget_parent_listr,   rÓ   rS   rT  rU  r™   ra   )rW  rS   rR  Úcan_failr>   Úfks_to_parentr=  rA   )r%  rR  rV  rW  rB   Ú_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  r;   rm   r:  r2  r#  r5  r*   r=   r3  r4  rl   r6  r7  rn   ro   rp   rc   )rq   r8  r9  r˜   rí   )r[  Úuniquer!   r=  )rW  rS   r;   r:  rR  r*   r=   r2  r5  r#  r4  rm   rl   r7  rn   ro   rp   rc   r3  r6  rq   r8  r9  r˜   rí   r=  rs   r<  rA   rA   rB   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  rG   rN   r‚   rw   )ru   r^  rK  rL  Úargsrs   r“   rA   rB   rw   P  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®   )	r7   rK  r^  rL  rG   rN   r„   r   rc   )ru   r~   ÚorigrA   rA   rB   r¬   \  s   

ÿzInlineForeignKeyField.cleanc                 C   s   dS ©NFrA   )ru   rž   r3   rA   rA   rB   r  m  s   z!InlineForeignKeyField.has_changed)ri   ry   rz   r1  r   r1   r  Údefault_error_messagesrw   r¬   r  r•   rA   rA   r“   rB   r´   E  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 rx   )r~   r<   )ru   r~   r<   rA   rA   rB   rw   r  s   
z!ModelChoiceIteratorValue.__init__c                 C   ó
   t | jƒS rx   )r„   r~   r«   rA   rA   rB   Ú__str__v  ó   
z ModelChoiceIteratorValue.__str__c                 C   re  rx   )Úhashr~   r«   rA   rA   rB   Ú__hash__y  rg  z!ModelChoiceIteratorValue.__hash__c                 C   s   t |tƒr|j}| j|kS rx   )r,   rd  r~   )ru   ÚotherrA   rA   rB   Ú__eq__|  s   

zModelChoiceIteratorValue.__eq__N)ri   ry   rz   rw   rf  ri  rk  rA   rA   rA   rB   rd  q  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 rx   )r0   rL   rÖ   rA   rA   rB   rw   ƒ  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  )r0   Úempty_labelrL   Ú_prefetch_related_lookupsÚiteratorÚchoice)ru   rL   rè   rA   rA   rB   Ú__iter__‡  s   €ÿzModelChoiceIterator.__iter__c                 C   s    | j  ¡ | jjd urd S d S )Nr  r   )rL   Úcountr0   rm  r«   rA   rA   rB   Ú__len__‘  s    zModelChoiceIterator.__len__c                 C   s   | j jd up
| j ¡ S rx   )r0   rm  rL   Úexistsr«   rA   rA   rB   Ú__bool__—  s   zModelChoiceIterator.__bool__c                 C   s   t | j |¡|ƒ| j |¡fS rx   )rd  r0   Úprepare_valueÚlabel_from_instance©ru   rè   rA   rA   rB   rp  š  s   
þzModelChoiceIterator.choiceN)ri   ry   rz   rw   rq  rs  ru  rp  rA   rA   rA   rB   rl  ‚  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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§   r1   ra   rž   rb   Úto_field_namerV   r¦   c       	         K   s^   t j| f|||||dœ|¤Ž |r|d ust| jtƒr!|
s!d | _n|| _|| _|	| _|| _d S )N)r§   r1   ra   rž   rb   )	r	   rw   r,   r1   r   rm  rL   rV   ry  )ru   rL   rm  r§   r1   ra   rž   rb   ry  rV   r¦   rs   rA   rA   rB   rw   ­  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.
        )rj   rV   r«   rA   rA   rB   rM   Ñ  s   
z%ModelChoiceField.get_limit_choices_toc                    s*   t t| ƒ |¡}| jd ur| j ¡ |_|S rx   )r‚   r   Ú__deepcopy__rL   Úall)ru   ÚmemoÚresultr“   rA   rB   rz  Û  s   
zModelChoiceField.__deepcopy__c                 C   s   | j S rx   )rÞ   r«   rA   rA   rB   Ú_get_querysetâ  s   zModelChoiceField._get_querysetc                 C   s$   |d u rd n|  ¡ | _| j| j_d S rx   )r{  rÞ   Úchoicesr1   )ru   rL   rA   rA   rB   Ú_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  rA   rA   rB   rw  í  s   z$ModelChoiceField.label_from_instancec                 C   s   t | dƒr| jS |  | ¡S )NÚ_choices)rQ   r‚  ro  r«   rA   rA   rB   Ú_get_choicesõ  s   

	zModelChoiceField._get_choicesc                    s.   t |dƒr| jr| | j¡S |jS tƒ  |¡S ©Nr(   )rQ   ry  Úserializable_valuerN   r‚   rv  ©ru   r~   r“   rA   rB   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 )NrN   r]  r~   ©r®   ÚparamsrA   )r7   ry  r,   rL   rS   rG   r6   r™   rk   ÚDoesNotExistr   rc   )ru   r~   ÚkeyrA   rA   rB   rÕ     s   


úýÿzModelChoiceField.to_pythonc                 C   s   t  | |¡S rx   )r	   Úvalidater†  rA   rA   rB   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  )ru   rž   r3   Úinitial_valueÚ
data_valuerA   rA   rB   r  !  s
   zModelChoiceField.has_changed)ri   ry   rz   r1  r  rc  rl  ro  rw   rM   rz  r~  r€  ÚpropertyrL   rw  rƒ  r   r  Úfsetrv  rÕ   r‹  r  r•   rA   rA   r“   rB   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‚   rw   )ru   rL   rs   r“   rA   rB   rw   6  s   z!ModelMultipleChoiceField.__init__c                 C   s   |sg S t |  |¡ƒS rx   )rø   Ú_check_valuesr†  rA   rA   rB   rÕ   9  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   rc   rL   r?  r,   rø   rù   r“  Úrun_validators)ru   r~   râ   rA   rA   rB   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.)
        rN   r‘  r`  r’  r‡  z%s__inc                    s   h | ]	}t t|ˆ ƒƒ’qS rA   )r„   rG   rÏ   ©rŠ  rA   rB   r�   j  rS  z9ModelMultipleChoiceField._check_values.<locals>.<setcomp>r]  r~   NrA   )	ry  Ú	frozensetrk   r   rc   rL   rR   r™   r„   )ru   r~   rN   râ   ÚpksÚvalrA   r•  rB   r“  O  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 rA   rA   )r]   r€   ©rv  rA   rB   r_   {  rÑ   z:ModelMultipleChoiceField.prepare_value.<locals>.<listcomp>)rQ   r,   r„   r‚   rv  r†  r“   r™  rB   rv  t  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 rA   r�  ©r]   r~   rA   rA   rB   r�   ‡  rÑ   z7ModelMultipleChoiceField.has_changed.<locals>.<setcomp>c                 S   rš  rA   r�  r›  rA   rA   rB   r�   ˆ  rÑ   )rŒ  rË   rv  )ru   rž   r3   Úinitial_setÚdata_setrA   rA   rB   r  ~  s   z$ModelMultipleChoiceField.has_changed)ri   ry   rz   r1  r   r1   r   Úhidden_widgetr  rc  rw   rÕ   r¬   r“  rv  r  r•   rA   rA   r“   rB   r   )  s     ÿû%
r   c                 C   s"   t | dƒo| jjd up| jjd uS r„  )rQ   r(   r*   r=   )rd   rA   rA   rB   Úmodelform_defines_fieldsŒ  s   
ÿrŸ  )NN)	NNNNNNNNNrb  )9r1  Ú	itertoolsr   Údjango.core.exceptionsr   r   r   r   Údjango.db.models.utilsr   Údjango.forms.fieldsr   r	   Údjango.forms.formsr
   r   Údjango.forms.formsetsr   r   Údjango.forms.utilsr   Údjango.forms.widgetsr   r   r   r   Údjango.utils.choicesr   Údjango.utils.textr   r   Údjango.utils.translationr   r   r  r%   r   rC   r   rX   r   rt   r{   r   r   r$   r    r!   r"   r[  r#   r´   rd  rl  r   r   rŸ  rA   rA   rA   rB   Ú<module>   sÔ    

0öóvI ]
õ[  g
éG
uR
çE, 	c