
    h                         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 d dlmZ d d	lmZ d d
lmZ dedeeeef      fdZ G d d      Z G d d      Z e       Zd Zy)    N)defaultdict)Iterator)forms)apps)get_template)autodiscover_modules)SekizaiContext)settings)CMSFrontendComponent	subfolderreturnc                 j   g }t        j                         D ]  }t        j                  j	                  |j                  d|j
                  |       }t        j                  j                  |      sZt        j                  |      D ]  \  }}}|D ]  }|j                  d      s|j                  d      s&t        j                  j                  t        j                  j	                  ||      |      }|j                  |j                  j                  |j
                   d|  d| f          |S )N	templatesz.htmlz.htm/)r   get_app_configsospathjoinlabelexistswalkendswithrelpathappendmodule__name__)	r   r   appapp_template_dirroot_filesfilerelative_paths	            Q/home/dcms/DCMS/lib/python3.12/site-packages/djangocms_frontend/component_pool.pyfind_cms_component_templatesr%      s    I##% l77<<+syy)T77>>*+"$''*:"; la! lD}}W-v1F(*T48PRb(c!((#***=*=#))Ai[XYZgYh?i)jklll     c            	       ~    e Zd Zdddg ddZd ZdefdZed	ed
e	e   de
defd       Zde	ee
e
f      dee   fdZy)CMSAutoComponentDiscoveryz#djangocms_text.fields.HTMLFormFieldz,djangocms_text_ckeditor.fields.HTMLFormFieldz#djangocms_link.fields.LinkFormField)z6djangocms_frontend.contrib.image.fields.ImageFormFieldz6djangocms_frontend.contrib.icon.fields.IconPickerFieldz-djangocms_frontend.fields.AttributesFormFieldz*djangocms_frontend.fields.ChoicesFormField)djangocms_textdjangocms_text_ckeditordjangocms_linkdjangocms_frontendc                     | j                   j                  t        j                         t	        t        j
                        }| j                  |      }|D ]  }|j                  |        y N)default_field_contextupdater
   COMPONENT_FIELDSr%   COMPONENT_FOLDER(scan_templates_for_component_declarationregister)selfregister_tor   auto_components	components        r$   __init__z"CMSAutoComponentDiscovery.__init__+   sY    ""))(*C*CD01J1JK	GG	R( 	,I  +	,r&   r   c                 l   i }| j                   j                         D ]  \  }}t        j                  |      rZt	        |t
              s|g}|D ]A  }d|v s|j                  dd      \  }}t        j                  |      j                  |   ||<   C u|dvszt        j                  d| dd        |S )N.   )r)   r*   zApp z+ not installed, skipping field registration   
stacklevel)r/   itemsr   is_installed
isinstancelistrsplit	importlibimport_module__dict__warningswarn)r5   field_contextkeyvaluefieldr   
field_names          r$   get_field_contextz+CMSAutoComponentDiscovery.get_field_context2   s    44::< 		eJC  %!%."GE" iEe|-2\\#q-A*
4=4K4KF4S4\4\]g4hj1i IISE)TUbcd		e r&   r8   fieldstemplatec                     |\  }}|\  }||d<   t        dd|      }t        |t        f|| d|D ci c];  \  }}t        |d   t        j                        r|d    |d   j
                  di |= c}}      S c c}}w )Nrender_templateMeta )rT   
__module__r<   r   )typer   rB   r   Field	__class__)r   r8   rP   rQ   argskwargsnamemetas           r$   component_factoryz+CMSAutoComponentDiscovery.component_factory@   s     f$, !FB'!#$	 )/ %f!$q'5;;7 G.T!W..888	
 	
s   A A:r   c              #   0  K   ddl m} | j                         }|D ]  \  }}t        t	        t
              |i d|      }	 t        |      }|j                  j                  |       |d   j                  dg       }|d   j                  dg       }	t        |      dk(  r| j                  ||d   |	|       nt        |      dkD  rt        d|        y # t        $ r3 dd l}
|
j                  t               }|j#                  d	| d
d       Y w xY ww)Nr   )rP   )_cms_componentsr   instancer`   cms_componentrP   r<   z%Multiple cms_component tags found in zError rendering template z$ to scan for cms frontend componentsT)exc_info)django.formsrP   rO   r	   r   rC   r   rQ   rendergetlenr^   
ValueError	Exceptionlogging	getLoggerr   error)r5   r   rP   rJ   r   template_namecontextrQ   rb   discovered_fieldsrj   loggers               r$   r3   zBCMSAutoComponentDiscovery.scan_templates_for_component_declarationV   s2     	(..0%. 	!FM$$/$5TVhZghG'6!!((1 '(9 : > >PR S$+,=$>$B$B8R$P!}%*00q9IK\^kll'!+$'L]O%\]]	   **84/>bcnr  s*   =D BCD9DDDDN)r   rV   __qualname__r/   r9   dictrO   staticmethodtuplerC   strr   r^   r   r3   rU   r&   r$   r(   r(      s    ?#Q?
	
,4  
U 
DK 
SV 
[o 
 
*eCHo.	&	'r&   r(   c                   6    e Zd ZU i Zeed<   dZeed<   d Zd Z	y)
Components	_registryF_discoveredc                     |j                   | j                  v r't        j                  d|j                    dd       |S |j	                         | j                  |j                   <   |S )Nz
Component z already registeredr=   r>   )r   rx   rH   rI   get_registration)r5   r8   s     r$   r4   zComponents.registerx   s]    /MMJy'9'9&::MN[\]-6-G-G-Iy))*r&   c                      | j                   |   S r.   )rx   )r5   items     r$   __getitem__zComponents.__getitem__   s    ~~d##r&   N)
r   rV   rq   rx   rr   __annotations__ry   boolr4   r~   rU   r&   r$   rw   rw   t   s     ItK$r&   rw   c                     t         j                  swddlm}  t	        dt                t        t                 |         dt         _        t        j                  d      r(ddlm	} t        j                  d      } |       |_        y y y )	Nr<   )update_plugin_poolcms_components)r6   Tr)   r   )discover_inline_editable_models)
componentsry   cms_pluginsr   r   r(   r   rA   djangocms_text.appsr   get_app_configinline_models)r   r   text_configs      r$   setupr      sl    !!3 	-:F!j9!%
-. L--.>?K(G(IK% / "r&   )rE   r   rH   collectionsr   collections.abcr   djangor   django.appsr   django.template.loaderr   django.utils.module_loadingr   sekizai.contextr	   r,   r
   !djangocms_frontend.component_baser   ru   rC   rt   r%   r(   rw   r   r   rU   r&   r$   <module>r      so     	  # $   / < * ' B
C 
DsCx4I 
S Sl$ $ \
Jr&   