B
    ©0H[ÌÚ  ã               @   s
  d Z ddlmZ ddlmZ ddlmZmZmZm	Z	 ddl
mZmZ ddlmZmZ ddlmZmZ ddlmZ dd	lmZmZmZ dd
lmZmZ ddlmZmZ dZ dZ!d;dd„Z"d<dd„Z#dd„ Z$d=ddœdd„Z%G dd„ dƒZ&G dd„ deƒZ'G dd„ deƒZ(G dd „ d e(e'd!�Z)e)dddddddddf
d"d#„Z*G d$d%„ d%eƒZ+e)de+d&d'd'ddddd'dddddd'dfd(d)„Z,G d*d+„ d+e+ƒZ-d>d,d-„Z.e)e-dddd.d'ddddd'dddddd'dfd/d0„Z/G d1d2„ d2eƒZ0G d3d4„ d4ƒZ1G d5d6„ d6eƒZ2G d7d8„ d8e2ƒZ3d9d:„ Z4dS )?z[
Helper functions for creating Form classes from Django models
and database field objects.
é    )ÚOrderedDict)Úchain)ÚNON_FIELD_ERRORSÚ
FieldErrorÚImproperlyConfiguredÚValidationError)ÚChoiceFieldÚField)ÚBaseFormÚDeclarativeFieldsMetaclass)ÚBaseFormSetÚformset_factory)Ú	ErrorList)ÚHiddenInputÚMultipleHiddenInputÚSelectMultiple)ÚcapfirstÚget_text_list)ÚgettextÚgettext_lazy)Ú	ModelFormÚBaseModelFormÚmodel_to_dictÚfields_for_modelÚModelChoiceFieldÚModelMultipleChoiceFieldÚ
ALL_FIELDSÚBaseModelFormSetÚmodelformset_factoryÚBaseInlineFormSetÚinlineformset_factoryÚmodelform_factoryÚ__all__Nc       	      C   sê   ddl m} |j}| j}g }x¨|jD ]ž}|jr$t||jƒs$|j|krFq$|dk	rZ|j|krZq$|rj|j|krjq$| 	¡ r˜| |j j
j | j| j|  |j¡¡r˜q$t||jƒr°| |¡ q$| |||j ¡ q$W x|D ]}| |||j ¡ qÌW |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_prefixZ	FileFieldÚappendÚsave_form_data)	ÚformÚinstancer&   Úexcluder#   Úoptsr%   Zfile_field_listÚf© r8   úWC:\Users\HIRONO~1\AppData\Local\Temp\pip-install-6bm3nxem\django\django\forms\models.pyÚconstruct_instance    s*    
$
r:   c             C   sh   | j }i }xXt|j|j|jƒD ]B}t|ddƒs0q|r@|j|kr@q|rP|j|krPq| | ¡||j< qW |S )a‚  
    Return a dict containing the data in ``instance`` suitable for passing as
    a Form's ``initial`` keyword argument.

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

    ``exclude`` is an optional list of field names. If provided, exclude the
    named from the returned dict, even if they are listed in the ``fields``
    argument.
    r'   F)r$   r   Úconcrete_fieldsÚprivate_fieldsÚmany_to_manyÚgetattrr*   Zvalue_from_object)r4   r&   r5   r6   r.   r7   r8   r8   r9   r   F   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)Úhasattrr@   r?   Zcomplex_filter)Ú	formfieldÚlimit_choices_tor8   r8   r9   Ú#apply_limit_choices_to_to_formfield_   s    rD   T)Úapply_limit_choices_toc
               s  g }g ‰| j }ddlm‰  ‡ fdd„|jD ƒ}�x°tt|j||jƒƒD �]–}t|ddƒs’|dk	rF|j	|krFˆdks||j	ˆkrFt
d|j	| jf ƒ‚qF|dk	r¦|j	|kr¦qFˆr¶|j	ˆkr¶qFi }|rÖ|j	|krÖ||j	 |d	< |tksì|rô|j	|krôd
|d< |�r|j	|k�r||j	 |d< |�r4|j	|k�r4||j	 |d< |�rT|j	|k�rT||j	 |d< |	�rt|j	|	k�rt|	|j	 |d< |dk�rŒ|jf |Ž}n t|ƒ�s tdƒ‚n||f|Ž}|�rÒ|
�rÀt|ƒ | |j	|f¡ qFˆ |j	¡ qFW t|ƒ‰|�r
t‡‡‡fdd„|D ƒƒ‰ˆS )a:  
    Return an ``OrderedDict`` containing form fields for the given model.

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

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

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

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

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

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

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

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

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

    ``apply_limit_choices_to`` is a boolean indicating if limit_choices_to
    should be applied to a field's queryset.
    r   )r	   c                s   g | ]}t |ˆ ƒr|‘qS r8   )r(   )Ú.0r7   )Ú
ModelFieldr8   r9   ú
<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                s2   g | ]*}ˆ rˆ r|ˆ kr|ˆkr|ˆ  |¡f‘qS r8   )Úget)rF   r7   )r5   Ú
field_dictÚignoredr8   r9   rH   »   s    )r$   Zdjango.db.models.fieldsr	   r<   Úsortedr   r;   r=   r>   r*   r   Ú__name__r   rB   ÚcallableÚ	TypeErrorrD   r1   r   )Úmodelr&   r5   ÚwidgetsÚformfield_callbackÚlocalized_fieldsÚlabelsÚ
help_textsrK   Úfield_classesrE   Z
field_listr6   Zsortable_private_fieldsr7   ÚkwargsrB   r8   )rG   r5   rN   rO   r9   r   g   sZ    "


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 )
NrT   r&   r5   rU   rW   rX   rY   rK   rZ   )
r>   rT   r&   r5   rU   rW   rX   rY   rK   rZ   )ÚselfÚoptionsr8   r8   r9   Ú__init__Â   s    zModelFormOptions.__init__)N)rQ   Ú
__module__Ú__qualname__r_   r8   r8   r8   r9   r\   Á   s   r\   c                   s   e Zd Z‡ fdd„Z‡  ZS )ÚModelFormMetaclassc                sˆ  d }x,|D ]$}t |dƒr
t |jdƒr
|jj}P q
W | d|¡}tt| ƒ | |||¡}|tfkrb|S tt	|dd ƒƒ }|_
xBdD ]:}	t	||	ƒ}
t|
tƒr~|
tkr~d|j|	|
dœ }t|ƒ‚q~W |j�rx|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¡}|�rjd}|d |¡|jjf }t|ƒ‚|  |j¡ n|j}||_!|S )NÚMetarV   )r&   r5   rW   zP%(model)s.Meta.%(opt)s cannot be a string. Did you mean to type: ('%(value)s',)?)rT   ÚoptÚvaluez|Creating a ModelForm without either the 'fields' attribute or the 'exclude' attribute is prohibited; form %s needs updating.F)rE   c             S   s   h | ]\}}|s|’qS r8   r8   )rF   ÚkÚvr8   r8   r9   ú	<setcomp>  s    z-ModelFormMetaclass.__new__.<locals>.<setcomp>z&Unknown field(s) (%s) specified for %sz, )"rA   rc   rV   ÚpopÚsuperrb   Ú__new__r   r\   r>   r$   r(   Ústrr   rQ   rS   rT   r&   r5   r   r   rU   rW   rX   rY   rK   rZ   ÚitemsÚ
differenceZdeclared_fieldsÚjoinr   ÚupdateZbase_fields)Zmcsr*   ÚbasesÚattrsZbase_formfield_callbackÚbrV   Ú	new_classr6   rd   re   Úmsgr&   Znone_model_fieldsZmissing_fieldsÚmessage)Ú	__class__r8   r9   rk   Ï   sR    







zModelFormMetaclass.__new__)rQ   r`   ra   rk   Ú__classcell__r8   r8   )rw   r9   rb   Î   s   rb   c            
       sr   e Zd Zddddde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   Nz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�	 x| j ¡ D ]}t|ƒ qˆW d S )Nz'ModelForm has no model class specified.F)Úuse_required_attribute)r$   rT   Ú
ValueErrorr4   r   r&   r5   rp   Ú_validate_uniquerj   r_   ÚvaluesrD   )r]   r.   r/   Úauto_idÚprefixÚinitialÚerror_classZlabel_suffixZempty_permittedr4   ry   r6   Zobject_datarB   )rw   r8   r9   r_     s     


zBaseModelForm.__init__c             C   sÎ   g }xÄ| j jjD ]¶}|j}|| jkr2| |j¡ q| jjrT|| jjkrT| |j¡ q| jjrv|| jjkrv| |j¡ q|| jkrŽ| |j¡ q| j| }| j |¡}|j	s|j
s||jkr| |j¡ qW |S )z‘
        For backwards-compatibility, exclude several types of fields from model
        validation. See tickets #12507, #12521, #12553.
        )r4   r$   r&   r*   r1   r5   Ú_errorsr%   rM   ZblankÚrequiredÚempty_values)r]   r5   r7   r,   Z
form_fieldZfield_valuer8   r8   r9   Ú_get_validation_exclusions2  s     

	
z(BaseModelForm._get_validation_exclusionsc             C   s   d| _ | jS )NT)r{   r%   )r]   r8   r8   r9   Úclean[  s    zBaseModelForm.cleanc             C   s²   | j }t|dƒr|j}nt|i}x€| ¡ D ]t\}}|tkrV|jrVt|jkrV|jt }n|| jkr*| j| j}nq*x,|D ]$}t|tƒrv|j	|krv||j	 |_
qvW q*W |  d |¡ d S )NÚ
error_dict)r$   rA   r†   r   rm   rK   r&   r(   r   Úcoderv   Z	add_error)r]   Úerrorsr6   r†   r,   ÚmessagesrK   rv   r8   r8   r9   Ú_update_errors_  s     





zBaseModelForm._update_errorsc          
   C   sÖ   | j }|  ¡ }x*| j ¡ D ]\}}t|tƒr| |¡ qW yt| | j|j|j	ƒ| _W n, t
k
r€ } z|  |¡ W d d }~X Y nX y| jj|dd� W n, t
k
rÂ } z|  |¡ W d d }~X Y nX | jrÒ|  ¡  d S )NF)r5   Úvalidate_unique)r$   r„   r&   rm   r(   ÚInlineForeignKeyFieldr1   r:   r4   r5   r   rŠ   Z
full_cleanr{   r‹   )r]   r6   r5   r*   r,   Úer8   r8   r9   Ú_post_clean{  s    	
zBaseModelForm._post_cleanc          
   C   sL   |   ¡ }y| jj|d� W n, tk
rF } z|  |¡ W d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.
        )r5   N)r„   r4   r‹   r   rŠ   )r]   r5   r�   r8   r8   r9   r‹   ™  s
    zBaseModelForm.validate_uniquec             C   s„   | j }| jj}| jj}| jj}x`t|j|jƒD ]N}t|dƒs>q.|rN|j	|krNq.|r^|j	|kr^q.|j	|kr.| 
| j||j	 ¡ q.W dS )zS
        Save the many-to-many fields and generic relations for this form.
        r2   N)r%   r$   r5   r&   r4   r   r=   r<   rA   r*   r2   )r]   r%   r5   r&   r6   r7   r8   r8   r9   Ú	_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ˆ   rz   r4   r$   Zobject_nameÚ_stateÚaddingÚsaver�   Úsave_m2m)r]   Úcommitr8   r8   r9   r”   ¹  s    

zBaseModelForm.save)T)rQ   r`   ra   r   r_   r„   r…   rŠ   rŽ   r‹   r�   r”   Úalters_datarx   r8   r8   )rw   r9   r     s   )
r   c               @   s   e Zd ZdS )r   N)rQ   r`   ra   r8   r8   r8   r9   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 )aÿ  
    Return a ModelForm containing form fields for the given model.

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

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

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

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

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

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

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

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

    ``field_classes`` is a dictionary of model field names mapped to a form
    field class.
    rT   Nr&   r5   rU   rW   rX   rY   rK   rZ   rc   r8   ZForm)rc   rV   zZCalling modelform_factory without defining 'fields' or 'exclude' explicitly is prohibited.)rA   rc   ÚtypeÚstaticmethodrV   rQ   r>   r   )rT   r3   r&   r5   rV   rU   rW   rX   rY   rK   rZ   rr   rq   rc   Ú
class_nameZform_class_attrsr8   r8   r9   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.
    Núid_%s)r   c      	         s6   || _ || _||||dœ}| |¡ tƒ jf |Ž d S )N)r.   r/   r}   r~   )r?   Úinitial_extrarp   rj   r_   )	r]   r.   r/   r}   r~   r?   r   r[   Údefaults)rw   r8   r9   r_   3  s
    
zBaseModelFormSet.__init__c                s"   | j s| jst|  ¡ ƒS tƒ  ¡ S )z=Return the number of forms that are required in this FormSet.)r.   r/   ÚlenÚget_querysetrj   Úinitial_form_count)r]   )rw   r8   r9   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 r8   )Úpk)rF   Úor8   r8   r9   ú
<dictcomp>C  s    z5BaseModelFormSet._existing_object.<locals>.<dictcomp>)rA   r    r¢   rM   )r]   r£   r8   r8   r9   Ú_existing_objectA  s    
z!BaseModelFormSet._existing_objectc             C   s    x|j dk	r|j  ¡ }qW |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)r]   r,   r8   r8   r9   Ú_get_to_pythonF  s    zBaseModelFormSet._get_to_pythonc                s  d}||   ¡ k r¬d}| jršd|  |¡| jjjjf }y| j| }W n tk
rV   Y qªX |  	| jjj¡}y||ƒ}W n t
k
rˆ   Y qªX |  |¡|d< qâ|  ¡ | |d< n6| jrây| j||   ¡   |d< W n tk
rà   Y nX tƒ j|f|Ž}|�rd|j| jjjj _|S )NFTz%s-%sr4   r   )r¡   Úis_boundr0   rT   r$   r£   r*   r.   ÚKeyErrorr©   r   r¦   r    r�   Ú
IndexErrorrj   Ú_construct_formr&   r‚   )r]   Úir[   Zpk_requiredZpk_keyr£   r¨   r3   )rw   r8   r9   r­   O  s2    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)rA   r?   rT   Ú_default_managerr    ÚorderedZorder_byr$   r£   r*   r¯   )r]   Úqsr8   r8   r9   r    r  s    

zBaseModelFormSet.get_querysetTc             C   s   |j |d�S )z8Save and return a new model instance for the given form.)r–   )r”   )r]   r3   r–   r8   r8   r9   Ú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”   )r]   r3   r4   r–   r8   r8   r9   Úsave_existing‰  s    zBaseModelFormSet.save_existingc             C   s   |r|  ¡  dS )z#Deletes an existing model instance.N)Údelete)r]   Úobjr–   r8   r8   r9   Ú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   xˆ j D ]} |  ¡  qW d S )N)Úsaved_formsr•   )r3   )r]   r8   r9   r•   š  s    z'BaseModelFormSet.save.<locals>.save_m2m)r¸   r•   Úsave_existing_objectsÚsave_new_objects)r]   r–   r•   r8   )r]   r9   r”   ’  s
    zBaseModelFormSet.savec             C   s   |   ¡  d S )N)r‹   )r]   r8   r8   r9   r…   ¢  s    zBaseModelFormSet.cleanc                s  t ƒ }t ƒ }ˆj‰‡fdd„ˆjD ƒ}x:|D ]2‰ ˆ  ¡ }ˆ jj|d�\}}| |¡ | |¡ q,W g }x°|D ]¨\}}	t ƒ }
x˜|D ]�‰ ‡ ‡fdd„|	D ƒ}tdd„ |D ƒƒ}|r€d |kr€||
k�r| ˆ 	|	¡¡ ˆ 
ˆ ¡ g¡ˆ jt< x|	D ]}|ˆ jkrìˆ j|= qìW |
 |¡ q€W qlW xâ|D ]Ú}t ƒ }
|\}}}}xÀ|D ]¸‰ ˆ j�r:ˆ j| d k	�r:ˆ j| d k	�r:|dk�rŒˆ j| }|j|j|jf}ntˆ j| |ƒf}ˆ j| f| }||
k�ræ| ˆ |¡¡ ˆ 
ˆ ¡ g¡ˆ jt< ˆ j|= |
 |¡ �q:W �qW |�r
t|ƒ‚d S )Nc                s    g | ]}|  ¡ r|ˆ kr|‘qS r8   )Zis_valid)rF   r3   )Úforms_to_deleter8   r9   rH   ª  s    z4BaseModelFormSet.validate_unique.<locals>.<listcomp>)r5   c             3   s0   | ](}|ˆ j kr|ˆjkr|nˆ j | V  qd S )N)r%   Úunique_fields)rF   r,   )r3   r]   r8   r9   ú	<genexpr>¸  s   z3BaseModelFormSet.validate_unique.<locals>.<genexpr>c             s   s$   | ]}t |d ƒr| ¡ n|V  qdS )Ú_get_pk_valN)rA   r¾   )rF   Údr8   r8   r9   r½   ¼  s   Údate)ÚsetÚdeleted_formsZformsr„   r4   Z_get_unique_checksrp   Útupler1   Úget_unique_error_messager€   Úget_form_errorr�   r   r%   ÚaddÚyearÚmonthÚdayr>   Úget_date_error_messager   )r]   Zall_unique_checksZall_date_checksZvalid_formsr5   Zunique_checksZdate_checksrˆ   ZuclassÚunique_checkZ	seen_dataZrow_datar,   Ú
date_checkÚlookupZ
unique_forrÀ   Z	date_datar.   r8   )r3   r»   r]   r9   r‹   ¥  sV    











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   Ú_)r]   rË   r8   r8   r9   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   rl   )r]   rÌ   r8   r8   r9   rÊ   ô  s
    z'BaseModelFormSet.get_date_error_messagec             C   s   t dƒS )Nz*Please correct the duplicate values below.)r   )r]   r8   r8   r9   rÅ   þ  s    zBaseModelFormSet.get_form_errorc             C   sª   g | _ g | _| jsg S g }| j}x„| jD ]z}|j}|jd kr>q(||krb| j |¡ | j||d� q(| ¡ r(| j  ||j	f¡ | | j
|||d�¡ |s(| j |¡ q(W |S )N)r–   )Zchanged_objectsZdeleted_objectsZinitial_formsrÂ   r4   r£   r1   r·   Úhas_changedZchanged_datar´   r¸   )r]   r–   Zsaved_instancesr»   r3   r¶   r8   r8   r9   r¹     s&    
z&BaseModelFormSet.save_existing_objectsc             C   s^   g | _ xP| jD ]F}| ¡ sq| jr.|  |¡r.q| j  | j||d�¡ |s| j |¡ qW | j S )N)r–   )Znew_objectsZextra_formsrÔ   Ú
can_deleteZ_should_delete_formr1   r³   r¸   )r]   r–   r3   r8   r8   r9   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:y |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 )N)r'   Zauto_createdr(   r§   Zparent_linkrT   r$   r£   )r£   )r)   Úpk_is_not_editabler8   r9   rØ   3  s    z7BaseModelFormSet.add_fields.<locals>.pk_is_not_editableNF)r   r‚   r-   )Údjango.db.modelsr)   rÖ   r×   rT   r$   r£   Ú	_pk_fieldr*   r&   rª   r4   r’   r“   r    r¬   r(   r§   r°   ZusingÚdbrU   rM   r   r   rj   Ú
add_fields)	r]   r3   ÚindexrÖ   r×   r£   Úpk_valuer²   r-   )rw   )r)   rØ   r9   rÜ   )  s*    
zBaseModelFormSet.add_fields)NNrœ   NN)T)T)T)T)T)T)rQ   r`   ra   Ú__doc__rT   rÁ   r¼   r_   r¡   r¦   r©   r­   r    r³   r´   r·   r”   r—   r…   r‹   rÄ   rÊ   rÅ   r¹   rº   rÜ   rx   r8   r8   )rw   r9   r   *  s.    	#



E



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.rc   Nr&   r5   z]Calling modelformset_factory without defining 'fields' or 'exclude' explicitly is prohibited.)
r3   r&   r5   rV   rU   rW   rX   rY   rK   rZ   )ÚextraÚmin_numÚmax_numÚ	can_orderrÕ   Úvalidate_minÚvalidate_max)r>   r   r!   r   rT   )rT   r3   rV   Úformsetrà   rÕ   rã   râ   r&   r5   rU   rå   rW   rX   rY   rK   rá   rä   rZ   ÚmetaÚFormSetr8   r8   r9   r   U  s    


r   c                   sh   e Zd ZdZd‡ fdd„	Z‡ fdd„Z‡ fdd	„Zed
d„ ƒZd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~   r?   )Úfkr§   rT   r4   Úsave_as_newr°   r£   Úfilterr*   Únoner¼   rj   r_   r3   r$   r&   r(   rÃ   Úlistr1   )	r]   r.   r/   r4   rê   r~   r?   r[   r²   )rw   r8   r9   r_   t  s    zBaseInlineFormSet.__init__c                s   | j r
dS tƒ  ¡ S )Nr   )rê   rj   r¡   )r]   )rw   r8   r9   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£   )rj   r­   rê   r>   r.   rî   r0   rÚ   r*   ré   r4   r£   r§   rÓ   rT   r$   ÚsetattrÚget_attname)r]   r®   r[   r3   ZmutableZfk_value)rw   r8   r9   r­   �  s    z!BaseInlineFormSet._construct_formc             C   s   | j jj| jd� dd¡S )N)rT   ú+Ú )ré   r§   Zget_accessor_namerT   Úreplace)Úclsr8   r8   r9   Úget_default_prefix§  s    z$BaseInlineFormSet.get_default_prefixTc             C   sr   t |j| jj| jƒ |jdd�}t| j| jjjƒ}t || j ¡ t|d|ƒƒ |rX| ¡  |rnt	|dƒrn| 
¡  |S )NF)r–   r£   r•   )rï   r4   ré   r*   r”   r>   r§   rÓ   rð   rA   r•   )r]   r3   r–   r¶   rÞ   r8   r8   r9   r³   «  s    zBaseInlineFormSet.save_newc                sâ   t ƒ  ||¡ | j| jkr,| jj}ddi}nP| 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_fieldTrI   Úto_field)rj   rÜ   rÚ   ré   r*   r>   r&   rM   r   Zverbose_namer§   rÓ   rT   r$   r£   r4   r’   r“   Ú	get_fieldr+   rï   ZattnamerŒ   )r]   r3   rÝ   r*   r[   r÷   )rw   r8   r9   rÜ   ¼  s    
 

zBaseInlineFormSet.add_fieldsc                s   ‡ fdd„|D ƒ}t ƒ  |¡S )Nc                s   g | ]}|ˆ j jkr|‘qS r8   )ré   r*   )rF   r,   )r]   r8   r9   rH   Ø  s    z>BaseInlineFormSet.get_unique_error_message.<locals>.<listcomp>)rj   rÄ   )r]   rË   )rw   )r]   r9   rÄ   ×  s    z*BaseInlineFormSet.get_unique_error_message)NNNFNN)T)rQ   r`   ra   rß   r_   r¡   r­   Úclassmethodrõ   r³   rÜ   rÄ   rx   r8   r8   )rw   r9   r   r  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 t|ƒdkrœt	d|jj
ˆf ƒ‚nr‡ ‡fdd„|jD ƒ}t|ƒdkrÊ|d }nFt|ƒdkrø|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 r8   )r*   )rF   r7   )Úfk_namer8   r9   rH   è  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 r8   )r(   r§   rT   r$   Úget_parent_list)rF   r7   )r×   Úparent_modelr8   r9   rH   ø  s    
Nz'%s' has no ForeignKey to '%s'.z*'%s' has more than one ForeignKey to '%s'.)rÙ   r×   r$   r&   rŸ   r(   r§   rT   rû   rz   rI   )rü   rT   rú   Zcan_failr6   Zfks_to_parentré   r8   )r×   rú   rü   r9   Ú_get_foreign_keyÜ  s:    	

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Î   )r3   rV   ræ   rà   rÕ   rã   r&   r5   rá   râ   rU   rä   rå   rW   rX   rY   rK   rZ   )rý   Úuniquer   ré   )rü   rT   r3   ræ   rú   r&   r5   rà   rã   rÕ   râ   rV   rU   rå   rW   rX   rY   rK   rá   rä   rZ   ré   r[   rè   r8   r8   r9   r      s0    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÷   r>   r£   rj   r_   )r]   r   rö   r÷   Úargsr[   )rw   r8   r9   r_   I  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‡   )	rƒ   rö   r   r÷   r>   r£   rl   r   rK   )r]   re   Úorigr8   r8   r9   r…   U  s    
zInlineForeignKeyField.cleanc             C   s   dS )NFr8   )r]   r   r.   r8   r8   r9   rÔ   d  s    z!InlineForeignKeyField.has_changed)rQ   r`   ra   rß   r   r-   rÐ   Údefault_error_messagesr_   r…   rÔ   rx   r8   r8   )rw   r9   rŒ   ?  s   rŒ   c               @   s,   e Zd Zdd„ Zdd„ Zdd„ Zdd„ Zd	S )
ÚModelChoiceIteratorc             C   s   || _ |j| _d S )N)r,   r?   )r]   r,   r8   r8   r9   r_   i  s    zModelChoiceIterator.__init__c             c   sP   | j jd k	rd| j jfV  | j ¡ }|js2| ¡ }x|D ]}|  |¡V  q8W d S )Nrò   )r,   Úempty_labelr?   ÚallZ_prefetch_related_lookupsÚiteratorÚchoice)r]   r?   r¶   r8   r8   r9   Ú__iter__m  s    

zModelChoiceIterator.__iter__c             C   s   t | jƒ| jjd k	rdnd S )NrÎ   r   )rŸ   r?   r,   r  )r]   r8   r8   r9   Ú__len__w  s    zModelChoiceIterator.__len__c             C   s   | j  |¡| j  |¡fS )N)r,   Úprepare_valueÚlabel_from_instance)r]   r¶   r8   r8   r9   r  z  s    zModelChoiceIterator.choiceN)rQ   r`   ra   r_   r	  r
  r  r8   r8   r8   r9   r  h  s   
r  c            	       s®   e Zd ZdZdedƒiZeZdddddddddœd	d
„Zdd„ Z	‡ fdd„Z
dd„ Zdd„ ZeeeƒZdd„ Zdd„ ZeeejƒZ‡ fdd„Zdd„ Zdd„ Zdd„ Z‡  ZS )r   z1A ChoiceField whose choices are a model QuerySet.rÿ   zGSelect a valid choice. That choice is not one of the available choices.z	---------TNrò   )r  r‚   r-   rI   r   rJ   Úto_field_namerC   c            K   sN   |r|d k	rd | _ n|| _ tj| f|||||dœ|
—Ž || _|	| _|| _d S )N)r‚   r-   rI   r   rJ   )r  r	   r_   r?   rC   r  )r]   r?   r  r‚   r-   rI   r   rJ   r  rC   r[   r8   r8   r9   r_   ˆ  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.
        )rR   rC   )r]   r8   r8   r9   r@   ›  s    
z%ModelChoiceField.get_limit_choices_toc                s*   t t| ƒ |¡}| jd k	r&| j ¡ |_|S )N)rj   r   Ú__deepcopy__r?   r  )r]   ÚmemoÚresult)rw   r8   r9   r  ¥  s    
zModelChoiceField.__deepcopy__c             C   s   | j S )N)r¯   )r]   r8   r8   r9   Ú_get_queryset¬  s    zModelChoiceField._get_querysetc             C   s   || _ | j| j_d S )N)r¯   Úchoicesr-   )r]   r?   r8   r8   r9   Ú_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.
        )rl   )r]   r¶   r8   r8   r9   r  ·  s    z$ModelChoiceField.label_from_instancec             C   s   t | dƒr| jS |  | ¡S )NÚ_choices)rA   r  r  )r]   r8   r8   r9   Ú_get_choices¿  s    
	zModelChoiceField._get_choicesc                s.   t |dƒr"| jr| | j¡S |jS tƒ  |¡S )Nr$   )rA   r  Zserializable_valuer£   rj   r  )r]   re   )rw   r8   r9   r  Ð  s
    
zModelChoiceField.prepare_valuec          
   C   sf   || j krd S y | jpd}| jjf ||iŽ}W n2 tt| jjjfk
r`   t| j	d dd�‚Y nX |S )Nr£   rÿ   )r‡   )
rƒ   r  r?   rM   rz   rS   rT   ZDoesNotExistr   rK   )r]   re   Úkeyr8   r8   r9   r¨   Ø  s    

zModelChoiceField.to_pythonc             C   s   t  | |¡S )N)r	   Úvalidate)r]   re   r8   r8   r9   r  â  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ò   )Údisabledrl   r  )r]   r   r.   Úinitial_valueZ
data_valuer8   r8   r9   rÔ   å  s
    zModelChoiceField.has_changed)rQ   r`   ra   rß   rÐ   r  r  r  r_   r@   r  r  r  Úpropertyr?   r  r  r   Z_set_choicesr  r  r¨   r  rÔ   rx   r8   r8   )rw   r9   r   ~  s$   


r   c                   sl   e Zd ZdZeZeZedƒedƒedƒdœZ	‡ fdd„Z
dd	„ Zd
d„ Zdd„ Z‡ fdd„Zdd„ Z‡  ZS )r   z9A MultipleChoiceField whose choices are a model QuerySet.zEnter a list of values.zESelect a valid choice. %(value)s is not one of the available choices.z"%(pk)s" is not a valid value.)rí   rÿ   Úinvalid_pk_valuec                s   t ƒ j|fdd i|—Ž d S )Nr  )rj   r_   )r]   r?   r[   )rw   r8   r9   r_   ø  s    z!ModelMultipleChoiceField.__init__c             C   s   |sg S t |  |¡ƒS )N)rí   Ú_check_values)r]   re   r8   r8   r9   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‡   rí   )r  r‚   r   rK   r?   rì   r(   rí   rÃ   r  Zrun_validators)r]   re   r²   r8   r8   r9   r…      s    





zModelMultipleChoiceField.cleanc          
      sò   | j pd‰ yt|ƒ}W n& tk
r<   t| jd dd�‚Y nX xT|D ]L}y| jjf ˆ |iŽ W qD ttfk
rŽ   t| jd dd|id�‚Y qDX qDW | jjf dˆ  |iŽ}‡ fdd„|D ƒ}x0|D ](}t|ƒ|krÂt| jd	 d	d
|id�‚qÂW |S )zÕ
        Given a list of possible PK values, return a QuerySet of the
        corresponding objects. Raise a ValidationError if a given value is
        invalid (not a valid PK, not in the queryset, etc.)
        r£   rí   )r‡   r  )r‡   Úparamsz%s__inc                s   h | ]}t t|ˆ ƒƒ’qS r8   )rl   r>   )rF   r¤   )r  r8   r9   rh   )  s    z9ModelMultipleChoiceField._check_values.<locals>.<setcomp>rÿ   re   )	r  Ú	frozensetrS   r   rK   r?   rë   rz   rl   )r]   re   r£   r²   ZpksÚvalr8   )r  r9   r    s0    


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 r8   r8   )rF   rg   )r  r8   r9   rH   8  s    z:ModelMultipleChoiceField.prepare_value.<locals>.<listcomp>)rA   r(   rl   rj   r  )r]   re   )rw   )r  r9   r  3  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 r8   )rl   )rF   re   r8   r8   r9   rh   D  s    z7ModelMultipleChoiceField.has_changed.<locals>.<setcomp>c             S   s   h | ]}t |ƒ’qS r8   )rl   )rF   re   r8   r8   r9   rh   E  s    )r  rŸ   r  )r]   r   r.   Zinitial_setZdata_setr8   r8   r9   rÔ   ;  s    z$ModelMultipleChoiceField.has_changed)rQ   r`   ra   rß   r   r-   r   Zhidden_widgetrÐ   r  r_   r¨   r…   r  r  rÔ   rx   r8   r8   )rw   r9   r   í  s   %r   c             C   s*   | d k	o(t | dƒo(| jjd k	p(| jjd k	S )Nr$   )rA   r$   r&   r5   )rL   r8   r8   r9   Úmodelform_defines_fieldsI  s    
r   )NN)NN)	NNNNNNNNN)NF)5rß   Úcollectionsr   Ú	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   Zdjango.utils.textr   r   Zdjango.utils.translationr   r   rÐ   r"   r   r:   r   rD   r   r\   rb   r   r   r!   r   r   r   rý   r    rŒ   r  r   r   r   r8   r8   r8   r9   Ú<module>   sf   
&
   WH >P  -j
7&)o\