
O'^c           @   s  d  d l  Z  d  d l m Z m Z d  d l m Z d  d l m Z d  d l Z d  d l m	 Z	 m
 Z
 m Z d  d l m Z d  d l m Z m Z m Z m Z m Z d e f d	     YZ d
 e f d     YZ d e f d     YZ d e f d     YZ d e e e  f d     YZ d S(   iN(   t   fieldst
   ImageField(   t   related(   t   with_metaclass(   t   constraintst
   generatorst   signals(   t   Values(   t   OrderedDictt   get_GenericRelationt   get_remote_fieldt   get_remote_field_tot   getargnamest   CreateInstanceErrorc           B   s   e  Z RS(    (   t   __name__t
   __module__(    (    (    s2   /tmp/pip-unpacked-wheel-K50F1D/autofixture/base.pyR      s   t   Linkc           B   sA   e  Z d  Z d d d  Z d   Z d   Z d   Z d   Z RS(   sQ   
    Handles logic of following or generating foreignkeys and m2m relations.
    c         C   s   i  |  _  i  |  _ | |  _ | p$ i  } | t k rC i d  d 6} n  t | t  sz t g  | D] } | d  f ^ q\  } n  xp | j   D]b \ } } y5 | j d d  \ } } | |  j j	 | i   | <Wq t
 k
 r | |  j  | <q Xq Wd  S(   Nt   ALLt   __i   (   R    t	   subfieldst   defaultt   Truet   Nonet
   isinstancet   dictt   itemst   splitt
   setdefaultt
   ValueError(   t   selfR    R   t   vt   fieldt   valuet	   fieldnamet   subfield(    (    s2   /tmp/pip-unpacked-wheel-K50F1D/autofixture/base.pyt   __init__   s    			(c         C   s%   |  j  j | |  j  j d |  j   S(   NR   (   R    t   getR   (   R   t   key(    (    s2   /tmp/pip-unpacked-wheel-K50F1D/autofixture/base.pyt   __getitem__,   s    c         c   sL   x |  j  D] } | Vq
 Wx, |  j j   D] \ } } d | | f Vq) Wd  S(   Ns   %s__%s(   R    R   R   (   R   R   R%   R    (    (    s2   /tmp/pip-unpacked-wheel-K50F1D/autofixture/base.pyt   __iter__0   s    	c         C   s*   d |  j  k r t S| |  j  k r& t St S(   NR   (   R    R   t   False(   R   R    (    (    s2   /tmp/pip-unpacked-wheel-K50F1D/autofixture/base.pyt   __contains__6   s
    c         C   sn   d |  j  k r& i |  j  d d 6} n5 |  j j | i   } d | k r[ i | d d 6} n  t | d |  j S(   NR   R   (   R    R   R$   R   R   (   R   R   R    (    (    s2   /tmp/pip-unpacked-wheel-K50F1D/autofixture/base.pyt   get_deep_links=   s    N(	   R   R   t   __doc__R   R#   R&   R'   R)   R*   (    (    (    s2   /tmp/pip-unpacked-wheel-K50F1D/autofixture/base.pyR      s   			t   AutoFixtureMetaclassc           B   s   e  Z d    Z RS(   c         C   s   t    } x( | d  d  d  D] } | | j 7} q W| t  | j d i    7} | | j d t     7} | | d <t t |   j |  | | |  S(   NiR   t   field_values(   R   R-   t   popR$   t   superR,   t   __new__(   t   mcst   namet   basest   attrst   valuest   base(    (    s2   /tmp/pip-unpacked-wheel-K50F1D/autofixture/base.pyR0   H   s    	
(   R   R   R0   (    (    (    s2   /tmp/pip-unpacked-wheel-K50F1D/autofixture/base.pyR,   G   s   t   AutoFixtureBasec           B   s-  e  Z d  Z d e f d     YZ e Z e Z e Z	 i d d 6Z
 e Z d Z d Z e e j e j f e j e j f e j e j f e j e j f e j e j f e j e j f e j e j f e j e j  f e j! e j" f e j# e j$ f e j% e j$ f e j& e j' f e j( e j) f e* e j+ f f  Z, e- e d  rPe j. e, e j/ <n  e0   Z1 e2 j3 e2 j4 g Z5 d d d d d d d d d	  Z7 d
   Z8 d   Z9 d   Z: d   Z; d   Z< d   Z= d   Z> d   Z? d   Z@ d   ZA d   ZB d   ZC e d  ZD d e d  ZE d e d  ZF d   ZG RS(   s   
    .. We don't support the following fields yet:

        * ``XMLField``
        * ``FileField``

        Patches are welcome.
    t   IGNORE_FIELDc           B   s   e  Z RS(    (   R   R   (    (    (    s2   /tmp/pip-unpacked-wheel-K50F1D/autofixture/base.pyR8   [   s   i   i   R   g?i  t	   UUIDFieldc
         C   s  | |  _  t |  j j  |  _ |  j t |  7_ | p< g  |  _ | d k	 rZ | |  _ n  | d k	 rr | |  _ n  | d k	 r | |  _ n  t	 |  j t
  s t
 |  j  |  _ n  | d k	 r | |  _ n  t	 |  j t
  s t
 |  j  |  _ n  | d k	 rBt	 | t  s6| r't
 i | d 6 } q6t
 t  } n  | |  _ n  t	 |  j t
  sit
 |  j  |  _ n  |	 d k	 rt	 |	 t  s|	 rt
 i |	 d 6 }	 qt
 t  }	 n  |	 |  _ n  t	 |  j t
  st
 |  j  |  _ n  x |  j D] }
 |  j |
  qWi  |  _ |  j   d S(   s  
        Parameters:
            ``model``: A model class which is used to create the test data.

            ``field_values``: A dictionary with field names of ``model`` as
            keys. Values may be static values that are assigned to the field,
            a ``Generator`` instance that generates a value on the fly or a
            callable which takes no arguments and returns the wanted value.

            ``none_p``: The chance (between 0 and 1, 1 equals 100%) to
            assign ``None`` to nullable fields.

            ``overwrite_defaults``: All default values of fields are preserved
            by default. If set to ``True``, default values will be treated
            like any other field.

            ``constraints``: A list of callables. The constraints are used to
            verify if the created model instance may be used. The callable
            gets the actual model as first and the instance as second
            parameter. The instance is not populated yet at this moment.  The
            callable may raise an :exc:`InvalidConstraint` exception to
            indicate which fields violate the constraint.

            ``follow_fk``: A boolean value indicating if foreign keys should be
            set to random, already existing, instances of the related model.

            ``generate_fk``: A boolean which indicates if related models should
            also be created with random values. The *follow_fk* parameter will
            be ignored if *generate_fk* is set to ``True``.

            ``follow_m2m``: A tuple containing minium and maximum of model
            instances that are assigned to ``ManyToManyField``. No new
            instances will be created. Default is (1, 5).  You can ignore
            ``ManyToManyField`` fields by setting this parameter to ``False``.

            ``generate_m2m``: A tuple containing minimum and maximum number of
            model instance that are newly created and assigned to the
            ``ManyToManyField``. Default is ``False`` which disables the
            generation of new related instances. The value of ``follow_m2m``
            will be ignored if this parameter is set.
        R   N(   t   modelR   t	   __class__R-   R   R   t   none_pt   overwrite_defaultst	   follow_fkR   R   t   generate_fkR   R(   t
   follow_m2mt   generate_m2mt   default_constraintst   add_constraintt   _field_generatorst   prepare_class(   R   R:   R-   R<   R=   R   R>   R?   R@   RA   t
   constraint(    (    s2   /tmp/pip-unpacked-wheel-K50F1D/autofixture/base.pyR#      sH    -		c         C   s   d S(   sq   
        This method is called after the :meth:`__init__` method. It has no
        semantic by default.
        N(    (   R   (    (    s2   /tmp/pip-unpacked-wheel-K50F1D/autofixture/base.pyRE      s    c         C   s   | |  j  | <d S(   s   
        Pass a *value* that should be assigned to the field called *name*.
        Thats the same as specifying it in the *field_values* argument of the
        :meth:`constructor <autofixture.base.AutoFixture.__init__>`.
        N(   R-   (   R   R2   R    (    (    s2   /tmp/pip-unpacked-wheel-K50F1D/autofixture/base.pyt   add_field_value   s    c         C   s   |  j  j |  d S(   s8   
        Add a *constraint* to the autofixture.
        N(   R   t   append(   R   RF   (    (    s2   /tmp/pip-unpacked-wheel-K50F1D/autofixture/base.pyRC      s    c         C   s1   t  | t j  o0 | j o0 t | j t |   S(   s   
        Checks if the field is the automatically created OneToOneField used by
        django mulit-table inheritance
        (   R   R   t   OneToOneFieldt   primary_keyt
   issubclassR:   R   (   R   R   (    (    s2   /tmp/pip-unpacked-wheel-K50F1D/autofixture/base.pyt   is_inheritance_parent   s    	c   
   
   C   s  t  | t j  r d S|  j |  r) d S| j t j k	 r[ |  j r[ | j |  j	 k r[ d Si  } | j |  j	 k r |  j	 | j } t  | t
 j  r | St  | t  r t
 j d |  St |  r t
 j d |  St
 j d |  S| j r |  j | d <n  | j rt
 j d | j |  St  | t j  rt |    j |  j k } | j |  j k r| rt
 j t j t |  d |  j j | j  d |  j j | j  d t |  j  S| j |  j k rt
 j! t |  d t |  j  } | j"   d k	 r| Sn  | j# s| j r"t
 j$   S| rp| j rpt% d | j d	 t |  j& j' t |  j& j( f f   n  t% d
 | j d	 t |  j& j' t |  j& j( f f   n  t  | t j)  r| j |  j* k r"|  j* | j \ } } t
 j+ t j t |   d t |  j  d | d | | S| j |  j, k r{|  j, | j \ } } t
 j! t |  d t |  j  d | d | | S| j# s| j rt
 j g   St% d d	 t |  j& j' t |  j& j( f | j f   n  t  | t j-  rt
 j. d | j/ d | j0 d | j1 d | j2 |  St  | t j3  rt  | t j4  rKt
 j5 } n t  | t j6  ryt
 j7 d t8 | j2 d   St  | t j9  rt
 j: d t8 | j2 d   S| j2 d k rt
 j; d t< d | j2  St
 j= } | d | j2  St  | t j>  rt
 j? d | j@ d | jA  StB t d  r^t  | t jC  r^t
 jD d | jE d d | jE |  Sn  t  | tF  rt
 jG d | jH |  Sx6 |  jI jJ   D]% \ }	 } t  | |	  r| |   SqWd S(   s   
        Return a value generator based on the field instance that is passed to
        this method. This function may return ``None`` which means that the
        specified field will be ignored (e.g. if no matching generator was
        found).
        t   autofixtureR    t   empty_pt   choicesR>   R?   t   limit_choices_touD   Cannot resolve self referencing field "%s" to "%s" without null=Trues   %s.%su_   Cannot resolve ForeignKey "%s" to "%s". Provide either "follow_fk" or "generate_fk" parameters.t	   min_countt	   max_countup   Cannot assign instances of "%s" to ManyToManyField "%s". Provide either "follow_m2m" or "generate_m2m" argument.t   patht   matcht	   recursivet
   max_lengthi   i   i   t   commont   decimal_placest
   max_digitst   BigIntegerFieldt	   min_valuei   t	   max_valuet   storageN(K   R   R    t	   AutoFieldR   RL   R   t   NOT_PROVIDEDR=   R2   R-   R   t	   Generatort   AutoFixturet   InstanceGeneratort   callablet   CallableGeneratort   StaticGeneratort   nullR<   RO   t   ChoicesGeneratorR   t
   ForeignKeyR   R;   R:   R?   RM   R$   R>   R*   R
   RP   t   InstanceSelectort	   get_valuet   blankt   NoneGeneratorR   t   _metat	   app_labelt   object_namet   ManyToManyFieldRA   t   MultipleInstanceGeneratorR@   t   FilePathFieldt   FilePathGeneratorRS   RT   RU   RV   t	   CharFieldt	   SlugFieldt   SlugGeneratort
   EmailFieldt   EmailGeneratort   mint   URLFieldt   URLGeneratort   LoremSentenceGeneratorR(   t   StringGeneratort   DecimalFieldt   DecimalGeneratorRX   RY   t   hasattrRZ   t   IntegerGeneratort
   MAX_BIGINTR   t   ImageGeneratorR]   t   field_to_generatorR   (
   R   R   t   kwargsR    t
   is_self_fkt   selectedRQ   RR   t	   generatort   field_class(    (    s2   /tmp/pip-unpacked-wheel-K50F1D/autofixture/base.pyt   get_generator   s    
		
  				
			
	
c         C   sU   | |  j  k r( |  j |  |  j  | <n  |  j  | } | d k rH |  j S|   } | S(   sd   
        Return a random value that can be assigned to the passed *field*
        instance.
        N(   RD   R   R   R8   (   R   R   R   R    (    (    s2   /tmp/pip-unpacked-wheel-K50F1D/autofixture/base.pyRj     s    	c         C   s9   |  j  |  } | |  j k r" d  St | | j |  d  S(   N(   Rj   R8   t   setattrR2   (   R   t   instanceR   R    (    (    s2   /tmp/pip-unpacked-wheel-K50F1D/autofixture/base.pyt   process_field  s    c      	   C   s  t  } t |  j } | j j } | r7 |  j | |  Si  } | j |  j k rg  | j j D]6 } t	 | t
 j  r\ t |  t |  k r\ | ^ q\ } g  | j j D]3 } t	 | t
 j  r t |  |  j k r | ^ q } t |  d k s t  t |  d k st  | d }	 | d }
 |  j | j \ } } t j t | d i | |
 j 6t j t j t |    |	 j 6d | d | | j   } n  d  S(   Ni   i    R-   RQ   RR   (   R(   R
   t   throughRm   t   auto_createdR   R2   RA   R    R   R   Rh   R   R:   t   lent   AssertionErrorR   Rq   Ra   Rb   RM   R$   t   generate(   R   R   R   t   auto_created_through_modelR   R   t   fkt   related_fkst   self_fkst
   related_fkt   self_fkRQ   RR   t   intermediary_model(    (    s2   /tmp/pip-unpacked-wheel-K50F1D/autofixture/base.pyt   process_m2m  s<    $!


"c         O   s   t  d   d  S(   Nsp   This method was renamed recently, since it contains a typo. Please use the check_constraints method from now on.(   t	   TypeError(   R   t   argsR   (    (    s2   /tmp/pip-unpacked-wheel-K50F1D/autofixture/base.pyt   check_constrains  s    c         C   s[   g  } xN |  j  D]C } y | |  j |  Wq t  j k
 rR } | j | j  q Xq W| S(   s=   
        Return fieldnames which need recalculation.
        (   R   R:   t   InvalidConstraintt   extendR    (   R   R   t   recalc_fieldsRF   t   e(    (    s2   /tmp/pip-unpacked-wheel-K50F1D/autofixture/base.pyt   check_constraints  s    c         C   s   | S(   s  
        Overwrite this method to modify the created *instance* before it gets
        returned by the :meth:`create` or :meth:`create_one`.
        It gets the generated *instance* and must return the modified
        instance. The *commit* parameter indicates the *commit* value that the
        user passed into the :meth:`create` method. It defaults to ``True``
        and should be respected, which means if it is set to ``False``, the
        *instance* should not be saved.
        (    (   R   R   t   commit(    (    s2   /tmp/pip-unpacked-wheel-K50F1D/autofixture/base.pyt   post_process_instance  s    
c         C   s   | S(   st   
        Same as :meth:`post_process_instance`, but it is being called before
        saving an *instance*.
        (    (   R   R   (    (    s2   /tmp/pip-unpacked-wheel-K50F1D/autofixture/base.pyt   pre_process_instance  s    c   	   	   C   s  |  j  } |  j   } | j j } xP | rs | d k rs x | D] } |  j | |  q= W|  j |  } | d 8} q$ W| d k r t d d |  j j j |  j j j f |  j  d j	 g  | D] } | j
 ^ q  f   n  |  j |  } | rN| j   g  | j j D] } t | t    s| ^ q} x! | D] } |  j | |  q1Wn  t j j d |  d |  j d | d	 |  i  } d
 t |  j  k r| | d
 <n t j d t  |  j | |  S(   s)  
        Create and return one model instance. If *commit* is ``False`` the
        instance will not be saved and many to many relations will not be
        processed.

        Subclasses that override ``create_one`` can specify arbitrary keyword
        arguments. They will be passed through by the
        :meth:`autofixture.base.AutoFixture.create` method and the helper
        functions :func:`autofixture.create` and
        :func:`autofixture.create_one`.

        May raise :exc:`CreateInstanceError` if constraints are not satisfied.
        i    i   u   Cannot solve constraints for "%s", tried %d times. Please check value generators or model constraints. At least the following fields are involved: %ss   %s.%ss   , t   senderR:   R   t	   committedR   s_   Subclasses of AutoFixture need to provide a `commit` argument for post_process_instance methods(   t   triesR:   Rm   R    R   R   R   Rn   Ro   t   joinR2   R   t   savet   many_to_manyR   R	   R   R   t   instance_createdt   sendR   R   t   warningst   warnt   DeprecationWarning(	   R   R   R   R   t   processR   t   fR   t   post_process_kwargs(    (    s2   /tmp/pip-unpacked-wheel-K50F1D/autofixture/base.pyt
   create_one  sF    	/
	c         K   sC   g  } x6 t  |  D]( } |  j d | |  } | j |  q W| S(   sU  
        Create and return ``count`` model instances. If *commit* is ``False``
        the instances will not be saved and many to many relations will not be
        processed.

        May raise ``CreateInstanceError`` if constraints are not satisfied.

        The method internally calls :meth:`create_one` to generate instances.
        R   (   t   rangeR   RH   (   R   t   countR   R   t   object_listt   iR   (    (    s2   /tmp/pip-unpacked-wheel-K50F1D/autofixture/base.pyt   create  s
    
c         c   s,   x% t  |  D] } |  j d |  Vq Wd  S(   NR   (   R   R   (   R   R   R   R   (    (    s2   /tmp/pip-unpacked-wheel-K50F1D/autofixture/base.pyt   iter.  s    c         c   s   |  j    Vd  S(   N(   R   (   R   (    (    s2   /tmp/pip-unpacked-wheel-K50F1D/autofixture/base.pyR'   2  s    (   i   i   N(H   R   R   R+   t   objectR8   R(   R=   R   R>   R?   R@   RA   R<   R   R   R    t   BooleanFieldR   t   BooleanGeneratort   NullBooleanFieldt   NullBooleanGeneratort   DateTimeFieldt   DateTimeGeneratort	   DateFieldt   DateGeneratort   PositiveSmallIntegerFieldt   PositiveSmallIntegerGeneratort   PositiveIntegerFieldt   PositiveIntegerGeneratort   SmallIntegerFieldt   SmallIntegerGeneratort   IntegerFieldR   t
   FloatFieldt   FloatGeneratort   IPAddressFieldt   IPAddressGeneratort   GenericIPAddressFieldt	   TextFieldt   LoremGeneratort	   TimeFieldt   TimeGeneratorR   R   R   R   t   UUIDGeneratorR9   R   R-   R   t   unique_constraintt   unique_together_constraintRB   R   R#   RE   RG   RC   RL   R   Rj   R   R   R   R   R   R   R   R   R   R'   (    (    (    s2   /tmp/pip-unpacked-wheel-K50F1D/autofixture/base.pyR7   R   s`   			X								)				<Ra   c           B   s   e  Z RS(    (   R   R   (    (    (    s2   /tmp/pip-unpacked-wheel-K50F1D/autofixture/base.pyRa   6  s   (   R   t   django.db.modelsR    R   t   django.db.models.fieldsR   t   django.utils.sixR   RM   R   R   R   t   autofixture.valuesR   t   autofixture.compatR   R	   R
   R   R   t	   ExceptionR   R   R   t   typeR,   R7   Ra   (    (    (    s2   /tmp/pip-unpacked-wheel-K50F1D/autofixture/base.pyt   <module>   s   (	0 