B
    ©0H[»7  ã               @   s$  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  yd dl!Z!dZ"W n e#k
rð   dZ"Y nX 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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   s|   |j ddddd� |j dddtd	d
� |j dddd dd
� |j dddddd� |j ddddg dd� |j dddd dd
� d S )NÚargsÚfixtureú+zFixture labels.)ÚmetavarÚnargsÚhelpz
--databaseÚstoreÚdatabasezXNominates a specific database to load fixtures into. Defaults to the "default" database.)ÚactionÚdestÚdefaultr   z--appÚ	app_labelz,Only look for fixtures in the specified app.z--ignorenonexistentz-iÚ
store_trueÚignorez[Ignores entries in the serialized data for fields that do not currently exist on the model.)r   r   r   z-ez	--excludeÚexcludeÚappendzKAn app_label or app_label.ModelName to exclude. Can be used multiple times.)r   r   r   r   z--formatÚformatz2Format of serialized data when reading from stdin.)Úadd_argumentr   )ÚselfÚparser© r(   úlC:\Users\HIRONO~1\AppData\Local\Temp\pip-install-6bm3nxem\django\django\core\management\commands\loaddata.pyÚadd_arguments'   s     
zCommand.add_argumentsc          	   O   sˆ   |d | _ |d | _|d | _|d | _t|d ƒ\| _| _|d | _tj	| jd�� |  
|¡ W d Q R X t | j¡r„t| j  ¡  d S )Nr!   r   r   Ú	verbosityr"   r$   )Ú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   sÄ  t | j }d| _d| _d| _tƒ | _t ¡ | _	t
dftjdftdfdd„ d fdœ| _trhtjdf| jd< x|D ]}|  |¡rnP qnW d S | ¡ � x|D ]}|  |¡ q–W W d Q R X dd	„ | jD ƒ}y|j|d
� W n2 tk
�r } zd| f|_‚ W d d }~X Y nX | jdk�rp|j tƒ | j¡}|�rp| jdk�rB| j d¡ | ¡ � }x|D ]}| |¡ �qRW W d Q R X | jdk�rÀ| j| jk�r¤| j d| j| jf ¡ n| j d| j| j| jf ¡ d S )Nr   ÚrbÚrc              W   s   t jS )N)ÚsysÚstdin)r   r(   r(   r)   Ú<lambda>`   s    z"Command.loaddata.<locals>.<lambda>)NÚgzÚzipr7   Úbz2c             S   s   g | ]}|j j‘qS r(   )Ú_metaZdb_table)Ú.0Úmodelr(   r(   r)   ú
<listcomp>u   s    z$Command.loaddata.<locals>.<listcomp>)Útable_nameszProblem installing fixtures: %sé   zResetting sequences
é   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ÚsetÚmodelsr   Úget_public_serializer_formatsÚserialization_formatsÚopenÚgzipÚGzipFileÚSingleZipReaderÚcompression_formatsÚhas_bz2r;   ÚBZ2FileÚfind_fixturesZconstraint_checks_disabledÚ
load_labelZcheck_constraintsÚ	Exceptionr   ÚopsZsequence_reset_sqlr	   r+   ÚstdoutÚwriteÚcursorÚexecute)	r&   r1   Ú
connectionÚfixture_labelr@   ÚeZsequence_sqlrW   Úliner(   r(   r)   r/   Q   sR    







zCommand.loaddatac             C   s4  | j dk}�x"|  |¡D �]\}}}|  tj |¡¡\}}}| j| \}	}
|	||
ƒ}�z²�yh|  jd7  _d}d}| j dkr–| j 	d||t
|ƒf ¡ tj||| j| jd�}xÞ|D ]Ö}|d7 }|jjj| jks²t|jƒ| jkràq²t | j|jj¡r²|d7 }| j |jj¡ y,|j| jd� |�r4| jj	d| d	d
� W q² ttfk
�r† } z,d|jjj|jjj|jj|dœ f|_ ‚ W dd}~X Y q²X q²W |�r¤|�r¤| j 	d	¡ |  j!|7  _!|  j"|7  _"W nB t#k
�r } z"t$|t%ƒ�sòd||f f|_ ‚ W dd}~X Y nX W d| &¡  X |dkrt' (d| t)¡ qW dS )z&Load fixtures files for a given label.é   rB   r   rA   z#Installing %s fixture '%s' from %s.)r,   Zignorenonexistent)r,   zProcessed %i object(s).Ú )ZendingzFCould not load %(app_label)s.%(object_name)s(pk=%(pk)s): %(error_msg)s)r   Úobject_nameÚpkZ	error_msgNz#Problem installing fixture '%s': %sz=No fixture data found for '%s'. (File format may be invalid.))*r+   rQ   Ú
parse_nameÚosÚpathÚbasenamerN   rC   rU   rV   Úhumanizer   Zdeserializer,   r!   Úobjectr<   Ú
app_configr.   Útyper-   r   Zallow_migrate_modelÚ	__class__rG   ÚaddÚsaver   r   r   r_   r`   r   rD   rE   rS   Ú
isinstancer   r0   ÚwarningsÚwarnÚRuntimeWarning)r&   rZ   Úshow_progressZfixture_fileÚfixture_dirÚfixture_nameÚ_Úser_fmtÚcmp_fmtZopen_methodÚmoder   Zobjects_in_fixtureZloaded_objects_in_fixtureÚobjectsÚobjr[   r(   r(   r)   rR   “   sd    




zCommand.load_labelN)Úmaxsizec                sò  |t krt dt fgS |  |¡\‰ }}| jdg}|dkr@t| jƒn|g}|dkrVt ¡ n|g}| jdkrv| j 	dˆ  ¡ t
j ˆ ¡ržt
j ˆ ¡g}t
j ˆ ¡‰ n8| j}t
jjt
j ˆ ¡krÖ‡ fdd„|D ƒ}t
j ˆ ¡‰ dd„ t|||ƒD ƒ}‡ fdd	„|D ƒ}	g }
xØ|D ]Ð}| jdk�r,| j 	d
t|ƒ ¡ g }t
j |ˆ ¡}x@t t |¡d ¡D ](}t
j |¡|	k�rT| ||ˆ f¡ �qTW | jdk�rª|�sª| j 	dˆ t|ƒf ¡ t|ƒdk�rÌtdˆ t|ƒf ƒ‚|
 |¡ �qW |
�sîtdˆ  ƒ‚|
S )z%Find fixture files for a given label.NrA   zLoading '%s' fixtures...c                s"   g | ]}t j |t j ˆ ¡¡‘qS r(   )rb   rc   ÚjoinÚdirname)r=   Zdir_)rr   r(   r)   r?   æ   s   z)Command.find_fixtures.<locals>.<listcomp>c             s   s"   | ]}d   dd„ |D ƒ¡V  qdS )Ú.c             s   s   | ]}|r|V  qd S )Nr(   )r=   Úextr(   r(   r)   ú	<genexpr>ë   s    z2Command.find_fixtures.<locals>.<genexpr>.<genexpr>N)rz   )r=   Zcombor(   r(   r)   r~   ë   s   z(Command.find_fixtures.<locals>.<genexpr>c                s   h | ]}d   ˆ |f¡’qS )r|   )rz   )r=   Úsuffix)rr   r(   r)   ú	<setcomp>î   s    z(Command.find_fixtures.<locals>.<setcomp>zChecking %s for fixtures...Ú*zNo fixture '%s' in %s.rB   z-Multiple fixtures named '%s' in %s. Aborting.zNo fixture named '%s' found.)Ú
READ_STDINra   r,   ÚlistrN   r   rH   r+   rU   rV   rb   rc   Úisabsr{   rd   Úfixture_dirsÚsepÚnormpathr   re   rz   ÚglobÚiglobÚescaper#   Úlenr   Úextend)r&   rZ   rt   ru   Z	databasesZcmp_fmtsZser_fmtsr…   ÚsuffixesÚtargetsZfixture_filesrq   Zfixture_files_in_dirrc   Ú	candidater(   )rr   r)   rQ   Ò   sN    



zCommand.find_fixturesc             C   s¸   g }t j}t|ƒtt|ƒƒkr&tdƒ‚xft ¡ D ]Z}|j}tj	 
|j	d¡}||krbtd||f ƒ‚| jrt|| jkrtq0tj	 |¡r0| |¡ q0W | t|ƒ¡ | 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fixtureszc'%s' is a default fixture directory for the '%s' app and cannot be listed in settings.FIXTURE_DIRS.r^   c             S   s    g | ]}t j t j |¡¡‘qS r(   )rb   rc   ÚabspathÚrealpath)r=   Údr(   r(   r)   r?   (  s    z(Command.fixture_dirs.<locals>.<listcomp>)r   ZFIXTURE_DIRSr‹   rF   r   r   Zget_app_configsÚlabelrb   rc   rz   r   Úisdirr#   rŒ   rƒ   )r&   Údirsr…   rg   r   Zapp_dirr(   r(   r)   r…     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kr^|d }|dd… }nd}t|ƒdkr¶|d | jk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.r7   r|   rA   rB   éÿÿÿÿNzHProblem installing fixture '%s': %s is not a known serialization format.r^   )r‚   r$   r   Úrsplitr‹   rN   rI   rz   )r&   rr   Úpartsru   rt   Únamer(   r(   r)   ra   +  s&     
zCommand.parse_name)Ú__name__Ú
__module__Ú__qualname__r   Zmissing_args_messager*   r3   r/   rR   Ú	functoolsÚ	lru_cacherQ   r   r…   ra   r(   r(   r(   r)   r       s   B?:r   c               @   s   e Zd Zdd„ Zdd„ ZdS )rM   c             O   s0   t jj| f|ž|Ž t|  ¡ ƒdkr,tdƒ‚d S )NrB   z.Zip-compressed fixtures must contain one file.)ÚzipfileÚZipFileÚ__init__r‹   ÚnamelistÚ
ValueError)r&   r   Úkwargsr(   r(   r)   r¡   N  s    zSingleZipReader.__init__c             C   s   t j | |  ¡ d ¡S )Nr   )rŸ   r    Úreadr¢   )r&   r(   r(   r)   r¥   S  s    zSingleZipReader.readN)rš   r›   rœ   r¡   r¥   r(   r(   r(   r)   rM   L  s   rM   c             C   s   | rd|  S dS )Nz'%s'zabsolute pathr(   )r{   r(   r(   r)   re   W  s    re   ))r�   rˆ   rK   rb   r6   rm   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;   rO   ÚImportErrorr‚   r   r    rM   re   r(   r(   r(   r)   Ú<module>   s6    
  .