a
    [fr>                     @   sL  d dl Z d dlZd dlZd dlZd dlZd dl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 d dlmZ d dlmZmZ d dlmZ d d	lmZ d d
lmZmZmZmZmZmZ d dlm Z  zd dl!Z!dZ"W n e#y   dZ"Y n0 zd dl$Z$dZ%W n e#y   dZ%Y n0 dZ&G dd deZ'G dd dej(Z)dd Z*dS )    N)product)apps)settings)serializers)ImproperlyConfigured)BaseCommandCommandError)no_style)parse_apps_and_model_labels)DEFAULT_DB_ALIASDatabaseErrorIntegrityErrorconnectionsroutertransaction)cached_propertyTF-c                   @   s   e Zd ZdZdZdd Zdd Zedd Zd	d
 Z	dd Z
dd Zdd Zdd Zdd Zdd Zejdddd Zedd Zdd ZdS )Commandz.Installs the named fixture(s) in the database.zcNo database fixture specified. Please provide the path of at least one fixture in the command line.c                 C   sl   |j ddddd |j dtdd |j d	d
dd |j dddddd |j dddg dd |j ddd d S )Nargsfixture+zFixture labels.)metavarnargshelpz
--databasezXNominates a specific database to load fixtures into. Defaults to the "default" database.)defaultr   z--app	app_labelz,Only look for fixtures in the specified app.)destr   z--ignorenonexistentz-i
store_trueignorez[Ignores entries in the serialized data for fields that do not currently exist on the model.)actionr   r   z-ez	--excludeappendzKAn app_label or app_label.ModelName to exclude. Can be used multiple times.)r   r   r   z--formatz2Format of serialized data when reading from stdin.)r   )add_argumentr   )selfparser r$   zC:\Users\91910\OneDrive\Desktop\vts_admin_rebuild-master\vts\Lib\site-packages\django/core/management/commands/loaddata.pyadd_arguments3   s>    
zCommand.add_argumentsc                 O   s   |d | _ |d | _|d | _|d | _t|d \| _| _|d | _tj	| jd | 
| W d    n1 st0    Y  t| jrt| j   d S )Nr   Zdatabaser   	verbosityexcludeformatusing)r   r+   r   r'   r
   excluded_modelsexcluded_appsr)   r   ZatomicloaddataZget_autocommitr   close)r"   fixture_labelsoptionsr$   r$   r%   handle[   s    




(zCommand.handlec                 C   sZ   t dftjdftdfdd dfd}tr6tjdf|d< trVtj	df|d< tj	df|d	< |S )
z@A dict mapping format names to (open function, mode arg) tuples.rbrc                  W   s   t jS )N)sysstdin)r   r$   r$   r%   <lambda>x       z-Command.compression_formats.<locals>.<lambda>N)Ngzzipr6   bz2lzmaxz)
opengzipGzipFileSingleZipReaderhas_bz2r;   BZ2Filehas_lzmar<   LZMAFile)r"   compression_formatsr$   r$   r%   rF   o   s    
zCommand.compression_formatsc                 C   sj   |j t |}|rf| jdkr*| jd | $}|D ]}|| q8W d   n1 s\0    Y  dS )z=Reset database sequences for the given connection and models.   zResetting sequencesN)opsZsequence_reset_sqlr	   r'   stdoutwritecursorexecute)r"   
connectionmodelsZsequence_sqlrK   liner$   r$   r%   reset_sequences   s    

zCommand.reset_sequencesc              
   C   sh  t | j }d| _d| _d| _t | _t | _	|D ]}| 
|r2 qJq2d S g | _| > |D ]}| | q^| jD ]}|j| jd qtW d    n1 s0    Y  dd | jD }z|j|d W n2 ty } zd| f|_ W Y d }~n
d }~0 0 | jdkr| || j | jdkrd| j| jkrH| jd| j| jf  n| jd	| j| j| jf  d S )
Nr   r*   c                 S   s   g | ]}|j jqS r$   )_metaZdb_table).0modelr$   r$   r%   
<listcomp>   r8   z$Command.loaddata.<locals>.<listcomp>)table_nameszProblem installing fixtures: %s   z)Installed %d object(s) from %d fixture(s)z1Installed %d object(s) (of %d) from %d fixture(s))r   r+   fixture_countloaded_object_countfixture_object_countsetrN   r   Zget_public_serializer_formatsserialization_formatsfind_fixturesobjs_with_deferred_fieldsZconstraint_checks_disabled
load_labelZsave_deferred_fieldsZcheck_constraints	Exceptionr   rP   r'   rI   rJ   )r"   r0   rM   fixture_labelobjrU   er$   r$   r%   r.      sN    




.
zCommand.loaddatac              
   C   s   |j jj| jv s t|j | jv r$dS d}t| j|j j	rd}| j
|j j	 z|j| jd W nJ tttfy } z,d|j jj|j j|d f|_ W Y d}~n
d}~0 0 |jr| j| |S )zSave an object if permitted.FTr*   z9Could not load %(object_label)s(pk=%(pk)s): %(error_msg)s)Zobject_labelpk	error_msgN)objectrQ   
app_configr-   typer,   r   Zallow_migrate_modelr+   	__class__rN   addsaver   r   
ValueErrorlabelrc   r   Zdeferred_fieldsr]   r    )r"   ra   Zsavedrb   r$   r$   r%   save_obj   s.    zCommand.save_objc                 C   s  | j dk}| |D ]t\}}}| tj|\}}}| j| \}	}
|	||
}|  jd7  _d}d}| j dkr| j	d||t
|f  zzXtj||| j| jdd}|D ]6}|d7 }| |r|d7 }|r| jj	d| d	d
 qW nD ty( } z*t|tsd||f f|_ W Y d}~n
d}~0 0 W |  n
|  0 |rV|rV| j	  |  j|7  _|  j|7  _|dkrtd| t qdS )z&Load fixtures files for a given label.   rV   r   rG   z#Installing %s fixture '%s' from %s.T)r+   ZignorenonexistentZhandle_forward_referenceszProcessed %i object(s). )endingz#Problem installing fixture '%s': %sNz=No fixture data found for '%s'. (File format may be invalid.))r'   r\   
parse_nameospathbasenamerF   rW   rI   rJ   humanizer   Zdeserializer+   r   rm   r_   
isinstancer   r   r/   rX   rY   warningswarnRuntimeWarning)r"   r`   show_progressZfixture_filefixture_dirfixture_name_ser_fmtcmp_fmtZopen_methodmoder   Zobjects_in_fixtureZloaded_objects_in_fixtureobjectsra   rb   r$   r$   r%   r^      sd    





zCommand.load_labelc                    sX   t j|\ }t j|r$ g}n,| j}t jjt j|v rP fdd|D }||fS )Nc                    s   g | ]}t j| qS r$   )rr   rs   join)rR   Zdir_dirnamer$   r%   rT      r8   z5Command.get_fixture_name_and_dirs.<locals>.<listcomp>)rr   rs   splitisabsfixture_dirssepnormpath)r"   r|   rt   r   r$   r   r%   get_fixture_name_and_dirs  s    z!Command.get_fixture_name_and_dirsc                    sL   | j d g}|d u r| jn|g}|d u r,| jn|g} fddt|||D S )Nc                    s(   h | ] }d  d dd |D f qS )z%s.%s.c                 S   s   g | ]}|r|qS r$   r$   )rR   extr$   r$   r%   rT   +  r8   z1Command.get_targets.<locals>.<setcomp>.<listcomp>)r   )rR   Zcombor|   r$   r%   	<setcomp>'  s   z&Command.get_targets.<locals>.<setcomp>)r+   rF   r[   r   )r"   r|   r~   r   Z	databasesZcmp_fmtsZser_fmtsr$   r   r%   get_targets#  s    


zCommand.get_targetsc                 C   sP   g }t j||}tt|d D ]$}t j||v r&||||f q&|S )N*)rr   rs   r   globiglobescapert   r    )r"   r{   r|   targetsfixture_files_in_dirrs   	candidater$   r$   r%   find_fixture_files_in_dir0  s    z!Command.find_fixture_files_in_dirN)maxsizec           
      C   s   |t krt dt fgS | |\}}}| jdkr>| jd|  | |\}}| |||}g }|D ]}| jdkr| jdt|  | |||}	| jdkr|	s| jd|t|f  t	|	dkrt
d|t|f ||	 qb|st
d| |S )	z%Find fixture files for a given label.NrG   zLoading '%s' fixtures...zChecking %s for fixtures...zNo fixture '%s' in %s.rV   z-Multiple fixtures named '%s' in %s. Aborting.zNo fixture named '%s' found.)
READ_STDINrq   r'   rI   rJ   r   r   ru   r   lenr   extend)
r"   r`   r|   r~   r   r   r   Zfixture_filesr{   r   r$   r$   r%   r\   :  s<    


zCommand.find_fixturesc                 C   s   g }t j}t|tt|kr&tdt D ]d}|j}tj	
|j	d}|dd |D v rjtd||f | jr||| jkr|q.tj	|r.|| q.|| |d dd |D S )z
        Return a list of fixture directories.

        The list contains the 'fixtures' subdirectory of each installed
        application, if it exists, the directories in FIXTURE_DIRS, and the
        current directory.
        z*settings.FIXTURE_DIRS contains duplicates.Zfixturesc                 S   s   g | ]}t |qS r$   )strrR   dr$   r$   r%   rT   r  r8   z(Command.fixture_dirs.<locals>.<listcomp>zc'%s' is a default fixture directory for the '%s' app and cannot be listed in settings.FIXTURE_DIRS.ro   c                 S   s   g | ]}t j|qS r$   )rr   rs   realpathr   r$   r$   r%   rT     r8   )r   ZFIXTURE_DIRSr   rZ   r   r   Zget_app_configsrl   rr   rs   r   r   isdirr    r   )r"   dirsr   rf   r   Zapp_dirr$   r$   r%   r   b  s(    	

zCommand.fixture_dirsc                 C   s   |t kr"| jstdt | jdfS |dd}t|dkr^|d | jv r^|d }|dd }nd}t|dkr|d | jv r|d }|dd }qtdd|dd |d f nd}d|}|||fS )	zW
        Split fixture name in name, serialization format, compression format.
        z3--format must be specified when reading from stdin.r6   r   rG   rV   NzHProblem installing fixture '%s': %s is not a known serialization format.)r   r)   r   rsplitr   rF   r[   r   )r"   r|   partsr   r~   namer$   r$   r%   rq     s.    
zCommand.parse_name)__name__
__module____qualname__r   Zmissing_args_messager&   r2   r   rF   rP   r.   rm   r^   r   r   r   	functools	lru_cacher\   r   rq   r$   r$   r$   r%   r   ,   s&   (

:8



'
r   c                       s$   e Zd Z fddZdd Z  ZS )rA   c                    s.   t  j|i | t|  dkr*tdd S )NrV   z.Zip-compressed fixtures must contain one file.)super__init__r   namelistrk   )r"   r   kwargsrh   r$   r%   r     s    zSingleZipReader.__init__c                 C   s   t j| |  d S )Nr   )zipfileZipFilereadr   )r"   r$   r$   r%   r     s    zSingleZipReader.read)r   r   r   r   r   __classcell__r$   r$   r   r%   rA     s   rA   c                 C   s   | rd|  S dS )Nz'%s'zabsolute pathr$   r   r$   r$   r%   ru     s    ru   )+r   r   r?   rr   r5   rw   r   	itertoolsr   Zdjango.appsr   Zdjango.confr   Zdjango.corer   Zdjango.core.exceptionsr   Zdjango.core.management.baser   r   Zdjango.core.management.colorr	   Zdjango.core.management.utilsr
   Z	django.dbr   r   r   r   r   r   Zdjango.utils.functionalr   r;   rB   ImportErrorr<   rD   r   r   r   rA   ru   r$   r$   r$   r%   <module>   s@    

  {
