o
    h                     @   s4  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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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 )@z[
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|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)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;   [/var/www/html/magazine_api/magazine_env/lib/python3.10/site-packages/django/forms/models.pyconstruct_instance    s0   

"r=   c                 C   sh   | j }i }t|j|j|jD ]#}t|ddsq|dur!|j|vr!q|r)|j|v r)q|| ||j< q|S )a  
    Return a dict containing the data in ``instance`` suitable for passing as
    a Form's ``initial`` keyword argument.

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

    ``exclude`` is an optional list of field names. If provided, exclude the
    named from the returned dict, even if they are listed in the ``fields``
    argument.
    r(   FN)r%   r   concrete_fieldsprivate_fieldsmany_to_manygetattrr+   Zvalue_from_object)r7   r'   r8   r9   r/   r:   r;   r;   r<   r   I   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rG   Nr;   )django.db.modelsrB   rC   rD   hasattrrF   r)   rE   filtermodelZ_base_manager)	formfieldrB   rC   rD   limit_choices_toZcomplex_filterr;   r;   r<   #apply_limit_choices_to_to_formfieldb   s   

rO   Tapply_limit_choices_toc
                   s  i g | j }ddlm   fdd|jD }tt|j||jD ]}t|ddsF|durE|j	|v rEdu s;|j	vrEt
d|j	| jf q!|durP|j	|vrPq!rX|j	v rXq!i }|rh|j	|v rh||j	 |d	< |tkss|rw|j	|v rwd
|d< |r|j	|v r||j	 |d< |r|j	|v r||j	 |d< |r|j	|v r||j	 |d< |	r|j	|	v r|	|j	 |d< |du r|jdi |}nt|std||fi |}|r|
rt| ||j	< q!|j	 q!|rfdd|D S )a4  
    Return a dictionary containing form fields for the given model.

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

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

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

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

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

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

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

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

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

    ``apply_limit_choices_to`` is a boolean indicating if limit_choices_to
    should be applied to a field's queryset.
    r   )r   c                    s   g | ]	}t | r|qS r;   )r)   .0r:   )
ModelFieldr;   r<   
<listcomp>       z$fields_for_model.<locals>.<listcomp>r(   FNzH'%s' cannot be specified for %s model form as it is a non-editable fieldr.   Tlocalizelabel	help_texterror_messages
form_classz1formfield_callback must be a function or callablec                    s,   i | ]} r
| vr|vr| |qS r;   )r2   rR   )r8   
field_dictignoredr;   r<   
<dictcomp>   s    
z$fields_for_model.<locals>.<dictcomp>r;   )r%   rI   r   r?   sortedr   r>   r@   rA   r+   r   __name__r   rM   callable	TypeErrorrO   r4   )rL   r'   r8   widgetsformfield_callbacklocalized_fieldslabels
help_textsrZ   field_classesrQ   r9   Zsortable_private_fieldsr:   kwargsrM   r;   )rT   r8   r\   r]   r<   r   r   s^   "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 )
NrL   r'   r8   rc   re   rf   rg   rZ   rh   )
rA   rL   r'   r8   rc   re   rf   rg   rZ   rh   )selfoptionsr;   r;   r<   __init__   s   zModelFormOptions.__init__N)r`   
__module____qualname__rm   r;   r;   r;   r<   rj      s    rj   c                       s   e Zd Z fddZ  ZS )ModelFormMetaclassc                    sz  d }|D ]}t |drt |jdr|jj} nq|d|}t | |||}|tfkr.|S tt|dd  }|_	dD ]}	t||	}
t
|
trX|
tkrXd|j|	|
d }t|q;|jr|jd u rl|jd u rltd| |jtkrtd |_t|j|j|j|j||j|j|j|j|jdd}d	d
 | D }||j}|rd}|d||jjf }t|||j n|j}||_ |S )NMetard   )r'   r8   re   zP%(model)s.Meta.%(opt)s cannot be a string. Did you mean to type: ('%(value)s',)?)rL   optvaluez|Creating a ModelForm without either the 'fields' attribute or the 'exclude' attribute is prohibited; form %s needs updating.FrP   c                 S   s   h | ]\}}|s|qS r;   r;   )rS   kvr;   r;   r<   	<setcomp>  s    z-ModelFormMetaclass.__new__.<locals>.<setcomp>z&Unknown field(s) (%s) specified for %sz, )!rJ   rr   rd   popsuper__new__r   rj   rA   r%   r)   strr   r`   rb   rL   r'   r8   r   r   rc   re   rf   rg   rZ   rh   items
differenceZdeclared_fieldsjoinr   updatebase_fields)mcsr+   basesattrsZbase_formfield_callbackbrd   	new_classr9   rs   rt   msgr'   Znone_model_fieldsZmissing_fieldsmessage	__class__r;   r<   rz      sb   



	
zModelFormMetaclass.__new__)r`   ro   rp   rz   __classcell__r;   r;   r   r<   rq      s    rq   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%   rL   
ValueErrorr7   r   r'   r8   r   _validate_uniquery   rm   valuesrO   )rk   r/   r0   auto_idprefixinitialerror_classZlabel_suffixZempty_permittedr7   r   r   r9   Zobject_datarM   r   r;   r<   rm   !  s&   



zBaseModelForm.__init__c                 C   s   g }| 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 rF||j q| j| }| j|}|j	sb|j
sb||jv rb||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   )rk   r8   r:   r-   Z
form_fieldZfield_valuer;   r;   r<   _get_validation_exclusions>  s"   


	z(BaseModelForm._get_validation_exclusionsc                 C   s   d| _ | jS )NT)r   r&   rk   r;   r;   r<   cleang  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%   rJ   r   r   r|   rZ   r'   r)   r   coder   Z	add_error)rk   errorsr9   r   r-   messagesrZ   r   r;   r;   r<   _update_errorsk  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)r8   validate_unique)r%   r   r'   r|   r)   InlineForeignKeyFieldr4   r=   r7   r8   r   r   Z
full_cleanr   r   )rk   r9   r8   r+   r-   er;   r;   r<   _post_clean  s*   	

zBaseModelForm._post_cleanc              
   C   sN   |   }z
| jj|d W dS  ty& } z| | W Y d}~dS d}~ww )z
        Call the instance's validate_unique() method and update the form's
        validation errors if any were raised.
        r8   N)r   r7   r   r   r   )rk   r8   r   r;   r;   r<   r     s   zBaseModelForm.validate_uniquec                 C   s   | j }| jj}| jj}| jj}t|j|jD ]'}t|dsq|r&|j	|vr&q|r.|j	|v r.q|j	|v r=|
| j||j	  qdS )zS
        Save the many-to-many fields and generic relations for this form.
        r5   N)r&   r%   r8   r'   r7   r   r@   r?   rJ   r+   r5   )rk   r&   r8   r'   r9   r:   r;   r;   r<   	_save_m2m  s   

zBaseModelForm._save_m2mTc                 C   sZ   | j rtd| jjj| jjjrdf df |r&| j  |   | jS | j| _	| jS )z
        Save this form's self.instance object if commit=True. Otherwise, add
        a save_m2m() method to the form which can be called after the instance
        is saved manually at a later time. Return the model instance.
        z8The %s could not be %s because the data didn't validate.createdchanged)
r   r   r7   r%   Zobject_name_stateaddingsaver   save_m2m)rk   commitr;   r;   r<   r     s    
zBaseModelForm.saveT)r`   ro   rp   r   rm   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)r`   ro   rp   r;   r;   r;   r<   r     s    r   )	metaclassc                 C   s  d| i}|dur||d< |dur||d< |dur||d< |dur$||d< |dur,||d< |dur4||d< |	dur<|	|d	< |
durD|
|d
< t |drM|jfnd}td||}|r\t||_| jd }||d}t|dddu rzt|dddu rztdt|||f|S )al  
    Return a ModelForm containing form fields for the given model. You can
    optionally pass a `form` argument to use as a starting point for
    constructing the ModelForm.

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

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

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

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

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

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

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

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

    ``field_classes`` is a dictionary of model field names mapped to a form
    field class.
    rL   Nr'   r8   rc   re   rf   rg   rZ   rh   rr   r;   ZForm)rr   rd   zZCalling modelform_factory without defining 'fields' or 'exclude' explicitly is prohibited.)rJ   rr   typestaticmethodrd   r`   rA   r   )rL   r6   r'   r8   rd   rc   re   rf   rg   rZ   rh   r   r   rr   
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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.
    Nr   )r   c                   s0   || _ || _t jdi ||||d| d S )N)r/   r0   r   r   r;   )rE   initial_extrary   rm   )rk   r/   r0   r   r   rE   r   ri   r   r;   r<   rm   A  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_querysetry   initial_form_countr   r   r;   r<   r   G  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;   rH   rS   or;   r;   r<   r^   O      z5BaseModelFormSet._existing_object.<locals>.<dictcomp>)rJ   r   r   r2   )rk   rG   r;   r;   r<   _existing_objectM  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_fieldZget_related_field	to_pythonrk   r-   r;   r;   r<   _get_to_pythonR  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 |}|rd|j| jjjj _|S )Nz%s-%sr7   r   T)r   r   r1   rL   r%   rG   r+   r/   KeyErrorr   r   r   r   r   
IndexErrorry   _construct_formr'   r   )rk   iri   Zpk_requiredZpk_keyrG   r   r6   r   r;   r<   r   [  s6   z BaseModelFormSet._construct_formc                 C   sL   t | ds#| jd ur| j}n| jj }|js || jjjj	}|| _
| j
S )N	_queryset)rJ   rE   rL   _default_managerr   orderedZorder_byr%   rG   r+   r   )rk   qsr;   r;   r<   r   }  s   

zBaseModelFormSet.get_querysetTc                 C   s   |j |dS )z8Save and return a new model instance for the given form.r   r   rk   r6   r   r;   r;   r<   save_new     zBaseModelFormSet.save_newc                 C   s   |j |dS )z>Save and return an existing model instance for the given form.r   r   )rk   r6   r7   r   r;   r;   r<   save_existing  r   zBaseModelFormSet.save_existingc                 C   s   |r|   dS dS )z#Deletes an existing model instance.N)delete)rk   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 rn   )saved_formsr   )r6   r   r;   r<   r     s   

z'BaseModelFormSet.save.<locals>.save_m2m)r   r   save_existing_objectssave_new_objects)rk   r   r   r;   r   r<   r     s
   zBaseModelFormSet.savec                 C   s   |    d S rn   )r   r   r;   r;   r<   r        zBaseModelFormSet.cleanc                    s  t  }t  }jfddjD }|D ]   } jj|d\}}|| || qg }|D ]O\}}	t  }
|D ]E  fdd|	D }tdd |D }|rd |vr||
v r||	|	 
 g jt< |	D ]}| jv r{ j|= qp|
| q<q3|D ]d}t  }
|\}}}}|D ]V  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|| 
 g jt<  j|= |
| qq|rt|d S )Nc                    s    g | ]}|  r| vr|qS r;   )Zis_valid)rS   r6   )forms_to_deleter;   r<   rU     s     z4BaseModelFormSet.validate_unique.<locals>.<listcomp>r   c                 3   s2    | ]}| j v r|jv r|n j | V  qd S rn   )r&   unique_fieldsrS   r-   )r6   rk   r;   r<   	<genexpr>  s    z3BaseModelFormSet.validate_unique.<locals>.<genexpr>c                 s   s8    | ]}t |d r| n
t|trt|n|V  qdS )_get_pk_valN)rJ   r   r)   listtuple)rS   dr;   r;   r<   r     s    
date)setdeleted_formsZformsr   r7   Z_get_unique_checksr   r   r4   get_unique_error_messager   get_form_errorr   r   r&   addyearmonthdayrA   get_date_error_messager   )rk   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   rk   r<   r     sf   




z BaseModelFormSet.validate_uniquec                 C   s:   t |dkrtdd|d i S tddt|tdi S )N   z0Please correct the duplicate data for %(field)s.r-   r   zFPlease correct the duplicate data for %(field)s, which must be unique.and)r   r   r   _rk   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   r{   )rk   r   r;   r;   r<   r     s   
z'BaseModelFormSet.get_date_error_messagec                 C   s   t dS )Nz*Please correct the duplicate values below.)r   r   r;   r;   r<   r     s   zBaseModelFormSet.get_form_errorc                 C   s   g | _ g | _| jsg S g }| j}| jD ]=}|j}|jd u rq||v r0| j| | j||d q| rP| j ||j	f || j
|||d |sP| j| q|S Nr   )Zchanged_objectsZdeleted_objectsZinitial_formsr   r7   rG   r4   r   has_changedZchanged_datar   r   )rk   r   Zsaved_instancesr   r6   r   r;   r;   r<   r     s(   

z&BaseModelFormSet.save_existing_objectsc                 C   sZ   g | _ | jD ]#}| sq| jr| |rq| j | j||d |s)| j| q| j S r   )Znew_objectsZextra_formsr   
can_deleteZ_should_delete_formr4   r   r   )rk   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|s#|j|j	vr|j
r2|jjjr-dn|jj}nz|dur?|  | j}nd}W n tyM   d}Y nw t|||fr]|jjj }n| jj }||jjj}|jjrz|jj| jjt}nt}t||d|d|j	| jj< t || dS )z0Add a hidden field for the object's primary key.r   )r*   
ForeignKeyOneToOneFieldc                    s6   | j  p| jpt|  p| jo| jjo| jjjjS rn   )r(   Zauto_createdr)   r   Zparent_linkrL   r%   rG   rH   r*   pk_is_not_editabler;   r<   r   B  s   z7BaseModelFormSet.add_fields.<locals>.pk_is_not_editableNF)r   r   r.   )rI   r*   r   r   rL   r%   rG   	_pk_fieldr+   r'   r   r7   r   r   r   r   r)   r   r   usingdbrc   r2   r   r   ry   
add_fields)	rk   r6   indexr   r   rG   Zpk_valuer   r.   r   r   r<   r   8  s.   zBaseModelFormSet.add_fields)NNr   NNr   )r`   ro   rp   __doc__rL   r   r   rm   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r;   r;   r   r<   r   8  s6    	"



I



r   r   Fc                 C   sz   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.rr   Nr'   r8   z]Calling modelformset_factory without defining 'fields' or 'exclude' explicitly is prohibited.)
r6   r'   r8   rd   rc   re   rf   rg   rZ   rh   )	extramin_nummax_num	can_orderr   validate_minvalidate_maxabsolute_maxcan_delete_extra)rA   r   r"   r   rL   )rL   r6   rd   formsetr   r   r  r  r'   r8   rc   r  re   rf   rg   rZ   r   r  rh   r  r  metaFormSetr;   r;   r<   r   d  s&   
r   c                       sp   e Zd ZdZ		d fdd	Z fddZ fdd	Zed
d Zd fdd	Z	 fddZ
 fddZ  ZS )r    z0A formset for child objects related to a parent.NFc           	         s   |d u r| j j | _n|| _|| _|d u r| jj}| jjd ur.|jdi | j j| ji}n|	 }| j jh| _
t j||f||d| | jjjrr| j j| jjjvrtt| jjjtrft| jjj| jj_| jjj| j j d S d S d S )N)r   rE   r;   )fkr   rL   r7   save_as_newr   rG   rK   r+   noner   ry   rm   r6   r%   r'   r)   r   r   r4   )	rk   r/   r0   r7   r  r   rE   ri   r   r   r;   r<   rm     s    zBaseInlineFormSet.__init__c                    s   | j rdS t  S )Nr   )r  ry   r   r   r   r;   r<   r     s   
z$BaseInlineFormSet.initial_form_countc                    s   t  j|fi |}| jr8t|jdd }|d urd|j_d |j|| jj< d |j|| j	j< |d ur8||j_| j
j}| j	jj| j	jjjjjkrXt| j
| j	jj}t|d|}t|j
| j	 | |S )N_mutableTrG   )ry   r   r  rA   r/   r  r1   r   r+   r
  r7   rG   r   r   rL   r%   setattrZget_attname)rk   r   ri   r6   ZmutableZfk_valuer   r;   r<   r     s   z!BaseInlineFormSet._construct_formc                 C   s   | j jj| jdddS )N)rL   + )r
  r   Zget_accessor_namerL   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+   ry   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re|dd urT| jj|d }n| jjj}| ret| j|jd  t| jfi ||j|< d S )Npk_fieldTrX   to_field)ry   r   r   r
  r+   rA   r'   r2   r   Zverbose_namer   r   rL   r%   rG   r7   r   r   	get_fieldr,   r  Zattnamer   )rk   r6   r   r+   ri   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<   rU     s    z>BaseInlineFormSet.get_unique_error_message.<locals>.<listcomp>)ry   r   r   r   r   r<   r     s   z*BaseInlineFormSet.get_unique_error_message)NNNFNNr   )r`   ro   rp   r   rm   r   r   classmethodr  r   r   r   r   r;   r;   r   r<   r      s    
r    c                    s  ddl m  |j}rMfdd|jD }t|dkr?|d }t| r3|jjkr=|jjj vr=t	djj
f |S |sKt	d|jj
f |S  fdd|jD }t|dkrd|d }|S |sv|rjd	S t	d
|jj
jj
f t	d|jj
jj
f )aD  
    Find and return the ForeignKey from model to parent if there is one
    (return None if can_fail is True and no such field exists). If fk_name is
    provided, assume it is the name of the ForeignKey field. Unless can_fail is
    True, raise an exception if there isn't a ForeignKey from model to
    parent_model.
    r   )r   c                    s   g | ]	}|j  kr|qS r;   )r+   rR   fk_namer;   r<   rU     rV   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 | ]}t | r|jjks|jjj v r|qS r;   )r)   r   rL   r%   get_parent_listrR   )r   parent_modelr;   r<   rU     s    Nz'%s' has no ForeignKey to '%s'.zR'%s' has more than one ForeignKey to '%s'. You must specify a 'fk_name' attribute.)rI   r   r%   r'   r   r)   r   rL   r  r   rX   )r  rL   r  Zcan_failr9   Zfks_to_parentr
  r;   )r   r  r  r<   _get_foreign_key  sP   	
#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``.
    r  r   r6   rd   r  r   r   r  r'   r8   r   r  rc   r  r  re   rf   rg   rZ   )rh   r  r  )r  uniquer   r
  )r  rL   r6   r  r  r'   r8   r   r  r   r  rd   rc   r  re   rf   rg   rZ   r   r  rh   r  r  r
  ri   r	  r;   r;   r<   r!     sZ   	
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 ur!| jrt| j | j|d< n| j j|d< d|d< t j|i | d S )Nr   Fr   )parent_instancer  r  rA   rG   ry   rm   )rk   r  r  r  argsri   r   r;   r<   rm   X  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   )	r3   r  r  r  rA   rG   r{   r   rZ   )rk   rt   origr;   r;   r<   r   d  s   
zInlineForeignKeyField.cleanc                 C   s   dS NFr;   )rk   r   r/   r;   r;   r<   r   s  s   z!InlineForeignKeyField.has_changed)r`   ro   rp   r   r   r.   r   default_error_messagesrm   r   r   r   r;   r;   r   r<   r   N  s    r   c                   @   s$   e Zd Zdd Zdd Zdd ZdS )ModelChoiceIteratorValuec                 C   s   || _ || _d S rn   )rt   r7   )rk   rt   r7   r;   r;   r<   rm   x  s   
z!ModelChoiceIteratorValue.__init__c                 C   s
   t | jS rn   )r{   rt   r   r;   r;   r<   __str__|  s   
z ModelChoiceIteratorValue.__str__c                 C   s   t |tr|j}| j|kS rn   )r)   r%  rt   )rk   otherr;   r;   r<   __eq__  s   

zModelChoiceIteratorValue.__eq__N)r`   ro   rp   rm   r&  r(  r;   r;   r;   r<   r%  w  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 rn   )r-   rE   r   r;   r;   r<   rm     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 )Nr  )r-   empty_labelrE   Z_prefetch_related_lookupsiteratorchoice)rk   rE   r   r;   r;   r<   __iter__  s   zModelChoiceIterator.__iter__c                 C   s    | j  | jjd urd S d S )Nr   r   )rE   countr-   r*  r   r;   r;   r<   __len__  s    zModelChoiceIterator.__len__c                 C   s   | j jd up
| j S rn   )r-   r*  rE   existsr   r;   r;   r<   __bool__  s   zModelChoiceIterator.__bool__c                 C   s   t | j||| j|fS rn   )r%  r-   prepare_valuelabel_from_instancerk   r   r;   r;   r<   r,    s   
zModelChoiceIterator.choiceN)r`   ro   rp   rm   r-  r/  r1  r,  r;   r;   r;   r<   r)    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.   rX   r   rY   to_field_namerN   r   c       	         K   s^   t j| f|||||d| |r|d ust| jtr!|
s!d | _n|| _|| _|	| _|| _d S )N)r   r.   rX   r   rY   )	r   rm   r)   r.   r   r*  rE   rN   r5  )rk   rE   r*  r   r.   rX   r   rY   r5  rN   r   ri   r;   r;   r<   rm     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.
        )ra   rN   r   r;   r;   r<   rF     s   
z%ModelChoiceField.get_limit_choices_toc                    s*   t t| |}| jd ur| j |_|S rn   )ry   r   __deepcopy__rE   all)rk   memoresultr   r;   r<   r6    s   
zModelChoiceField.__deepcopy__c                 C   s   | j S rn   )r   r   r;   r;   r<   _get_queryset  s   zModelChoiceField._get_querysetc                 C   s$   |d u rd n|  | _| j| j_d S rn   )r7  r   choicesr.   )rk   rE   r;   r;   r<   _set_queryset  s   zModelChoiceField._set_querysetc                 C   s   t |S )z
        Convert objects into strings and generate the labels for the choices
        presented by this object. Subclasses can override this method to
        customize the display of the choices.
        r{   r4  r;   r;   r<   r3    s   z$ModelChoiceField.label_from_instancec                 C   s   t | dr| jS | | S )N_choices)rJ   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%   )rJ   r5  Zserializable_valuerG   ry   r2  rk   rt   r   r;   r<   r2    s
   
zModelChoiceField.prepare_valuec              
   C   sz   || j v rd S z| jpd}t|| jjrt||}| jjdi ||i}W |S  tt| jjj	fy<   t
| jd ddw )NrG   r  r!  r;   )r3   r5  r)   rE   rL   rA   r2   r   rb   ZDoesNotExistr   rZ   )rk   rt   keyr;   r;   r<   r      s   


zModelChoiceField.to_pythonc                 C   s   t | |S rn   )r   validaterA  r;   r;   r<   rC    r   zModelChoiceField.validatec                 C   s@   | j rdS |d ur|nd}|d ur|nd}t| |t|kS )NFr  )disabledr{   r2  )rk   r   r/   initial_valueZ
data_valuer;   r;   r<   r     s
   zModelChoiceField.has_changed)r`   ro   rp   r   r   r$  r)  r+  rm   rF   r6  r:  r<  propertyrE   r3  r?  r   Z_set_choicesr;  r2  r   rC  r   r   r;   r;   r   r<   r     s,    

r   c                       sl   e Zd ZdZeZeZedededdZ	 fddZ
dd	 Zd
d Zdd Z fddZdd Z  ZS )r   z9A MultipleChoiceField whose choices are a model QuerySet.zEnter a list of values.zESelect a valid choice. %(value)s is not one of the available choices.u"   “%(pk)s” is not a valid value.)invalid_listr  invalid_pk_valuec                    sP   t  j|fdd i| | jdd ur&tjdtdd | jd | jd< d S d S )Nr*  r   zFThe 'list' error message key is deprecated in favor of 'invalid_list'.r   )
stacklevelrG  )ry   rm   rZ   r2   warningswarnr   )rk   rE   ri   r   r;   r<   rm   "  s   z!ModelMultipleChoiceField.__init__c                 C   s   |sg S t | |S rn   )r   _check_valuesrA  r;   r;   r<   r   ,  s   z"ModelMultipleChoiceField.to_pythonc                 C   sr   |  |}| jr|st| jd dd| js|s| j S t|ttfs-t| jd dd| 	|}| 
| |S )Nr   r!  rG  )r2  r   r   rZ   rE   r  r)   r   r   rL  Zrun_validators)rk   rt   r   r;   r;   r<   r   1  s   





zModelMultipleChoiceField.cleanc              
      s   | j pd zt|}W n ty   t| jd ddw |D ]$}z| jjdi  |i W q ttfyB   t| jd dd|idw | jjdi d  |i} fdd|D }|D ]}t||vrot| jd	 d	d
|idq[|S )z
        Given a list of possible PK values, return a QuerySet of the
        corresponding objects. Raise a ValidationError if a given value is
        invalid (not a valid PK, not in the queryset, etc.)
        rG   rG  r!  rH  )r   paramsz%s__inc                    s   h | ]	}t t| qS r;   )r{   rA   r   rB  r;   r<   rw   ]  rV   z9ModelMultipleChoiceField._check_values.<locals>.<setcomp>r  rt   Nr;   )	r5  	frozensetrb   r   rZ   rE   rK   r   r{   )rk   rt   rG   r   Zpksvalr;   rN  r<   rL  B  s<   
z&ModelMultipleChoiceField._check_valuesc                    sD   t |drt|tst |dst j  fdd|D S t |S )Nr-  r%   c                    s   g | ]} |qS r;   r;   )rS   rv   r2  r;   r<   rU   l  r   z:ModelMultipleChoiceField.prepare_value.<locals>.<listcomp>)rJ   r)   r{   ry   r2  rA  r   rQ  r<   r2  g  s   
z&ModelMultipleChoiceField.prepare_valuec                 C   s`   | j rdS |d u rg }|d u rg }t|t|krdS dd | |D }dd |D }||kS )NFTc                 S      h | ]}t |qS r;   r=  rS   rt   r;   r;   r<   rw   x  r   z7ModelMultipleChoiceField.has_changed.<locals>.<setcomp>c                 S   rR  r;   r=  rS  r;   r;   r<   rw   y  r   )rD  r   r2  )rk   r   r/   Zinitial_setZdata_setr;   r;   r<   r   o  s   z$ModelMultipleChoiceField.has_changed)r`   ro   rp   r   r   r.   r   Zhidden_widgetr   r$  rm   r   r   rL  r2  r   r   r;   r;   r   r<   r     s    
%r   c                 C   s"   t | do| jjd up| jjd uS r@  )rJ   r%   r'   r8   )r[   r;   r;   r<   modelform_defines_fields}  s   

rT  )NN)	NNNNNNNNNr#  )8r   rJ  	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   rO   r   rj   rq   r   r   r"   r   r   r    r  r!   r   r%  r)  r   r   rT  r;   r;   r;   r<   <module>   s|    

)YH @
U  .

d8
/)sf