
    df`>                     n   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  	 d dl!Z!dZ"n# e#$ r dZ"Y nw xY w	 d dl$Z$dZ%n# e#$ r dZ%Y nw xY wdZ& G d de          Z' G d dej(                  Z)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                       e Zd ZdZdZd Zd Zed             Zd Z	d Z
d Zd	 Zd
 Zd Zd Zej        d             Zed             Z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                 8   |                     dddd           |                     dt          d           |                     d	d
d           |                     ddddd           |                     dddg 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parsers     dD:\Python Project\djangoTemplates\venv\Lib\site-packages\django/core/management/commands/loaddata.pyadd_argumentszCommand.add_arguments3   s
   IS7H 	 	
 	
 	
 	$&	 	 	
 	
 	
 	? 	 	
 	
 	

 	!, 	 	
 	
 	
 	 	 		
 		
 		
 	E 	 	
 	
 	
 	
 	
    c                    |d         | _         |d         | _        |d         | _        |d         | _        t	          |d                   \  | _        | _        |d         | _        t          j	        | j                  5  | 
                    |           d d d            n# 1 swxY w Y   t          j        | j                  r&t          | j                                                  d S d S )Nr!   databaser   	verbosityexcludeformatusing)r!   r0   r   r,   r   excluded_modelsexcluded_appsr.   r   atomicloaddataget_autocommitr   close)r%   fixture_labelsoptionss      r'   handlezCommand.handle[   s   h'Z(
 - -3NI4
 4
0d0 h'dj111 	* 	*MM.)))	* 	* 	* 	* 	* 	* 	* 	* 	* 	* 	* 	* 	* 	* 	* %dj11 	,
#))+++++	, 	,s   >B  B$'B$c                     t           dft          j        dft          dfd dfd}t          rt
          j        df|d<   t          r"t          j	        df|d<   t          j	        df|d<   |S )	z@A dict mapping format names to (open function, mode arg) tuples.rbrc                      t           j        S )N)sysstdin)r   s    r'   <lambda>z-Command.compression_formats.<locals>.<lambda>x   s    CI r)   N)Ngzzipr?   bz2lzmaxz)
opengzipGzipFileSingleZipReaderhas_bz2rC   BZ2Filehas_lzmarD   LZMAFile)r%   compression_formatss     r'   rN   zCommand.compression_formatso   s     ,=$'#S)--t4	
 
  	<*-+s);& 	=+/=#*>')-(<%""r)   c                 6   |j                             t                      |          }|ro| j        dk    r| j                            d           |                                5 }|D ]}|                    |           	 ddd           dS # 1 swxY w Y   dS dS )z=Reset database sequences for the given connection and models.   zResetting sequencesN)opssequence_reset_sqlr
   r,   stdoutwritecursorexecute)r%   
connectionmodelssequence_sqlrU   lines         r'   reset_sequenceszCommand.reset_sequences   s    !~88VLL 	)~""!!"7888""$$ )( ) )DNN4(((()) ) ) ) ) ) ) ) ) ) ) ) ) ) ) ) ) )	) 	)s   #BBBc                    t           | j                 }d| _        d| _        d| _        t                      | _        t          j                    | _	        |D ]}| 
                    |          r nd S g | _        |                                5  |D ]}|                     |           | j        D ]}|                    | j                   	 d d d            n# 1 swxY w Y   d | j        D             }	 |                    |           n# t           $ r}d|z  f|_         d }~ww xY w| j        dk    r|                     || j                   | j        dk    rl| j        | j        k    r+| j                            d| j        | j        fz             d S | j                            d| j        | j        | j        fz             d S d S )	Nr   r/   c                 &    g | ]}|j         j        S  )_metadb_table).0models     r'   
<listcomp>z$Command.loaddata.<locals>.<listcomp>   s    EEEu{+EEEr)   )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   r0   fixture_countloaded_object_countfixture_object_countsetrX   r   get_public_serializer_formatsserialization_formatsfind_fixturesobjs_with_deferred_fieldsconstraint_checks_disabled
load_labelsave_deferred_fieldscheck_constraints	Exceptionr   r[   r,   rS   rT   )r%   r7   rW   fixture_labelobjrd   es          r'   r4   zCommand.loaddata   sx    ,
 #$ $%!ee%0%N%P%P" , 	 	M!!-00  F)+&2244 	; 	;!/ / /....5 ; ;((tz(::::;	; 	; 	; 	; 	; 	; 	; 	; 	; 	; 	; 	; 	; 	; 	; FEEEE	(([(AAAA 	 	 	7!;=AF	 #a''  T[999>Q(D,DDD!!?/1CDE    
 !!G01*     s+   A CC C6D 
D(D##D(c                    |j         j        j        | j        v st	          |j                   | j        v rdS d}t          j        | j        |j         j	                  rd}| j
                            |j         j	                   	 |                    | j                   nH# t          t          t          f$ r.}d|j         j        j        |j         j        |dz  f|_         d}~ww xY w|j        r| j                            |           |S )zSave an object if permitted.FTr/   z9Could not load %(object_label)s(pk=%(pk)s): %(error_msg)s)object_labelpk	error_msgN)objectr_   
app_configr2   typer1   r   allow_migrate_modelr0   	__class__rX   addsaver   r   
ValueErrorlabelrx   r   deferred_fieldsrm   r#   )r%   rt   savedru   s       r'   save_objzCommand.save_obj   s    J'4+===CJ4#7775%dj#*2FGG 	EKOOCJ0111tz****!>:> 	 	 	O(+
(8(>!jm%&  	  	7*11#666s   B C$6)CC$c                    | j         dk    }|                     |          D ]\  }}}|                     t          j                            |                    \  }}}| j        |         \  }	}
 |	||
          }| xj        dz  c_        d}d}| j         dk    r1| j        	                    d|d|dt          |          d           	 t          j        ||| j        | j        d	
          }|D ]B}|dz  }|                     |          r&|dz  }|r| j        	                    d|z  d           Cn6# t           $ r)}t#          |t$                    sd|d|f|_         d}~ww xY w	 |                                 n# |                                 w xY w|r|r| j        	                                 | xj        |z  c_        | xj        |z  c_        |dk    rt/          j        d|z  t2                     dS )z&Load fixtures files for a given label.   re   r   rP   zInstalling z
 fixture 'z' from .T)r0   ignorenonexistenthandle_forward_referenceszProcessed %i object(s). )endingProblem installing fixture '': Nz=No fixture data found for '%s'. (File format may be invalid.))r,   rl   
parse_nameospathbasenamerN   rf   rS   rT   humanizer   deserializer0   r!   r   rr   
isinstancer	   r   r6   rg   rh   warningswarnRuntimeWarning)r%   rs   show_progressfixture_filefixture_dirfixture_name_ser_fmtcmp_fmtopen_methodmoder   objects_in_fixtureloaded_objects_in_fixtureobjectsrt   ru   s                    r'   ro   zCommand.load_label   s   !+7;7I7I8
 8
 3	 3	3L+| #'//"'2B2B<2P2P"Q"QAw $ 8 AK!k,55G!#!"()%~""!!!wwh{.C.C.C.CE   %1*&*k.2   #  C&!+&}}S)) 1Q61(  K-- ;>W W') .       !!\22  AMqqQAF   ! $m $!!###$$(AA$$%%);;%%!Q&& "./"  _3	 3	s+   A(D43F 4
E'>$E""E''F  Fc                     t           j                            |          \  }t           j                            |          rg}nE| j        }t           j        j        t           j                            |          v rfd|D             }||fS )Nc                 P    g | ]"}t           j                            |          #S r^   )r   r   join)ra   dir_dirnames     r'   rc   z5Command.get_fixture_name_and_dirs.<locals>.<listcomp>   s)    UUUT7 ; ;UUUr)   )r   r   splitisabsfixture_dirssepnormpath)r%   r   r   r   r   s       @r'   get_fixture_name_and_dirsz!Command.get_fixture_name_and_dirs  s    GMM,777==&& 	V#9LL,Lw{bg..|<<<<UUUUUUU%%r)   c                     | j         d g}|| j        n|g}|| j        n|g}fdt          |||          D             S )Nc                 X    h | ]&}d d                      d |D                       'S )r   c                     g | ]}||S r^   r^   )ra   exts     r'   rc   z1Command.get_targets.<locals>.<setcomp>.<listcomp>+  s    666##6#666r)   )r   )ra   combor   s     r'   	<setcomp>z&Command.get_targets.<locals>.<setcomp>'  sU     
 
 
  66666777
 
 
r)   )r0   rN   rk   r   )r%   r   r   r   	databasescmp_fmtsser_fmtss    `     r'   get_targetszCommand.get_targets#  sn    Z&	/64++WI184--wi
 
 
 
 !Hh??
 
 
 	
r)   c                    g }t           j                            ||          }t          j        t          j        |          dz             D ];}t           j                            |          |v r|                    |||f           <|S )N*)r   r   r   globiglobescaper   r#   )r%   r   r   targetsfixture_files_in_dirr   	candidates          r'   find_fixture_files_in_dirz!Command.find_fixture_files_in_dir0  s    !w||K66DK$5$5$;<< 	T 	TIw	**g55 %++Y\,RSSS##r)   c           	         |t           k    rt           dt           fgS |                     |          \  }}}| j        dk    r| j                            d|z             |                     |          \  }}|                     |||          }g }|D ]}| j        dk    r*| j                            dt          |          z             |                     |||          }	| j        dk    r0|	s.| j                            d|dt          |          d           t          |	          dk    r#t          d	|dt          |          d
          |                    |	           |st          d|z            |S )z%Find fixture files for a given label.NrP   zLoading '%s' fixtures...zChecking %s for fixtures...zNo fixture 'z' in r   re   zMultiple fixtures named 'z. Aborting.zNo fixture named '%s' found.)
READ_STDINr   r,   rS   rT   r   r   r   r   lenr	   extend)
r%   rs   r   r   r   r   r   fixture_filesr   r   s
             r'   rl   zCommand.find_fixtures:  s    J&&z233)-)G)G&gw>QK8<GHHH%)%C%CL%Q%Q"l""<'BB' 	7 	7K~""!!"?(;BWBW"WXXX#'#A#A$ $ 
 ~""+?"!!!0<h{>S>S>S>ST   '((1,,"l#||Xk%:%:%:%:<     !56666 	N=LMMMr)   c                 \   g }t           j        }t          |          t          t          |                    k    rt	          d          t          j                    D ]}|j        }t          j	        
                    |j	        d          }|d |D             v rt	          d|d|d          | j        r|| j        k    ret          j	                            |          r|                    |           |                    |           |                    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.fixturesc                 ,    g | ]}t          |          S r^   )strra   ds     r'   rc   z(Command.fixture_dirs.<locals>.<listcomp>r  s    888a3q66888r)   'z*' is a default fixture directory for the 'z4' app and cannot be listed in settings.FIXTURE_DIRS.r   c                 L    g | ]!}t           j                            |          "S r^   )r   r   realpathr   s     r'   rc   z(Command.fixture_dirs.<locals>.<listcomp>  s(    222  ##222r)   )r   FIXTURE_DIRSr   ri   r   r   get_app_configsr   r   r   r   r   isdirr#   r   )r%   dirsr   r{   r   app_dirs         r'   r   zCommand.fixture_dirsb  s6    ,|C$5$5 6 666&'STTT.00 	% 	%J"(Igll:?J??G88<88888** ww			+   ~ )t~"="=w}}W%% %G$$$L!!!B22T2222r)   c                    |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    rY|d         | j        v r|d         }|dd         }n9t          dd                    |dd                   d	|d         d
          d}d                    |          }|||fS )zW
        Split fixture name in name, serialization format, compression format.
        z3--format must be specified when reading from stdin.r?   r   rP   re   Nr   r   z% is not a known serialization format.)r   r.   r	   rsplitr   rN   rk   r   )r%   r   partsr   r   names         r'   r   zCommand.parse_name  s.    :%%; "I   t{G33##C++u::>>eBi4+CCCBiG#2#JEEGu::>>RyD666)crc
"l/2xxcrc
/C/C/C/CU2YYYP  
 GxxWg%%r)   N)__name__
__module____qualname__r   missing_args_messager(   r9   r   rN   r[   r4   r   ro   r   r   r   	functoolscacherl   r   r   r^   r)   r'   r   r   ,   s       ;D	+ 
&
 &
 &
P, , ,( # # _#") ) )8 8 8t  86 6 6p& & &
 
 
$ $ $ _% % _%N 3 3 _3<!& !& !& !& !&r)   r   c                   $     e Zd Z fdZd Z xZS )rI   c                      t                      j        |i | t          |                                           dk    rt	          d          d S )Nre   z.Zip-compressed fixtures must contain one file.)super__init__r   namelistr   )r%   r   kwargsr~   s      r'   r   zSingleZipReader.__init__  sQ    $)&)))t}}1$$MNNN %$r)   c                 r    t           j                            | |                                 d                   S )Nr   )zipfileZipFilereadr   )r%   s    r'   r   zSingleZipReader.read  s'    ##D$--//!*<===r)   )r   r   r   r   r   __classcell__)r~   s   @r'   rI   rI     sL        O O O O O
> > > > > > >r)   rI   c                     | rd| z  ndS )Nz'%s'zabsolute pathr^   )r   s    r'   r   r     s    &;6GO;r)   )+r   r   rG   r   r>   r   r   	itertoolsr   django.appsr   django.confr   django.corer   django.core.exceptionsr   django.core.management.baser   r	   django.core.management.colorr
   django.core.management.utilsr   	django.dbr   r   r   r   r   r   django.utils.functionalr   rC   rJ   ImportErrorrD   rL   r   r   r   rI   r   r^   r)   r'   <module>r      s2         				 



                           # # # # # # 7 7 7 7 7 7 A A A A A A A A 1 1 1 1 1 1 D D D D D D                4 3 3 3 3 3JJJGG   GGGKKKHH   HHH 
v& v& v& v& v&k v& v& v&r> > > > >go > > >< < < < <s$   &A- -A76A7;B BB