a
    Lcòfr>  ã                   @   sL  d dl Z d dlZd dlZd dlZd dlZd dl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 d dlmZ d dlmZmZ d dlmZ d d	lmZ d d
lmZmZmZmZmZmZ d dlm Z  zd dl!Z!dZ"W n e#yî   dZ"Y n0 zd dl$Z$dZ%W n e#�y   dZ%Y n0 dZ&G dd„ deƒZ'G dd„ dej(ƒZ)dd„ Z*dS )é    N)Úproduct)Úapps)Úsettings)Úserializers)ÚImproperlyConfigured)ÚBaseCommandÚCommandError)Úno_style)Úparse_apps_and_model_labels)ÚDEFAULT_DB_ALIASÚDatabaseErrorÚIntegrityErrorÚconnectionsÚrouterÚtransaction)Úcached_propertyTFú-c                   @   s�   e Zd ZdZdZdd„ Zdd„ Zedd„ ƒZd	d
„ Z	dd„ Z
dd„ Zdd„ Zdd„ Zdd„ Zdd„ Zejdd�dd„ ƒZedd„ ƒZdd„ ZdS )ÚCommandz.Installs the named fixture(s) in the database.zcNo database fixture specified. Please provide the path of at least one fixture in the command line.c                 C   sl   |j ddddd� |j dtdd� |j d	d
dd� |j dddddd� |j dddg dd� |j ddd� d S )NÚargsÚfixtureú+zFixture labels.)ÚmetavarÚnargsÚhelpz
--databasezXNominates a specific database to load fixtures into. Defaults to the "default" database.)Údefaultr   z--appÚ	app_labelz,Only look for fixtures in the specified app.)Údestr   z--ignorenonexistentz-iÚ
store_trueÚignorez[Ignores entries in the serialized data for fields that do not currently exist on the model.)Úactionr   r   z-ez	--excludeÚappendzKAn app_label or app_label.ModelName to exclude. Can be used multiple times.)r   r   r   z--formatz2Format of serialized data when reading from stdin.)r   )Úadd_argumentr   )ÚselfÚparser© r$   úe/home/httpd/docs/test/DocsMgr/lib/python3.9/site-packages/django/core/management/commands/loaddata.pyÚadd_arguments3   s>    ÿüýûú
þzCommand.add_argumentsc                 O   sœ   |d | _ |d | _|d | _|d | _t|d ƒ\| _| _|d | _tj	| jd�� |  
|¡ W d   ƒ n1 st0    Y  t | j¡r˜t| j  ¡  d S )Nr   Zdatabaser   Ú	verbosityÚexcludeÚformat©Úusing)r   r+   r   r'   r
   Úexcluded_modelsÚexcluded_appsr)   r   ZatomicÚloaddataZget_autocommitr   Úclose)r"   Úfixture_labelsÚoptionsr$   r$   r%   Úhandle[   s    



ÿ
(zCommand.handlec                 C   sZ   t dftjdftdfdd„ dfdœ}tr6tjdf|d< trVtj	df|d< tj	df|d	< |S )
z@A dict mapping format names to (open function, mode arg) tuples.ÚrbÚrc                  W   s   t jS )N)ÚsysÚstdin)r   r$   r$   r%   Ú<lambda>x   ó    z-Command.compression_formats.<locals>.<lambda>N)NÚgzÚzipr6   Úbz2ÚlzmaÚxz)
ÚopenÚgzipÚGzipFileÚSingleZipReaderÚhas_bz2r;   ÚBZ2FileÚhas_lzmar<   ÚLZMAFile)r"   Úcompression_formatsr$   r$   r%   rF   o   s    
üzCommand.compression_formatsc                 C   sj   |j  tƒ |¡}|rf| jdkr*| j d¡ | ¡ �$}|D ]}| |¡ q8W d  ƒ n1 s\0    Y  dS )z=Reset database sequences for the given connection and models.é   zResetting sequencesN)ÚopsZsequence_reset_sqlr	   r'   ÚstdoutÚwriteÚcursorÚexecute)r"   Ú
connectionÚmodelsZsequence_sqlrK   Úliner$   r$   r%   Úreset_sequences�   s    

zCommand.reset_sequencesc              
   C   sh  t | j }d| _d| _d| _tƒ | _t ¡ | _	|D ]}|  
|¡r2 qJq2d S g | _| ¡ �> |D ]}|  |¡ q^| jD ]}|j| jd� qtW d   ƒ n1 sœ0    Y  dd„ | jD ƒ}z|j|d� W n2 tyø } zd| f|_‚ W Y d }~n
d }~0 0 | jdk�r|  || j¡ | jdk�rd| j| jk�rH| j d| j| jf ¡ n| j d	| j| j| jf ¡ d S )
Nr   r*   c                 S   s   g | ]}|j j‘qS r$   )Ú_metaZdb_table)Ú.0Úmodelr$   r$   r%   Ú
<listcomp>©   r8   z$Command.loaddata.<locals>.<listcomp>)Útable_nameszProblem installing fixtures: %sé   z)Installed %d object(s) from %d fixture(s)z1Installed %d object(s) (of %d) from %d fixture(s))r   r+   Úfixture_countÚloaded_object_countÚfixture_object_countÚsetrN   r   Zget_public_serializer_formatsÚserialization_formatsÚfind_fixturesÚobjs_with_deferred_fieldsZconstraint_checks_disabledÚ
load_labelZsave_deferred_fieldsZcheck_constraintsÚ	Exceptionr   rP   r'   rI   rJ   )r"   r0   rM   Úfixture_labelÚobjrU   Úer$   r$   r%   r.   ‹   sN    




.
ÿÿýÿÿzCommand.loaddatac              
   C   sÂ   |j jj| jv s t|j ƒ| jv r$dS d}t | j|j j	¡r¬d}| j
 |j j	¡ z|j| jd� W nJ tttfyª } z,d|j jj|j j|dœ f|_‚ W Y d}~n
d}~0 0 |jr¾| j |¡ |S )zSave an object if permitted.FTr*   z9Could not load %(object_label)s(pk=%(pk)s): %(error_msg)s)Zobject_labelÚpkÚ	error_msgN)ÚobjectrQ   Ú
app_configr-   Útyper,   r   Zallow_migrate_modelr+   Ú	__class__rN   ÚaddÚsaver   r   Ú
ValueErrorÚlabelrc   r   Zdeferred_fieldsr]   r    )r"   ra   Zsavedrb   r$   r$   r%   Úsave_objÅ   s.    ÿþýÿÿzCommand.save_objc                 C   s�  | j dk}|  |¡D �]t\}}}|  tj |¡¡\}}}| j| \}	}
|	||
ƒ}|  jd7  _d}d}| j dkrŠ| j 	d||t
|ƒf ¡ zªzXtj||| j| jdd�}|D ]6}|d7 }|  |¡rª|d7 }|rª| jj	d| d	d
� qªW nD t�y( } z*t|tƒ�sd||f f|_‚ W Y d}~n
d}~0 0 W | ¡  n
| ¡  0 |�rV|�rV| j 	¡  |  j|7  _|  j|7  _|dkrt d| t¡ qdS )z&Load fixtures files for a given label.é   rV   r   rG   z#Installing %s fixture '%s' from %s.T)r+   ZignorenonexistentZhandle_forward_referenceszProcessed %i object(s).Ú )Úendingz#Problem installing fixture '%s': %sNz=No fixture data found for '%s'. (File format may be invalid.))r'   r\   Ú
parse_nameÚosÚpathÚbasenamerF   rW   rI   rJ   Úhumanizer   Zdeserializer+   r   rm   r_   Ú
isinstancer   r   r/   rX   rY   ÚwarningsÚwarnÚRuntimeWarning)r"   r`   Úshow_progressZfixture_fileÚfixture_dirÚfixture_nameÚ_Úser_fmtÚcmp_fmtZopen_methodÚmoder   Zobjects_in_fixtureZloaded_objects_in_fixtureÚobjectsra   rb   r$   r$   r%   r^   á   sd    
ÿ

ÿÿû
þ
ÿ
ÿýzCommand.load_labelc                    sX   t j |¡\‰ }t j |¡r$ˆ g}n,| j}t jjt j |¡v rP‡ fdd„|D ƒ}||fS )Nc                    s   g | ]}t j |ˆ ¡‘qS r$   )rr   rs   Újoin)rR   Zdir_©Údirnamer$   r%   rT      r8   z5Command.get_fixture_name_and_dirs.<locals>.<listcomp>)rr   rs   ÚsplitÚisabsÚfixture_dirsÚsepÚnormpath)r"   r|   rt   r‡   r$   rƒ   r%   Úget_fixture_name_and_dirs  s    z!Command.get_fixture_name_and_dirsc                    sL   | j d g}|d u r| jn|g}|d u r,| jn|g}‡ fdd„t|||ƒD ƒS )Nc                    s(   h | ] }d ˆ d  dd„ |D ƒ¡f ’qS )z%s.%sÚ.c                 S   s   g | ]}|r|‘qS r$   r$   )rR   Úextr$   r$   r%   rT   +  r8   z1Command.get_targets.<locals>.<setcomp>.<listcomp>)r‚   )rR   Zcombo©r|   r$   r%   Ú	<setcomp>'  s   ûþÿz&Command.get_targets.<locals>.<setcomp>)r+   rF   r[   r   )r"   r|   r~   r   Z	databasesZcmp_fmtsZser_fmtsr$   r�   r%   Úget_targets#  s    


úzCommand.get_targetsc                 C   sP   g }t j ||¡}t t |¡d ¡D ]$}t j |¡|v r&| |||f¡ q&|S )NÚ*)rr   rs   r‚   ÚglobÚiglobÚescapert   r    )r"   r{   r|   ÚtargetsÚfixture_files_in_dirrs   Ú	candidater$   r$   r%   Úfind_fixture_files_in_dir0  s    z!Command.find_fixture_files_in_dirN)Úmaxsizec           
      C   sø   |t krt dt fgS |  |¡\}}}| jdkr>| j d| ¡ |  |¡\}}|  |||¡}g }|D ]€}| jdkr„| j dt|ƒ ¡ |  |||¡}	| jdkr¸|	s¸| j d|t|ƒf ¡ t	|	ƒdkrØt
d|t|ƒf ƒ‚| |	¡ qb|sôt
d| ƒ‚|S )	z%Find fixture files for a given label.NrG   zLoading '%s' fixtures...zChecking %s for fixtures...zNo fixture '%s' in %s.rV   z-Multiple fixtures named '%s' in %s. Aborting.zNo fixture named '%s' found.)Ú
READ_STDINrq   r'   rI   rJ   rŠ   r�   ru   r—   Úlenr   Úextend)
r"   r`   r|   r~   r   r‡   r”   Zfixture_filesr{   r•   r$   r$   r%   r\   :  s<    

ýÿ
ÿÿzCommand.find_fixturesc                 C   s¶   g }t j}t|ƒtt|ƒƒkr&tdƒ‚t ¡ D ]d}|j}tj	 
|j	d¡}|dd„ |D ƒv rjtd||f ƒ‚| jr||| jkr|q.tj	 |¡r.| |¡ q.| |¡ | d¡ dd„ |D ƒS )zç
        Return a list of fixture directories.

        The list contains the 'fixtures' subdirectory of each installed
        application, if it exists, the directories in FIXTURE_DIRS, and the
        current directory.
        z*settings.FIXTURE_DIRS contains duplicates.Zfixturesc                 S   s   g | ]}t |ƒ‘qS r$   )Ústr©rR   Údr$   r$   r%   rT   r  r8   z(Command.fixture_dirs.<locals>.<listcomp>zc'%s' is a default fixture directory for the '%s' app and cannot be listed in settings.FIXTURE_DIRS.ro   c                 S   s   g | ]}t j |¡‘qS r$   )rr   rs   Úrealpathr�   r$   r$   r%   rT     r8   )r   ZFIXTURE_DIRSrš   rZ   r   r   Zget_app_configsrl   rr   rs   r‚   r   Úisdirr    r›   )r"   Údirsr‡   rf   r   Zapp_dirr$   r$   r%   r‡   b  s(    	þÿ

zCommand.fixture_dirsc                 C   sÎ   |t kr"| jstdƒ‚t | jdfS | dd¡}t|ƒdkr^|d | jv r^|d }|dd… }nd}t|ƒdkr¶|d | jv r’|d }|dd… }qºtdd |dd… ¡|d f ƒ‚nd}d |¡}|||fS )	zW
        Split fixture name in name, serialization format, compression format.
        z3--format must be specified when reading from stdin.r6   r‹   rG   rV   éÿÿÿÿNzHProblem installing fixture '%s': %s is not a known serialization format.)r™   r)   r   Úrsplitrš   rF   r[   r‚   )r"   r|   Úpartsr   r~   Únamer$   r$   r%   rq   �  s.    ÿÿÿ
zCommand.parse_name)Ú__name__Ú
__module__Ú__qualname__r   Zmissing_args_messager&   r2   r   rF   rP   r.   rm   r^   rŠ   r�   r—   Ú	functoolsÚ	lru_cacher\   r‡   rq   r$   r$   r$   r%   r   ,   s&   ÿ(

:8



'
r   c                       s$   e Zd Z‡ fdd„Zdd„ Z‡  ZS )rA   c                    s.   t ƒ j|i |¤Ž t|  ¡ ƒdkr*tdƒ‚d S )NrV   z.Zip-compressed fixtures must contain one file.)ÚsuperÚ__init__rš   Únamelistrk   )r"   r   Úkwargs©rh   r$   r%   r¬   ¦  s    zSingleZipReader.__init__c                 C   s   t j | |  ¡ d ¡S )Nr   )ÚzipfileÚZipFileÚreadr­   )r"   r$   r$   r%   r²   «  s    zSingleZipReader.read)r¦   r§   r¨   r¬   r²   Ú__classcell__r$   r$   r¯   r%   rA   ¥  s   rA   c                 C   s   | rd|  S dS )Nz'%s'zabsolute pathr$   rƒ   r$   r$   r%   ru   ¯  s    ru   )+r©   r‘   r?   rr   r5   rw   r°   Ú	itertoolsr   Zdjango.appsr   Zdjango.confr   Zdjango.corer   Zdjango.core.exceptionsr   Zdjango.core.management.baser   r   Zdjango.core.management.colorr	   Zdjango.core.management.utilsr
   Z	django.dbr   r   r   r   r   r   Zdjango.utils.functionalr   r;   rB   ÚImportErrorr<   rD   r™   r   r±   rA   ru   r$   r$   r$   r%   Ú<module>   s@    

  {
