
    Bh0                         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mZ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mZ d dlmZmZ d dlmZ d dlm Z  d dl!m"Z" d dl#m$Z$  G d d      Z% e%       Z&y)    )
attrgetter)Optional)ImproperlyConfigured)TemplateDoesNotExistTemplateSyntaxError)slugify)URLResolverincludere_path)	force_str)cached_propertylazy)autodiscover_modules)activatedeactivate_allget_language)PluginAlreadyRegisteredPluginNotRegistered)Page)CMSPluginBase)get_cms_setting)normalize_namec                   &   e Zd Zd Zd Zd Zd ZddZd Zd Z		 dd	Z
d
eee      fdZd
ee   fdZd
ee   fdZd
ee   fdZed
eee      fd       Zed
eee      fd       Zed
eee      fd       Zddededee   fdZdZded
efdZy)
PluginPoolc                     i | _         d| _        d i it        d      j                         D ci c]  \  }}d|v sd|v r|i  c}}| _        t        d | j                  D              | _        y c c}}w )NFPLACEHOLDER_CONFparent_classeschild_classesc              3   ,   K   | ]  }d |xs dv   yw)z.htm N ).0keys     ?/home/dcms/DCMS/lib/python3.12/site-packages/cms/plugin_pool.py	<genexpr>z&PluginPool.__init__.<locals>.<genexpr>   s     /p#39"0E/ps   )plugins
discoveredr   itemsglobal_restrictions_cacheanyglobal_template_restrictions)selfr#   values      r$   __init__zPluginPool.__init__   s     "*
 *99K)L)R)R)T I:3"e+%/G Bw I	*
& -0/pQUQoQo/p,p)Is   A/c                     d| j                   v r| j                   d= d| j                   v r| j                   d= d| j                   v r| j                   d= y y )Nregistered_pluginsplugins_with_extra_menu#plugins_with_extra_placeholder_menu)__dict__r,   s    r$   _clear_cachedzPluginPool._clear_cached!   sS    4==023$5780DMMACD B    c                     | j                   ry t        d       d| _         t        t        | j                  j                         d             | _        y )Ncms_pluginsTc                 >    | d   j                   | d   j                  fS )N   )modulenamer#   s    r$   <lambda>z-PluginPool.discover_plugins.<locals>.<lambda>0   s     #a&--Y\]^Y_YdYdIe r6   r=   )r'   r   dictsortedr&   r(   r4   s    r$   discover_pluginszPluginPool.discover_plugins)   s<    ??]+F4<<#5#5#7=efgr6   c                 @    d| _         i | _        | j                          y )NF)r'   r&   r5   r4   s    r$   clearzPluginPool.clear2   s    r6   Nc                 r   |r|g}n| j                   j                         }|D ]  }|j                  rt        |j                        t        us"t        |j                  d      st        |d      rl|j                  t        |d      st        d|z        t        |d      rddl	m
} |j                  }t        |t              s|s	 |j                  |       |j                   st        d	|z         y# t        $ r*}t        |      |k(  rt        d|d|      Y d}~d}~wt        $ r Y w xY w)
z?
        Plugins templates are validated at this stage
        render_templateget_render_templateNzfCMS Plugins must define a render template, a get_render_template method or set render_plugin=False: %sr   )loaderz+CMS Plugins must define a render template (z) that exists: zJCMS Plugins can not define render_plugin=False and allow_children=True: %s)r&   valuesrender_plugintypepropertyhasattrmodelrE   r   django.templaterG   
isinstancestrget_templater   r   allow_children)r,   pluginr&   plugin_classrG   templatees          r$   validate_templateszPluginPool.validate_templates7   s;    hGll))+G# *	L**334HD<--/@A<)>?//7Vk@l.68DE  |-BC6+;;H!(C0X!"//9   ...dgss Q*	0  4 %
  #1v1&:'3X%?'" !"
 !%2 ! !s   	C88	D6D&&D65D6c                     t        |t              st        d|z        |j                  }|| j                  v rt        d|d|d      ||_        || j                  |<   |S )z
        Registers the given plugin(s).

        Static sanity checks is also performed.

        If a plugin is already registered, this will raise PluginAlreadyRegistered.
        z;CMS Plugins must be subclasses of CMSPluginBase, %r is not.zCannot register z, a plugin with this name (z) is already registered.)
issubclassr   r   __name__r&   r   r-   r,   rS   plugin_names      r$   register_pluginzPluginPool.register_plugink   sr     &-0&'dgm'mnnoo$,,&)"6*,GVno  #$*[!r6   c                 n    |j                   }|| j                  vrt        d|z        | j                  |= y)z
        Unregisters the given plugin(s).

        If a plugin isn't already registered, this will raise PluginNotRegistered.
        zThe plugin %r is not registeredN)rZ   r&   r   r[   s      r$   unregister_pluginzPluginPool.unregister_plugin   s6     oodll*%&G&&PQQLL%r6   c                 2  	
 ddl m} | j                  j                         }r t	        j
                  t                     nd } |||      xs d	 |d|      xs d
|s	d |D        }	r	fd|D        }
r
fd|D        }|rfd|D        }|S )	Nr   )get_placeholder_confr!   excluded_pluginsc              3   :   K   | ]  }|j                   r|  y wN)	page_only)r"   rS   s     r$   r%   z-PluginPool.get_all_plugins.<locals>.<genexpr>   s     L&6;K;KvL   c              3   @   K   | ]  }|j                   v s|  y wrd   rZ   )r"   rS   allowed_pluginss     r$   r%   z-PluginPool.get_all_plugins.<locals>.<genexpr>   s     Z&v/7YvZ   c              3   @   K   | ]  }|j                   vs|  y wrd   rh   )r"   rS   rb   s     r$   r%   z-PluginPool.get_all_plugins.<locals>.<genexpr>   s     _&vN^7^v_rj   c              3   H   K   | ]  }|j                        r|  y wrd   )requires_parent_plugin)r"   rS   pageplaceholders     r$   r%   z-PluginPool.get_all_plugins.<locals>.<genexpr>   s"     l&6;X;XYdfj;kvls   "")cms.utils.placeholderra   r&   rH   r   rQ   rP   )r,   ro   rn   setting_keyinclude_page_onlyroot_pluginra   r&   rU   ri   rb   s    ``      @@r$   get_all_pluginszPluginPool.get_all_plugins   s     	?,,%%'.2(D""C(* 	
 ! 
  	 !" 
  	 ! MGLGZGZG_G_GlGlGr6   returnc                     t        | j                  ||d            }|j                  | j                  ||dd             t        d |D        t	        dd            S )	NFrs   text_only_plugins)rq   rs   c              3   :   K   | ]  }|j                   s|  y wrd   )text_enabled)r"   ps     r$   r%   z6PluginPool.get_text_enabled_plugins.<locals>.<genexpr>   s     <QQ^^q<rf   r;   r<   r=   )setrt   updater@   r   )r,   ro   rn   r&   s       r$   get_text_enabled_pluginsz#PluginPool.get_text_enabled_plugins   s[    d**;%*PQt++KK^lq+rs<'<*XW]B^__r6   c                      | j                   |   S )z3
        Retrieve a plugin from the cache.
        )r&   )r,   r<   s     r$   
get_pluginzPluginPool.get_plugin   s     ||D!!r6   c                 l   | j                          t               }t                	 g }| j                  D ]a  } |       }t	        t        t        |j                  j                                    }|t        d| dt        |j                              gz  }c 	 t        |       |S # t        |       w xY w)Nz^plugin//)rA   r   r   r0   r   r   r   	__class__rZ   r   r
   plugin_urlsr   )r,   langurl_patternsrS   r{   slugs         r$   get_patternszPluginPool.get_patterns   s     ~
	L11 Hy8L8L)MNOxvQ/1GH!  TN TNs   A2B& &B3c                     | j                   j                         D cg c]  }|j                  s|j                   c}S c c}w rd   )r&   rH   systemrZ   )r,   rS   s     r$   get_system_pluginszPluginPool.get_system_plugins   s-    .2ll.A.A.CUFv}}UUUs
   A A c                 P    t        | j                  d      t        dd            S )NFrw   r;   r<   r=   )r@   rt   r   r4   s    r$   r0   zPluginPool.registered_plugins   s%    d**u*=:hX^C_``r6   c                 \    | j                   D cg c]  }|j                  s| }}|S c c}w rd   )r0   _has_extra_plugin_menu_itemsr,   clsplugin_classess      r$   r1   z"PluginPool.plugins_with_extra_menu   s/    )-)@)@e#CDdDd#ee f   ))c                 \    | j                   D cg c]  }|j                  s| }}|S c c}w rd   )r0   !_has_extra_placeholder_menu_itemsr   s      r$   r2   z.PluginPool.plugins_with_extra_placeholder_menu   s/    )-)@)@j#CDiDi#jj kr   request_cacheinstancern   c                    | j                  |j                        }| j                  |      s|S |j                  j                  }| j
                  r|j                  |      }nd}|r'| d| | j                  v r| j                  | d|    S |r|| j                  v r| j                  |   S |r|| j                  v r| j                  |   S | j                  d   S )a  
        Retrieve the restrictions cache for a given plugin instance.

        This method checks if the plugin class can be cached globally. This is the case if the
        plugin restrictions only depend on template and placeholder slot as described by the
        CMS_PLACEHOLDER_CONF setting.

        If it can, it retrieves the appropriate restrictions cache based on the template and slot
        of the plugin instance's placeholder. If not, it returns the (local) request cache which will
        be recalculated for each request.

        Args:
            request_cache (dict): The current request cache (only filled is non globally cacheable).
            instance (CMSPluginBase): The plugin instance for which to retrieve the restrictions cache.
            page (Optional[Page]): The page associated with the plugin instance, if any.

        Returns:
            dict: The restrictions cache for the given plugin instance - or the cache valid for the request.
        r     N)r   plugin_typecan_cache_globallyro   slotr+   _get_template_for_confr)   )r,   r   r   rn   rT   r   rU   s          r$   get_restrictions_cachez!PluginPool.get_restrictions_cache   s    ( x';';<&&|4  ##((,,#::4@HH8*AdV,0N0NN11XJav2FGGD$B$BB11(;;DD:::11$77--d33r6   )get_require_parentget_child_class_overridesget_parent_classesrT   c                 z    t        d      s#t        fd| j                  D              _        j                  S )a  
        Check if the restrictions for a given plugin class can be cached globally.

        This is the case if the plugin restrictions only depend on template and placeholder slot as
        described by the CMS_PLACEHOLDER_CONF setting.

        Args:
            plugin_class (CMSPluginBase): The plugin class for which to check if restrictions can be cached globally.

        Returns:
            bool: True if the restrictions can be cached globally, False otherwise.
        _cache_restrictions_globallyc              3   J   K   | ]  }t        t        |      d         yw)_template_slot_cachingN)rL   getattr)r"   method_namerT   s     r$   r%   z0PluginPool.can_cache_globally.<locals>.<genexpr>  s(      < k:<TU<s    #)rL   allrestriction_methodsr   )r,   rT   s    `r$   r   zPluginPool.can_cache_globally  s>     |%CD8; <#'#;#;< 9L5 888r6   rd   )NNr&   TT)rZ   
__module____qualname__r.   r5   rA   rC   rW   r]   r_   rt   listrJ   r   r~   r   r	   r   rP   r   r   r0   r1   r2   r?   r   r   r   r   boolr   r!   r6   r$   r   r      s2   
qEh
2h(	& gk+Z`T$}BU=V `
"$}"5 "d;/ *VDI V aDm)<$= a a d=.A)B   T$}:M5N  #4D #4M #4YabfYg #4J d9} 9 9r6   r   N)'operatorr   typingr   django.core.exceptionsr   rN   r   r   django.template.defaultfiltersr   django.urlsr	   r
   r   django.utils.encodingr   django.utils.functionalr   r   django.utils.module_loadingr   django.utils.translationr   r   r   cms.exceptionsr   r   cms.models.pagemodelr   cms.plugin_baser   cms.utils.confr   cms.utils.helpersr   r   plugin_poolr!   r6   r$   <module>r      sN      7 E 2 5 5 + 9 < K K G % ) * ,M9 M9` lr6   