a
    ÆZEg+&  ã                   @   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   úg/var/www/sistema_ama/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rªt|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}z„z8t ¡ � |  ||¡ W d   ƒ n1 sZ0    Y  W n, ty’ } zt|ƒ‚W Y d }~n
d }~0 0 W t 	| j¡rÎt
| j  ¡  nt 	| j¡rÌt
| 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rÜt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 ƒ �z´tj 
|d 
||g¡¡}t|dƒ}|�r¢| ¡  td|t|ƒf ƒ‚�nh|d7 }| d¡ |dk�rÔtd||t|ƒf ƒ zÎi }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 �rŠtd| ƒ‚|dk�rØt| j j# $| j%|¡}|�rØ|dk�rÂt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   
