ó
O'—^c           @   sÙ   d  Z  d d l Z d d l m Z d d l m Z d d l m Z m Z d d l	 Z	 d d l	 m
 Z
 d d l m Z d d	 l m Z d d
 l m Z e j d k  r¶ d d l m Z n	 d „  Z d e f d „  ƒ  YZ d S(   sJ  
Use the ``loadtestdata`` command like this::

    django-admin.py loadtestdata [options] app.Model:# [app.Model:# ...]

Its nearly self explanatory. Supply names of models, prefixed with their app
name. After that, place a colon and tell the command how many objects you want
to create. Here is an example of how to create three categories and twenty
entries for you blogging app::

    django-admin.py loadtestdata blog.Category:3 blog.Entry:20

Voila! You have ready to use testing data populated to your database. The
model fields are filled with data by producing randomly generated values
depending on the type of the field. E.g. text fields are filled with lorem
ipsum dummies, date fields are populated with random dates from the last
years etc.

There are a few command line options available. Mainly to control the
behavior of related fields. If foreingkey or many to many fields should be
populated with existing data or if the related models are also generated on
the fly. Please have a look at the help page of the command for more
information::

    django-admin.py help loadtestdata
iÿÿÿÿN(   t
   smart_text(   t   models(   t   BaseCommandt   CommandError(   t   signalsi   (   t   atomic(   t	   importlib(   t	   get_modeli   i	   (   t   make_optionc          O   s   i |  d 6| d 6S(   Nt   argst   kwargs(    (   R	   R
   (    (    sN   /tmp/pip-unpacked-wheel-K50F1D/autofixture/management/commands/loadtestdata.pyR   )   s    t   Commandc           B   sG   e  Z d  Z d Z d „  Z d „  Z d „  Z d „  Z e d „  ƒ Z	 RS(   s3   Create random model instances for testing purposes.s   app.Model:# [app.Model:# ...]c         O   ss  t  d d d d d d d d  d d	 d
 ƒt  d d d d d d d  d d ƒt  d d d d d d d  d d d d d ƒt  d d d d d d d  d d ƒt  d d d d d d d  d d d d d d ƒt  d d d d d  d d  d d! d" d# d$ d% d& ƒt  d' d( d d d d) d d* d d+ d, ƒf } t j d/ k  rMt j | |  _ n	 | |  _ t t |  ƒ j	 | | Ž  d  S(0   Ns   -ds   --overwrite-defaultst   actiont
   store_truet   destt   overwrite_defaultst   defaultt   helpuB   Generate values for fields with default values. Default is to use s   default values.s   --no-follow-fkt   no_follow_fku2   Ignore foreignkeys while creating model instances.s   --generate-fkt   storet   generate_fku@   Do not use already existing instances for ForeignKey relations. sG   Create new instances instead. You can specify a comma sperated list of sH   field names or ALL to indicate that all foreignkeys should be generated s   automatically.s   --no-follow-m2mt   no_follow_m2mu:   Ignore many to many fields while creating model instances.s   --follow-m2mt
   follow_m2muB   Specify minimum and maximum number of instances that are assigned sE   to  a m2m relation. Use two, colon separated numbers in the form of: sA   min,max. Default is 1,5.
You can limit following of many to many sG   relations to specific fields using the following format:
field1:min:maxs   ,field2:min:max ...s   --generate-m2mt   generate_m2mu?   Specify minimum and maximum number of instances that are newly sA   created and assigned to a m2m relation. Use two, colon separated sD   numbers in the form of: min:max. Default is to not generate many to sF   many related models automatically. You can select specific of many to sA   many fields which are automatically generated. Use the following s)   format:
field1:min:max,field2:min:max ...s   -us   --uset   uset    u?   Specify a autofixture subclass that is used to create the test s+   data. E.g. myapp.autofixtures.MyAutoFixturei   i	   (   i   i	   (
   R   t   Nonet   djangot   VERSIONR   t   option_listt   argument_paramst   superR   t   __init__(   t   selfR	   R
   t   params(    (    sN   /tmp/pip-unpacked-wheel-K50F1D/autofixture/management/commands/loadtestdata.pyR    1   s0    	c         C   sC   | j  d d d ƒx) |  j D] } | j  | d | d Ž  q Wd  S(   NR	   t   nargst   +R
   (   t   add_argumentR   (   R!   t   parsert   option(    (    sN   /tmp/pip-unpacked-wheel-K50F1D/autofixture/management/commands/loadtestdata.pyt   add_argumentsX   s    c         C   s8   t  | ƒ } t | ƒ d k r4 d j | d  ƒ } n  | S(   Ni2   u   {0} ...(   R    t   lent   format(   R!   t   objt   output(    (    sN   /tmp/pip-unpacked-wheel-K50F1D/autofixture/management/commands/loadtestdata.pyt   format_output]   s    c         K   sg  |  j  d k  r d  Sd j d j | j j | j j f ƒ t | j ƒ |  j | ƒ ƒ GH|  j  d k  rg d  Sxs | j j	 D]e } t
 | t j ƒ rt t | | j ƒ } t
 | t j ƒ rÙ d j | j | j |  j | ƒ ƒ GHqÙ qt qt Wxƒ | j j D]u } t | | j ƒ j ƒ  } | j ƒ  rê d j | j | j ƒ  ƒ GHx. | D]# } d j | j |  j | ƒ ƒ GHq5Wqê qê Wd  S(   Ni   u   {0}(pk={1}): {2}t   .i   s   |   {0} (pk={1}): {2}s   |   {0} (count={1}):s   |   |   (pk={0}): {1}(   t	   verbosityR*   t   joint   _metat	   app_labelt   object_nameR    t   pkR-   t   fieldst
   isinstanceR   t
   ForeignKeyt   getattrt   namet   Modelt   many_to_manyt   allt   existst   count(   R!   t   sendert   modelt   instanceR
   t   fieldR+   t   qs(    (    sN   /tmp/pip-unpacked-wheel-K50F1D/autofixture/management/commands/loadtestdata.pyt   print_instancec   s8    	c         O   s^  d  } | d d  k r d  } n t } | d d  k r.d  } | d r4y¼ | d j d ƒ } t | ƒ d k rµ | d j d ƒ d k rµ g  | d j d ƒ D] } t | ƒ ^ qš } nK i  } xB | D]: } | j d ƒ \ }	 }
 } t |
 ƒ t | ƒ f | |	 <qÂ WWq+t k
 r'd j | d ƒ } q+Xq4n t } | d	 d  k rMd  } n | d	 j d ƒ } d  } | d
 royÕ g  | d
 j d ƒ D] } | r‡| ^ q‡} t | ƒ d k rù| d j d ƒ d k rùg  | d j d ƒ D] } t | ƒ ^ qÞ} nK i  } xB | D]: } | j d ƒ \ }	 }
 } t |
 ƒ t | ƒ f | |	 <qWWqot k
 rkd j | d
 ƒ } qoXn  | r t d j | | j d d ƒ d ƒ ƒ ‚ n  | d } | rë| j d ƒ } t	 t
 j d j | d  ƒ ƒ | d ƒ } n  | d } t | d ƒ |  _ g  } x´ | D]¬ } y: | j d ƒ \ } } | j d ƒ \ } } t | ƒ } Wn& t k
 r}t d j | ƒ ƒ ‚ n Xt | | ƒ } | s®t d j | | ƒ ƒ ‚ n  | j | | f ƒ qWt j j |  j ƒ t j ƒ  i | d 6| d 6| d	 6| d 6| d
 6} xL | D]D \ } } | rC| | |  } | j | ƒ qt j | | |  qWd  S(   NR   R   R   t   ,i   i    t   :s   --follow-m2m={0}R   R   s   --generate-m2m={0}u^   Invalid option {0}
Expected: {1}=field:min:max,field2:min:max... (min and max must be numbers)t   =R   R.   iÿÿÿÿR   R/   uR   Invalid argument: {0}
Expected: app_label.ModelName:count (count must be a number)u   Unknown model: {0}.{1}t	   follow_fk(   R   t   Falset   splitR)   R>   t   intt
   ValueErrorR*   R   R8   R   t   import_moduleR0   R/   R   t   appendR   t   instance_createdt   connectRD   t   autofixturet   autodiscovert   create(   R!   t   attrst   optionst   error_optionRH   R   t   valuet   iRB   t   keyt   minvalt   maxvalR   R   t   vR   R   R   t   attrR2   t   model_labelR>   R@   R
   t   fixture(    (    sN   /tmp/pip-unpacked-wheel-K50F1D/autofixture/management/commands/loadtestdata.pyt   handle‚   s    	
+/$	
,+/$
,
	


(
   t   __name__t
   __module__R   R	   R    R(   R-   RD   R   R`   (    (    (    sN   /tmp/pip-unpacked-wheel-K50F1D/autofixture/management/commands/loadtestdata.pyR   -   s   	'			(   i   i	   (   t   __doc__R   t   django.utils.encodingR    t	   django.dbR   t   django.core.management.baseR   R   RQ   R   t   compatR   R   R   R   t   optparseR   R   (    (    (    sN   /tmp/pip-unpacked-wheel-K50F1D/autofixture/management/commands/loadtestdata.pyt   <module>   s   	