
    g+&                         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y)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  S dS )Nz'%s'zabsolute path )dirnames    k/var/www/django_project/virt/lib/python3.12/site-packages/django_extensions/management/commands/syncdata.pyhumanizer      s    &6G;O;    c                       e Zd Zy)SyncDataErrorN)__name__
__module____qualname__r   r   r   r   r      s    r   r   c                   F     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 command zRMakes the current database have the same data as the fixture(s), no more, no less.zfixture [fixture ...]c                     t         |   |       |j                  ddddd       |j                  ddd	d
d       |j                  dt        d       |j                  ddt        d       y )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%M$2 	 	
 	lRWQ 	 	
 	"2k 	 	
 	Cc> 	 	
r   c                 D   |j                         D ]  }|j                  j                         }t        d |D              }t        d ||   D              }|j	                  |      }|rC|D ]>  }|j
                  |v s|j                          |dk\  s(t        dt        |      z         @ |dkD  s|st        |      }	|	dkD  r t        |j                  j                        }
nt        |j                  j                        }
t        dt        |	      d|
        y	)
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   4   K   | ]  }|j                     y wNpk.0xs     r   	<genexpr>z0Command.remove_objects_not_in.<locals>.<genexpr>B   s     4qadd4   c              3   4   K   | ]  }|j                     y wr1   r2   r4   s     r   r7   z0Command.remove_objects_not_in.<locals>.<genexpr>C   s     AA144Ar8      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_in:   s    &**, 	JFnn((*G4G44KA)@AAH + 6 6x @ " CCvv!22

$>!"6S"AB	C 1}!2!"34?#&v||'G'G#HL#&v||'@'@#AL[)9<HI)	Jr   c                 *   t               | _        |d   | _        |d   r|d   j                  d      nd}	 t	        j
                         5  | j                  ||       d d d        t	        j                  | j                        r"t        | j                     j                          y y # 1 sw Y   KxY w# t        $ r}t        |      d }~ww xY w# t	        j                  | j                        r"t        | j                     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.handleV   s    Z
Z(
AHIYAZ!1288=`b	0##% 7ng67 ))$**5DJJ'--/ 67 7 	$s##	$ ))$**5DJJ'--/ 6s<   B3 
B'B3 'B0,B3 3	C<CCC ADc                 	   |d   }|d   }d}d}g }t               }t        | j                     j                         }	t	        j
                         D 
cg c]  }
|
j                   }}
|D 
cg c]I  }
t        j                  j                  t        j                  j                  |
j                        d      K }}
|D ]  }|j                  d      }t        |      dk(  r|}t        j                         }n5dj                  |d d       |d   }}|t        j                         v r|g}ng }|r|dkD  r!t!        d|z         nt#        d	|d
d      t        j                  j%                  |      r|g}n |t'        t(        j*                        z   dgz   }|D ]  }|dkD  rt!        dt-        |      z         d}|D ]  }|dkD  rt!        dt-        |      d|d|d       	 t        j                  j                  |dj                  ||g            }t/        |d      }|r+|j1                          t#        d|dt-        |      d      |dz  }|j3                  d       |dkD  rt!        d|d|dt-        |      d       	 i }t'        t        j4                  ||            }|D ]G  }|j6                  j8                  }||vrt               ||<   ||   j;                  |j6                         I |d   r|d   r| j=                  ||       |D ]I  }|dz  }|dxx   dz  cc<   |j;                  |j6                  j8                         |j?                          K |d   r|d   s| j=                  ||       d}|j1                             d|v rt#        dz        |dkD  rat        | j                     jL                  jO                  | jP                  |      }|r(|dkD  rt!        d       |D ]  }|	jS                  |        |dk(  r|dkD  rt!        d        y y |dkD  r%t!        d!|tU        |      |tU        |      fz         y y c c}
w c c}
w # t@        tB        f$ r  tD        $ rG dd l#}|j1                          |r|jI                          t#        d	|d
|jK                         d      w xY w# t"        $ r}|d }~wtD        $ r' |dkD  rt!        d|d|dt-        |      d       Y =w xY w)"NrI   	tracebackr   fixtures.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
   rW   cursorr   get_app_configsmoduleospathjoinr   __file__rX   rD   r   get_public_serializer_formatsrC   r   isabslistr   FIXTURE_DIRSr   openr\   appenddeserializeobjectr.   addrR   save
SystemExitKeyboardInterrupt	Exceptionrb   	print_exc
format_excopssequence_reset_sqlrV   executer   )r,   r$   r^   rI   show_tracebackfixture_countobject_countobjects_per_fixturemodelsri   appapp_modulesapp_fixturesfixture_labelpartsfixture_nameformatsformat_fixture_dirsfixture_dirlabel_found	full_pathfixturerH   r>   rO   rJ   rb   esequence_sqllines                                  r   rZ   zCommand.syncdatah   s   K(	 -  
 TZZ(//1-1-A-A-CDcszzDD[fgTWRWW__S\\%BJOgg+ N	pM!'',E5zQ,%CCE(+s(<eBigkGGII&iG Gq=4|CD#r~  AH  %I  J  Jww}}\* ,~+d83H3H.IIRDP+ 7pq=7(;:OOP#& 2pG 1}H[DY[bdpqr/p$&GGLLchhV]G^>_$`	"&y#"6&#MMO"/coqy  {F  rG  1H  #I  I)Q.M/66q9(1} %wXdfnozf{&| }C24*.{/F/FwPW/X*Y+2 !LC-0ZZ-A-AF'-_'DBE%(?$3F$;$?$?

$K	!L $+8#49Q$($>$>PY$Z+2 !/C$0A$5L$7$;q$@$;$*JJszz/C/C$D$'HHJ	!/ $+8#4W_=U$($>$>PY$Z.2 $MMO[2p7p/N	pd ## _bn noo !&tzz266II$**V\]Lq=/0( )DNN4() 11}*+  1}? )L"9!9]#;C   K EgB %/0A#B & %#, C 0 '#1$-$7$7$9&3_hjsj~j~  kA  5B  'C  !CC )  $ p$q=!,X`alXm"nopsF   Q+AQ%B#R=	C5Q>R=AR:	:R==	S;	S	/S;	:S;	)r   r   r   __doc__r!   r]   r)   rR   r   r`   rZ   __classcell__)r.   s   @r   r   r   !   s5    _D"D
&J8 0 0"xr   r   )r   rl   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      sI    
    # A 1 @ @ 4 <<	I 	k r   