

_V                 @   sN  d  Z  d d l m 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 d d l m Z m Z d d l  m! Z! m" Z" d d l# m$ Z$ m% Z& d d d d d d d d d d d d f Z' d Z( d d d d  Z) d d d d  Z* d d d d d d d d d d  d 	 Z+ Gd! d"   d" e,  Z- Gd# d$   d$ e  Z. Gd% d   d e  Z/ Gd& d   d e j0 e. e/   Z1 e1 d d d d d d d d d d' d 
 Z2 Gd( d   d e  Z3 e1 d e3 d) d* d* d d d d d* d d d d d d* d d+ d  Z4 Gd, d   d e3  Z5 d d* d- d.  Z6 e1 e5 d d d d/ d* d0 d d d d* d d d d d d* d d1 d  Z7 Gd2 d3   d3 e  Z8 Gd4 d5   d5 e,  Z9 Gd6 d   d e  Z: Gd7 d   d e:  Z; d8 d9   Z< d S):z[
Helper functions for creating Form classes from Django models
and database field objects.
    )unicode_literals)OrderedDict)chain)NON_FIELD_ERRORS
FieldErrorImproperlyConfiguredValidationError)ChoiceFieldField)BaseFormDeclarativeFieldsMetaclass)BaseFormSetformset_factory)	ErrorList)HiddenInputMultipleHiddenInputSelectMultiple)six)
force_text
smart_text)capfirstget_text_list)ugettextugettext_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 s2 t | | j  s2 | j | k ri q2 n  | d k	 r | j | k r q2 n  | r | j | k r q2 n  t | | j	  r | j
 |  q2 | j | | | j  q2 Wx% | D] } | j | | | j  q W| S)z
    Constructs and returns a model instance from the bound ``form``'s
    ``cleaned_data``, but does not save the returned instance to the
    database.
    r   )modelsN)Z	django.dbr'   _metacleaned_datafieldseditable
isinstance	AutoFieldnameZ	FileFieldappendsave_form_data)	forminstancer*   excluder'   optsr)   Zfile_field_listf r6   5/tmp/pip-build-ghmbqnp_/Django/django/forms/models.pyconstruct_instance$   s$    		r8   c             C   s;  d d l  m } |  j } i  } xt | j | j | j  D] } t | d d  sY q; n  | rt | j | k rt q; n  | r | j | k r q; n  t	 | |  r|  j
 d k r g  | | j <q3| j |   } | j d k	 r d d   | D | | j <q3t | j d d	 d
  | | j <q; | j |   | | j <q; W| S)a  
    Returns 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, only the named
    fields will be included in the returned dict.

    ``exclude`` is an optional list of field names. If provided, the named
    fields will be excluded from the returned dict, even if they are listed in
    the ``fields`` argument.
    r   )ManyToManyFieldr+   FNc             S   s   g  |  ] } | j   q Sr6   )pk).0itemr6   r6   r7   
<listcomp>g   s   	 z!model_to_dict.<locals>.<listcomp>r:   ZflatT)Zdjango.db.models.fields.relatedr9   r(   r   concrete_fieldsvirtual_fieldsmany_to_manygetattrr.   r,   r:   Zvalue_from_objectZ_result_cachelistZvalues_list)r2   r*   r3   r9   r4   datar5   qsr6   r6   r7   r   F   s&    	"%c
                st  g  }
 g   |  j  } d d l m     f d d   | j D } xt t | j | | j   D]} t | d d  s~ q` n  | d k	 r | j	 | k r q` n   r | j	  k r q` n  i  } | r | j	 | k r | | j	 | d <n  | t
 k s
| r| j	 | k rd	 | d
 <n  | r@| j	 | k r@| | j	 | d <n  | ri| j	 | k ri| | j	 | d <n  | r| j	 | k r| | j	 | d <n  |	 r| j	 |	 k r|	 | j	 | d <n  | d k r| j |   } n* t |  st d   n | | |  } | r"|
 j | j	 | f  q`  j | j	  q` Wt |
   | rpt    f d d   | D   n   S)a  
    Returns a ``OrderedDict`` containing form fields for the given model.

    ``fields`` is an optional list of field names. If provided, only the named
    fields will be included in the returned fields.

    ``exclude`` is an optional list of field names. If provided, the named
    fields will be excluded 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.
    r   )r
   c                s%   g  |  ] } t  |    r |  q Sr6   )r,   )r;   r5   )
ModelFieldr6   r7   r=      s   	 z$fields_for_model.<locals>.<listcomp>r+   FNwidgetTZlocalizelabel	help_texterror_messages
form_classz1formfield_callback must be a function or callablec                sJ   g  |  ]@ }   s%   r |   k r |  k r |  j  |  f  q Sr6   )get)r;   r5   )r3   
field_dictignoredr6   r7   r=      s   	 )r(   Zdjango.db.models.fieldsr
   r?   sortedr   r>   r@   rA   r.   r    	formfieldcallable	TypeErrorr/   r   )modelr*   r3   widgetsformfield_callbacklocalized_fieldslabels
help_textsrI   field_classesZ
field_listr4   Zsortable_virtual_fieldsr5   kwargsrO   r6   )rE   r3   rL   rM   r7   r   o   sL    	%!%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)
NrR   r*   r3   rS   rU   rV   rW   rI   rX   )
rA   rR   r*   r3   rS   rU   rV   rW   rI   rX   )selfoptionsr6   r6   r7   __init__   s    zModelFormOptions.__init__)__name__
__module____qualname__r]   r6   r6   r6   r7   rZ      s   rZ   c                   s"   e  Z d  Z   f d d   Z   S)ModelFormMetaclassc                s  | j  d d   } t t |   j |  | | |  } | t f k rF | St t | d d    } | _ xu d d d g D]d } t | |  } t | t	 j
  ru | t k ru d i | j d 6| d 6| d	 6}	 t |	   qu qu W| j r| j d  k r| j d  k rt d
 |   n  | j t k r2d  | _ n  t | j | j | j | j | | j | j | j | j | j 
 }
 d d   t	 j |
  D } t |  t | j j    } | rd } | d j |  | j j f } t |   n  |
 j | j  n	 | j }
 |
 | _  | S)NrT   Metar*   r3   rU   zP%(model)s.Meta.%(opt)s cannot be a string. Did you mean to type: ('%(value)s',)?rR   optvaluez|Creating a ModelForm without either the 'fields' attribute or the 'exclude' attribute is prohibited; form %s needs updating.c             S   s"   g  |  ] \ } } | s |  q Sr6   r6   )r;   kvr6   r6   r7   r=      s   	 z.ModelFormMetaclass.__new__.<locals>.<listcomp>z&Unknown field(s) (%s) specified for %sz, )!popsuperra   __new__r   rZ   rA   r(   r,   r   string_typesr    r^   rQ   rR   r*   r3   r   r   rS   rU   rV   rW   rI   rX   	iteritemssetZdeclared_fieldskeysjoinr   updateZbase_fields)Zmcsr.   basesattrsrT   	new_classr4   rc   rd   msgr*   Znone_model_fieldsZmissing_fieldsmessage)	__class__r6   r7   ri      sL    !
					zModelFormMetaclass.__new__)r^   r_   r`   ri   r6   r6   )ru   r7   ra      s   ra   c                   s   e  Z d  Z d d d d d e d d d   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 _   S)r   Nzid_%sFc
          	      s2  |  j  }
 |
 j d  k r' t d   n  |	 d  k rK |
 j   |  _ i  } n! |	 |  _ t |	 |
 j |
 j  } | d  k	 r | j |  n  d |  _ t	 t
 |   j | | | | | | | |  xo |  j D]d } |  j | } t | d  r t | d  r | j   } | d  k	 r*| j j |  | _ q*q q Wd  S)Nz'ModelForm has no model class specified.Fquerysetget_limit_choices_to)r(   rR   
ValueErrorr2   r   r*   r3   ro   _validate_uniquerh   r   r]   hasattrrw   rv   Zcomplex_filter)r[   rC   filesauto_idprefixinitialerror_classZlabel_suffixZempty_permittedr2   r4   Zobject_data
field_namerO   limit_choices_to)ru   r6   r7   r]     s&    				zBaseModelForm.__init__c             C   s.  g  } x!|  j  j j D]} | j } | |  j k rG | j | j  q |  j j rx | |  j j k rx | j | j  q |  j j r | |  j j k r | j | j  q | |  j j   k r | j | j  q |  j | } |  j j	 |  } | j
 r | j r | | j k r | j | j  q q W| S)z
        For backwards-compatibility, several types of fields need to be
        excluded from model validation. See the following tickets for
        details: #12507, #12521, #12553
        )r2   r(   r*   r.   r/   r3   _errorsrm   r)   rK   Zblankrequiredempty_values)r[   r3   r5   fieldZ
form_fieldZfield_valuer6   r6   r7   _get_validation_exclusions,  s     		#z(BaseModelForm._get_validation_exclusionsc             C   s   d |  _  |  j S)NT)ry   r)   )r[   r6   r6   r7   cleanV  s    	zBaseModelForm.cleanc             C   s   |  j  } t | d  r$ | j } n i | t 6} x | j   D] \ } } | t k r~ | j r~ t | j k r~ | j t } n% | |  j k r> |  j | j } n q> x? | D]7 } t | t  r | j	 | k r | | j	 | _
 q q Wq> W|  j d  |  d  S)N
error_dict)r(   rz   r   r   itemsrI   r*   r,   r   codert   Z	add_error)r[   errorsr4   r   r   messagesrI   rt   r6   r6   r7   _update_errorsZ  s     	zBaseModelForm._update_errorsc             C   s  |  j  } |  j   } y" t |  |  j | j |  |  _ Wn2 t k
 rk } z |  j |  WYd  d  } ~ Xn Xx< |  j j   D]+ \ } } t | t	  r| | j
 |  q| q| Wy |  j j d | d d  Wn2 t k
 r } z |  j |  WYd  d  } ~ Xn X|  j r|  j   n  d  S)Nr3   validate_uniqueF)r(   r   r8   r2   r*   r   r   r   r,   InlineForeignKeyFieldr/   Z
full_cleanry   r   )r[   r4   r3   er.   r   r6   r6   r7   _post_cleanv  s    	" 	 	zBaseModelForm._post_cleanc             C   s\   |  j    } y |  j j d |  Wn2 t k
 rW } z |  j |  WYd d } ~ Xn Xd S)z
        Calls the instance's validate_unique() method and updates the form's
        validation errors if any were raised.
        r3   N)r   r2   r   r   r   )r[   r3   r   r6   r6   r7   r     s
    zBaseModelForm.validate_uniquec             C   s   |  j  } |  j j } |  j j } |  j j } x t | j | j  D]} } t | d  s^ qC n  | ry | j	 | k ry qC n  | r | j	 | k r qC n  | j	 | k rC | j
 |  j | | j	  qC qC Wd S)zS
        Save the many-to-many fields and generic relations for this form.
        r0   N)r)   r(   r3   r*   r2   r   r@   r?   rz   r.   r0   )r[   r)   r3   r*   r4   r5   r6   r6   r7   	_save_m2m  s    	zBaseModelForm._save_m2mTc             C   ss   |  j  r@ t d |  j j j |  j j j r0 d n d f   n  | r` |  j j   |  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   rx   r2   r(   Zobject_name_stateaddingsaver   save_m2m)r[   commitr6   r6   r7   r     s    	%zBaseModelForm.save)r^   r_   r`   r   r]   r   r   r   r   r   r   r   alters_datar6   r6   )ru   r7   r     s   	*c               @   s   e  Z d  Z d S)r   N)r^   r_   r`   r6   r6   r6   r7   r     s   c             C   s  i |  d 6} | d k	 r& | | d <n  | d k	 r? | | d <n  | d k	 rX | | d <n  | d k	 rq | | d <n  | d k	 r | | d <n  | d k	 r | | d <n  |	 d k	 r |	 | d	 <n  |
 d k	 r |
 | d
 <n  t  f } t | d  r | j t  f } n  t t d  | |  } |  j t d  } i | d 6| d 6} t | d d  d k r}t | d d  d k r}t d   n  t |  | | f |  S)a  
    Returns a ModelForm containing form fields for the given model.

    ``fields`` is an optional list of field names. If provided, only the named
    fields will be included in the returned fields. If omitted or '__all__',
    all fields will be used.

    ``exclude`` is an optional list of field names. If provided, the named
    fields will be excluded 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.
    rR   Nr*   r3   rS   rU   rV   rW   rI   rX   rb   ZFormrT   zZCalling modelform_factory without defining 'fields' or 'exclude' explicitly is prohibited.)objectrz   rb   typestrr^   rA   r   )rR   r1   r*   r3   rT   rS   rU   rV   rW   rI   rX   rq   parentrb   
class_nameZform_class_attrsr6   r6   r7   r%     s<    %	
c                   s6  e  Z d  Z d Z d Z d d 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   S))r!   zO
    A ``FormSet`` for editing a queryset and/or adding new objects to it.
    Nzid_%sc                sg   | |  _  | j d d   |  _ i | d 6| d 6| d 6| d 6} | j |  t t |   j |   d  S)Nr~   rC   r{   r|   r}   )rv   rg   initial_extraro   rh   r!   r]   )r[   rC   r{   r|   r}   rv   rY   defaults)ru   r6   r7   r]   ,  s
    	"zBaseModelFormSet.__init__c                s5   |  j  p |  j s" t |  j    St t |   j   S)z>Returns the number of forms that are required in this FormSet.)rC   r{   lenget_querysetrh   r!   initial_form_count)r[   )ru   r6   r7   r   4  s    z#BaseModelFormSet.initial_form_countc             C   s>   t  |  d  s. d d   |  j   D |  _ n  |  j j |  S)N_object_dictc             S   s   i  |  ] } | | j   q Sr6   )r:   )r;   or6   r6   r7   
<dictcomp><  s   	 z5BaseModelFormSet._existing_object.<locals>.<dictcomp>)rz   r   r   rK   )r[   r:   r6   r6   r7   _existing_object:  s    z!BaseModelFormSet._existing_objectc             C   s,   x" | j  d k	 r$ | j  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   r6   r6   r7   _get_to_python?  s    zBaseModelFormSet._get_to_pythonc                s-  |  j  r | |  j   k  r d |  j |  |  j j j j f } |  j | } |  j j j } |  j |  } | |  } |  j	 |  | d <n  | |  j   k  r d | k r |  j
   | | d <n  | |  j   k r|  j ry |  j | |  j   | d <Wqt k
 rYqXn  t t |   j | |  S)Nz%s-%sr2   r~   )is_boundr   
add_prefixrR   r(   r:   r.   rC   r   r   r   r   
IndexErrorrh   r!   _construct_form)r[   irY   Zpk_keyr:   pk_fieldr   )ru   r6   r7   r   H  s    %z BaseModelFormSet._construct_formc             C   sv   t  |  d  so |  j d  k	 r* |  j } n |  j j j   } | j sc | j |  j j j j	  } n  | |  _
 n  |  j
 S)N	_queryset)rz   rv   rR   _default_managerr   orderedZorder_byr(   r:   r.   r   )r[   rD   r6   r6   r7   r   Z  s    	zBaseModelFormSet.get_querysetTc             C   s   | j  d |  S)z:Saves and returns a new model instance for the given form.r   )r   )r[   r1   r   r6   r6   r7   save_newm  s    zBaseModelFormSet.save_newc             C   s   | j  d |  S)z@Saves and returns an existing model instance for the given form.r   )r   )r[   r1   r2   r   r6   r6   r7   save_existingq  s    zBaseModelFormSet.save_existingc             C   s   | r | j    n  d S)z#Deletes an existing model instance.N)delete)r[   objr   r6   r6   r7   delete_existingu  s    z BaseModelFormSet.delete_existingc                sG   | s- g    _    f d d   } |   _ n    j |    j |  S)zSaves model instances for every form, adding and changing instances
        as necessary, and returns the list of instances.
        c                 s"   x   j  D] }  |  j   q
 Wd  S)N)saved_formsr   )r1   )r[   r6   r7   r     s    z'BaseModelFormSet.save.<locals>.save_m2m)r   r   save_existing_objectssave_new_objects)r[   r   r   r6   )r[   r7   r   z  s
    	zBaseModelFormSet.savec             C   s   |  j    d  S)N)r   )r[   r6   r6   r7   r     s    zBaseModelFormSet.cleanc                s  t    } t    } |  j   f d d   |  j D } x_ | D]W     j   }   j j d |  \ } } | j t  |   } | j t  |   } q> Wg  } x | D] \ } }	 t    }
 x | D]     f d d   |	 D } t d d   | D  } | r d  | k r | |
 k r}| j |  j	 |	   |  j
 |  j   g    j t <x- |	 D]" } |   j k rT  j | =qTqTWn  |
 j |  q q Wq Wx(| 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| j |  j |   |  j
 |  j   g    j t <  j | =n  |
 j |  qqWqW| rt |   n  d  S)Nc                s.   g  |  ]$ } | j    r |   k r |  q Sr6   )Zis_valid)r;   r1   )forms_to_deleter6   r7   r=     s   	 z4BaseModelFormSet.validate_unique.<locals>.<listcomp>r3   c             3   s+   |  ]! } |   j  k r   j  | Vq d  S)N)r)   )r;   r   )r1   r6   r7   	<genexpr>  s   z3BaseModelFormSet.validate_unique.<locals>.<genexpr>c             s   s0   |  ]& } t  | d   r$ | j   n | Vq d S)_get_pk_valN)rz   r   )r;   dr6   r6   r7   r     s   date)rl   deleted_formsZformsr   r2   Z_get_unique_checksuniontupler/   get_unique_error_messager   get_form_errorr   r   r)   addyearmonthdayrA   get_date_error_messager   )r[   Zall_unique_checksZall_date_checksZvalid_formsr3   Zunique_checksZdate_checksr   Zuclassunique_checkZ	seen_dataZrow_datar   
date_checklookupZ
unique_forr   Z	date_datarC   r6   )r1   r   r7   r     sV    				
	z BaseModelFormSet.validate_uniquec             C   s\   t  |  d k r+ t d  i | d d 6St d  i t | t j t d    d 6Sd  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   	text_type_)r[   r   r6   r6   r7   r     s    	z)BaseModelFormSet.get_unique_error_messagec             C   s8   t  d  i | d d 6| d d 6t j | d  d 6S)NzoPlease correct the duplicate data for %(field_name)s which must be unique for the %(lookup)s in %(date_field)s.   r      Z
date_fieldr   r   )r   r   r   )r[   r   r6   r6   r7   r     s
    	z'BaseModelFormSet.get_date_error_messagec             C   s
   t  d  S)Nz*Please correct the duplicate values below.)r   )r[   r6   r6   r7   r     s    zBaseModelFormSet.get_form_errorc             C   s   g  |  _  g  |  _ |  j s g  Sg  } |  j } x |  j D] } | j } | | k r | j d  k rh q8 n  |  j j |  |  j | d | q8 | j   r8 |  j  j | | j	 f  | j |  j
 | | d |  | s |  j j |  q q8 q8 W| S)Nr   )Zchanged_objectsZdeleted_objectsZinitial_formsr   r2   r:   r/   r   has_changedZchanged_datar   r   )r[   r   Zsaved_instancesr   r1   r   r6   r6   r7   r     s&    					z&BaseModelFormSet.save_existing_objectsc             C   s   g  |  _  xy |  j D]n } | j   s+ q n  |  j rI |  j |  rI q n  |  j  j |  j | d |  | s |  j j |  q q W|  j  S)Nr   )Znew_objectsZextra_formsr   
can_deleteZ_should_delete_formr/   r   r   )r[   r   r1   r6   r6   r7   r     s    	z!BaseModelFormSet.save_new_objectsc       	         s  d d l  m   m } m } |  j j j |  _ }    f d d     |  se | j | j	 k r| j
 r | j j j r d n	 | j j } nG y, | d k	 r |  j   | j } n d } Wn t k
 r d } Yn Xt | |  s t | |  r| j j j j   } n |  j j j   } | j | j j j  } | j j rf| j j j |  j j t  } n t } t | d | d d d	 | | j	 |  j j <n  t t |   j | |  d S)
z0Add a hidden field for the object's primary key.r   )r-   OneToOneField
ForeignKeyc                sM   |  j  pL |  j pL t |     pL |  j oL |  j j oL  |  j j j j  S)N)r+   Zauto_createdr,   r   Zparent_linkrR   r(   r:   )r:   )r-   pk_is_not_editabler6   r7   r     s    "z7BaseModelFormSet.add_fields.<locals>.pk_is_not_editableNr~   r   FrF   )django.db.modelsr-   r   r   rR   r(   r:   	_pk_fieldr.   r*   r   r2   r   r   r   r   r,   r   r   ZusingdbrS   rK   r   r   rh   r!   
add_fields)	r[   r1   indexr   r   r:   pk_valuerD   rF   )ru   )r-   r   r7   r     s*    	$
!.zBaseModelFormSet.add_fields)r^   r_   r`   __doc__rR   r]   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r6   r6   )ru   r7   r!   &  s,   		Cr   Fc             C   s	  t  | d d  } | d k r< t t d  t f i   } n  t  | d |  d k r{ t  | d |	  d k r{ t d   n  t |  d | d | d |	 d | d |
 d	 | d
 | d | d | d | 
} t | | d | d | d | d | d | d | d | } |  | _ | S)zC
    Returns a FormSet class for the given Django model class.
    rb   Nr*   r3   z]Calling modelformset_factory without defining 'fields' or 'exclude' explicitly is prohibited.r1   rT   rS   rU   rV   rW   rI   rX   extramin_nummax_num	can_orderr   validate_minvalidate_max)rA   r   r   r   r   r%   r   rR   )rR   r1   rT   formsetr   r   r   r   r*   r3   rS   r   rU   rV   rW   rI   r   r   rX   metaFormSetr6   r6   r7   r"   8  s"    		c            	       s   e  Z d  Z d Z d d d d d 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   S)r#   z0A formset for child objects related to a parent.NFc       	         s   | d  k r$ |  j  j j   |  _ n	 | |  _ | |  _ | d  k rQ |  j j } n  |  j j d  k	 r | j i |  j |  j  j 6  } n | j	   } t
 t |   j | | d | d | | d  S)Nr}   rv   )fkr   rR   r2   save_as_newr   r:   filterr.   nonerh   r#   r]   )	r[   rC   r{   r2   r   r}   rv   rY   rD   )ru   r6   r7   r]   [  s    		"zBaseInlineFormSet.__init__c                s    |  j  r d St t |   j   S)Nr   )r   rh   r#   r   )r[   )ru   r6   r7   r   k  s    	z$BaseInlineFormSet.initial_form_countc                s   t  t |   j | |  } |  j r_ d  | j | j |  j j  <d  | j | j |  j j  <n  |  j	 j
 } |  j j j |  j j j j j
 j k r t |  j	 |  j j j  } t | d |  } n  t | j	 |  j j   |  | S)Nr:   )rh   r#   r   r   rC   r   r   r.   r   r2   r:   r   r   rR   r(   rA   setattrget_attname)r[   r   rY   r1   Zfk_value)ru   r6   r7   r   p  s    	'z!BaseInlineFormSet._construct_formc             C   s%   |  j  j j d |  j  j d d  S)NrR   + )r   r   Zget_accessor_namerR   replace)clsr6   r6   r7   get_default_prefix  s    z$BaseInlineFormSet.get_default_prefixTc             C   s   t  | j |  j j |  j  | j d d  } t |  j |  j j j  } t  | |  j j   t | d |   | r | j   n  | r t	 | d  r | j
   n  | S)Nr   Fr:   r   )r   r2   r   r.   r   rA   r   r   r   rz   r   )r[   r1   r   r   r   r6   r6   r7   r     s    %zBaseInlineFormSet.save_newc                s  t  t |   j | |  |  j |  j k rG |  j j } i d d 6} nz |  j j } i t | j j |  d t	 |  j j
   d 6} |  j j j |  j j j j j j k r |  j j j | d <n  |  j j j r8| j d  d  k	 r|  j j j | d  } n |  j j j } | j   r8t |  j | j d   q8n  t |  j |  | j | <| j j rt | j j t  rt | j j  | j _ n  | j j j |  j j  n  d  S)NTr   rG   to_field)rh   r#   r   r   r   r.   rA   r*   rK   r   Zverbose_namer   r   rR   r(   r:   r2   r   r   	get_fieldZhas_defaultr   Zattnamer   r,   r   rB   r/   )r[   r1   r   r.   rY   r   )ru   r6   r7   r     s(    .'zBaseInlineFormSet.add_fieldsc                s/     f d d   | D } t  t    j |  S)Nc                s(   g  |  ] } |   j  j k r |  q Sr6   )r   r.   )r;   r   )r[   r6   r7   r=     s   	 z>BaseInlineFormSet.get_unique_error_message.<locals>.<listcomp>)rh   r#   r   )r[   r   )ru   )r[   r7   r     s    z*BaseInlineFormSet.get_unique_error_message)r^   r_   r`   r   r]   r   r   classmethodr   r   r   r   r6   r6   )ru   r7   r#   Y  s   	"c                s  d d l  m   | j }  r  f d d   | j D } t |  d k r | d } t |    s | j j  k r | j j  j j   k r t	 d   j j
 f   q qt |  d k rt	 d | j j
  f   qn    f d d   | j D } t |  d k r(| d } nc t |  d k ri| rDd	 St	 d
 | j j
  j j
 f   n" t	 d | j j
  j j
 f   | S)aI  
    Finds and returns the ForeignKey from model to parent if there is one
    (returns 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, an exception is raised if there is no ForeignKey from model to
    parent_model.
    r   )r   c                s%   g  |  ] } | j    k r |  q Sr6   )r.   )r;   r5   )fk_namer6   r7   r=     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                sR   g  |  ]H } t  |    r | j j  k sH | j j  j j   k r |  q Sr6   )r,   r   rR   r(   get_parent_list)r;   r5   )r   parent_modelr6   r7   r=     s   	 Nz'%s' has no ForeignKey to '%s'.z*'%s' has more than one ForeignKey to '%s'.)r   r   r(   r*   r   r,   r   rR   r   rx   rG   )r   rR   r   Zcan_failr4   Zfks_to_parentr   r6   )r   r   r   r7   _get_foreign_key  s:    		
		r   r   Tc             C   s   t  |  | d | } | j r' d }
 n  i | d 6| d 6| d 6| d 6|	 d 6| d 6| d	 6| d
 6| d 6|
 d 6| d 6| d 6| d 6| d 6| d 6| d 6| d 6| d 6} t | |  } | | _ | S)z
    Returns an ``InlineFormSet`` for the given kwargs.

    You must provide ``fk_name`` if ``model`` has more than one ``ForeignKey``
    to ``parent_model``.
    r   r   r1   rT   r   r   r   r   r*   r3   r   r   rS   r   r   rU   rV   rW   rI   rX   )r   uniquer"   r   )r   rR   r1   r   r   r*   r3   r   r   r   r   rT   rS   r   rU   rV   rW   rI   r   r   rX   r   rY   r   r6   r6   r7   r$     s2    		
	c                   sY   e  Z d  Z d Z e Z i e d  d 6Z   f d d   Z d d   Z	 d d	   Z
   S)
r   zw
    A basic integer field that deals with validating the given value to a
    given parent instance in an inline.
    zEThe inline foreign key did not match the parent instance primary key.invalid_choicec                s   | |  _  | j d d  |  _ | j d d   |  _ |  j  d  k	 rz |  j rg t |  j  |  j  | d <qz |  j  j | d <n  d | d <t t |   j | |   d  S)Nr   Fr   r~   r   )	parent_instancerg   r   r   rA   r:   rh   r   r]   )r[   r   argsrY   )ru   r6   r7   r]   *  s    		
zInlineForeignKeyField.__init__c             C   s   | |  j  k r# |  j r d  S|  j S|  j rD t |  j |  j  } n |  j j } t |  t |  k r t |  j d d d  n  |  j S)Nr   r   )	r   r   r   r   rA   r:   r   r   rI   )r[   rd   origr6   r6   r7   r   6  s    		zInlineForeignKeyField.cleanc             C   s   d S)NFr6   )r[   r~   rC   r6   r6   r7   r   E  s    z!InlineForeignKeyField.has_changed)r^   r_   r`   r   r   rF   r   default_error_messagesr]   r   r   r6   r6   )ru   r7   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   rv   )r[   r   r6   r6   r7   r]   J  s    	zModelChoiceIterator.__init__c             c   sp   |  j  j d  k	 r& d |  j  j f Vn  |  j j   } | j sM | j   } n  x | D] } |  j |  VqT Wd  S)Nr   )r   empty_labelrv   allZ_prefetch_related_lookupsiteratorchoice)r[   rv   r   r6   r6   r7   __iter__N  s    	zModelChoiceIterator.__iter__c             C   s)   t  |  j  |  j j d  k	 r$ d n d S)Nr   r   )r   rv   r   r  )r[   r6   r6   r7   __len__X  s    zModelChoiceIterator.__len__c             C   s"   |  j  j |  |  j  j |  f S)N)r   prepare_valuelabel_from_instance)r[   r   r6   r6   r7   r  \  s    zModelChoiceIterator.choiceN)r^   r_   r`   r]   r  r  r  r6   r6   r6   r7   r  I  s   
r  c            
       s   e  Z d  Z d Z i e d  d 6Z 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   S)r   z1A ChoiceField whose choices are a model QuerySet.zGSelect a valid choice. That choice is not one of the available choices.r   z	---------TNr   c
          	   O   sh   | r | d  k	 r d  |  _  n	 | |  _  t j |  | | | | | |
 |  | |  _ |	 |  _ | |  _ d  S)N)r  r
   r]   rv   r   to_field_name)r[   rv   r  r   rF   rG   r~   rH   r  r   r   rY   r6   r6   r7   r]   i  s    	
		zModelChoiceField.__init__c             C   s    t  |  j  r |  j   S|  j S)z
        Returns ``limit_choices_to`` for this form field.

        If it is a callable, it will be invoked and the result will be
        returned.
        )rP   r   )r[   r6   r6   r7   rw   z  s    
z%ModelChoiceField.get_limit_choices_toc                s(   t  t |   j |  } | j | _ | S)N)rh   r	   __deepcopy__rv   )r[   memoresult)ru   r6   r7   r    s    zModelChoiceField.__deepcopy__c             C   s   |  j  S)N)r   )r[   r6   r6   r7   _get_queryset  s    zModelChoiceField._get_querysetc             C   s   | |  _  |  j |  j _ d  S)N)r   choicesrF   )r[   rv   r6   r6   r7   _set_queryset  s    	zModelChoiceField._set_querysetc             C   s
   t  |  S)z
        This method is used to convert objects into strings; it's used to
        generate the labels for the choices presented by this object. Subclasses
        can override this method to customize the display of the choices.
        )r   )r[   r   r6   r6   r7   r
    s    z$ModelChoiceField.label_from_instancec             C   s    t  |  d  r |  j St |   S)N_choices)rz   r  r  )r[   r6   r6   r7   _get_choices  s    	zModelChoiceField._get_choicesc                sH   t  | d  r2 |  j r( | j |  j  S| j Sn  t t |   j |  S)Nr(   )rz   r  Zserializable_valuer:   rh   r   r	  )r[   rd   )ru   r6   r7   r	    s
    	
zModelChoiceField.prepare_valuec             C   s   | |  j  k r d  Sy, |  j p" d } |  j j i | | 6  } Wn= t t |  j j j f k
 r~ t |  j	 d d d  Yn X| S)Nr:   r   r   )
r   r  rv   rK   rx   rQ   rR   ZDoesNotExistr   rI   )r[   rd   keyr6   r6   r7   r     s    zModelChoiceField.to_pythonc             C   s   t  j |  |  S)N)r
   validate)r[   rd   r6   r6   r7   r    s    zModelChoiceField.validatec             C   sO   | d  k	 r | n d } | d  k	 r* | n d } t  |  j |   t  |  k S)Nr   )r   r	  )r[   r~   rC   Zinitial_valueZ
data_valuer6   r6   r7   r     s    zModelChoiceField.has_changed)r^   r_   r`   r   r   r  r]   rw   r  r  r  propertyrv   r
  r  r	   Z_set_choicesr  r	  r   r  r   r6   r6   )ru   r7   r   `  s$   
c                   s   e  Z d  Z d Z e Z e Z i e d  d 6e d  d 6e d  d 6Z	 d d	 d	 d	 d
   f d d  Z
 d d   Z d d   Z d d   Z   f d d   Z d d   Z   S)r   z9A MultipleChoiceField whose choices are a model QuerySet.zEnter a list of values.rB   zESelect a valid choice. %(value)s is not one of the available choices.r   z0"%(pk)s" is not a valid value for a primary key.invalid_pk_valueTNr   c       	   
      s2   t  t |   j | d  | | | | | | |  d  S)N)rh   r   r]   )	r[   rv   r   rF   rG   r~   rH   r   rY   )ru   r6   r7   r]     s    z!ModelMultipleChoiceField.__init__c             C   s   | s
 g  St  |  j |   S)N)rB   _check_values)r[   rd   r6   r6   r7   r     s    z"ModelMultipleChoiceField.to_pythonc             C   s   |  j  r, | r, t |  j d d d  n |  j  rJ | rJ |  j j   St | t t f  s{ t |  j d d d  n  |  j |  } |  j	 |  | S)Nr   r   rB   )
r   r   rI   rv   r   r,   rB   r   r  Zrun_validators)r[   rd   rD   r6   r6   r7   r     s    zModelMultipleChoiceField.cleanc                sF  |  j  p d   y t |  } Wn+ t k
 rL t |  j d d d  Yn Xxj | D]b } y |  j j i |   6  WqT t t f k
 r t |  j d d d d i | d 6 YqT XqT W|  j j i | d   6  } t   f d d   | D  } xI | D]A } t	 |  | k r t |  j d	 d d	 d i | d
 6 q q W| S)z
        Given a list of possible PK values, returns a QuerySet of the
        corresponding objects. Raises a ValidationError if a given value is
        invalid (not a valid PK, not in the queryset, etc.)
        r:   rB   r   r  paramsz%s__inc             3   s$   |  ] } t  t |     Vq d  S)N)r   rA   )r;   r   )r  r6   r7   r     s    z9ModelMultipleChoiceField._check_values.<locals>.<genexpr>r   rd   )
r  	frozensetrQ   r   rI   rv   r   rx   rl   r   )r[   rd   r:   rD   Zpksvalr6   )r  r7   r    s0    z&ModelMultipleChoiceField._check_valuesc                sb   t  | d  rL t | t j  rL t  | d  rL    f d d   | D St t    j |  S)Nr  r(   c                s(   g  |  ] } t  t   j |   q Sr6   )rh   r   r	  )r;   rf   )ru   r[   r6   r7   r=     s   	 z:ModelMultipleChoiceField.prepare_value.<locals>.<listcomp>)rz   r,   r   r   rh   r   r	  )r[   rd   )ru   )r[   r7   r	    s
    z&ModelMultipleChoiceField.prepare_valuec             C   s   | d  k r g  } n  | d  k r* g  } n  t  |  t  |  k rF d St d d   |  j |  D  } t d d   | D  } | | k S)NTc             s   s   |  ] } t  |  Vq d  S)N)r   )r;   rd   r6   r6   r7   r     s    z7ModelMultipleChoiceField.has_changed.<locals>.<genexpr>c             s   s   |  ] } t  |  Vq d  S)N)r   )r;   rd   r6   r6   r7   r      s    )r   rl   r	  )r[   r~   rC   Zinitial_setZdata_setr6   r6   r7   r     s    		"z$ModelMultipleChoiceField.has_changed)r^   r_   r`   r   r   rF   r   Zhidden_widgetr   r  r]   r   r   r  r	  r   r6   r6   )ru   r7   r     s   	%c             C   s=   |  d  k	 o< t  |  d  o< |  j j d  k	 p< |  j j d  k	 S)Nr(   )rz   r(   r*   r3   )rJ   r6   r6   r7   modelform_defines_fields$  s    r  )=r   
__future__r   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.utilsr   Zdjango.utils.encodingr   r   Zdjango.utils.textr   r   Zdjango.utils.translationr   r   r   r&   r    r8   r   r   r   rZ   ra   r   with_metaclassr   r%   r!   r"   r#   r   r$   r   r  r   r   r  r6   r6   r6   r7   <module>   sl   "		")		MA"			Q 			e6			&)jZ