
    Bh3                       d dl mZ d dlZd dl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 d d	l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 d dlmZm Z  d dl!m"Z"m#Z#m$Z$ d dl%m&Z& d dl'm(Z( d dl)m*Z* d!d"dZ+d#d$dZ,d#d%dZ-d&dZ.d'dZ/	 	 d#	 	 	 	 	 	 	 	 	 d(dZ0d!d)dZ1d*dZ2d+dZ3d!d,dZ4d!d-dZ5d!d-dZ6d!d-dZ7d.d/d Z8y)0    )annotationsN)defaultdictdeque)AnyOptional)apps)ContentType)models)HttpRequest)NoReverseMatch)	force_str)get_languagegettextoverride)SekizaiContext)get_varname)PLACEHOLDER_TOOLBAR_JSPLUGIN_TOOLBAR_JS)	CMSPluginPageContentPlaceholder)RemovedInDjangoCMS51Warning)get_cms_setting)admin_reversec           	     *   | j                         xs d}t        d      d|iz  }dt        |      t        | j                        |xs g t        |      t        d      t        d      dd}t        | j                  t        j                  |      d	z  S )
N z1Add plugin to placeholder "%(placeholder_label)s"placeholder_labelplaceholdercms_placeholder_add_plugincms_placeholder_copy_plugins)
add_plugincopy_plugin)typenameplaceholder_idplugin_restrictionaddPluginHelpTitleurlspkconfig)		get_labelr   r   strr*   r   r   jsondumps)r   allowed_pluginslabel	help_textdatas        A/home/dcms/DCMS/lib/python3.12/site-packages/cms/toolbar/utils.pyget_placeholder_toolbar_jsr5      s    !!#)rE;	e$%I
 % knn--3'	2'(DE()GH

D ";>>TZZPTEU$VVV    c                    | j                  ||      }t        d      d|d   iz  }|j                  dt        |      d|xs g d       |S )NchildrenparentszAdd plugin to %(plugin_name)splugin_nameFr   )onCloser'   plugin_orderr&   )get_plugin_infor   updater   )pluginr9   r:   r3   r2   s        r4   get_plugin_toolbar_inforA   0   se    !!8W!ED'	]+,-I 	KK'	2&n"	  Kr6   c                p    t        | ||      }t        | j                  t        j                  |      dz  S )Nr8   r)   )rA   r   r*   r.   r/   )r@   r9   r:   r3   s       r4   get_plugin_toolbar_jsrC   >   s4    "D
 fii4::d;KLLLr6   c                ~    dd l }|j                  dt        d       t        j                  t        | |      d         S )Nr   zCget_plugin_tree_as_json is deprecated. Use get_plugin_tree instead.   )
stacklevel)warningswarnr   r.   r/   get_plugin_tree)requestpluginsrG   s      r4   get_plugin_tree_as_jsonrL   G   s9    MMW-!  =::ogw7:;;r6   c                    t        t              }t               }t        d | D              }t        |       D ]T  }||j                     |_        |j                  |v r||j                     j                  |       D|j                  |       V |S )z[Creates the ``child_plugin_instances`` attribute on each plugin instance after downcasting.c              3  4   K   | ]  }|j                     y wNr*   .0r@   s     r4   	<genexpr>z1create_child_plugin_references.<locals>.<genexpr>T   s     ;699;s   )r   r   	frozensetreversedr*   child_plugin_instances	parent_id
appendleft)rK   plugin_childrenroot_plugins
plugin_idsr@   s        r4   create_child_plugin_referencesr\   O   s    !%(O7L;7;;J7# ,(7		(B%z)F,,-88@##F+, r6   c                    ddl m}m g g }xs i t        |       }|j                  j
                  }t        |d   j                  }|j                  |j                  j                  k(  }	t         ||d            }t        |      }
fdt        |j                        5  |
D ]1  } |       || |	|d}|j                  |j                  |             3 	 ddd       j!                          i }r\t#        d |D              rJt%        fd	|D        d      }j&                  rt%        fd
|D        d      nd}t)        | |d|i      |d<   dj+                  |      d|S # 1 sw Y   xY w)a  
    Constructs a tree structure of CMS plugins for the toolbar.

    Args:
        request (HttpRequest): The HTTP request object.
        plugins (list[CMSPlugin]): A list of CMSPlugin instances to be organized into a tree.
        restrictions (Optional[dict], optional): A dictionary of plugin restrictions. Defaults to None.
        target_plugin (Optional[CMSPlugin], optional): The target plugin to render.
            Content will only be rendered if given. Defaults to None.

    Returns:
        tuple[dict[str, Any]]: A dictionary with up to five keys:
            - 'html': A string of rendered HTML for the plugin tree.
            - 'plugins': A flat list of plugin information dictionaries.
            - 'content': The rendered content of target_plugin (if given) including its children
            - 'target_position': The position of the target_plugin (if given)
            - 'target_placeholder_id': The placeholder id of the target_plugin (if given)
    r   )downcast_pluginsget_plugin_restrictionsT)select_placeholderc                     |       \  }} | ||      }	j                  |       | j                  D ]
  } |        y )N)r@   restrictions_cacher8   )appendrV   )
r@   child_classesparent_classesplugin_infoplugin_instancecollect_plugin_datar>   r_   restrictions	tree_datas
        r4   rh   z,get_plugin_tree.<locals>.collect_plugin_data   sZ    (?+)
%~ &""
 	%%<< 	1O0	1r6   )r@   rJ   	clipboardcms_toolbarNc              3  H   K   | ]  }|j                   j                    y wrO   )plugin_classis_localrQ   s     r4   rS   z"get_plugin_tree.<locals>.<genexpr>   s     PfV0099Ps    "c              3  V   K   | ]   }|j                   j                   k(  s| " y wrO   rP   rR   r@   target_plugins     r4   rS   z"get_plugin_tree.<locals>.<genexpr>   s"     KVYY-:J:J-JVK   ))c              3  V   K   | ]   }|j                   j                  k(  s| " y wrO   )r*   rW   rq   s     r4   rS   z"get_plugin_tree.<locals>.<genexpr>   s"     RVYY-:Q:Q-QVRrs   parentcontent
)htmlrK   )cms.utils.pluginsr^   r_   get_toolbar_from_request	templatesdrag_item_templaterA   r   r*   rk   listr\   force_languagetoolbar_languagerc   renderreverseallnextrW   get_plugin_contentjoin)rJ   rK   ri   rr   r^   tree_structuretoolbartemplater   copy_to_clipboardrZ   root_plugincontextrv   
downcastedru   rh   r>   r_   rj   s     ``            @@@@r4   rI   rI   `   s   0 LIN%2L&w/G  33H-O!*((K#'*;*;*>*>>#GEFG1':L1 1  
00	1 	<' 	<K,%".&	G !!(//'":;	<	< GPPPK'KT


 $$ R'RTX
*. 	 0hPVEWX	IIn-)OwOO1	< 	<s   .7E44E=c                   |i }t        |t              r|n|g}t        |       }|j                  }d|_        d|_        t        d| i|      }	 |D cg c]  }|j                  |||j                  d      dj                  |t                  j                  dg             dj                  |t                  j                  dg             |j                  |j                  t        |      d c}S c c}w # t        $ r g cY S w xY w)NTrJ   )r   editabler   jscss)rx   r   r   positionr%   	pluginIds)
isinstancer}   rz   content_rendereredit_mode_active_placeholders_are_editabler   render_pluginr   r   r   getr   r%   get_plugin_tree_ids	Exception)rJ   r@   r   plugin_listr   renderers         r4   r   r      s   &vt4&6(K&w/G''H#G*.H'i<G<=G
 $%  **67HZHZei*j'''+-044T2>?777;=155eR@A$33,V4
 % 	% %  	s%   C9 BC41C9 4C9 9DDc                f    | j                   g}| j                  xs g D ]  }|t        |      z  } |S rO   )r*   rV   r   )r@   r[   childs      r4   r   r      s=    ))J..4" 1)%00
1r6   c                4    ddl m} t        | d ||             S )N   )EmptyToolbarr   )r   r   getattr)rJ   r   s     r4   rz   rz      s    %7I|G'<==r6   c                    t        d      }t        | d      s|S 	 | j                         }|j	                  d      }t        |      dkD  r|d| d| z  }|S |d| d| z  }|S # t        $ r |cY S w xY w)a/  
    Append a live url to a given object url using a supplied url parameter configured
    by the setting: CMS_ENDPOINT_LIVE_URL_QUERYSTRING_PARAM

    :param obj: Placeholder source object
    :param url: Url string
    :param language: The current language code or None
    :returns: A url string
    #ENDPOINT_LIVE_URL_QUERYSTRING_PARAMget_absolute_url?r   &=)r   hasattrr   r   splitlen)objurllanguage	url_paramlive_urlurl_fragmentss         r4   add_live_url_querystring_paramr      s       EFI3*+
'') IIcNM
=A9+Qxj)) J 	9+Qxj))J  
s   A$ $A21A2c                ,   t         j                  j                  |       }t        | d|      }|
t	               }t        |      5  t        d|j                  | j                  g      }ddd       t        d      rt        | |      }S # 1 sw Y   #xY w)z
    Returns the url of the edit endpoint for the given object. The object must be frontend-editable
    and registered as such with cms.

    If the object has a language property, the language parameter is ignored.
    r   N"cms_placeholder_render_object_editargs+ENDPOINT_LIVE_URL_QUERYSTRING_PARAM_ENABLED
r	   objectsget_for_modelr   r   r~   r   r*   r   r   r   r   content_typer   s       r4   get_object_edit_urlr      s     &&44S9LsJ1H>		! b@Y\Y_Y_G`abDE,S#x@J	b b   $B

Bc                ,   t         j                  j                  |       }t        | d|      }|
t	               }t        |      5  t        d|j                  | j                  g      }ddd       t        d      rt        | |      }S # 1 sw Y   #xY w)z
    Returns the url of the preview endpoint for the given object. The object must be frontend-editable
    and registered as such with cms.

    If the object has a language property, the language parameter is ignored.
    r   N%cms_placeholder_render_object_previewr   r   r   r   s       r4   get_object_preview_urlr     s     &&44S9LsJ1H>		! eC<??\_\b\bJcdeDE,S#x@J	e er   c                    t         j                  j                  |       }t        | d|      }|
t	               }t        |      5  t        d|j                  | j                  g      cddd       S # 1 sw Y   yxY w)z
    Returns the url of the structure endpoint for the given object. The object must be frontend-editable
    and registered as such with cms.

    If the object has a language property, the language parameter is ignored.
    r   N'cms_placeholder_render_object_structurer   )r	   r   r   r   r   r~   r   r*   )r   r   r   s      r4   get_object_structure_urlr     sq     &&44S9LsJ1H>		! hFloo_b_e_eMfgh h hs   #A11A:c                ~   t        | d|      |k(  r| S t        | t              r!| j                  j	                  |d      xs dS t        | di       }|r|j                  |      S t        j                  d      j                  }| j                  }|j                  j                  |      }|sy|t        | |      i}|r&t        |d      r|j                  j                         n|j                  } |j                  di |D 	ci c]  }	|	j                   |	 c}	| _        | j"                  j                  |      S c c}	w )	a	  
    Retrieves the correct content object for the target language. The object must be frontend-editable
    and registered as such with cms.

    Two cases have to be distinguished:

    1. **Object has a language property:** If the language of the passed object is different,
       sibling objects are retrieved from the database and cached in the object passed.
    2. **Object has no language property:** The placeholders of the object contain the different
       language content. The object itself is returned
    r   F)fallbackN#_sibling_objects_for_language_cachecmsadmin_manager )r   r   r   pageget_admin_contentr   r   get_app_configcms_extension	__class__model_groupersr   r   latest_contentr   filterr   r   )
r   r   latestcached_object	extensionmodelfieldgrouper_filterqsresults
             r4   get_object_for_languager   (  s+    sJ)X5 
#{#xx))(U)CKtKC!FKM  **##E*88IMME$$((/E WS%01N17GE?<[			+	+	-afananB.7bii.I..I/$*/C+ 2266x@@/s   D:rO   )r   r   r0   Optional[list[str]]returnr-   )NN)r@   r   r9   r   r:   r   r   dict[str, Any])r@   r   r9   r   r:   r   r   r-   )rJ   r   rK   list[CMSPlugin]r   r-   )rK   r   r   zdeque[CMSPlugin])
rJ   r   rK   r   ri   zOptional[dict]rr   zOptional[CMSPlugin]r   r   )rJ   r   r@   zCMSPlugin | list[CMSPlugin]r   dictr   r   )r@   r   r   z	list[int])rJ   r   )r   models.Modelr   r-   r   zOptional[str]r   r-   )r   r   r   r-   r   r-   )F)r   r   r   r-   r   boolr   zOptional[models.Model])9
__future__r   r.   collectionsr   r   typingr   r   django.appsr   "django.contrib.contenttypes.modelsr	   	django.dbr
   django.httpr   django.urlsr   django.utils.encodingr   django.utils.translationr   r   r   r~   sekizai.contextr   sekizai.helpersr   cms.constantsr   r   
cms.modelsr   r   r   cms.utils.compat.warningsr   cms.utils.confr   cms.utils.urlutilsr   r5   rA   rC   rL   r\   rI   r   r   rz   r   r   r   r   r   r   r6   r4   <module>r      s    "  *    :  # & + 
 + ' C : : A * ,W(M<( $()-	MPMPMP !MP '	MP
 MP`0>2((h$%Ar6   