
 X                 @   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   sT  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  | j	   r |  | j j
 j j |  j |  j |  j | j   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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 r<   A/home/ubuntu/projects/ifolica/build/django/django/forms/models.pyconstruct_instance$   s*    		4r>   c             C   s   |  j  } i  } x t | j | j | j  D]j } t | d d  sI q+ n  | rd | j | k rd q+ n  | r | j | k r q+ n  | 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+   F)r(   r   concrete_fieldsprivate_fieldsmany_to_manygetattrr.   Zvalue_from_object)r8   r*   r9   r:   r2   r;   r<   r<   r=   r   K   s    	"c
                s  g  }
 g   |  j  } d d l m     f d d   | j D } xGt t | j | | j   D]'} t | d d  s | d k	 r` | j	 | k r`  d k s | j	  k r` t
 d | j	 |  j f   q` 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_| rl| j	 | k rld
 | d <n  | r| j	 | k r| | j	 | d <n  | r| j	 | k r| | j	 | d <n  | r| j	 | k r| | j	 | d <n  |	 r| j	 |	 k r|	 | j	 | d <n  | d k r.| j |   } n* t |  sIt d   n | | |  } | rw|
 j | j	 | f  q`  j | j	  q` Wt |
   | r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 Sr<   )r,   ).0r;   )
ModelFieldr<   r=   
<listcomp>   s   	 z$fields_for_model.<locals>.<listcomp>r+   FNzH'%s' cannot be specified for %s model form as it is a non-editable fieldr1   TZ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 Sr<   )get)rC   r;   )r9   
field_dictignoredr<   r=   rE      s   	 )r(   Zdjango.db.models.fieldsr
   r@   sortedr   r?   rA   rB   r.   r   __name__r    	formfieldcallable	TypeErrorr5   r   )modelr*   r9   widgetsformfield_callbacklocalized_fieldslabels
help_textsrH   field_classesZ
field_listr:   Zsortable_private_fieldsr;   kwargsrO   r<   )rD   r9   rK   rL   r=   r   d   sV    	%!%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*   r9   rS   rU   rV   rW   rH   rX   )
rB   rR   r*   r9   rS   rU   rV   rW   rH   rX   )selfoptionsr<   r<   r=   __init__   s    zModelFormOptions.__init__)rN   
__module____qualname__r]   r<   r<   r<   r=   rZ      s   rZ   c                   s"   e  Z d  Z   f d d   Z   S)ModelFormMetaclassc                sZ  d  } x? | D]7 } t  | d  r t  | j d  r | j j } Pq q W| j d |  } t t |   j |  | | |  } | t f k r | St t	 | d d    } | _
 xu d d d g D]d }	 t	 | |	  }
 t |
 t j  r |
 t k r d i | j d 6|	 d 6|
 d	 6} t |   q q W| j rD| j d  k r_| j d  k r_t d
 |   n  | j t k rzd  | _ n  t | j | j | j | j | | j | j | j | j | j 
 } d d   t j |  D } t |  t | j j    } | r1d } | d j  |  | j j f } t! |   n  | j" | j  n	 | j } | | _# | S)NMetarT   r*   r9   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 Sr<   r<   )rC   kvr<   r<   r=   rE      s   	 z.ModelFormMetaclass.__new__.<locals>.<listcomp>z&Unknown field(s) (%s) specified for %sz, )$hasattrra   rT   popsuperr`   __new__r   rZ   rB   r(   r,   r   string_typesr    rN   rQ   rR   r*   r9   r   r   rS   rU   rV   rW   rH   rX   	iteritemssetZdeclared_fieldskeysjoinr   updateZbase_fields)Zmcsr.   basesattrsZbase_formfield_callbackbrT   	new_classr:   rb   rc   msgr*   Znone_model_fieldsZmissing_fieldsmessage)	__class__r<   r=   ri      sV    !!
					zModelFormMetaclass.__new__)rN   r^   r_   ri   r<   r<   )rv   r=   r`      s   r`   c                   s   e  Z d  Z d d d d d e d 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                s8  |  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 | | | | | | | | d |
 xo |  j D]d } |  j | } t | d  r t | d  r | j   } | d  k	 r0| j j |  | _ q0q q Wd  S)Nz'ModelForm has no model class specified.Fuse_required_attributequerysetget_limit_choices_to)r(   rR   
ValueErrorr8   r   r*   r9   ro   _validate_uniquerh   r   r]   rf   ry   rx   Zcomplex_filter)r[   r2   r3   auto_idprefixinitialerror_classZlabel_suffixZempty_permittedr8   rw   r:   Zobject_data
field_namerO   limit_choices_to)rv   r<   r=   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
        )r8   r(   r*   r.   r5   r9   _errorsrm   r)   rJ   Zblankrequiredempty_values)r[   r9   r;   r0   Z
form_fieldZfield_valuer<   r<   r=   _get_validation_exclusions.  s     		#z(BaseModelForm._get_validation_exclusionsc             C   s   d |  _  |  j S)NT)r{   r)   )r[   r<   r<   r=   cleanX  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(   rf   r   r   itemsrH   r*   r,   r   coderu   Z	add_error)r[   errorsr:   r   r0   messagesrH   ru   r<   r<   r=   _update_errors\  s     	zBaseModelForm._update_errorsc             C   s  |  j  } |  j   } x< |  j j   D]+ \ } } t | t  r% | j |  q% q% Wy% t |  |  j | j | j	  |  _ Wn2 t
 k
 r } z |  j |  WYd  d  } ~ Xn Xy |  j j d | d d  Wn2 t
 k
 r } z |  j |  WYd  d  } ~ Xn X|  j r|  j   n  d  S)Nr9   validate_uniqueF)r(   r   r*   r   r,   InlineForeignKeyFieldr5   r>   r8   r9   r   r   Z
full_cleanr{   r   )r[   r:   r9   r.   r0   er<   r<   r=   _post_cleanx  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.
        r9   N)r   r8   r   r   r   )r[   r9   r   r<   r<   r=   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.
        r6   N)r)   r(   r9   r*   r8   r   rA   r@   rf   r.   r6   )r[   r)   r9   r*   r:   r;   r<   r<   r=   	_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   rz   r8   r(   Zobject_name_stateaddingsaver   save_m2m)r[   commitr<   r<   r=   r     s    	%zBaseModelForm.save)rN   r^   r_   r   r]   r   r   r   r   r   r   r   alters_datar<   r<   )rv   r=   r     s   	*c               @   s   e  Z d  Z d S)r   N)rN   r^   r_   r<   r<   r<   r=   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  | |  } | r/t |  | _ n  |  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*   r9   rS   rU   rV   rW   rH   rX   ra   ZFormrT   zZCalling modelform_factory without defining 'fields' or 'exclude' explicitly is prohibited.)
objectrf   ra   typestrstaticmethodrT   rN   rB   r   )rR   r7   r*   r9   rT   rS   rU   rV   rW   rH   rX   rq   parentra   
class_nameZform_class_attrsr<   r<   r=   r%     s@    %	
c                   s?  e  Z d  Z d Z d Z e   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~   r2   r3   r|   r}   )rx   rg   initial_extraro   rh   r!   r]   )r[   r2   r3   r|   r}   rx   rY   defaults)rv   r<   r=   r]   2  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.)r2   r3   lenget_querysetrh   r!   initial_form_count)r[   )rv   r<   r=   r   :  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 Sr<   )pk)rC   or<   r<   r=   
<dictcomp>B  s   	 z5BaseModelFormSet._existing_object.<locals>.<dictcomp>)rf   r   r   rJ   )r[   r   r<   r<   r=   _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[   r0   r<   r<   r=   _get_to_pythonE  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-%sr8   r~   )is_boundr   r4   rR   r(   r   r.   r2   r   r   r   r   
IndexErrorrh   r!   _construct_form)r[   irY   Zpk_keyr   pk_fieldr   )rv   r<   r=   r   N  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)rf   rx   rR   _default_managerr   orderedZorder_byr(   r   r.   r   )r[   qsr<   r<   r=   r   `  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[   r7   r   r<   r<   r=   save_news  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[   r7   r8   r   r<   r<   r=   save_existingw  s    zBaseModelFormSet.save_existingc             C   s   | r | j    n  d S)z#Deletes an existing model instance.N)delete)r[   objr   r<   r<   r=   delete_existing{  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   )r7   )r[   r<   r=   r     s    z'BaseModelFormSet.save.<locals>.save_m2m)r   r   save_existing_objectssave_new_objects)r[   r   r   r<   )r[   r=   r     s
    	zBaseModelFormSet.savec             C   s   |  j    d  S)N)r   )r[   r<   r<   r=   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 rW  j | =qWqWWn  |
 j |  q q Wq Wx(| D] } t    }
 | \ } } } } x | D]     j r  j | d  k	 r  j | d  k	 r| d k r0  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 Sr<   )Zis_valid)rC   r7   )forms_to_deleter<   r=   rE     s   	 z4BaseModelFormSet.validate_unique.<locals>.<listcomp>r9   c             3   s@   |  ]6 } |   j  k r |  j k r- | n
   j  | Vq d  S)N)r)   unique_fields)rC   r0   )r7   r[   r<   r=   	<genexpr>  s   z3BaseModelFormSet.validate_unique.<locals>.<genexpr>c             s   s0   |  ]& } t  | d   r$ | j   n | Vq d S)_get_pk_valN)rf   r   )rC   dr<   r<   r=   r     s   date)rl   deleted_formsZformsr   r8   Z_get_unique_checksuniontupler5   get_unique_error_messager   get_form_errorr   r   r)   addyearmonthdayrB   get_date_error_messager   )r[   Zall_unique_checksZall_date_checksZvalid_formsr9   Zunique_checksZdate_checksr   Zuclassunique_checkZ	seen_dataZrow_datar0   
date_checklookupZ
unique_forr   Z	date_datar2   r<   )r7   r   r[   r=   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   r0   zFPlease correct the duplicate data for %(field)s, which must be unique.and)r   r   r   r   	text_type_)r[   r   r<   r<   r=   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   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  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   r8   r   r5   r   has_changedZchanged_datar   r   )r[   r   Zsaved_instancesr   r7   r   r<   r<   r=   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_formr5   r   r   )r[   r   r7   r<   r<   r=   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_editabler<   r=   r     s    "z7BaseModelFormSet.add_fields.<locals>.pk_is_not_editableNr~   r   Fr1   )django.db.modelsr-   r   r   rR   r(   r   	_pk_fieldr.   r*   r   r8   r   r   r   r   r,   r   r   ZusingdbrS   rJ   r   r   rh   r!   
add_fields)	r[   r7   indexr   r   r   pk_valuer   r1   )rv   )r-   r   r=   r     s*    	$
!.zBaseModelFormSet.add_fields)rN   r^   r_   __doc__rR   rl   r   r]   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r<   r<   )rv   r=   r!   )  s.   			E

r   Fc             C   s   t  | d d  } t  | d |  d k rQ t  | d |	  d k rQ 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.
    ra   Nr*   r9   z]Calling modelformset_factory without defining 'fields' or 'exclude' explicitly is prohibited.r7   rT   rS   rU   rV   rW   rH   rX   extramin_nummax_num	can_orderr   validate_minvalidate_max)rB   r   r%   r   rR   )rR   r7   rT   formsetr   r   r   r   r*   r9   rS   r   rU   rV   rW   rH   r   r   rX   metaFormSetr<   r<   r=   r"   A  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       	         sQ  | 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	   } |  j  j h |  _
 t t |   j | | d | d | | |  j j j rM|  j  j |  j j j k rMt |  j j j t  r.t |  j j j  |  j j _ n  |  j j j j |  j  j  n  d  S)Nr}   rx   )fkr   rR   r8   save_as_newr   r   filterr.   noner   rh   r#   r]   r7   r(   r*   r,   r   listr5   )	r[   r2   r3   r8   r   r}   rx   rY   r   )rv   r<   r=   r]   b  s     		"
*!zBaseInlineFormSet.__init__c                s    |  j  r d St t |   j   S)Nr   )r   rh   r#   r   )r[   )rv   r<   r=   r   z  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   r2   r4   r   r.   r   r8   r   r   r   rR   r(   rB   setattrget_attname)r[   r   rY   r7   Zfk_value)rv   r<   r=   r     s    	'z!BaseInlineFormSet._construct_formc             C   s%   |  j  j j d |  j  j d d  S)NrR   + )r   r   Zget_accessor_namerR   replace)clsr<   r<   r=   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   r8   r   r.   r   rB   r   r   r   rf   r   )r[   r7   r   r   r   r<   r<   r=   r     s    %zBaseInlineFormSet.save_newc                sU  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 | <d  S)NTr   rF   to_field)rh   r#   r   r   r   r.   rB   r*   rJ   r   Zverbose_namer   r   rR   r(   r   r8   r   r   	get_fieldr/   r   Zattnamer   )r[   r7   r   r.   rY   r   )rv   r<   r=   r     s     .'zBaseInlineFormSet.add_fieldsc                s/     f d d   | D } t  t    j |  S)Nc                s(   g  |  ] } |   j  j k r |  q Sr<   )r   r.   )rC   r0   )r[   r<   r=   rE     s   	 z>BaseInlineFormSet.get_unique_error_message.<locals>.<listcomp>)rh   r#   r   )r[   r   )rv   )r[   r=   r     s    z*BaseInlineFormSet.get_unique_error_message)rN   r^   r_   r   r]   r   r   classmethodr   r   r   r   r<   r<   )rv   r=   r#   `  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 Sr<   )r.   )rC   r;   )fk_namer<   r=   rE     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 Sr<   )r,   r   rR   r(   get_parent_list)rC   r;   )r   parent_modelr<   r=   rE     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   rz   rF   )r   rR   r   Zcan_failr:   Zfks_to_parentr   r<   )r   r   r   r=   _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   r7   rT   r   r   r   r   r*   r9   r   r   rS   r   r   rU   rV   rW   rH   rX   )r   uniquer"   r   )r   rR   r7   r   r   r*   r9   r   r   r   r   rT   rS   r   rU   rV   rW   rH   r   r   rX   r   rY   r   r<   r<   r=   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   rB   r   rh   r   r]   )r[   r  argsrY   )rv   r<   r=   r]   3  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   rB   r   r   r   rH   )r[   rc   origr<   r<   r=   r   ?  s    		zInlineForeignKeyField.cleanc             C   s   d S)NFr<   )r[   r~   r2   r<   r<   r=   r   N  s    z!InlineForeignKeyField.has_changed)rN   r^   r_   r   r   r1   r   default_error_messagesr]   r   r   r<   r<   )rv   r=   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)r0   rx   )r[   r0   r<   r<   r=   r]   S  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   )r0   empty_labelrx   allZ_prefetch_related_lookupsiteratorchoice)r[   rx   r   r<   r<   r=   __iter__W  s    	zModelChoiceIterator.__iter__c             C   s)   t  |  j  |  j j d  k	 r$ d n d S)Nr   r   )r   rx   r0   r  )r[   r<   r<   r=   __len__a  s    zModelChoiceIterator.__len__c             C   s"   |  j  j |  |  j  j |  f S)N)r0   prepare_valuelabel_from_instance)r[   r   r<   r<   r=   r	  d  s    zModelChoiceIterator.choiceN)rN   r^   r_   r]   r
  r  r	  r<   r<   r<   r=   r  R  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]   rx   r   to_field_name)r[   rx   r  r   r1   rF   r~   rG   r  r   r  rY   r<   r<   r=   r]   q  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[   r<   r<   r=   ry     s    
z%ModelChoiceField.get_limit_choices_toc                s(   t  t |   j |  } | j | _ | S)N)rh   r	   __deepcopy__rx   )r[   memoresult)rv   r<   r=   r    s    zModelChoiceField.__deepcopy__c             C   s   |  j  S)N)r   )r[   r<   r<   r=   _get_queryset  s    zModelChoiceField._get_querysetc             C   s   | |  _  |  j |  j _ d  S)N)r   choicesr1   )r[   rx   r<   r<   r=   _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   r<   r<   r=   r    s    z$ModelChoiceField.label_from_instancec             C   s    t  |  d  r |  j St |   S)N_choices)rf   r  r  )r[   r<   r<   r=   _get_choices  s    	zModelChoiceField._get_choicesc                sH   t  | d  r2 |  j r( | j |  j  S| j Sn  t t |   j |  S)Nr(   )rf   r  Zserializable_valuer   rh   r   r  )r[   rc   )rv   r<   r=   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  rx   rJ   rz   rQ   rR   ZDoesNotExistr   rH   )r[   rc   keyr<   r<   r=   r     s    zModelChoiceField.to_pythonc             C   s   t  j |  |  S)N)r
   validate)r[   rc   r<   r<   r=   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~   r2   Zinitial_valueZ
data_valuer<   r<   r=   r     s    zModelChoiceField.has_changed)rN   r^   r_   r   r   r  r]   ry   r  r  r  propertyrx   r  r  r	   Z_set_choicesr  r  r   r  r   r<   r<   )rv   r=   r   h  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.r   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[   rx   r   r1   rF   r~   rG   r  rY   )rv   r<   r=   r]     s    z!ModelMultipleChoiceField.__init__c             C   s   | s
 g  St  |  j |   S)N)r   _check_values)r[   rc   r<   r<   r=   r     s    z"ModelMultipleChoiceField.to_pythonc             C   s   |  j  |  } |  j r; | r; t |  j d d d  n |  j rY | rY |  j j   St | t t f  s t |  j d d d  n  |  j	 |  } |  j
 |  | S)Nr   r   r   )r  r   r   rH   rx   r   r,   r   r   r  Zrun_validators)r[   rc   r   r<   r<   r=   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   r   r   r  paramsz%s__inc             3   s$   |  ] } t  t |     Vq d  S)N)r   rB   )rC   r   )r  r<   r=   r     s    z9ModelMultipleChoiceField._check_values.<locals>.<genexpr>r   rc   )
r  	frozensetrQ   r   rH   rx   r   rz   rl   r   )r[   rc   r   r   Zpksvalr<   )r  r=   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 Sr<   )rh   r   r  )rC   re   )rv   r[   r<   r=   rE      s   	 z:ModelMultipleChoiceField.prepare_value.<locals>.<listcomp>)rf   r,   r   r   rh   r   r  )r[   rc   )rv   )r[   r=   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   )rC   rc   r<   r<   r=   r   *  s    z7ModelMultipleChoiceField.has_changed.<locals>.<genexpr>c             s   s   |  ] } t  |  Vq d  S)N)r   )rC   rc   r<   r<   r=   r   +  s    )r   rl   r  )r[   r~   r2   Zinitial_setZdata_setr<   r<   r=   r   #  s    		"z$ModelMultipleChoiceField.has_changed)rN   r^   r_   r   r   r1   r   Zhidden_widgetr   r  r]   r   r   r  r  r   r<   r<   )rv   r=   r     s   	%c             C   s=   |  d  k	 o< t  |  d  o< |  j j d  k	 p< |  j j d  k	 S)Nr(   )rf   r(   r*   r9   )rI   r<   r<   r=   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    r>   r   r   r   rZ   r`   r   with_metaclassr   r%   r!   r"   r#   r   r$   r   r  r   r   r  r<   r<   r<   r=   <module>   sl   "		'		RG"			R 			f7			&)j]