o
    þÞhï  ã                   @   sr  d Z ddlmZ ddlmZmZmZmZ ddlm	Z	 ddl
mZ ddlmZmZ ddlmZmZ ddlmZmZ dd	lmZ dd
lmZmZmZmZ ddlmZ ddlmZ ddl m!Z!m"Z" ddl#m$Z$ ddl#m%Z& dZ'dZ(dAdd„Z)dAdd„Z*dd„ Z+									dBd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%�Z0e0dddddddddf
d&d'„Z1G d(d)„ d)eeƒZ2e0de2d*d+d+ddddd+dddddd+ddddd+fd,d-„Z3G d.d/„ d/e2ƒZ4dCd0d1„Z5e0e4dddd2d+ddddd+dddddd+ddddd+fd3d4„Z6G d5d6„ d6eƒZ7G d7d8„ d8ƒZ8G d9d:„ d:eƒZ9G d;d<„ d<eƒZ:G d=d>„ d>e:ƒZ;d?d@„ Z<dS )Dz[
Helper functions for creating Form classes from Django models
and database field objects.
é    )Úchain)ÚNON_FIELD_ERRORSÚ
FieldErrorÚImproperlyConfiguredÚValidationError©ÚProhibitNullCharactersValidator)Ú
AltersData)ÚChoiceFieldÚField)ÚBaseFormÚDeclarativeFieldsMetaclass)ÚBaseFormSetÚformset_factory)Ú	ErrorList)ÚHiddenInputÚMultipleHiddenInputÚRadioSelectÚSelectMultiple)ÚBaseChoiceIterator)Úmake_hashable)ÚcapfirstÚget_text_list©Úgettext)Úgettext_lazy)Ú	ModelFormÚBaseModelFormÚmodel_to_dictÚfields_for_modelÚModelChoiceFieldÚModelMultipleChoiceFieldÚ
ALL_FIELDSÚBaseModelFormSetÚmodelformset_factoryÚBaseInlineFormSetÚinlineformset_factoryÚmodelform_factoryÚ__all__Nc           	      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© rD   úS/var/www/html/premium_crap/venv/lib/python3.10/site-packages/django/forms/models.pyÚconstruct_instance2   s:   
ÿ
þ
ÿÿþrF   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Úgetattrr1   Úvalue_from_object)r?   r-   r@   rA   r6   rC   rD   rD   rE   r   b   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©rQ   NrD   )Údjango.db.modelsrL   rM   rN   ÚhasattrrP   r/   rO   ÚfilterÚmodelÚ_base_manager)Ú	formfieldrL   rM   rN   Úlimit_choices_toÚcomplex_filterrD   rD   rE   Ú#apply_limit_choices_to_to_formfield{   s   

ÿør[   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 rD   )r/   ©Ú.0rC   )Ú
ModelFieldrD   rE   Ú
<listcomp>Ã   s
    
ÿÿz$fields_for_model.<locals>.<listcomp>r.   FNzH'%s' cannot be specified for %s model form as it is a non-editable fieldr4   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 rD   )r9   r_   )r@   Ú
field_dictÚignoredrD   rE   Ú
<dictcomp>ú   s    ý
þz$fields_for_model.<locals>.<dictcomp>rD   )r+   rS   r   rH   Úsortedr   rG   rI   rJ   r1   r   Ú__name__r"   rX   ÚcallableÚ	TypeErrorr[   r<   )rV   r-   r@   ÚwidgetsÚformfield_callbackÚlocalized_fieldsÚlabelsÚ
help_textsrf   Úfield_classesr]   r^   rA   Úsortable_private_fieldsrC   ÚkwargsrX   rD   )ra   r@   rh   ri   rE   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 )NrV   r-   r@   ro   rq   rr   rs   rf   rt   rp   )rJ   rV   r-   r@   ro   rq   rr   rs   rf   rt   rp   )ÚselfÚoptionsrD   rD   rE   Ú__init__  s   zModelFormOptions.__init__©N)rl   Ú
__module__Ú__qualname__rz   rD   rD   rD   rE   rw     s    rw   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@   rq   zP%(model)s.Meta.%(opt)s cannot be a string. Did you mean to type: ('%(value)s',)?)rV   ÚoptÚvaluez|Creating a ModelForm without either the 'fields' attribute or the 'exclude' attribute is prohibited; form %s needs updating.Fr\   c                 S   s   h | ]\}}|s|’qS rD   rD   )r`   ÚkÚvrD   rD   rE   Ú	<setcomp>I  s    z-ModelFormMetaclass.__new__.<locals>.<setcomp>z&Unknown field(s) (%s) specified for %sz, )ÚsuperÚ__new__r   rw   rJ   r+   r/   Ústrr"   rl   rn   rV   r-   r@   r   r   ro   rp   rq   rr   rs   rf   rt   Údeclared_fieldsÚitemsÚ
differenceÚjoinr   ÚupdateÚbase_fields)Úmcsr1   ÚbasesÚattrsÚ	new_classrA   r€   r�   Úmsgr-   Únone_model_fieldsÚmissing_fieldsÚmessage©Ú	__class__rD   rE   r†     sb   

ýþÿ	€þÿ
ózModelFormMetaclass.__new__)rl   r|   r}   r†   Ú__classcell__rD   rD   r–   rE   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+   rV   Ú
ValueErrorr?   r   r-   r@   rŒ   Ú_validate_uniquer…   rz   Úvaluesr[   )rx   r6   r7   Úauto_idÚprefixÚinitialÚerror_classÚlabel_suffixÚempty_permittedr?   rš   r›   rA   Úobject_datarX   r–   rD   rE   rz   Z  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-   r1   Úaddr@   Ú_errorsr,   r9   ÚblankÚrequiredr:   )rx   r@   rC   r3   Ú
form_fieldÚfield_valuerD   rD   rE   Ú_get_validation_exclusionsˆ  s*   


	ÿþ
€z(BaseModelForm._get_validation_exclusionsc                 C   s   d| _ | jS )NT)r�   r,   ©rx   rD   rD   rE   Ú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+   rT   r°   r   r‰   rf   r-   r/   r   Úcoder•   Ú	add_error)rx   ÚerrorsrA   r°   r3   Úmessagesrf   r•   rD   rD   rE   Ú_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§   rF   r?   r@   r   rµ   Ú
full_cleanr�   r¶   )rx   rA   r@   r1   r3   ÚerD   rD   rE   Ú_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µ   )rx   r@   r¹   rD   rD   rE   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   rI   rH   rT   r1   r=   )rx   r,   r@   r-   rA   rC   rD   rD   rE   Ú	_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)rx   ÚcommitrD   rD   rE   rÁ     s$   þÿÿþÿÿ
ÿzBaseModelForm.save©T)rl   r|   r}   r   rz   r­   r¯   rµ   rº   r¶   r»   rÁ   Úalters_datar˜   rD   rD   r–   rE   r   Y  s*    ô.-! 
r   c                   @   s   e Zd ZdS )r   N)rl   r|   r}   rD   rD   rD   rE   r   5  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.
    rV   Nr-   r@   ro   rq   rr   rs   rf   rt   r   rD   ÚFormzZCalling modelform_factory without defining 'fields' or 'exclude' explicitly is prohibited.)rT   r   ÚtypeÚstaticmethodrp   rl   rJ   r   )rV   r>   r-   r@   rp   ro   rq   rr   rs   rf   rt   r�   r�   r   Ú
class_nameÚform_class_attrsrD   rD   rE   r'   9  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)r6   r7   rŸ   r    rD   )rO   Úinitial_extrar…   rz   )rx   r6   r7   rŸ   r    rO   r¡   rv   r–   rD   rE   rz   Ÿ  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–   rD   rE   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 rD   rR   ©r`   ÚorD   rD   rE   rj   ¾  ó    z5BaseModelFormSet._existing_object.<locals>.<dictcomp>)rT   rÏ   rÑ   r9   )rx   rQ   rD   rD   rE   Ú_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©rx   r3   rD   rD   rE   Ú_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Í   r8   rV   r+   rQ   r1   r6   ÚKeyErrorrÚ   r   rÕ   rÏ   rÌ   Ú
IndexErrorr…   Ú_construct_formr-   rª   )rx   Úirv   Úpk_requiredÚpk_keyrQ   rØ   r>   r–   rD   rE   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)rT   rO   rV   Ú_default_managerrÏ   ÚorderedÚorder_byr+   rQ   r1   rá   )rx   ÚqsrD   rD   rE   rÏ   ì  s   

zBaseModelFormSet.get_querysetTc                 C   s   |j |d�S )z8Save and return a new model instance for the given form.©rÃ   ©rÁ   ©rx   r>   rÃ   rD   rD   rE   Ú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ç   )rx   r>   ÚobjrÃ   rD   rD   rE   Úsave_existing  rê   zBaseModelFormSet.save_existingc                 C   s   |r|  ¡  dS dS )z#Deletes an existing model instance.N)Údelete)rx   rë   rÃ   rD   rD   rE   Ú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 r{   )Úsaved_formsrÂ   )r>   r®   rD   rE   rÂ     s   

ÿz'BaseModelFormSet.save.<locals>.save_m2m)rï   rÂ   Ú	edit_onlyÚsave_existing_objectsÚsave_new_objects)rx   rÃ   rÂ   rD   r®   rE   rÁ     s   
zBaseModelFormSet.savec                 C   s   |   ¡  d S r{   )r¶   r®   rD   rD   rE   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 rD   )Úis_valid)r`   r>   )Úforms_to_deleterD   rE   rb   (  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 r{   )r,   Úunique_fields©r`   r3   )r>   rx   rD   rE   Ú	<genexpr>=  s   € 
þýz3BaseModelFormSet.validate_unique.<locals>.<genexpr>c                 s   s*   � | ]}t |d ƒr| ¡ nt|ƒV  qdS )Ú_get_pk_valN)rT   rú   r   )r`   ÚdrD   rD   rE   rù   C  s   € ü
ÿ
û)r›   Údate)r¦   Údeleted_formsÚformsr­   r?   Ú_get_unique_checksrŒ   Útupler<   Úget_unique_error_messager¢   Úget_form_errorr›   r¨   r   r,   r§   ÚyearÚmonthÚdayrJ   Úget_date_error_messager   )rx   Úall_unique_checksÚall_date_checksÚvalid_formsr@   Úunique_checksÚdate_checksr³   ÚuclassÚunique_checkÚ	seen_dataÚrow_datar3   Ú
date_checkÚlookupÚ
unique_forrü   Ú	date_datar6   rD   )r>   rõ   rx   rE   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.r3   r   zFPlease correct the duplicate data for %(field)s, which must be unique.Úand)rÎ   r   r   Ú_©rx   r  rD   rD   rE   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‡   )rx   r  rD   rD   rE   r  �  s   ÿ
ýýz'BaseModelFormSet.get_date_error_messagec                 C   s   t dƒS )Nz*Please correct the duplicate values below.r   r®   rD   rD   rE   r  ™  s   zBaseModelFormSet.get_form_errorc                 C   s¤   g | _ g | _| jsg S g }| j}| jD ]<}|j}| ¡ sq||v r/| j |¡ | j||d� q| ¡ rO| j  ||j	f¡ | | j
|||d�¡ |sO| j |¡ q|S ©Nræ   )Úchanged_objectsÚdeleted_objectsÚinitial_formsrý   r?   Ú
_is_pk_setr<   rî   Úhas_changedÚchanged_datarì   rï   )rx   rÃ   Úsaved_instancesrõ   r>   rë   rD   rD   rE   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_formr<   ré   rï   )rx   rÃ   r>   rD   rD   rE   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   )r0   Ú
ForeignKeyÚOneToOneFieldc                    s6   | j  p| jpt| ˆ ƒp| jo| jjoˆ| jjjjƒS r{   )r.   Úauto_createdr/   rÖ   Úparent_linkrV   r+   rQ   rR   ©r0   Úpk_is_not_editablerD   rE   r-  Ï  s   ÿÿúz7BaseModelFormSet.add_fields.<locals>.pk_is_not_editableNF)r¡   rª   r4   )rS   r0   r(  r)  rV   r+   rQ   Ú	_pk_fieldr1   r-   rÍ   r?   r¿   rÀ   rÏ   rÜ   r/   rÖ   râ   ÚusingÚdbro   r9   r   r    r…   Ú
add_fields)	rx   r>   Úindexr(  r)  rQ   Úpk_valuerå   r4   r–   r,  rE   r1  Ä  s2   €ÿÿzBaseModelFormSet.add_fields)NNr™   NNrÄ   )rl   r|   r}   Ú__doc__rV   rð   r¦   r÷   rz   rÐ   rÕ   rÚ   rÝ   rÏ   ré   rì   rî   rÁ   rÅ   r¯   r¶   r  r  r  rñ   rò   r1  r˜   rD   rD   r–   rE   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@   rp   ro   rq   rr   rs   rf   rt   )
ÚextraÚmin_numÚmax_numÚ	can_orderr&  Úvalidate_minÚvalidate_maxÚabsolute_maxÚcan_delete_extrar›   )rJ   r   r'   r   rV   rð   )rV   r>   rp   Úformsetr5  r&  r8  r7  r-   r@   ro   r:  rq   rr   rs   rf   r6  r9  rt   r;  r<  r›   rð   ÚmetaÚFormSetrD   rD   rE   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 ¡ r-|jdi | j j| ji¤Ž}n| 	¡ }| j jh| _
tƒ j||f||dœ|¤Ž | jjjrq| j j| jjjvrst| jjjtƒret| jjjƒ| jj_| jjj | j j¡ d S d S d S )N)r    rO   rD   )ÚfkrÖ   rV   r?   Úsave_as_newrâ   r   rU   r1   Únoner÷   r…   rz   r>   r+   r-   r/   r   Úlistr<   )	rx   r6   r7   r?   rA  r    rO   rv   rå   r–   rD   rE   rz   A  s    

ýzBaseInlineFormSet.__init__c                    s   | j rdS tƒ  ¡ S )Nr   )rA  r…   rÐ   r®   r–   rD   rE   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	j|ƒ |S )NÚ_mutableTrQ   )r…   rÝ   rA  rJ   r6   rD  r8   r.  r1   r@  r?   rQ   rÖ   r  rV   r+   ÚsetattrÚattname)rx   rÞ   rv   r>   ÚmutableÚfk_valuer–   rD   rE   rÝ   e  s   z!BaseInlineFormSet._construct_formc                 C   s   | j jj| jd� dd¡S )N)rV   ú+Ú )r@  rÖ   Úget_accessor_namerV   Úreplace)ÚclsrD   rD   rE   Úget_default_prefix|  s   z$BaseInlineFormSet.get_default_prefixTc                    s$   t |j| jj| jƒ tƒ j||d�S r  )rE  r?   r@  r1   r…   ré   rè   r–   rD   rE   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_fieldTrd   Úto_field)r…   r1  r.  r@  r1   rJ   r-   r9   r   Úverbose_namerÖ   r  rV   r+   rQ   r?   r¿   rÀ   Ú	get_fieldr2   rF  r6   rE  r·   )rx   r>   r2  r1   rv   rP  r–   rD   rE   r1  ‡  s*   
ÿÿ	

ÿzBaseInlineFormSet.add_fieldsc                    s   ‡ fdd„|D ƒ}t ƒ  |¡S )Nc                    s   g | ]
}|ˆ j jkr|‘qS rD   )r@  r1   rø   r®   rD   rE   rb   ¯  s    z>BaseInlineFormSet.get_unique_error_message.<locals>.<listcomp>)r…   r  r  r–   r®   rE   r  ®  s   z*BaseInlineFormSet.get_unique_error_message)NNNFNNrÄ   )rl   r|   r}   r4  rz   rÐ   rÝ   ÚclassmethodrN  ré   r1  r  r˜   rD   rD   r–   rE   r%   >  s     ù
'r%   c                    sL  ddl m‰  |j}ˆrg‡fdd„|jD ƒ}t|ƒdkrY|d }g ˆjj¢ˆ‘R ‰t|ˆ ƒrM|jjjj	r;|jjjj
ˆvsM|jjjj	sW|jjˆkrW|jjˆvrWtdˆˆjjf ƒ‚|S |setd|jjˆf ƒ‚|S g ˆjj¢ˆ‘R ‰‡ ‡‡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 rD   )r1   r_   ©Úfk_namerD   rE   rb   À  ó    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 rD   )r/   rÖ   rV   r+   ÚproxyÚproxy_for_modelr_   )r(  Úall_parentsÚparent_modelrD   rE   rb   Ý  s    ý
þøNz'%s' has no ForeignKey to '%s'.zR'%s' has more than one ForeignKey to '%s'. You must specify a 'fk_name' attribute.)rS   r(  r+   r-   rÎ   rY  r/   rÖ   rV   rW  rX  rœ   rd   )rZ  rV   rU  Úcan_failrA   Úfks_to_parentr@  rD   )r(  rY  rU  rZ  rE   Ú_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``.
    rT  r  r>   rp   r=  r5  r&  r8  r-   r@   r6  r7  ro   r9  r:  rq   rr   rs   rf   )rt   r;  r<  r›   rð   )r]  Úuniquer$   r@  )rZ  rV   r>   r=  rU  r-   r@   r5  r8  r&  r7  rp   ro   r:  rq   rr   rs   rf   r6  r9  rt   r;  r<  r›   rð   r@  rv   r?  rD   rD   rE   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)rO  rP  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_instancerO  rP  rJ   rQ   r…   rz   )rx   r`  rO  rP  Úargsrv   r–   rD   rE   rz   R  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±   )	r:   rO  r`  rP  rJ   rQ   r‡   r   rf   )rx   r�   ÚorigrD   rD   rE   r¯   ^  s   

ÿzInlineForeignKeyField.cleanc                 C   s   dS ©NFrD   )rx   r¡   r6   rD   rD   rE   r!  o  s   z!InlineForeignKeyField.has_changed)rl   r|   r}   r4  r   r4   r  Údefault_error_messagesrz   r¯   r!  r˜   rD   rD   r–   rE   r·   G  s    ÿr·   c                   @   s,   e Zd Zdd„ Zdd„ Zdd„ Zdd„ Zd	S )
ÚModelChoiceIteratorValuec                 C   s   || _ || _d S r{   )r�   r?   )rx   r�   r?   rD   rD   rE   rz   t  s   
z!ModelChoiceIteratorValue.__init__c                 C   ó
   t | jƒS r{   )r‡   r�   r®   rD   rD   rE   Ú__str__x  ó   
z ModelChoiceIteratorValue.__str__c                 C   rg  r{   )Úhashr�   r®   rD   rD   rE   Ú__hash__{  ri  z!ModelChoiceIteratorValue.__hash__c                 C   s   t |tƒr|j}| j|kS r{   )r/   rf  r�   )rx   ÚotherrD   rD   rE   Ú__eq__~  s   

zModelChoiceIteratorValue.__eq__N)rl   r|   r}   rz   rh  rk  rm  rD   rD   rD   rE   rf  s  s
    rf  c                   @   s4   e Zd Zdd„ Zdd„ Zdd„ Zdd„ Zd	d
„ ZdS )ÚModelChoiceIteratorc                 C   s   || _ |j| _d S r{   )r3   rO   rÙ   rD   rD   rE   rz   …  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 )NrJ  )r3   Úempty_labelrO   Ú_prefetch_related_lookupsÚiteratorÚchoice)rx   rO   rë   rD   rD   rE   Ú__iter__‰  s   €ÿzModelChoiceIterator.__iter__c                 C   s    | j  ¡ | jjd urd S d S )Nr  r   )rO   Úcountr3   ro  r®   rD   rD   rE   Ú__len__“  s    zModelChoiceIterator.__len__c                 C   s   | j jd up
| j ¡ S r{   )r3   ro  rO   Úexistsr®   rD   rD   rE   Ú__bool__™  s   zModelChoiceIterator.__bool__c                 C   s   t | j |¡|ƒ| j |¡fS r{   )rf  r3   Úprepare_valueÚlabel_from_instance©rx   rë   rD   rD   rE   rr  œ  s   
þzModelChoiceIterator.choiceN)rl   r|   r}   rz   rs  ru  rw  rr  rD   rD   rD   rE   rn  „  s    
rn  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	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	---------TNrJ  F)	ro  rª   r4   rd   r¡   re   Úto_field_namerY   r©   c       	         K   s^   t j| f|||||dœ|¤Ž |r|d ust| jtƒr!|
s!d | _n|| _|| _|	| _|| _d S )N)rª   r4   rd   r¡   re   )	r   rz   r/   r4   r   ro  rO   rY   r{  )rx   rO   ro  rª   r4   rd   r¡   re   r{  rY   r©   rv   rD   rD   rE   rz   ¯  s*   ÿúù	
ÿÿ
zModelChoiceField.__init__c                 C   s   t ƒ }||ƒS r{   r   )rx   r�   Únon_null_character_validatorrD   rD   rE   Úvalidate_no_null_charactersÓ  s   z,ModelChoiceField.validate_no_null_charactersc                 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.
        )rm   rY   r®   rD   rD   rE   rP   ×  s   
z%ModelChoiceField.get_limit_choices_toc                    s*   t t| ƒ |¡}| jd ur| j ¡ |_|S r{   )r…   r
   Ú__deepcopy__rO   Úall)rx   ÚmemoÚresultr–   rD   rE   r~  á  s   
zModelChoiceField.__deepcopy__c                 C   s   | j S r{   )rá   r®   rD   rD   rE   Ú_get_querysetè  s   zModelChoiceField._get_querysetc                 C   s$   |d u rd n|  ¡ | _| j| j_d S r{   )r  rá   Úchoicesr4   )rx   rO   rD   rD   rE   Ú_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‡   rz  rD   rD   rE   ry  ó  s   z$ModelChoiceField.label_from_instancec                 C   s   t | dƒr| jS |  | ¡S )NÚ_choices)rT   r†  rq  r®   rD   rD   rE   Ú_get_choicesû  s   

	zModelChoiceField._get_choicesc                    s.   t |dƒr| jr| | j¡S |jS tƒ  |¡S ©Nr+   )rT   r{  Úserializable_valuerQ   r…   rx  ©rx   r�   r–   rD   rE   rx    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yD   t| jd dd|id�‚w )NrQ   r_  r�   ©r±   ÚparamsrD   )r:   r}  r{  r/   rO   rV   rJ   r9   rœ   rn   ÚDoesNotExistr   rf   )rx   r�   ÚkeyrD   rD   rE   rØ     s    



úýÿzModelChoiceField.to_pythonc                 C   s   t  | |¡S r{   )r   ÚvalidaterŠ  rD   rD   rE   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 )NFrJ  )Údisabledr‡   rx  )rx   r¡   r6   Úinitial_valueÚ
data_valuerD   rD   rE   r!  (  s
   zModelChoiceField.has_changed)rl   r|   r}   r4  r  re  rn  rq  rz   r}  rP   r~  r‚  r„  ÚpropertyrO   ry  r‡  r
   rƒ  Úfsetrx  rØ   r�  r!  r˜   rD   rD   r–   rE   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 )Nro  )r…   rz   )rx   rO   rv   r–   rD   rE   rz   =  s   z!ModelMultipleChoiceField.__init__c                 C   s   |sg S t |  |¡ƒS r{   )rC  Ú_check_valuesrŠ  rD   rD   rE   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ª   rb  r•  )rx  rª   r   rf   rO   rB  r/   rC  r   r—  Úrun_validators)rx   r�   rå   rD   rD   rE   r¯   E  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yG   t| jd dd|id�‚w | jjdi dˆ  |i¤Ž}‡ fdd„|D ƒ}|D ]}t	|ƒ|vrtt| 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.)
        rQ   r•  rb  r–  r‹  z%s__inc                    s   h | ]	}t t|ˆ ƒƒ’qS rD   )r‡   rJ   rÒ   ©rŽ  rD   rE   r„   r  rV  z9ModelMultipleChoiceField._check_values.<locals>.<setcomp>r_  r�   NrD   )
r{  Ú	frozensetrn   r   rf   r}  rO   rU   rœ   r‡   )rx   r�   rQ   rå   ÚpksÚvalrD   r™  rE   r—  V  s>   
þþ
ýÿýÿz&ModelMultipleChoiceField._check_valuesc                    sD   t |dƒrt|tƒst |dƒstƒ j‰ ‡ fdd„|D ƒS tƒ  |¡S )Nrs  r+   c                    s   g | ]}ˆ |ƒ‘qS rD   rD   )r`   rƒ   ©rx  rD   rE   rb   ƒ  rÔ   z:ModelMultipleChoiceField.prepare_value.<locals>.<listcomp>)rT   r/   r‡   r…   rx  rŠ  r–   r�  rE   rx  |  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 rD   r…  ©r`   r�   rD   rD   rE   r„   �  rÔ   z7ModelMultipleChoiceField.has_changed.<locals>.<setcomp>c                 S   rž  rD   r…  rŸ  rD   rD   rE   r„   �  rÔ   )r�  rÎ   rx  )rx   r¡   r6   Úinitial_setÚdata_setrD   rD   rE   r!  †  s   z$ModelMultipleChoiceField.has_changed)rl   r|   r}   r4  r   r4   r   Úhidden_widgetr  re  rz   rØ   r¯   r—  rx  r!  r˜   rD   rD   r–   rE   r!   0  s     ÿû&
r!   c                 C   s"   t | dƒo| jjd up| jjd uS rˆ  )rT   r+   r-   r@   )rg   rD   rD   rE   Úmodelform_defines_fields”  s   
ÿr£  )NN)	NNNNNNNNNrd  )=r4  Ú	itertoolsr   Údjango.core.exceptionsr   r   r   r   Údjango.core.validatorsr   Údjango.db.models.utilsr	   Údjango.forms.fieldsr
   r   Údjango.forms.formsr   r   Údjango.forms.formsetsr   r   Údjango.forms.utilsr   Údjango.forms.widgetsr   r   r   r   Údjango.utils.choicesr   Údjango.utils.hashabler   Údjango.utils.textr   r   Údjango.utils.translationr   r   r  r(   r"   rF   r   r[   r   rw   r~   r   r   r'   r#   r$   r%   r]  r&   r·   rf  rn  r    r!   r£  rD   rD   rD   rE   Ú<module>   sØ    

0öóvI ]
õ[  g
éG
uR
çE, d