
v^                  @   s   d  d l  Z  d  d l m Z d  d l m Z d  d l m Z m Z d  d l m	 Z	 d  d l
 m Z m Z Gd d   d e  Z Gd	 d
   d
 e  Z d S)    N)apps)serializers)BaseCommandCommandError)parse_apps_and_model_labels)DEFAULT_DB_ALIASrouterc               @   s   e  Z d  Z d S)ProxyModelWarningN)__name__
__module____qualname__ r   r   J/tmp/pip-build-8lau8j11/django/django/core/management/commands/dumpdata.pyr	   
   s   r	   c               @   s.   e  Z d  Z d Z d d   Z d d   Z d S)CommandzOutput the contents of the database as a fixture of the given format (using each model's default manager unless --all is specified).c          	   C   s  | j  d d d d d d d | j  d d	 d
 d d | j  d d t d d | j  d d	 t d d | j  d d d d d	 g  d d | j  d d d d d d d | j  d d d d d d d | j  d d d d d d  d d! | j  d" d d# d d$ | j  d% d& d d' d  S)(Nargsmetavarzapp_label[.ModelName]nargs*helpzHRestricts dumped data to the specified app_label or app_label.ModelName.z--formatdefaultjsonz7Specifies the output serialization format for fixtures.z--indenttypez>Specifies the indent level to use when pretty-printing output.z
--databasezXNominates a specific database to dump fixtures from. Defaults to the "default" database.z-ez	--excludeactionappendzhAn app_label or app_label.ModelName to exclude (use multiple --exclude to exclude multiple apps/models).z--natural-foreign
store_truedestuse_natural_foreign_keysz/Use natural foreign keys if they are available.z--natural-primaryuse_natural_primary_keysz/Use natural primary keys if they are available.z-az--alluse_base_managerzUse Django's base manager to dump all models stored in the database, including those that would otherwise be filtered or modified by a custom manager.z--pksprimary_keyszOnly dump objects with given primary keys. Accepts a comma-separated list of keys. This option only works when you specify one model.z-oz--outputz.Specifies file to which the output is written.)add_argumentintr   )selfparserr   r   r   add_arguments   s>    	zCommand.add_argumentsc          #      s=  | d } | d } | d  | d } | d } | d } | d } | d }	 | d	  | d
 }
 |
 r d d   |
 j  d  D  n g   t |  \   | s  r t d   t j  f d d   t j   D    nt |  d k r rt d   i    x| D]} y | j  d  \ } } y t j |  } Wn7 t	 k
 r} z t t
 |    WYd  d  } ~ Xn X| j d  k s|  k rwy | j |  } Wn( t	 k
 rt d | | f   Yn X  j | g   } | d  k	 r| | k r| j |  Wqt k
 r r6t d   | } y t j |  } Wn7 t	 k
 r} z t t
 |    WYd  d  } ~ Xn X| j d  k s|  k rwd    | <YqXqW| t j   k ry t j |  Wn t j k
 rYn Xt d |   d       f d d  } y d  |  j _ d  } d } | r|  j j   r| d d k r|  j } t | d d   } | rt | d  n d  } zD t j | |   d | d | d |	 d | p|  j d | d | Wd  | r| j   XWn> t k
 r8} z | r  t d  |   WYd  d  } ~ Xn Xd  S)!NformatindentZdatabaseexcludeoutput	tracebackr   r   r   r   c             S   s   g  |  ] } | j     q Sr   )strip).0pkr   r   r   
<listcomp>P   s   	 z"Command.handle.<locals>.<listcomp>,z,You can only use --pks option with one modelc             3   s0   |  ]& } | j  d  k	 r |   k r | Vq d  S)N)models_module)r+   
app_config)excluded_appsr   r   	<genexpr>Z   s    z!Command.handle.<locals>.<genexpr>   .zUnknown model: %s.%sz Unknown serialization format: %sFc             3   s  t  j   j    } x | D] } |  k r1 q | j j rl | j j | k rl t j d | j j d t	 | j j r t
 j  |  r  r | j } n	 | j } | j   j | j j j  }  r | j d   } |  r | j   j   Vq | j   Ed Hq Wd S)z
            Collate the objects to be serialized. If count_only is True, just
            count the number of objects to be serialized.
            z,%s is a proxy model and won't be serialized.categoryZpk__inN)r   Zsort_dependenciesitemsZ_metaproxyZproxy_for_modelwarningswarnlabelr	   r   Zallow_migrate_modelZ_base_managerZ_default_managerusingZorder_byr,   namefiltercountiterator)
count_onlymodelsmodelZobjectsZqueryset)app_listexcluded_modelsr   r   r;   r   r   get_objects   s$    	!z#Command.handle.<locals>.get_objectsr   	verbosityr@   Twstreamprogress_outputobject_countz Unable to serialize database: %s)splitr   r   dictfromkeysr   Zget_app_configslenZget_app_configLookupErrorstrr/   Z	get_model
setdefaultr   
ValueErrorr   Zget_public_serializer_formatsZget_serializerZSerializerDoesNotExiststdoutZendingisattysumopen	serializeclose	Exception)r"   Z
app_labelsoptionsr%   r&   Zexcludesr(   Zshow_tracebackr   r   Zpksr:   Z	app_labelZmodel_labelr0   erB   Zapp_list_valuerE   rI   rJ   rH   r   )rC   r1   rD   r   r   r;   r   handleC   s    









%%%!%	zCommand.handleN)r
   r   r   r   r$   r\   r   r   r   r   r      s   /r   )r8   Zdjango.appsr   Zdjango.corer   Zdjango.core.management.baser   r   Zdjango.core.management.utilsr   Z	django.dbr   r   Warningr	   r   r   r   r   r   <module>   s   