
    khS+                         d 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	m
Z
 ddlmZ ddlmZmZmZ dd	lmZ dd
lmZ d Z G d de          Z G d de	          ZdS )a  
SyncData
========

Django command similar to 'loaddata' but also deletes.
After 'syncdata' has run, the database will have the same data as the fixture - anything
missing will of been added, anything different will of been updated,
and anything extra will of been deleted.
    N)apps)settings)serializers)BaseCommandCommandError)no_style)DEFAULT_DB_ALIASconnectionstransaction)	pluralize)signalcommandc                     | rd| z  ndS )Nz'%s'zabsolute path )dirnames    e/var/www/histauto/venv/lib/python3.11/site-packages/django_extensions/management/commands/syncdata.pyhumanizer      s    &;6GO;    c                       e Zd ZdS )SyncDataErrorN)__name__
__module____qualname__r   r   r   r   r      s        Dr   r   c                   L     e Zd ZdZdZdZ fdZd Zed             Z	d Z
 xZS )Commandzsyncdata commandzRMakes the current database have the same data as the fixture(s), no more, no less.zfixture [fixture ...]c                 (   t                                          |           |                    ddddd           |                    ddd	d
d           |                    dt          d           |                    ddt          d           d S )Nz--skip-removestore_falseremoveTzAvoid remove any object from db)actiondestdefaulthelpz--remove-before
store_trueremove_beforeFz>Remove existing objects before inserting and updating new onesz
--databasezXNominates a specific database to load fixtures into. Defaults to the "default" database.)r    r!   fixture_labels?z+Specify the fixture label (comma separated))nargstyper!   )superadd_argumentsadd_argumentr	   str)selfparser	__class__s     r   r)   zCommand.add_arguments'   s    f%%% 2 	 	
 	
 	
 	 Q 	 	
 	
 	
 	$6	 	 	
 	
 	
 	>	 	 	
 	
 	
 	
 	
r   c                 z   |                                 D ]$}|j                                        }t          d |D                       }t          d ||         D                       }|                    |          }|rG|D ]D}|j        |v r9|                                 |dk    rt          dt          |          z             E|dk    rl|rjt          |          }	|	dk    rt          |j
        j                  }
nt          |j
        j                  }
t          dt          |	          d|
           &d	S )
z
        Delete all the objects in the database that are not in objects_to_keep.
        - objects_to_keep: A map where the keys are classes, and the values are a
         set of the objects of that class we should keep.
        c              3   $   K   | ]}|j         V  d S Npk.0xs     r   	<genexpr>z0Command.remove_objects_not_in.<locals>.<genexpr>N   s$      44qad444444r   c              3   $   K   | ]}|j         V  d S r1   r2   r4   s     r   r7   z0Command.remove_objects_not_in.<locals>.<genexpr>O   s$      AAA14AAAAAAr      zDeleted object: %sr      zDeleted  N)keysobjectsallset
differencer3   deleteprintr+   len_metaverbose_name_pluralverbose_name)r,   objects_to_keep	verbosityclass_currentcurrent_idskeep_idsremove_these_onesobjnum_deletedtype_deleteds              r   remove_objects_not_inzCommand.remove_objects_not_inF   s[    &**,, 	J 	JFn((**G44G44444KAA)@AAAAAH + 6 6x @ @  C" C CCv!222

$>>!"6S"ABBB1}}!2}!"344??#&v|'G#H#HLL#&v|'@#A#AL[)9)9)9)9<<HIII)	J 	Jr   c                 \   t                      | _        |d         | _        |d         r|d                             d          nd}	 t	          j                    5  |                     ||           d d d            n# 1 swxY w Y   n!# t          $ r}t          |          d }~ww xY wt	          j	        | j                  r&t          | j                                                  d S d S # t	          j	        | j                  r%t          | j                                                  w w xY w)Ndatabaser$   ,r   )r   styleusingsplitr   atomicsyncdatar   r   get_autocommitr
   close)r,   argsoptionsr$   excs        r   handlezCommand.handleb   sj   ZZ
Z(
4;<L4MUG$%++C000SU 		0#%% 7 7ng6667 7 7 7 7 7 7 7 7 7 7 7 7 7 7 	$ 	$ 	$s###	$ )$*55 0DJ'--/////0 0{)$*55 0DJ'--////0sO   B	 A=1B	 =BB	 BB	 C+ 	
B'B""B''C+ +A D+c                 
   |d         }|d         }d}d}g }t                      }t          | j                                                 }	d t	          j                    D             }
d |
D             }|D ]}|                    d          }t          |          dk    r|}t          j	                    }n@d
                    |d d                   |d         }}|t          j	                    v r|g}ng }|r|dk    rt          d	|z             nt          d
|d|d          t          j                            |          r|g}n |t!          t"          j                  z   dgz   }|D ]}|dk    rt          dt'          |          z             d}|D ]}|dk    r&t          dt'          |          d|d|d           	 t          j        
                    |d
                    ||g                    }t)          |d          }|r7|                                 t          d|dt'          |          d          |dz  }|                    d           |dk    r&t          d|d|dt'          |          d           	 i }t!          t          j        ||                    }|D ]C}|j        j        }||vrt                      ||<   ||                             |j                   D|d         r|d         r|                     ||           |D ]J}|dz  }|dxx         dz  cc<   |                    |j        j                   |                                 K|d         r|d         s|                     ||           d}nu# t:          t<          f$ r  t>          $ rW dd l }|                                 |r|!                                 t          d
|d|"                                d          w xY w|                                 # t          $ r}|d }~wt>          $ r0 |dk    r&t          d|d|dt'          |          d           Y w xY w d|v rt          d|z            |dk    rat          | j                 j#        $                    | j%        |          }|r/|dk    rt          d            |D ]}|	&                    |           |dk    r|dk    rt          d!           d S d S |dk    r2t          d"|tO          |          |tO          |          fz             d S d S )#NrH   	tracebackr   c                     g | ]	}|j         
S r   )moduler5   apps     r   
<listcomp>z$Command.syncdata.<locals>.<listcomp>   s    DDDcszDDDr   c                     g | ]D}t           j                            t           j                            |j                  d           ES )fixtures)ospathjoinr   __file__rd   s     r   rf   z$Command.syncdata.<locals>.<listcomp>   sH     
 
 
 GLL66
CC
 
 
r   .r:   zLoading '%s' fixtures...zProblem installing fixture 'z': z% is not a known serialization format. zChecking %s for fixtures...FzTrying z for z
 fixture 'z'...rzMultiple fixtures named 'z' in z. Aborting.zInstalling z' from r   r#   T
zNo z=No fixture data found for '%s'. (File format may be invalid.)zResetting sequenceszNo fixtures found.z'Installed %d object%s from %d fixture%s)(r?   r
   rV   cursorr   get_app_configsrW   rC   r   get_public_serializer_formatsrk   rB   r   ri   rj   isabslistr   FIXTURE_DIRSr   openr[   appenddeserializeobjectr.   addrQ   save
SystemExitKeyboardInterrupt	Exceptionra   	print_exc
format_excopssequence_reset_sqlrU   executer   )r,   r$   r]   rH   show_tracebackfixture_countobject_countobjects_per_fixturemodelsrr   app_modulesapp_fixturesfixture_labelpartsfixture_nameformatsformat_fixture_dirsfixture_dirlabel_found	full_pathfixturerG   r=   rN   rI   ra   esequence_sqllines                                 r   rY   zCommand.syncdatav   s   K(	 -  
 TZ(//11DDT-A-C-CDDD
 
"
 
 
 , l	 l	M!'',,E5zzQ,%CEE(+ss(<(<eBigkGIIII&iGG G 
q==4|CDDD#m
 $||WWW	.   w}}\** Q ,~+d83H.I.IIRDP+ O Oq==7(;:O:OOPPP#& J JG 1}}'4444ggg|||M  D$&GLL'<2I)J)J% %	 #'y#"6"6& 7,#MMOOO"/-#/<<+1F1F1F1F!H# # 
 *Q.M/66q999(1}} %'.wwh{>S>S>S>S%U!" !" !"'"24*.$/$;GW$M$M+" +" ,3 !L !LC-0Z-AF'-_'D'DBE%%(?$3F$;$?$?
$K$K$K$K#*8#4 !&9Q !&$($>$>(7%& %& %& ,3 !/ !/C$0A$5L$7$;$;$;q$@$;$;$;$*JJsz/C$D$D$D$'HHJJJJ#*8#4 !&W_=U !&$($>$>(7%& %& %& /3$.0A#B & & & %#, 	" 	" 	" 0 0 0 0 '#1 !:$-$7$7$9$9$9&3m'0yy)2F2F2H2H2H2H%J'" '" !"	" $MMOOOO(      $   $q==!E#*77LLL(;:O:O:O:O!Q  KJOf ###O   !&tz26II
F L  )q==/000( ) )DNN4((((11}}*+++++ } 1}}=$!,//%!-00	     }s9   CPDNPA2P	P
Q&	'P)	)9Q&	%Q&	)r   r   r   __doc__r!   r\   r)   rQ   r   r_   rY   __classcell__)r.   s   @r   r   r   !   s        _D"D
 
 
 
 
>J J J8 0 0 ]0&c c c c c c cr   r   )r   ri   django.appsr   django.confr   django.corer   django.core.management.baser   r   django.core.management.colorr   	django.dbr	   r
   r   django.template.defaultfiltersr   "django_extensions.management.utilsr   r   r   r   r   r   r   r   <module>r      s3    
			                   # # # # # # A A A A A A A A 1 1 1 1 1 1 @ @ @ @ @ @ @ @ @ @ 4 4 4 4 4 4 < < < < < << < <	 	 	 	 	I 	 	 	x x x x xk x x x x xr   