
    Vh>                     f   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"	 d dl$Z$dZ%dZ& G d de      Z' G d dejP                        Z)d Z*y# e#$ r dZ"Y 6w xY w# e#$ r dZ%Y <w xY w)    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y)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                     |j                  dddd       |j                  dt        t        t              d       |j                  d	d
d       |j                  ddddd       |j                  dddg d       |j                  dd       y )Nargsfixture+zFixture labels.)metavarnargshelpz
--databasezXNominates a specific database to load fixtures into. Defaults to the "default" database.)defaultchoicesr   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   tupler   )selfparsers     X/home/dcms/DCMS/lib/python3.12/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  | j                  |       d d d        t        j                  | j                        r"t        | j                     j                          y y # 1 sw Y   KxY w)Nr"   databaser   	verbosityexcludeformatusing)r"   r2   r   r.   r   excluded_modelsexcluded_appsr0   r   atomicloaddataget_autocommitr   close)r'   fixture_labelsoptionss      r)   handlezCommand.handle\   s    h'Z(
 - -3NI4
0d0 h'djj1 	*MM.)	* %%djj1

#))+ 2	* 	*s   .C

C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>y   s
    CII r+   N)NgzziprB   bz2lzmaxz)
opengzipGzipFileSingleZipReaderhas_bz2rF   BZ2Filehas_lzmarG   LZMAFile)r'   compression_formatss     r)   rQ   zCommand.compression_formatsp   sy     ,==$'#S)-t4	
 *-++s);&+/==#*>')-(<%""r+   c                 "   |j                   j                  t               |      }|r]| j                  dk\  r| j                  j                  d       |j                         5 }|D ]  }|j                  |        	 ddd       yy# 1 sw Y   yxY w)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_sqlrX   lines         r)   reset_sequenceszCommand.reset_sequences   s    !~~88VL~~"!!"78""$ )( )DNN4()) ) ) )s   !BBc                    t         | j                     }d| _        d| _        d| _        t               | _        t        j                         | _	        |D ]  }| j                  |      s n y g | _        |j                         5  |D ]  }| j                  |        | j                  D ]  }|j                  | j                           	 d d d        | j                  D cg c]  }|j                  j                    }}	 |j#                  |       | j                  dkD  r| j)                  || j                         | j*                  dk\  r| j                  | j                  k(  r5| j,                  j/                  d| j                  | j                  fz         y | j,                  j/                  d| j                  | j                  | j                  fz         y y # 1 sw Y   xY wc c}w # t$        $ r}d|z  f|_         d }~ww xY w)Nr   r1   )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   r2   fixture_countloaded_object_countfixture_object_countsetr[   r   get_public_serializer_formatsserialization_formatsfind_fixturesobjs_with_deferred_fieldsconstraint_checks_disabled
load_labelsave_deferred_fields_metadb_tablecheck_constraints	Exceptionr   r^   r.   rV   rW   )r'   r9   rZ   fixture_labelobjmodelr`   es           r)   r6   zCommand.loaddata   s    ,
 #$ $%!e%0%N%N%P" , 	M!!-0	 )+&224 	;!/ /./55 ;((tzz(:;	; :>Eu{{++EE	(([(A ##a'  T[[9>>Q((D,D,DD!!?//1C1CDE
 !!G0011** )	; 	; F  	7!;=AF	s+   AG G( G- G%-	H6HHc                    |j                   j                  j                  | j                  v s!t	        |j                         | j
                  v ryd}t        j                  | j                  |j                   j                        rNd}| j                  j                  |j                   j                         	 |j                  | j                         |j&                  r| j(                  j+                  |       |S # t        t        t        f$ rG}d|j                   j                  j                   |j                   j"                  |dz  f|_         d}~ww xY w)zSave an object if permitted.FTr1   z9Could not load %(object_label)s(pk=%(pk)s): %(error_msg)s)object_labelpk	error_msgN)objectrm   
app_configr4   typer3   r   allow_migrate_modelr2   	__class__r[   addsaver   r   
ValueErrorlabelrw   r   deferred_fieldsri   r$   )r'   rr   savedrt   s       r)   save_objzCommand.save_obj   s    JJ''4+=+==CJJ4#7#77%%djj#**2F2FGEKKOOCJJ001tzz* **11#6 ">:> 	O(+

(8(8(>(>!jjmm%& 	s   7C< <EAEEc                    | j                   dk\  }| j                  |      D ]  \  }}}| j                  t        j                  j                  |            \  }}}| j                  |   \  }	}
 |	||
      }| xj                  dz  c_        d}d}| j                   dk\  r.| j                  j                  d|d|dt        |      d       	 t        j                  ||| j                  | j                  d	
      }|D ]A  }|dz  }| j                  |      s|dz  }|s"| j                  j                  d|z  d       C 	 |j)                          |r|r| j                  j                          | xj*                  |z  c_        | xj,                  |z  c_        |dk(  st/        j0                  d|z  t2                y# t         $ r$}t#        |t$              sd|d|f|_         d}~ww xY w# |j)                          w xY w)z&Load fixtures files for a given label.   ra   r   rS   zInstalling z
 fixture 'z' from .T)r2   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.   rh   
parse_nameospathbasenamerQ   rb   rV   rW   humanizer   deserializer2   r"   r   rp   
isinstancer	   r   r8   rc   rd   warningswarnRuntimeWarning)r'   rq   show_progressfixture_filefixture_dirfixture_name_ser_fmtcmp_fmtopen_methodmoder   objects_in_fixtureloaded_objects_in_fixtureobjectsrr   rt   s                    r)   rk   zCommand.load_label   s   !+7;7I7I8
 3	3L+| #'//"''2B2B<2P"QAw $ 8 8 AK!,5G!#!"()%~~"!!h{.CE %11**&*kk.2 # C&!+&}}S)1Q61( KK-- ;>W W') .   !m!!#$$(AA$%%);;%!Q& "./"_3	@  !!\2AMqQAF  s1   A	GG"G	G/G**G//G22Hc                    t         j                  j                  |      \  }}t         j                  j                  |      r|g}||fS | j                  }t         j                  j
                  t         j                  j                  |      v r-|D cg c]"  }t         j                  j                  ||      $ }}||fS c c}w r@   )r   r   splitisabsfixture_dirssepnormpathjoin)r'   r   dirnamer   r   dir_s         r)   get_fixture_name_and_dirsz!Command.get_fixture_name_and_dirs  s    GGMM,777==&#9L
 %%  ,,Lww{{bgg..|<<HTUT7 ;UU%%  Vs   'B?c                     | j                   d g}|| j                  n|g}|| j                  n|g}t        |||      D ch c])  }|ddj	                  |D cg c]  }|s|	 c}      + c}}S c c}w c c}}w )Nr   )r2   rQ   rg   r   r   )	r'   r   r   r   	databasescmp_fmtsser_fmtscomboexts	            r)   get_targetszCommand.get_targets$  s    ZZ&	/64++WI184--wi !Hh?
  6###67
 	
 7	
s   A8A3 A3$A83A8c                    g }t         j                  j                  ||      }t        j                  t        j
                  |      dz         D ]8  }t         j                  j                  |      |v s%|j                  |||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_dir1  sz    !ww||K6DKK$5$;< 	TIww	*g5 %++Y\,RS		T
 $#r+   c           	         |t         k(  rt         dt         fgS | j                  |      \  }}}| j                  dk\  r| j                  j	                  d|z         | j                  |      \  }}| j                  |||      }g }|D ]  }| j                  dk\  r'| j                  j	                  dt        |      z         | j                  |||      }	| j                  dk\  r-|	s+| j                  j	                  d|dt        |      d       t        |	      dkD  rt        d	|dt        |      d
      |j                  |	        |st        d|z        |S )z%Find fixture files for a given label.NrS   zLoading '%s' fixtures...zChecking %s for fixtures...zNo fixture 'z' in r   ra   zMultiple fixtures named 'z. Aborting.zNo fixture named '%s' found.)
READ_STDINr   r.   rV   rW   r   r   r   r   lenr	   extend)
r'   rq   r   r   r   r   r   fixture_filesr   r   s
             r)   rh   zCommand.find_fixtures;  s^    J&z233)-)G&gw>>QKK8<GH%)%C%CL%Q"l""<'B' 	7K~~"!!"?(;BW"WX#'#A#A$ 
 ~~"+?!!0<h{>ST '(1,"#Xk%:<    !56)	7, =LMMr+   c                    g }t         j                  }t        |      t        t        |            k7  rt	        d      t        j                         D ]  }|j                  }t        j                  j                  |j                  d      }||D cg c]  }t        |       c}v rt	        d|d|d      | j                  r|| j                  k7  rt        j                  j                  |      s|j                  |        |j                  |       |j                  d       |D cg c]!  }t        j                  j!                  |      # c}S c c}w c c}w )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.fixtures'z*' is a default fixture directory for the 'z4' app and cannot be listed in settings.FIXTURE_DIRS.r   )r   FIXTURE_DIRSr   re   r   r   get_app_configsr   r   r   r   strr   isdirr$   r   realpath)r'   dirsr   rz   r   app_dirds          r)   r   zCommand.fixture_dirsc  s    ,,|C$5 66&'STT..0 	%J"((Iggll:??J?G<8a3q688* 	+  ~~)t~~"=ww}}W%G$	% 	L!B-12  #22 9 3s   E-&Ec                    |t         k(  r*| j                  st        d      t         | j                  dfS |j                  dd      }t	        |      dkD  r|d   | j
                  v r|d   }|dd }nd}t	        |      dkD  rC|d   | j                  v r|d   }|dd }n)t        ddj                  |dd       d	|d   d
      d}dj                  |      }|||fS )zW
        Split fixture name in name, serialization format, compression format.
        z3--format must be specified when reading from stdin.rB   r   rS   ra   Nr   r   z% is not a known serialization format.)r   r0   r	   rsplitr   rQ   rg   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#2JEGu:>RyD666)cr
"/2xxcr
/CU2YP 
 GxxWg%%r+   N)__name__
__module____qualname__r   missing_args_messager*   r;   r   rQ   r^   r6   r   rk   r   r   r   	functoolscacherh   r   r    r+   r)   r   r   ,   s    ;D	+ 
'
R,( # #")8t86p&
$ __% %N 3 3<!&r+   r   c                   $     e Zd Z fdZd Z xZS )rL   c                 r    t        |   |i | t        | j                               dk7  rt	        d      y )Nra   z.Zip-compressed fixtures must contain one file.)super__init__r   namelistr   )r'   r   kwargsr}   s      r)   r   zSingleZipReader.__init__  s9    $)&)t}}1$MNN %r+   c                 d    t         j                  j                  | | 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)   rL   rL     s    O
>r+   rL   c                     | rd| z  S dS )Nz'%s'zabsolute pathr   )r   s    r)   r   r     s    &6G;O;r+   )+r   r   rJ   r   rA   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   rF   rM   ImportErrorrG   rO   r   r   r   rL   r   r   r+   r)   <module>r      s       	 
       # 7 A 1 D  4GH 
w&k w&t>goo ><c  G  Hs$   &B -B& B#"B#&B0/B0