U
    f+&                     @   s   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d ZG dd deZG d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                 C   s   | rd|  S dS )Nz'%s'zabsolute path )dirnamer   r   a/home/mars/bis/venv/lib/python3.8/site-packages/django_extensions/management/commands/syncdata.pyhumanize   s    r   c                   @   s   e Zd ZdS )SyncDataErrorN)__name__
__module____qualname__r   r   r   r   r      s   r   c                       sD   e Zd ZdZdZdZ fddZdd Zedd	 Z	d
d Z
  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                    sZ   t  | |jdddddd |jddd	d
dd |jdtdd |jddtd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__r   r   r#   '   s4             zCommand.add_argumentsc                 C   s   |  D ]}|j }tdd |D }tdd || D }||}|r|D ].}|j|krP|  |dkrPtdt|  qP|dkr|rt	|}	|	dkrt|j
j}
nt|j
j}
tdt|	|
f  q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                 s   s   | ]}|j V  qd S Npk.0xr   r   r   	<genexpr>B   s     z0Command.remove_objects_not_in.<locals>.<genexpr>c                 s   s   | ]}|j V  qd S r*   r+   r-   r   r   r   r0   C   s        zDeleted object: %sr      zDeleted %s %sN)keysobjectsallset
differencer,   deleteprintr%   len_metaZverbose_name_pluralZverbose_name)r&   objects_to_keep	verbosityclass_currentZcurrent_idsZkeep_idsZremove_these_onesobjZnum_deletedZtype_deletedr   r   r   remove_objects_not_in:   s"    


zCommand.remove_objects_not_inc              
   O   s   t  | _|d | _|d r(|d dnd}zTz$t  | 	|| W 5 Q R X W n* t
k
r| } zt|W 5 d }~X Y nX W 5 t| jrt| j   X d S )NZdatabaser   ,r   )r   styleusingsplitr
   Zget_autocommitr	   closeZatomicsyncdatar   r   )r&   argsoptionsr   excr   r   r   handleV   s    

zCommand.handlec                 C   s$  |d }|d }d}d}g }t  }t| j  }	dd t D }
dd |
D }|D ] }|d}t|dkr~|}t	 }n4d
|d d	 |d	  }}|t	 kr|g}ng }|r|dkrtd
|  ntd||f tj|r|g}n|ttj dg }|D ]j}|dkr(tdt|  d}|D ]>}|dkrVtdt|||f  ztj
|d
||g}t|d}|r|  td|t|f nl|d7 }|d |dkrtd||t|f  zi }tt||}|D ]2}|jj}||krt  ||< || |j q|d rB|d rB| || |D ]4}|d7 }|d	  d7  < ||jj |  qF|d r|d s| || d}W nb ttfk
r    Y nH tk
r   dd l }|  |r|!  td||" f Y nX |  W n\ tk
r8 } z|W 5 d }~X Y n6 tk
rl   |dkrhtd||t|f  Y nX q0qqTd|krtd| |dkrt| j j#$| j%|}|r|dkrtd |D ]}|	&| q|dkr|dkr td n&|dkr td|t'||t'|f  d S )Nr=   	tracebackr   c                 S   s   g | ]
}|j qS r   )moduler.   Zappr   r   r   
<listcomp>w   s     z$Command.syncdata.<locals>.<listcomp>c                 S   s$   g | ]}t jt j|jd qS )Zfixtures)ospathjoinr   __file__rN   r   r   r   rO   x   s     .r2   zLoading '%s' fixtures...zHProblem installing fixture '%s': %s is not a known serialization format. zChecking %s for fixtures...Fz Trying %s for %s fixture '%s'...rz-Multiple fixtures named '%s' in %s. Aborting.z#Installing %s fixture '%s' from %s.r   r   Tz$Problem installing fixture '%s': %s
zNo %s fixture '%s' in %s.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)(r6   r	   rD   cursorr   Zget_app_configsrE   r:   r   Zget_public_serializer_formatsrR   r9   r   rP   rQ   isabslistr   ZFIXTURE_DIRSr   openrF   appendZdeserializeobjectr)   addrA   save
SystemExitKeyboardInterrupt	ExceptionrL   	print_exc
format_excZopsZsequence_reset_sqlrC   executer   )r&   r   rI   r=   show_tracebackZfixture_countZobject_countZobjects_per_fixturemodelsrX   Zapp_modulesZapp_fixturesZfixture_labelpartsZfixture_nameformatsZformat_Zfixture_dirsZfixture_dirZlabel_found	full_pathZfixturer<   r4   r@   r>   rL   eZsequence_sqlliner   r   r   rG   h   s    












&






  zCommand.syncdata)r   r   r   __doc__r   rH   r#   rA   r   rK   rG   __classcell__r   r   r(   r   r   !   s   
r   )rm   rP   Zdjango.appsr   Zdjango.confr   Zdjango.corer   Zdjango.core.management.baser   r   Zdjango.core.management.colorr   Z	django.dbr   r	   r
   Zdjango.template.defaultfiltersr   Z"django_extensions.management.utilsr   r   rb   r   r   r   r   r   r   <module>   s   
