
    NwgD                     z    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	e      Zy)
    N)settings)ImproperlyConfigured)BaseCommandCommandError)loader)connections	constants)SolrSearchBackendc                   @    e Zd ZdZdZdZd Zd Zd ZefdZ	d Z
d	 Zy
)CommandzGenerates 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                     |j                  ddd       |j                  ddt        j                  d       |j                  d	d
d       |j                  ddd       y )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-corezIf 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parsers     e/var/www/horilla/myenv/lib/python3.12/site-packages/haystack/management/commands/build_solr_schema.pyadd_argumentszCommand.add_arguments   s    . 	 	
 	++B	 	 	
 	#= 	 	
 	= 	 	
    c                    |j                  d      }t        t        |   j                         t              st        d|z        | j                  |t        j                        }| j                  |t        j                        }|j                  d      }|j                  d      }|j                  d      }|rc| j                  j                  dj                  |             | j                  ||       |r%t        |   j                         j                          |r| j                  j                  dj                  |             t        j                   j#                  |d	      }t        j                   j%                  |      r	 t        j&                  |d
|z         t        j                   j#                  |d      }
	 | j                  |
|       t        j                   j#                  |d      }	 | j                  ||       |rt.        j0                  |   d   j3                  dd      d   }dt.        j0                  |   vrt        d|z        dt.        j0                  |   vrt        d|z        	 | j                  j                  dj                  |             t5        j                   t.        j0                  |   d   d|d      }|j6                  st+        dj                  |            	 |s|s|s| j;                  |       yyyy# t(        $ r }	t+        dj                  ||	            d}	~	ww xY w# t,        $ r }	t+        dj                  |
|	            d}	~	ww xY w# t,        $ r }	t+        dj                  ||	            d}	~	ww xY w# t*        $ r  t8        $ r }	t+        dj                  ||	            d}	~	ww xY w)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%   resps                 r   handlezCommand.handle4   s   G$+e,88:<MN&'NQV'VWW((7+F+F ) 

 ))7+G+G * 
 ;;z*%kk*?@kk-0KK;BB8L OOHj1E"..0779KK8??@ST #%'',,/BDT"Uww~~12II18>Q3QR !ggll+>MO< !ggll+>@PQO= 007>EEc1MbQD("?"?"FF**,12  H99%@@**,12 
U!!"B"I"I$"OP||11%8E&.=
 ww&BII$O    3Kj) =H 3xo  &IPP/  $ "077M  $ ",33OSI <    U"#A#H#Hs#STTUsa   L M 8M. <A>N 	L?L::L?	M+M&&M+.	N7NNO,OOc                 p   t         |   j                         }t        |t              st	        d|j
                  z        |j                  t         |   j                         j                               \  }}||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_OPERATORrL   rM   rN   )r   r   backendrI   rJ   s        r   build_contextzCommand.build_context   s    e$002'#45&7':R:RR  &-%9%9002CCE&
"F #5 ) : :,,",,",,
 	
r   c                 r    t        j                  |      }| j                  |      }|j                  |      S )N)r   )r   get_templaterU   render)r   r   r   tcs        r   r*   zCommand.build_template   s4     12U+xx{r   c                 ~   | j                   j                  d       | j                   j                  d       | j                   j                  d       | j                   j                  d       | j                   j                  d       | j                   j                  d       | j                  j                  |       y )N
z]Save the following output to 'schema.xml' and place it in your Solr configuration directory.
z]--------------------------------------------------------------------------------------------
)stderrr.   r-   )r   r@   s     r   r>   zCommand.print_stdout   s    $$$l	
 	l	
 	$*%r   c                     t        |d      5 }|j                  |       t        j                  |j	                                d d d        y # 1 sw Y   y xY w)Nw)openr.   r2   fsyncfileno)r   r   r@   schema_files       r   r0   zCommand.write_file   sF    (C  	+Kj)HH['')*	+ 	+ 	+s   5AAN)__name__
__module____qualname__r   r+   r,   r   rG   rU   r*   r>   r0    r   r   r   r      s@    	> 	
 <@
<\*|
( 7J 
&+r   r   )r2   r;   django.confr   django.core.exceptionsr   django.core.management.baser   r   django.templater   haystackr   r	   haystack.backends.solr_backendr
   r   rg   r   r   <module>rn      s+    	    7 A " + <n+k n+r   