
    BhE              	          d dl Z d dlZd dl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 d dlmZmZ d dlmZ d d	lmZmZmZmZmZ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, e	rd dl-m.Z. dZ/d Z0d Z1d Z2 e2       \  a3a4d Z5d2de6de6de
e6   fdZ7d2dZ8d Z9 G d d      Z: G d d e:      Z;d! Z<	 d3d"Z=de6d#e>d$   fd%Z?ej                  d&u sej                  j                  d'      r ee?      Z?d( ZCd) ZDd* ZEd+ej                  d#eGe6e*f   fd,ZHd+eej                  e)df   d#e>d$   fd-ZI	 d2d.ej                  d/e6d0e
eK   d#e*fd1ZLy)4    N)OrderedDictdefaultdict)cache)TYPE_CHECKINGOptionalUnion)settings)ImproperlyConfigured)
connectionmodels)Q)ContextNodeListTemplateTemplateSyntaxErrorVariableengines)VariableNode)get_template)	BlockNodeExtendsNodeIncludeNode)get_varname)DuplicatePlaceholderWarning)EmptyPageContentPlaceholder)get_cms_settingDeclaredPlaceholder   c                 ^    t        | d      r| j                  j                  S | j                  S )Ntemplate)hasattrr"   nodelist)tpls    E/home/dcms/DCMS/lib/python3.12/site-packages/cms/utils/placeholder.py_get_nodelistr'   $   s&    sJ||$$$||    c                  J    t         t               } t        d      | _        | S i S )N )r   r   r   r"   )contexts    r&   get_contextr,   +   s%    )#B<	r(   c            	        	
 t        d      	t        d 	D              } d	
fd	
i }	j                         D ]Y  \  }}d|v rK 
|d         ||<   ||   j                  |j                         D ci c]  \  }}|dk7  s|| c}}       U|||<   [ t	        t
              }|j                         D ]%  \  }}|j                         D ]  \  }}|||   |<    ' || fS c c}}w )a(  Convert CMS_PLACEHOLDER_CONF into a faster to access format since it is accessed many times
    in each request response cycle:
    * setting key on the first level dict, scope on the second level
    * resolve inheritance
    * Check of template-specific configs exist (expensive to access)
    PLACEHOLDER_CONFc              3   *   K   | ]  }|sd |v   yw)z.htmN ).0keys     r&   	<genexpr>z,_get_placeholder_settings.<locals>.<genexpr><   s     @SC6S=@s   	c                     |
t               }| |v rt        d|  d      |j                  |        d|    v r: |    d   |      j                         D ci c]  \  }}|dk7  s|| c}}z  S |    S c c}}w )Nz>Circular inheritance detected in CMS_PLACEHOLDER_CONF at key ''inherit)setr
   additems)r2   visitedkvconfresolve_inheritances       r&   r>   z6_get_placeholder_settings.<locals>.resolve_inheritance>   s    ?eG'>&)ghkgllm'nooCS	!&tCy';WEZ^ZdZdZfHyRVRSUVjkoxjxAHyyyCy Izs   A8(A8r6   N)r   anyr9   updater   dict)template_in_confnew_confr2   valuer;   r<   r	   settingsetting_valuer=   r>   s            @@r&   _get_placeholder_settingsrH   4   s     -.D@D@@ Hjjl "
U/i0@AHSMSM  5;;=!S41aAN!Q$!ST!HSM" 4 Hnn& 3
U&+kkm 	3"G]%2HWc"	33 %%% "Ts   2C  C c                      t               \  aay r?   )rH   _placeholder_settings_template_in_confr0   r(   r&   _clear_placeholder_conf_cacherL   \   s     0I/J,,r(   rF   placeholderr"   c                     | t         vr|S t        r|| d| |t        |      dg}n|dg}t         |    }|D ]  }|j                  |d      x}|c S  |S )a  
    Returns the placeholder configuration for a given setting. The key would for
    example be 'plugins' or 'name'.

    Resulting value will be the last from:

    CMS_PLACEHOLDER_CONF[None] (global)
    CMS_PLACEHOLDER_CONF['template'] (if template is given)
    CMS_PLACEHOLDER_CONF['placeholder'] (where placeholder denotes a slot)
    CMS_PLACEHOLDER_CONF['template placeholder'] (if template is given)

    Template is only evaluated if the placeholder configuration contains key with ".html" or ".htm"
    N )rJ   rK   strget)rF   rM   r"   defaultkeysr=   r2   rE   s           r&   get_placeholder_confrT   b   s|     ++X1*Ak]+[#h-NT" )D XXc4((E5L Nr(   c           	         d}|r|j                         }t        d||i       }t        d||i       }g }| D ]  }|j                  |j                  |j	                  |j                  |j
                        |j	                  |j                  |j                        |j                  xr |j                   d        t        |t        j                  d            S )a  
    Return the list of plugins to render in the toolbar.
    The dictionary contains the label, the classname and the module for the
    plugin.
    Names and modules can be defined on a per-placeholder basis using
    'plugin_modules' and 'plugin_labels' attributes in CMS_PLACEHOLDER_CONF

    :param plugins: list of plugins
    :param slot: placeholder slot name
    :param page: the page
    :return: list of dictionaries
    Nplugin_modules)rR   plugin_labels)rE   namemoduleadd_formrY   )r2   )r   rT   appendrE   rQ   rX   rY   show_add_formdisable_editsortedoperator
itemgetter)pluginsslotpager"   modulesnames	main_listplugins           r&   get_toolbar_plugin_structrh      s     H$$&"#3T8RPG $"MEI  
		&,,<!++fllFMMB"00L9L9L5L		

 )!4!4X!>??r(   c                     t        | t              st        d      	 | j                  d       y # t        $ r t        d      w xY w)Nz8Placeholder identifier names need to be of type string. asciia   Placeholder identifiers names may not contain non-ascii characters. If you wish your placeholder identifiers to contain non-ascii characters when displayed to users, please use the CMS_PLACEHOLDER_CONF setting with the 'name' key to specify a verbose name.)
isinstancerP   r
   encodeUnicodeEncodeError)rX   s    r&   validate_placeholder_namern      sN    dC "F
 	
	
G 
"-
 	

s	   / Ac                       e Zd ZdZd Zd Zy)PlaceholderNoActionFc                      y)NFr0   selfkwargss     r&   copyzPlaceholderNoAction.copy   s    r(   c                     g S r?   r0   rr   s     r&   get_copy_languagesz&PlaceholderNoAction.get_copy_languages   s    	r(   N__name__
__module____qualname__can_copyru   rw   r0   r(   r&   rp   rp      s    Hr(   rp   c                       e Zd ZdZd Zd Zy)MLNGPlaceholderActionsTc                     ddl m}  |j                  j                  di ||i}|j                  j                  |j                  |      }	t        |	|d       }
|
sy ||
j                         ||      S )Nr   )copy_plugins_to_placeholder)masterlanguage_codeF)rM   languager0   )cms.utils.pluginsr   objectsrQ   r   getattrget_plugins_list)rs   target_placeholdersource_language	fieldnamemodeltarget_languagert   r   trgtsrcsource_placeholders              r&   ru   zMLNGPlaceholderActions.copy   sx     	B u}}  CI/A#BCmmt{{/R$S)T:!*//1*$
 	
r(   c                    |j                   } |j                  di ||i}t        |j                        }|t        di d|z  diz  }|t        |j                         z  }|j                  |      j                  dd      j                         }|D 	cg c]   }	|	t        t        j                        |	   f" c}	S c c}	w )	N)r   z%s__cmsplugin__isnullF)pkr   T)flatr0   )r   rQ   r   r   r   filtervalues_listdistinctrB   r	   	LANGUAGES)
rs   rM   r   r   rt   managerr   querylanguage_codeslcs
             r&   rw   z)MLNGPlaceholderActions.get_copy_languages   s    --gkk5Y45$B-	95ABB!svv, NN5!--oD-IRRT 	 >LLrT(,,-b12LLLs   %C Nrx   r0   r(   r&   r~   r~      s    H
.
Mr(   r~   c                     t               }| j                  |      }|j                         D ]"  \  }}||   }|D ]  }|j                  |        $ y r?   )r   rQ   r9   r[   )r+   changesvarnamesekizai_containerr2   valuessekizai_namespacerE   s           r&   restore_sekizai_contextr      sY    mGG,}} ,V-c2 	,E$$U+	,,r(   c           	         ddl m} |s|}g }|g }| D ]B  }t        ||      r|j                  |       "t        |t              r|j
                  s?t        t        |j
                  dd             sEt        |j
                  j                  t              rt        |j
                  j                        }n|j
                  }|t        t        |      ||      z  }t        |t              r|t        ||      z  }t        |t              rm|rk|j                   j"                  dk(  st%        |j&                  d      st)        d      |t        t        |j&                        ||j&                        z  }gt        |t*              r|j,                  |v rt%        |d      r`|j.                  D ]O  }t%        ||      st        ||      }	t        |	t0              s-t        |t*              r|}|t        |	|||      z  }Q t3        |      D ]B  }
t        ||
      }t        |t0              s t        |t*              r|}|t        ||||      z  }D E |S )Nr   )r   renderzblock.superr$   z6Cannot render block.super for blocks without a parent.child_nodelists)cms.templatetags.cms_tagsr   rk   r[   r   r"   callabler   varr   r   _scan_placeholdersr'   r   "_get_placeholder_nodes_from_extendr   filter_expressiontokenr#   superr   r   rX   r   r   dir)r$   
node_classcurrent_blockignore_blocksr   nodesnoder"   nodelist_namesubnodelistattrobjs               r&   r   r      s0    6 
E  :dJ'LLk*}}  x FG "$--"3"3X> #/0A0A#B#}}H+!(+Z  k*7jIIEl+%%++}<}22J?-P  +!-"5"56
MDWDW  i(TYY--G T,-!%!5!5 4/")$">K!+x8%dI6,0M!3']M"  D	 dD)c8,!$	2(,/Z Eg:v Lr(   returnr   c                 8   t        |       }g }t        t        |            }g }|D ]q  }|j                         }|j                  }||v r"t        j                  d| d|  dt               Et        |       |j                  |       |j                  |       s |S )NzDuplicate {% placeholder "z" %} in template .)
r   r   r'   get_declarationrb   warningswarnr   rn   r[   )r"   compiled_templateplaceholdersr   clean_placeholdersr   rM   rb   s           r&   get_placeholdersr   >  s    $X.L}->?@E ,**,%%MM-dV3I(STU+
 &d+,%%d+, r(   FDJANGO_TESTSc                    | j                  t                     }t        |      }|j                  t              }|j                  t
              }|rt        |d         }n
t               }|D ]  }|||j                  <    t        |       j                  t              }|D ]3  }|j                  |v r||j                     |_	        |||j                  <   5 |S )Nr   )

get_parentr,   r'   get_nodes_by_typer   r   _get_block_nodesr   rX   r   )extend_nodeparentparent_nodelistparent_nodesparent_extend_nodesr   r   current_nodess           r&   r   r   Z  s    ##KM2F#F+O"44Y?L);;KH !!4Q!78   dii  "+.@@KM  99tyy)DJdii	 
 Lr(   c           	         t        |       }t        |j                               }g }|j                         D ](  }|j	                  t        t        |      |||             * t        |       }|t        t        |      |d|      z  }|S )z`
    Returns a list of placeholders found in the parent template(s) of this
    ExtendsNode
    N)r   listrS   r   extendr   r'   _find_topmost_template)r   r   block_nodesblock_namesr   blockparent_templates          r&   r   r   v  s     #;/K{'')*KL##% 
}U3ZT	

 -[9O&o&
D+ L r(   c                     | j                  t                     }t        |      j                  t              D ]  }t        |      c S  | j                  t                     S r?   )r   r,   r'   r   r   r   )r   r   r   s      r&   r   r     sS    !,,[];Oo.@@M ,%d++, !!+-00r(   r   c                    ddl m}m} t        |       }|D ci c]  }|j                  d  }}|j                  |j                  j                  |       D ci c]  }|j                  |v s|j                  |  c}       |j                         D cg c]  \  }}|	 |||        }}}|rt        j                  j                  rT|j                  j                  |       |D ]2  }d|j                  j                         i|_        |||j                  <   4 |S |D ]B  }|j                          d|j                  j                         i|_        |||j                  <   D |S c c}w c c}w c c}}w )Nr   )	CMSPluginr   )rb   sourcecmsplugin_set)
cms.modelsr   r   !get_declared_placeholders_for_objrb   rA   r   get_for_objr9   r   features can_return_rows_from_bulk_insertbulk_createnone_prefetched_objects_cachesave)	r   r   r   declared_placeholdersplr   rM   rb   new_placeholderss	            r&   rescan_placeholders_for_objr     s   1=cB,ABbBGGTMBLB +J]J]JiJijmJn  T;r}  sC  sC  GS  sS));6  T  U UaTfTfTh  A?Pt[lwlc:  A  A??++,<=/ =9H)J[J[J`J`Jb8c51<[--.=   0 =  "9H)J[J[J`J`Jb8c51<[--.	= - C T As   E&E+*E+
E0E0c                 0    t        | dd              }|rt        |      S t        | d      rDddlm} | j                         D cg c]$  }t        |t              r
 ||d      n |d	i |& c}S t        d| j                  j                  z        c c}w )
a  Returns declared placeholders for an object. The object is supposed to either have a method
    ``get_placeholder_slots`` which returns the list of placeholders or a method ``get_template``
    which returns the template path as a string that renders the object. ``get_declared_placeholders`` returns
    a list of placeholders used in the template by the ``{% placeholder %}`` template tag.
    r   c                       y r?   r0   r0   r(   r&   <lambda>z3get_declared_placeholders_for_obj.<locals>.<lambda>  s    r(   get_placeholder_slotsr   r   F)rb   r6   z@%s should implement either get_placeholder_slots or get_templater0   )r   r   r#   r   r   r   rk   rP   NotImplementedError	__class__ry   )r   r"   r   rb   s       r&   r   r     s     :wsNL9;H))s+,A 113
 >Hc=RT59XkXsnrXss
 	
 JS]]McMcc 	
s   )Bplaceholder_relationrb   default_widthc                     t        |d      r	 | j                  |      S | j                  ||      d   S # t        j                  $ r t	        |      j                  |      cY S w xY w)zRetrieves the placeholder instance for a PlaceholderRelationField either by scanning the template
    of the template_obj (if given) or by creating or getting a Placeholder in the database
    r   )rb   )rb   r   r   )r#   rQ   r   DoesNotExistr   get_or_create)r   rb   template_objr   s       r&   get_placeholder_from_slotr     su     |^,	G'+++66 $11t=1YZ[\\ '' 	G.|<@@FF	Gs   6 -A&%A&)NN)NNN)Mr_   osr   collectionsr   r   	functoolsr   typingr   r   r   django.confr	   django.core.exceptionsr
   	django.dbr   r   django.db.models.query_utilsr   django.templater   r   r   r   r   r   django.template.baser   django.template.loaderr   django.template.loader_tagsr   r   r   sekizai.helpersr   cms.exceptionsr   r   r   r   cms.utils.confr   r   r   RANGE_STARTr'   r,   rH   rJ   rK   rL   rP   rT   rh   rn   rp   r~   r   r   r   r   DEBUGenvironrQ   r   r   r   ModelrB   r   r   Managerintr   r0   r(   r&   <module>r     s    	  0  1 1   7 ( *  . / K K ' 6 4 *= "&J ,E+F ( (K# C 8C= <"@J
$ $M0 $MN, BFJZs t,A'B , >>Ubjjnn^< -.821V\\ d3;K6L :5?OQU1U+V [_`u[v . hl] ..]03]W_`cWd]]r(   