a
    8Xh+&                     @   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   s/var/www/viveiro_mudafortebrasil/venv/lib/python3.9/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#   '   s"    zCommand.add_argumentsc                 C   s   |  D ]}|j }tdd |D }tdd || D }||}|r|D ].}|j|v 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       z0Command.remove_objects_not_in.<locals>.<genexpr>c                 s   s   | ]}|j V  qd S r*   r+   r-   r   r   r   r0   C   r1      zDeleted object: %sr      zDeleted %s %sN)keysobjectsallset
differencer,   deleteprintr%   lenZ_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}zz8t  | || W d    n1 sZ0    Y  W n, ty } zt|W Y d }~n
d }~0 0 W t	| jrt
| j   nt	| jrt
| j   0 d S )NZdatabaser   ,r   )r   styleusingsplitr
   Zatomicsyncdatar   r   Zget_autocommitr	   close)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	 v r|g}ng }|r|dkrtd
|  ntd||f tj|r|g}n|ttj dg }|D ]f}|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 nh|d7 }|d |dkrtd||t|f  zi }tt||}|D ]2}|jj}||vrt  ||< || |j q|d rB|d rB| || |D ]4}|d7 }|d	  d7  < ||jj |  qF|d r|d s| || d}W n^ ttfy    Y nF ty    dd l }|  |r|!  td||" f Y n0 |  W n\ ty6 } z|W Y d }~n<d }~0  tyh   |dkrdtd||t|f  Y n0 q0qqTd|v 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   r1   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   r1   .r3   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)(r7   r	   rD   cursorr   Zget_app_configsrE   r;   r   Zget_public_serializer_formatsrR   r:   r   rP   rQ   isabslistr   ZFIXTURE_DIRSr   openrG   appendZdeserializeobjectr)   addrA   save
SystemExitKeyboardInterrupt	ExceptionrL   	print_exc
format_exc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<   r5   r@   r>   rL   eZsequence_sqlliner   r   r   rF   h   s    












&






zCommand.syncdata)r   r   r   __doc__r   rH   r#   rA   r   rK   rF   __classcell__r   r   r(   r   r   !   s   
r   )rn   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   
