U
    8ÄT_3à  ã                   @   s$  d Z ddl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 ddlmZmZ ddlmZm Z! dZ"dZ#d=dd„Z$d>dd„Z%dd„ Z&d?ddœdd„Z'G dd„ dƒZ(G dd„ deƒZ)G dd„ deƒZ*G dd „ d e*e)d!�Z+e+dddddddddf
d"d#„Z,G d$d%„ d%eƒZ-e+de-d&d'd'ddddd'dddddd'dfd(d)„Z.G d*d+„ d+e-ƒZ/d@d,d-„Z0e+e/dddd.d'ddddd'dddddd'dfd/d0„Z1G d1d2„ d2eƒZ2G d3d4„ d4ƒZ3G d5d6„ d6ƒZ4G d7d8„ d8e
ƒZ5G d9d:„ d:e5ƒZ6d;d<„ Z7dS )Az[
Helper functions for creating Form classes from Django models
and database field objects.
é    N)Úchain)ÚNON_FIELD_ERRORSÚ
FieldErrorÚImproperlyConfiguredÚValidationError)ÚChoiceFieldÚField)ÚBaseFormÚDeclarativeFieldsMetaclass)ÚBaseFormSetÚformset_factory)Ú	ErrorList)ÚHiddenInputÚMultipleHiddenInputÚRadioSelectÚSelectMultiple)ÚRemovedInDjango40Warning)Ú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__c           	      C   sü   ddl m} |j}| j}g }|jD ]¸}|jr"t||jƒs"|j|krDq"|dk	rX|j|krXq"|rh|j|krhq"| 	¡ r°| |j j
j | j| j|  |j¡¡r°| |j¡| |j j
jkr°q"t||jƒrÈ| |¡ q"| |||j ¡ q"|D ]}| |||j ¡ qà|S )z–
    Construct and return a model instance from the bound ``form``'s
    ``cleaned_data``, but do not save the returned instance to the database.
    r   )ÚmodelsN)Z	django.dbr$   Ú_metaÚcleaned_dataÚfieldsÚeditableÚ
isinstanceÚ	AutoFieldÚnameÚhas_defaultÚfieldÚwidgetZvalue_omitted_from_dataÚdataÚfilesÚ
add_prefixÚgetÚempty_valuesZ	FileFieldÚappendÚsave_form_data)	ÚformÚinstancer'   Úexcluder$   Úoptsr&   Zfile_field_listÚf© r;   ú7/tmp/pip-unpacked-wheel-dv63caxw/django/forms/models.pyÚconstruct_instance    s4    
ÿÿ"þýr=   c                 C   sh   | j }i }t|j|j|jƒD ]F}t|ddƒs.q|dk	rB|j|krBq|rR|j|krRq| | ¡||j< q|S )a‚  
    Return a dict containing the data in ``instance`` suitable for passing as
    a Form's ``initial`` keyword argument.

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

    ``exclude`` is an optional list of field names. If provided, exclude the
    named from the returned dict, even if they are listed in the ``fields``
    argument.
    r(   FN)r%   r   Úconcrete_fieldsÚprivate_fieldsÚmany_to_manyÚgetattrr+   Zvalue_from_object)r7   r'   r8   r9   r/   r:   r;   r;   r<   r   I   s    r   c                 C   s6   t | dƒr2t | dƒr2|  ¡ }|dk	r2| j |¡| _dS )z=Apply limit_choices_to to the formfield's queryset if needed.ÚquerysetÚget_limit_choices_toN)ÚhasattrrC   rB   Zcomplex_filter)Ú	formfieldÚlimit_choices_tor;   r;   r<   Ú#apply_limit_choices_to_to_formfieldb   s    rG   T©Úapply_limit_choices_toc
                   sö  i ‰g ‰| j }ddlm‰  ‡ fdd„|jD ƒ}tt|j||jƒƒD �]�}t|ddƒsŽ|dk	rB|j	|krBˆdksx|j	ˆkrBt
d|j	| jf ƒ‚qB|dk	r¢|j	|kr¢qBˆr²|j	ˆkr²qBi }|rÒ|j	|krÒ||j	 |d	< |tksè|rð|j	|krðd
|d< |�r|j	|k�r||j	 |d< |�r0|j	|k�r0||j	 |d< |�rP|j	|k�rP||j	 |d< |	�rp|j	|	k�rp|	|j	 |d< |dk�rˆ|jf |Ž}n t|ƒ�sœtdƒ‚n||f|Ž}|�rÈ|
�r¼t|ƒ |ˆ|j	< qBˆ |j	¡ qB|�rò‡‡‡fdd„|D ƒ‰ˆS )a4  
    Return a dictionary containing form fields for the given model.

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

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

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

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

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

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

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

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

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

    ``apply_limit_choices_to`` is a boolean indicating if limit_choices_to
    should be applied to a field's queryset.
    r   )r   c                    s   g | ]}t |ˆ ƒr|‘qS r;   )r)   ©Ú.0r:   )Ú
ModelFieldr;   r<   Ú
<listcomp>‘   s     
 z$fields_for_model.<locals>.<listcomp>r(   FNzH'%s' cannot be specified for %s model form as it is a non-editable fieldr.   TZlocalizeÚlabelÚ	help_textÚerror_messagesÚ
form_classz1formfield_callback must be a function or callablec                    s,   i | ]$}ˆ r|ˆ kr|ˆkr|ˆ  |¡“qS r;   )r2   rJ   )r8   Ú
field_dictÚignoredr;   r<   Ú
<dictcomp>¼   s     ÿ z$fields_for_model.<locals>.<dictcomp>)r%   Údjango.db.modelsr   r?   Úsortedr   r>   r@   rA   r+   r   Ú__name__r   rE   ÚcallableÚ	TypeErrorrG   r4   )Úmodelr'   r8   ÚwidgetsÚformfield_callbackÚlocalized_fieldsÚlabelsÚ
help_textsrP   Úfield_classesrI   r9   Zsortable_private_fieldsr:   ÚkwargsrE   r;   )rL   r8   rR   rS   r<   r   j   sf    "ÿÿ ÿÿ


ÿ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 )
NrZ   r'   r8   r[   r]   r^   r_   rP   r`   )
rA   rZ   r'   r8   r[   r]   r^   r_   rP   r`   )ÚselfÚoptionsr;   r;   r<   Ú__init__Ä   s    zModelFormOptions.__init__)N)rW   Ú
__module__Ú__qualname__re   r;   r;   r;   r<   rb   Ã   s   rb   c                       s   e Zd Z‡ fdd„Z‡  ZS )ÚModelFormMetaclassc                    s~  d }|D ]&}t |dƒrt |jdƒr|jj} q0q| d|¡}tƒ  | |||¡}|tfkr\|S tt|dd ƒƒ }|_	dD ]:}	t||	ƒ}
t
|
tƒrv|
tkrvd|j|	|
dœ }t|ƒ‚qv|j�rn|jd krÚ|jd krÚtd| ƒ‚|jtkrêd |_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ÚMetar\   )r'   r8   r]   zP%(model)s.Meta.%(opt)s cannot be a string. Did you mean to type: ('%(value)s',)?)rZ   ÚoptÚvaluez|Creating a ModelForm without either the 'fields' attribute or the 'exclude' attribute is prohibited; form %s needs updating.FrH   c                 S   s   h | ]\}}|s|’qS r;   r;   )rK   ÚkÚvr;   r;   r<   Ú	<setcomp>  s      z-ModelFormMetaclass.__new__.<locals>.<setcomp>z&Unknown field(s) (%s) specified for %sz, )!rD   ri   r\   ÚpopÚsuperÚ__new__r   rb   rA   r%   r)   Ústrr   rW   rY   rZ   r'   r8   r   r   r[   r]   r^   r_   rP   r`   ÚitemsÚ
differenceZdeclared_fieldsÚjoinr   ÚupdateZbase_fields)Zmcsr+   ÚbasesÚattrsZbase_formfield_callbackÚbr\   Ú	new_classr9   rj   rk   Úmsgr'   Znone_model_fieldsZmissing_fieldsÚmessage©Ú	__class__r;   r<   rq   Ñ   sl    

ýÿ
þÿ
       û	
ÿzModelFormMetaclass.__new__)rW   rf   rg   rq   Ú__classcell__r;   r;   r}   r<   rh   Ð   s   rh   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 krtdƒ‚|	d kr0| ¡ | _i }n|	| _t|	|j|jƒ}|d k	rX| |¡ d| _t	ƒ j
|||||||||
|d�
 | j ¡ D ]}t|ƒ qˆd S )Nz'ModelForm has no model class specified.F)Úuse_required_attributeÚrenderer)r%   rZ   Ú
ValueErrorr7   r   r'   r8   rv   Ú_validate_uniquerp   re   ÚvaluesrG   )rc   r/   r0   Úauto_idÚprefixÚinitialÚerror_classZlabel_suffixZempty_permittedr7   r�   r‚   r9   Zobject_datarE   r}   r;   r<   re     s2    


       ýzBaseModelForm.__init__c                 C   sÊ   g }| j jjD ]¶}|j}|| jkr0| |j¡ q| jjrR|| jjkrR| |j¡ q| jjrt|| jjkrt| |j¡ q|| jkrŒ| |j¡ q| j| }| j |¡}|j	s|j
s||jkr| |j¡ q|S )z‘
        For backwards-compatibility, exclude several types of fields from model
        validation. See tickets #12507, #12521, #12553.
        )r7   r%   r'   r+   r4   r8   Ú_errorsr&   r2   ÚblankÚrequiredr3   )rc   r8   r:   r-   Z
form_fieldZfield_valuer;   r;   r<   Ú_get_validation_exclusions6  s     

	
z(BaseModelForm._get_validation_exclusionsc                 C   s   d| _ | jS )NT)r„   r&   ©rc   r;   r;   r<   Úclean_  s    zBaseModelForm.cleanc                 C   sª   | j }t|dƒr|j}nt|i}| ¡ D ]p\}}|tkrT|jrTt|jkrT|jt }n|| jkr(| j| j}nq(|D ]$}t|tƒrr|j	|krr||j	 |_
qrq(|  d |¡ d S )NÚ
error_dict)r%   rD   r�   r   rs   rP   r'   r)   r   Úcoder|   Z	add_error)rc   Úerrorsr9   r�   r-   ÚmessagesrP   r|   r;   r;   r<   Ú_update_errorsc  s$    
ÿ

ÿzBaseModelForm._update_errorsc              
   C   sÒ   | j }|  ¡ }| j ¡ D ]\}}t|tƒr| |¡ qzt| | j|j|j	ƒ| _W n, t
k
r| } z|  |¡ W 5 d }~X Y nX z| jj|dd� W n, t
k
r¾ } z|  |¡ W 5 d }~X Y nX | jrÎ|  ¡  d S )NF)r8   Úvalidate_unique)r%   r�   r'   rs   r)   ÚInlineForeignKeyFieldr4   r=   r7   r8   r   r”   Z
full_cleanr„   r•   )rc   r9   r8   r+   r-   Úer;   r;   r<   Ú_post_clean  s    	
zBaseModelForm._post_cleanc              
   C   sL   |   ¡ }z| jj|d� W n, tk
rF } z|  |¡ W 5 d}~X Y nX dS )z‚
        Call the instance's validate_unique() method and update the form's
        validation errors if any were raised.
        ©r8   N)r�   r7   r•   r   r”   )rc   r8   r—   r;   r;   r<   r•   �  s
    zBaseModelForm.validate_uniquec                 C   s€   | j }| jj}| jj}| jj}t|j|jƒD ]N}t|dƒs<q,|rL|j	|krLq,|r\|j	|kr\q,|j	|kr,| 
| j||j	 ¡ q,dS )zS
        Save the many-to-many fields and generic relations for this form.
        r5   N)r&   r%   r8   r'   r7   r   r@   r?   rD   r+   r5   )rc   r&   r8   r'   r9   r:   r;   r;   r<   Ú	_save_m2m¨  s    

zBaseModelForm._save_m2mTc                 C   sP   | j r*td| jjj| jjjr dndf ƒ‚|rB| j ¡  |  ¡  n| j| _	| jS )zë
        Save this form's self.instance object if commit=True. Otherwise, add
        a save_m2m() method to the form which can be called after the instance
        is saved manually at a later time. Return the model instance.
        z8The %s could not be %s because the data didn't validate.ÚcreatedÚchanged)
r’   rƒ   r7   r%   Zobject_nameÚ_stateÚaddingÚsaverš   Úsave_m2m)rc   Úcommitr;   r;   r<   rŸ   ½  s    þÿ

zBaseModelForm.save)T)rW   rf   rg   r   re   r�   r�   r”   r˜   r•   rš   rŸ   Úalters_datar   r;   r;   r}   r<   r     s"       ý)
r   c                   @   s   e Zd ZdS )r   N)rW   rf   rg   r;   r;   r;   r<   r   ×  s   r   )Ú	metaclassc                 C   s  d| i}|dk	r||d< |dk	r(||d< |dk	r8||d< |dk	rH||d< |dk	rX||d< |dk	rh||d< |	dk	rx|	|d	< |
dk	rˆ|
|d
< t |dƒrš|jfnd}td||ƒ}|r¸t|ƒ|_| jd }||dœ}t|ddƒdkrôt|ddƒdkrôtdƒ‚t|ƒ||f|ƒS )al  
    Return a ModelForm containing form fields for the given model. You can
    optionally pass a `form` argument to use as a starting point for
    constructing the ModelForm.

    ``fields`` is an optional list of field names. If provided, include only
    the named fields in the returned fields. If omitted or '__all__', use all
    fields.

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

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

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

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

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

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

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

    ``field_classes`` is a dictionary of model field names mapped to a form
    field class.
    rZ   Nr'   r8   r[   r]   r^   r_   rP   r`   ri   r;   ZForm)ri   r\   zZCalling modelform_factory without defining 'fields' or 'exclude' explicitly is prohibited.)rD   ri   ÚtypeÚstaticmethodr\   rW   rA   r   )rZ   r6   r'   r8   r\   r[   r]   r^   r_   rP   r`   rx   rw   ri   Ú
class_nameZform_class_attrsr;   r;   r<   r"   Û  s@    '

þÿÿr"   c                       sØ   e Zd ZdZdZeƒ Zd*ddœ‡ fdd„Z‡ fdd„Zd	d
„ Z	dd„ Z
‡ fdd„Zdd„ Zd+dd„Zd,dd„Zd-dd„Zd.dd„Zde_dd„ Zdd„ Zdd„ Zd d!„ Zd"d#„ Zd/d$d%„Zd0d&d'„Z‡ fd(d)„Z‡  ZS )1r   zO
    A ``FormSet`` for editing a queryset and/or adding new objects to it.
    Nr€   )rˆ   c                   s,   || _ || _tƒ jf ||||dœ|–Ž d S )N)r/   r0   r†   r‡   )rB   Úinitial_extrarp   re   )rc   r/   r0   r†   r‡   rB   rˆ   ra   r}   r;   r<   re   9  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_querysetrp   Úinitial_form_countrŽ   r}   r;   r<   r«   ?  s    z#BaseModelFormSet.initial_form_countc                 C   s*   t | dƒsdd„ |  ¡ D ƒ| _| j |¡S )NÚ_object_dictc                 S   s   i | ]}|j |“qS r;   ©Úpk©rK   Úor;   r;   r<   rT   G  s      z5BaseModelFormSet._existing_object.<locals>.<dictcomp>)rD   rª   r¬   r2   )rc   r®   r;   r;   r<   Ú_existing_objectE  s    
z!BaseModelFormSet._existing_objectc                 C   s   |j dk	r|j  ¡ }q |jS )z‹
        If the field is a related field, fetch the concrete field's (that
        is, the ultimate pointed-to field's) to_python.
        N)Úremote_fieldZget_related_fieldÚ	to_python©rc   r-   r;   r;   r<   Ú_get_to_pythonJ  s    
zBaseModelFormSet._get_to_pythonc                    s  ||   ¡ k }|r¨| jr–d|  |¡| jjjjf }z| j| }W n tk
rR   Y q¦X |  	| jjj¡}z||ƒ}W n t
k
r„   Y q¦X |  |¡|d< qÞ|  ¡ | |d< n6| jrÞz| j||   ¡   |d< W n tk
rÜ   Y nX tƒ j|f|Ž}|�rd|j| jjjj _|S )Nz%s-%sr7   rˆ   T)r«   r¨   r1   rZ   r%   r®   r+   r/   ÚKeyErrorrµ   r   r±   rª   r§   Ú
IndexErrorrp   Ú_construct_formr'   rŒ   )rc   Úira   Zpk_requiredZpk_keyr®   r³   r6   r}   r;   r<   r¸   S  s0    z BaseModelFormSet._construct_formc                 C   sL   t | dƒsF| jd k	r| j}n| jj ¡ }|js@| | jjjj	¡}|| _
| j
S )NÚ	_queryset)rD   rB   rZ   Ú_default_managerrª   ZorderedZorder_byr%   r®   r+   rº   )rc   Úqsr;   r;   r<   rª   u  s    

zBaseModelFormSet.get_querysetTc                 C   s   |j |d�S )z8Save and return a new model instance for the given form.©r¡   ©rŸ   ©rc   r6   r¡   r;   r;   r<   Úsave_newˆ  s    zBaseModelFormSet.save_newc                 C   s   |j |d�S )z>Save and return an existing model instance for the given form.r½   r¾   )rc   r6   r7   r¡   r;   r;   r<   Úsave_existingŒ  s    zBaseModelFormSet.save_existingc                 C   s   |r|  ¡  dS )z#Deletes an existing model instance.N)Údelete)rc   Úobjr¡   r;   r;   r<   Údelete_existing�  s    z BaseModelFormSet.delete_existingc                    s0   |sg ˆ _ ‡ fdd„}|ˆ _ˆ  |¡ˆ  |¡ S )zŒ
        Save model instances for every form, adding and changing instances
        as necessary, and return the list of instances.
        c                     s   ˆ j D ]} |  ¡  qd S ©N)Úsaved_formsr    )r6   rŽ   r;   r<   r    �  s    
z'BaseModelFormSet.save.<locals>.save_m2m)rÆ   r    Úsave_existing_objectsÚsave_new_objects)rc   r¡   r    r;   rŽ   r<   rŸ   •  s
    zBaseModelFormSet.savec                 C   s   |   ¡  d S rÅ   )r•   rŽ   r;   r;   r<   r�   ¥  s    zBaseModelFormSet.cleanc                    sô  t ƒ }t ƒ }ˆj‰‡fdd„ˆjD ƒ}|D ]2‰ ˆ  ¡ }ˆ jj|d�\}}| |¡ | |¡ q*g }|D ]ž\}}	t ƒ }
|D ]Š‰ ‡ ‡fdd„|	D ƒ}tdd„ |D ƒƒ}|rxd |krx||
krø| ˆ 	|	¡¡ ˆ 
ˆ ¡ g¡ˆ jt< |	D ]}|ˆ jkràˆ j|= qà|
 |¡ qxqf|D ]Ö}t ƒ }
|\}}}}|D ]¸‰ ˆ j�r$ˆ j| d k	�r$ˆ j| d k	�r$|dk�rvˆ j| }|j|j|jf}ntˆ j| |ƒf}ˆ j| f| }||
k�rÐ| ˆ |¡¡ ˆ 
ˆ ¡ g¡ˆ jt< ˆ j|= |
 |¡ �q$�q
|�rðt|ƒ‚d S )Nc                    s    g | ]}|  ¡ r|ˆ kr|‘qS r;   )Zis_valid)rK   r6   )Úforms_to_deleter;   r<   rM   ­  s       z4BaseModelFormSet.validate_unique.<locals>.<listcomp>r™   c                 3   s0   | ](}|ˆ j kr|ˆjkr|nˆ j | V  qd S rÅ   )r&   Úunique_fields©rK   r-   )r6   rc   r;   r<   Ú	<genexpr>º  s    
ÿz3BaseModelFormSet.validate_unique.<locals>.<genexpr>c                 s   s6   | ].}t |d ƒr| ¡ nt|tƒr*t|ƒn|V  qdS )Ú_get_pk_valN)rD   rÍ   r)   ÚlistÚtuple)rK   Údr;   r;   r<   rÌ   ¿  s   ýÚdate)ÚsetÚdeleted_formsZformsr�   r7   Z_get_unique_checksrv   rÏ   r4   Úget_unique_error_messager‰   Úget_form_errorrŠ   r   r&   ÚaddÚyearÚmonthÚdayrA   Úget_date_error_messager   )rc   Zall_unique_checksZall_date_checksZvalid_formsr8   Zunique_checksZdate_checksr’   ZuclassÚunique_checkZ	seen_dataZrow_datar-   Ú
date_checkÚlookupZ
unique_forrÑ   Z	date_datar/   r;   )r6   rÉ   rc   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 d S )Né   z0Please correct the duplicate data for %(field)s.r-   r   zFPlease correct the duplicate data for %(field)s, which must be unique.Úand)r©   r   r   Ú_©rc   rÛ   r;   r;   r<   rÔ   ñ  s     ÿ ÿz)BaseModelFormSet.get_unique_error_messagec                 C   s$   t dƒ|d |d t|d ƒdœ S )NzoPlease correct the duplicate data for %(field_name)s which must be unique for the %(lookup)s in %(date_field)s.é   é   rÞ   )Ú
field_nameZ
date_fieldrÝ   )r   rr   )rc   rÜ   r;   r;   r<   rÚ   û  s    ÿ
ýýz'BaseModelFormSet.get_date_error_messagec                 C   s   t dƒS )Nz*Please correct the duplicate values below.)r   rŽ   r;   r;   r<   rÕ     s    zBaseModelFormSet.get_form_errorc                 C   s¦   g | _ g | _| jsg S g }| j}| jD ]z}|j}|jd kr<q&||kr`| j |¡ | j||d� q&| ¡ r&| j  ||j	f¡ | | j
|||d�¡ |s&| j |¡ q&|S ©Nr½   )Zchanged_objectsZdeleted_objectsZinitial_formsrÓ   r7   r®   r4   rÄ   Úhas_changedZchanged_datarÁ   rÆ   )rc   r¡   Zsaved_instancesrÉ   r6   rÃ   r;   r;   r<   rÇ     s&    

z&BaseModelFormSet.save_existing_objectsc                 C   sZ   g | _ | jD ]F}| ¡ sq| jr,|  |¡r,q| j  | j||d�¡ |s| j |¡ q| j S rå   )Znew_objectsZextra_formsræ   Ú
can_deleteZ_should_delete_formr4   rÀ   rÆ   )rc   r¡   r6   r;   r;   r<   rÈ   "  s    
z!BaseModelFormSet.save_new_objectsc           	         s(  ddl m‰ m}m} | jjj | _}‡ ‡fdd„‰ˆ|ƒsH|j|j	k�r|j
rf|jjjr\dn|jj}n:z |dk	r€|  ¡ | j}nd}W n tk
rž   d}Y nX t|||fƒr¾|jjj ¡ }n| jj ¡ }| |jjj¡}|jjrø|jj | jjt¡}nt}t||d|d�|j	| jj< tƒ  ||¡ dS )z0Add a hidden field for the object's primary key.r   )r*   ÚOneToOneFieldÚ
ForeignKeyc                    s6   | j  p4| jp4t| ˆ ƒp4| jo4| jjo4ˆ| jjjjƒS rÅ   )r(   Zauto_createdr)   r²   Zparent_linkrZ   r%   r®   r­   ©r*   Úpk_is_not_editabler;   r<   rë   :  s    ýz7BaseModelFormSet.add_fields.<locals>.pk_is_not_editableNF)rˆ   rŒ   r.   )rU   r*   rè   ré   rZ   r%   r®   Ú	_pk_fieldr+   r'   r¨   r7   r�   rž   rª   r·   r)   r²   r»   ZusingÚdbr[   r2   r   r   rp   Ú
add_fields)	rc   r6   Úindexrè   ré   r®   Zpk_valuer¼   r.   r}   rê   r<   rî   0  s*    
zBaseModelFormSet.add_fields)NNr€   NN)T)T)T)T)T)T)rW   rf   rg   Ú__doc__rZ   rÒ   rÊ   re   r«   r±   rµ   r¸   rª   rÀ   rÁ   rÄ   rŸ   r¢   r�   r•   rÔ   rÚ   rÕ   rÇ   rÈ   rî   r   r;   r;   r}   r<   r   0  s4     ÿÿ	"



I



r   rÞ   Fc                 C   sv   t |ddƒ}t |d|ƒdkr4t |d|	ƒdkr4tdƒ‚t| |||	||
|||||d�}t|||||||||d�	}| |_|S )z8Return a FormSet class for the given Django model class.ri   Nr'   r8   z]Calling modelformset_factory without defining 'fields' or 'exclude' explicitly is prohibited.)
r6   r'   r8   r\   r[   r]   r^   r_   rP   r`   )ÚextraÚmin_numÚmax_numÚ	can_orderrç   Úvalidate_minÚvalidate_max)rA   r   r"   r   rZ   )rZ   r6   r\   Úformsetrñ   rç   rô   ró   r'   r8   r[   rö   r]   r^   r_   rP   rò   rõ   r`   ÚmetaÚFormSetr;   r;   r<   r   \  s0    ÿÿ
   ü  þr   c                       sl   e Zd ZdZd‡ fdd„	Z‡ fdd„Z‡ fdd	„Zed
d„ ƒZd‡ fdd„	Z	‡ fdd„Z
‡ fdd„Z‡  ZS )r    z0A formset for child objects related to a parent.NFc           	         sà   |d kr| j j ¡ | _n|| _|| _|d kr4| jj}| jjd k	rX|jf | j j| jiŽ}n| 	¡ }| j jh| _
tƒ j||f||dœ|—Ž | jjjrÜ| j j| jjjkrÜt| jjjtƒrÈt| jjjƒ| jj_| jjj | j j¡ d S )N)r‡   rB   )Úfkr²   rZ   r7   Úsave_as_newr»   r®   Úfilterr+   ÚnonerÊ   rp   re   r6   r%   r'   r)   rÏ   rÎ   r4   )	rc   r/   r0   r7   rû   r‡   rB   ra   r¼   r}   r;   r<   re   {  s    zBaseInlineFormSet.__init__c                    s   | j r
dS tƒ  ¡ S )Nr   )rû   rp   r«   rŽ   r}   r;   r<   r«   ’  s    z$BaseInlineFormSet.initial_form_countc                    sÄ   t ƒ j|f|Ž}| jrlt|jdd ƒ}|d k	r4d|j_d |j| | jj¡< d |j| | j	j¡< |d k	rl||j_| j
j}| j	jj| j	jjjjjkr¬t| j
| j	jjƒ}t|d|ƒ}t|j
| j	 ¡ |ƒ |S )NÚ_mutableTr®   )rp   r¸   rû   rA   r/   rþ   r1   rì   r+   rú   r7   r®   r²   rä   rZ   r%   ÚsetattrZget_attname)rc   r¹   ra   r6   ZmutableZfk_valuer}   r;   r<   r¸   —  s    z!BaseInlineFormSet._construct_formc                 C   s   | j jj| jd� dd¡S )N)rZ   ú+Ú )rú   r²   Zget_accessor_namerZ   Úreplace)Úclsr;   r;   r<   Úget_default_prefix®  s    z$BaseInlineFormSet.get_default_prefixTc                    s$   t |j| jj| jƒ tƒ j||d�S rå   )rÿ   r7   rú   r+   rp   rÀ   r¿   r}   r;   r<   rÀ   ²  s    zBaseInlineFormSet.save_newc                    sâ   t ƒ  ||¡ | j| jkr,| jj}ddi}n(| jj}dt|j |¡dt| jj	ƒƒi}| jj
j| jj
jjjjkr|| jj
j|d< | jjjrÊ| d¡d k	r¨| jj |d ¡}n
| jjj}| ¡ rÊt| j|jd ƒ t| jf|Ž|j|< d S )NÚpk_fieldTrN   Úto_field)rp   rî   rì   rú   r+   rA   r'   r2   r   Zverbose_namer²   rä   rZ   r%   r®   r7   r�   rž   Ú	get_fieldr,   rÿ   Zattnamer–   )rc   r6   rï   r+   ra   r  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<   rM   Ù  s      z>BaseInlineFormSet.get_unique_error_message.<locals>.<listcomp>)rp   rÔ   rá   r}   rŽ   r<   rÔ   Ø  s    z*BaseInlineFormSet.get_unique_error_message)NNNFNN)T)rW   rf   rg   rð   re   r«   r¸   Úclassmethodr  rÀ   rî   rÔ   r   r;   r;   r}   r<   r    y  s         ÿ
r    c                    s  ddl m‰  |j}ˆr–‡fdd„|jD ƒ}t|ƒdkr||d }t|ˆ ƒrf|jjˆkr”|jjˆj ¡ kr”t	dˆˆjj
f ƒ‚n|s”t	d|jj
ˆf ƒ‚nj‡ ‡fdd„|jD ƒ}t|ƒdkrÂ|d }n>|sè|rÎd	S t	d
|jj
ˆjj
f ƒ‚nt	d|jj
ˆjj
f ƒ‚|S )aD  
    Find and return the ForeignKey from model to parent if there is one
    (return None if can_fail is True and no such field exists). If fk_name is
    provided, assume it is the name of the ForeignKey field. Unless can_fail is
    True, raise an exception if there isn't a ForeignKey from model to
    parent_model.
    r   )ré   c                    s   g | ]}|j ˆ kr|‘qS r;   )r+   rJ   ©Úfk_namer;   r<   rM   é  s     
 z$_get_foreign_key.<locals>.<listcomp>rÞ   z)fk_name '%s' is not a ForeignKey to '%s'.z'%s' has no field named '%s'.c                    s8   g | ]0}t |ˆ ƒr|jjˆks0|jjˆj ¡ kr|‘qS r;   )r)   r²   rZ   r%   Úget_parent_listrJ   )ré   Úparent_modelr;   r<   rM   ø  s
   
ýNz'%s' has no ForeignKey to '%s'.zR'%s' has more than one ForeignKey to '%s'. You must specify a 'fk_name' attribute.)rU   ré   r%   r'   r©   r)   r²   rZ   r  rƒ   rN   )r  rZ   r
  Zcan_failr9   Zfks_to_parentrú   r;   )ré   r
  r  r<   Ú_get_foreign_keyÝ  sP    	

ÿþÿÿÿ
þÿþÿÿr  rã   c                 C   sX   t | ||d�}|jrd}
|||||	|||||
||||||||dœ}t|f|Ž}||_|S )z¤
    Return an ``InlineFormSet`` for the given kwargs.

    ``fk_name`` must be provided if ``model`` has more than one ``ForeignKey``
    to ``parent_model``.
    r	  rÞ   )r6   r\   r÷   rñ   rç   rô   r'   r8   rò   ró   r[   rõ   rö   r]   r^   r_   rP   r`   )r  Úuniquer   rú   )r  rZ   r6   r÷   r
  r'   r8   rñ   rô   rç   ró   r\   r[   rö   r]   r^   r_   rP   rò   rõ   r`   rú   ra   rù   r;   r;   r<   r!     s2    îr!   c                       sH   e Zd ZdZeZdedƒiZdddœ‡ fdd„
Zd	d
„ Z	dd„ Z
‡  ZS )r–   zw
    A basic integer field that deals with validating the given value to a
    given parent instance in an inline.
    Úinvalid_choicez3The inline value did not match the parent instance.FN)r  r  c                   s\   || _ || _|| _| j d k	rB| jr6t| j | jƒ|d< n| j j|d< d|d< tƒ j||Ž d S )Nrˆ   FrŒ   )Úparent_instancer  r  rA   r®   rp   re   )rc   r  r  r  Úargsra   r}   r;   r<   re   K  s    
zInlineForeignKeyField.__init__c                 C   s`   || j kr| jrd S | jS | jr0t| j| jƒ}n| jj}t|ƒt|ƒkrZt| jd dd�‚| jS )Nr  ©r‘   )	r3   r  r  r  rA   r®   rr   r   rP   )rc   rk   Úorigr;   r;   r<   r�   W  s    
zInlineForeignKeyField.cleanc                 C   s   dS )NFr;   )rc   rˆ   r/   r;   r;   r<   ræ   f  s    z!InlineForeignKeyField.has_changed)rW   rf   rg   rð   r   r.   rà   Údefault_error_messagesre   r�   ræ   r   r;   r;   r}   r<   r–   A  s    ÿr–   c                   @   s$   e Zd Zdd„ Zdd„ Zdd„ ZdS )ÚModelChoiceIteratorValuec                 C   s   || _ || _d S rÅ   )rk   r7   )rc   rk   r7   r;   r;   r<   re   k  s    z!ModelChoiceIteratorValue.__init__c                 C   s
   t | jƒS rÅ   )rr   rk   rŽ   r;   r;   r<   Ú__str__o  s    z ModelChoiceIteratorValue.__str__c                 C   s   t |tƒr|j}| j|kS rÅ   )r)   r  rk   )rc   Úotherr;   r;   r<   Ú__eq__r  s    
zModelChoiceIteratorValue.__eq__N)rW   rf   rg   re   r  r  r;   r;   r;   r<   r  j  s   r  c                   @   s4   e Zd Zdd„ Zdd„ Zdd„ Zdd„ Zd	d
„ ZdS )ÚModelChoiceIteratorc                 C   s   || _ |j| _d S rÅ   )r-   rB   r´   r;   r;   r<   re   y  s    zModelChoiceIterator.__init__c                 c   sH   | j jd k	rd| j jfV  | j}|js.| ¡ }|D ]}|  |¡V  q2d S )Nr  )r-   Úempty_labelrB   Z_prefetch_related_lookupsÚiteratorÚchoice)rc   rB   rÃ   r;   r;   r<   Ú__iter__}  s    zModelChoiceIterator.__iter__c                 C   s   | j  ¡ | jjd k	rdnd S )NrÞ   r   )rB   Úcountr-   r  rŽ   r;   r;   r<   Ú__len__‡  s    zModelChoiceIterator.__len__c                 C   s   | j jd k	p| j ¡ S rÅ   )r-   r  rB   ÚexistsrŽ   r;   r;   r<   Ú__bool__�  s    zModelChoiceIterator.__bool__c                 C   s   t | j |¡|ƒ| j |¡fS rÅ   )r  r-   Úprepare_valueÚlabel_from_instance©rc   rÃ   r;   r;   r<   r  �  s    
þzModelChoiceIterator.choiceN)rW   rf   rg   re   r  r  r!  r  r;   r;   r;   r<   r  x  s
   
r  c                
       s°   e Zd ZdZdedƒiZeZdddddddddd	œ	d
d„Zdd„ Z	‡ fdd„Z
dd„ Zdd„ ZeeeƒZdd„ Zdd„ ZeeejƒZ‡ fdd„Zdd„ Zdd„ Zdd„ Z‡  ZS ) r   z1A ChoiceField whose choices are a model QuerySet.r  zGSelect a valid choice. That choice is not one of the available choices.z	---------TNr  F)	r  rŒ   r.   rN   rˆ   rO   Úto_field_namerF   r‹   c       	         K   s^   t j| f|||||dœ|—Ž |r*|d k	s:t| jtƒrB|
sBd | _n|| _|| _|	| _|| _d S )N)rŒ   r.   rN   rˆ   rO   )	r   re   r)   r.   r   r  rB   rF   r%  )rc   rB   r  rŒ   r.   rN   rˆ   rO   r%  rF   r‹   ra   r;   r;   r<   re   ¡  s0    ÿ   þþÿÿ
þþ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.
        )rX   rF   rŽ   r;   r;   r<   rC   ¶  s    
z%ModelChoiceField.get_limit_choices_toc                    s*   t t| ƒ |¡}| jd k	r&| j ¡ |_|S rÅ   )rp   r   Ú__deepcopy__rB   Úall)rc   ÚmemoÚresultr}   r;   r<   r&  À  s    
zModelChoiceField.__deepcopy__c                 C   s   | j S rÅ   )rº   rŽ   r;   r;   r<   Ú_get_querysetÇ  s    zModelChoiceField._get_querysetc                 C   s$   |d krd n|  ¡ | _| j| j_d S rÅ   )r'  rº   Úchoicesr.   )rc   rB   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.
        ©rr   r$  r;   r;   r<   r#  Ò  s    z$ModelChoiceField.label_from_instancec                 C   s   t | dƒr| jS |  | ¡S )NÚ_choices)rD   r.  r  rŽ   r;   r;   r<   Ú_get_choicesÚ  s    
	zModelChoiceField._get_choicesc                    s.   t |dƒr"| jr| | j¡S |jS tƒ  |¡S ©Nr%   )rD   r%  Zserializable_valuer®   rp   r"  ©rc   rk   r}   r;   r<   r"  ë  s
    
zModelChoiceField.prepare_valuec              
   C   s~   || j krd S z8| jpd}t|| jjƒr2t||ƒ}| jjf ||iŽ}W n2 tt| jjj	fk
rx   t
| jd dd�‚Y nX |S )Nr®   r  r  )r3   r%  r)   rB   rZ   rA   r2   rƒ   rY   ZDoesNotExistr   rP   )rc   rk   Úkeyr;   r;   r<   r³   ó  s    


zModelChoiceField.to_pythonc                 C   s   t  | |¡S rÅ   )r   Úvalidater1  r;   r;   r<   r3  ÿ  s    zModelChoiceField.validatec                 C   s@   | j r
dS |d k	r|nd}|d k	r&|nd}t|  |¡ƒt|ƒkS )NFr  )Údisabledrr   r"  )rc   rˆ   r/   Úinitial_valueZ
data_valuer;   r;   r<   ræ     s
    zModelChoiceField.has_changed)rW   rf   rg   rð   rà   r  r  r  re   rC   r&  r*  r,  ÚpropertyrB   r#  r/  r   Z_set_choicesr+  r"  r³   r3  ræ   r   r;   r;   r}   r<   r   —  s6    ÿ     ý

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                    sL   t ƒ j|fdd i|—Ž | j d¡d k	rHtjdtdd� | jd | jd< d S )Nr  rÎ   zFThe 'list' error message key is deprecated in favor of 'invalid_list'.râ   )Ú
stacklevelr7  )rp   re   rP   r2   ÚwarningsÚwarnr   )rc   rB   ra   r}   r;   r<   re     s     ýz!ModelMultipleChoiceField.__init__c                 C   s   |sg S t |  |¡ƒS rÅ   )rÎ   Ú_check_valuesr1  r;   r;   r<   r³     s    z"ModelMultipleChoiceField.to_pythonc                 C   st   |   |¡}| jr(|s(t| jd dd�‚n| js<|s<| j ¡ S t|ttfƒs\t| jd dd�‚|  	|¡}|  
|¡ |S )NrŒ   r  r7  )r"  rŒ   r   rP   rB   rý   r)   rÎ   rÏ   r<  Zrun_validators)rc   rk   r¼   r;   r;   r<   r�   $  s    



þ

zModelMultipleChoiceField.cleanc              
      sê   | j pd‰ zt|ƒ}W n& tk
r<   t| jd dd�‚Y nX |D ]L}z| jjf ˆ |iŽ W qB ttfk
rŒ   t| jd dd|id�‚Y qBX qB| jjf dˆ  |iŽ}‡ fdd„|D ƒ}|D ](}t|ƒ|kr¼t| jd	 d	d
|id�‚q¼|S )zÕ
        Given a list of possible PK values, return a QuerySet of the
        corresponding objects. Raise a ValidationError if a given value is
        invalid (not a valid PK, not in the queryset, etc.)
        r®   r7  r  r8  )r‘   Úparamsz%s__inc                    s   h | ]}t t|ˆ ƒƒ’qS r;   )rr   rA   r¯   ©r2  r;   r<   rn   P  s     z9ModelMultipleChoiceField._check_values.<locals>.<setcomp>r  rk   )	r%  Ú	frozensetrY   r   rP   rB   rü   rƒ   rr   )rc   rk   r®   r¼   ZpksÚvalr;   r>  r<   r<  5  s6    
þýýz&ModelMultipleChoiceField._check_valuesc                    sD   t |dƒr8t|tƒs8t |dƒs8tƒ j‰ ‡ fdd„|D ƒS tƒ  |¡S )Nr  r%   c                    s   g | ]}ˆ |ƒ‘qS r;   r;   )rK   rm   ©r"  r;   r<   rM   _  s     z:ModelMultipleChoiceField.prepare_value.<locals>.<listcomp>)rD   r)   rr   rp   r"  r1  r}   rA  r<   r"  Z  s    
ÿþz&ModelMultipleChoiceField.prepare_valuec                 C   s`   | j r
dS |d krg }|d kr"g }t|ƒt|ƒkr6dS dd„ |  |¡D ƒ}dd„ |D ƒ}||kS )NFTc                 S   s   h | ]}t |ƒ’qS r;   r-  ©rK   rk   r;   r;   r<   rn   k  s     z7ModelMultipleChoiceField.has_changed.<locals>.<setcomp>c                 S   s   h | ]}t |ƒ’qS r;   r-  rB  r;   r;   r<   rn   l  s     )r4  r©   r"  )rc   rˆ   r/   Zinitial_setZdata_setr;   r;   r<   ræ   b  s    z$ModelMultipleChoiceField.has_changed)rW   rf   rg   rð   r   r.   r   Zhidden_widgetrà   r  re   r³   r�   r<  r"  ræ   r   r;   r;   r}   r<   r   
  s   ü
%r   c                 C   s"   t | dƒo | jjd k	p | jjd k	S r0  )rD   r%   r'   r8   )rQ   r;   r;   r<   Úmodelform_defines_fieldsp  s    

þrC  )NN)NN)	NNNNNNNNN)NF)8rð   r:  Ú	itertoolsr   Zdjango.core.exceptionsr   r   r   r   Zdjango.forms.fieldsr   r   Zdjango.forms.formsr	   r
   Zdjango.forms.formsetsr   r   Zdjango.forms.utilsr   Zdjango.forms.widgetsr   r   r   r   Zdjango.utils.deprecationr   Zdjango.utils.textr   r   Zdjango.utils.translationr   r   rà   r#   r   r=   r   rG   r   rb   rh   r   r   r"   r   r   r    r  r!   r–   r  r  r   r   rC  r;   r;   r;   r<   Ú<module>   s²   
)
          ýýYH @    ý
U  .           û
d
8            ú
,)sf