o
    ›¨ÊhD  ã                   @   st   d dl Z d dl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 d dlmZ G dd	„ d	eƒZdS )
é    N)Úsettings)ÚImproperlyConfigured)ÚBaseCommandÚCommandError)Úloader)ÚconnectionsÚ	constants)ÚSolrSearchBackendc                   @   sL   e Zd ZdZdZdZdd„ Zdd„ Zdd	„ Zefd
d„Z	dd„ Z
dd„ ZdS )ÚCommandz½Generates a Solr schema that reflects the indexes using templates  under a django template dir 'search_configuration/*.xml'.  If none are  found, then provides defaults suitable to Solr 6.4zsearch_configuration/schema.xmlz#search_configuration/solrconfig.xmlc                 C   sH   |j dddd� |j ddtjdd� |j d	d
dd� |j dddd� d S )Nz-fz
--filenamezZGenerate schema.xml directly into a file instead of stdout. Does not render solrconfig.xml)Úhelpz-uz--usingz/Select a specific Solr connection to work with.)Údefaultr   z-cz--configure-directorya  Attempt to configure a core located in the given directory by removing the managed-schema.xml(renaming) if it  exists, configuring the core by rendering the schema.xml and  solrconfig.xml templates provided in the django project's  TEMPLATE_DIR/search_configuration directoriesz-rz--reload-corez¨If provided, attempts to automatically reload the solr core via the urls in the "URL" and "ADMIN_URL" settings of the SOLR HAYSTACK_CONNECTIONS entry. Both MUST be set.)Úadd_argumentr   ÚDEFAULT_ALIAS)ÚselfÚparser© r   ú`/var/www/html/env/lib/python3.10/site-packages/haystack/management/commands/build_solr_schema.pyÚadd_arguments   s*   ýüý	
ýzCommand.add_argumentsc              
   K   s   |  d¡}tt|  ¡ tƒstd| ƒ‚| j|tjd�}| j|tj	d�}|  d¡}|  d¡}|  d¡}|rN| j
 d |¡¡ |  ||¡ |rNt|  ¡  ¡  |rÍ| j
 d |¡¡ tj |d	¡}tj |¡r…z
t |d
| ¡ W n ty„ }	 ztd ||	¡ƒ‚d}	~	ww tj |d¡}
z|  |
|¡ W n ty¨ }	 ztd |
|	¡ƒ‚d}	~	ww tj |d¡}z|  ||¡ W n tyÌ }	 ztd ||	¡ƒ‚d}	~	ww |�r:tj| d  dd¡d }dtj| vrêtd| ƒ‚dtj| vr÷td| ƒ‚z%| j
 d |¡¡ tj tj| d d|dœd�}|j�std |¡ƒ‚W n t�y%   ‚  t�y9 }	 ztd ||	¡ƒ‚d}	~	ww |�sJ|�sL|�sN|  |¡ dS dS dS dS )z2Generates a Solr schema that reflects the indexes.Úusingú%'%s' isn't configured as a SolrEngine)r   Útemplate_filenameÚfilenameÚconfigure_directoryÚreload_corez)Trying to write schema file located at {}z&Trying to configure core located at {}zmanaged-schemaz%s.oldz/Could not rename old managed schema file {}: {}Nz
schema.xmlzCould not configure {}: {}zsolrconfig.xmlzCould not write {}: {}ÚURLú/é   éÿÿÿÿÚ	ADMIN_URLzL'ADMIN_URL' must be specified in the HAYSTACK_CONNECTIONS for the %s backendzF'URL' must be specified in the HAYSTACK_CONNECTIONS for the %s backendzTrying to reload core named {}ÚRELOAD)ÚactionÚcore)Úparamsu(   Failed to reload core â€“ Solr error: {}zFailed to reload core {}: {})ÚgetÚ
isinstancer   Úget_backendr	   r   Úbuild_templater
   Úschema_template_locÚsolrcfg_template_locÚstdoutÚwriteÚformatÚ
write_fileÚreloadÚosÚpathÚjoinÚisfileÚrenameÚOSErrorr   ÚEnvironmentErrorr   ÚHAYSTACK_CONNECTIONSÚrsplitÚrequestsÚokÚ	ExceptionÚprint_stdout)r   Úoptionsr   Ú
schema_xmlÚsolrcfg_xmlr   r   r   Úmanaged_schema_pathÚexcÚschema_xml_pathÚsolrconfig_pathr!   Úrespr   r   r   Úhandle4   s®   
ÿÿ


ÿÿÿÿ€ÿ
ÿ€ÿ
ÿ€ÿÿÿÿÿþÿÿ€ÿÿzCommand.handlec                 C   sX   t |  ¡ }t|tƒstd|j ƒ‚| t |  ¡  ¡ ¡\}}||t	j
t	jt	jt	jdœS )Nr   )Úcontent_field_nameÚfieldsÚdefault_operatorÚIDÚ	DJANGO_CTÚ	DJANGO_ID)r   r%   r$   r	   r   Úconnection_aliasÚbuild_schemaÚget_unified_indexÚall_searchfieldsr   ÚDEFAULT_OPERATORrG   rH   rI   )r   r   ÚbackendrD   rE   r   r   r   Úbuild_context’   s   
ÿÿúzCommand.build_contextc                 C   s    t  |¡}| j|d�}| |¡S )N)r   )r   Úget_templaterP   Úrender)r   r   r   ÚtÚcr   r   r   r&   ¦   s   

zCommand.build_templatec                 C   sX   | j  d¡ | j  d¡ | j  d¡ | j  d¡ | j  d¡ | j  d¡ | j |¡ d S )NÚ
z]Save the following output to 'schema.xml' and place it in your Solr configuration directory.
z]--------------------------------------------------------------------------------------------
)Ústderrr*   r)   )r   r<   r   r   r   r:   «   s   ÿÿzCommand.print_stdoutc                 C   sH   t |dƒ�}| |¡ t | ¡ ¡ W d   ƒ d S 1 sw   Y  d S )NÚw)Úopenr*   r.   ÚfsyncÚfileno)r   r   r<   Úschema_filer   r   r   r,   ¸   s   
"þzCommand.write_fileN)Ú__name__Ú
__module__Ú__qualname__r   r'   r(   r   rC   rP   r&   r:   r,   r   r   r   r   r
      s    ÿ^r
   )r.   r7   Údjango.confr   Údjango.core.exceptionsr   Údjango.core.management.baser   r   Údjango.templater   Úhaystackr   r   Úhaystack.backends.solr_backendr	   r
   r   r   r   r   Ú<module>   s    