
    h5                     ^   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 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dl#m(Z(m)Z) ddl*m+Z+m,Z,m-Z- ddl.m/Z0 ddl.m1Z1m2Z2 dgZ3ejh                   G d de             Z/y)    )Page)CMSPluginBasePluginMenuItem)plugin_pool)get_object_edit_urlget_plugin_toolbar_infoget_plugin_tree)get_language_from_request)get_model_permission_codenamehas_plugin_permission)copy_plugins_to_placeholder)add_url_parametersadmin_reverse)PermissionDenied)HttpResponseBadRequest)get_object_or_404)TemplateResponse)path)gettext_lazy)	constants)emit_content_change   )views)CREATE_ALIAS_URL_NAMEDETACH_ALIAS_PLUGIN_URL_NAME)AliasPluginFormBaseCreateAliasFormCreateAliasForm)Alias)AliasContentAliasPluginr   c                        e Zd Z ed       ZeZeZdddiffZ	dgZ
d Zed        Zed        Zed        Zedd	       Zed
        Zed        Z fdZd Zd ZddZd Z xZS )r   Nfields)namesitecategoryreplacepluginplaceholderlanguagealiasc                     t        |j                  j                  t              r|j	                         ryd|j
                   dS )Nz$djangocms_alias/alias_recursive.htmlzdjangocms_alias/z/alias.html)
isinstancer)   sourcer    is_recursivetemplate)selfcontextinstancer)   s       K/home/dcms/DCMS/lib/python3.12/site-packages/djangocms_alias/cms_plugins.pyget_render_templatezAlias.get_render_template<   s<    h**11<@XEZEZE\9!("3"3!4K@@    c                 z    t        | d      s$t        t        j                  d            | _        | j                  S )N_cached_allowed_root_pluginsT)root_plugin)hasattrsetr   get_all_pluginsr8   )clss    r4   _get_allowed_root_pluginszAlias._get_allowed_root_pluginsA   s2    s:;/2;3N3N[_3`/aC,///r6   c           	         |j                   | j                  k(  r|j                  j                  d      }t	        t
        |j                  g      }g }|r2|j                  t        t        d      t        |      dddi             | j                  |j                  |j                  |j                  j                               r)|j                  t        t        d	      |d
ddi             |S |j                  t        |      d}|j!                         | j#                         v }|r4t%        t	        t&              fi |}t        t        d      |d
ddi      gS g S )NTshow_draft_content)argsz
Edit Alias cms-iconr+   action
attributesDetach Aliasmodalr(   r*   Create Alias)plugin_type__name__r+   get_contentr   r   pkappendr   _r   
can_detachuserr)   get_pluginsr
   get_plugin_classr>   r   r   )	r=   requestr(   alias_contentdetach_endpointplugin_menu_itemsdatacan_become_aliasendpoints	            r4   get_extra_plugin_menu_itemsz!Alias.get_extra_plugin_menu_itemsG   s_   -"LL444MM+,ii[O
 !#!((",+M:!$.#8	 ~~""((*
 "((".)'&$.#8	 %$ ii1':

 "2248U8U8WW)-8M*NWRVWHn%" *G4	  	r6   c                     |j                   t        |      d}t        t        t              fi |}|j
                  j                         rt        t        d      |dddi      gS g S )N)r)   r*   rK   rI   rD   r+   rE   )	rO   r
   r   r   r   cmsplugin_setexistsr   rQ   )r=   rV   r)   rZ   r\   s        r4    get_extra_placeholder_menu_itemsz&Alias.get_extra_placeholder_menu_items|   sr     '>>1':
 &m4I&JSdS$$++-n%" *G4	  	r6   c                     j                  t        t        d            sy|sy|r<|d   j                  }|j	                        rt        t        j                  d      syt        fd|D              S )NaddFTr   c              3   L   K   | ]  }t        |j                  d         ywrc   Nr   rL   .0r(   rS   s     r4   	<genexpr>z)Alias.can_create_alias.<locals>.<genexpr>   /      
  """
   !$)	has_permr   
AliasModelr)   check_sourcer   r   rM   all)r=   rS   pluginsr'   target_placeholders    `   r4   can_create_aliaszAlias.can_create_alias   sy    }})*e<
 !(!7!7%2248@UVZ\a\j\jlq@r 
 "
 
 	
r6   c                 R    t        fd|D              xr |j                        S )Nc              3   L   K   | ]  }t        |j                  d         ywre   rf   rg   s     r4   ri   z#Alias.can_detach.<locals>.<genexpr>   rj   rk   )ro   rn   )r=   rS   rq   rp   s    `  r4   rR   zAlias.can_detach   s4     
 "
 
 4 !--d3	4r6   c                 f   |j                   j                  |d      }|j                  }|j                  }|j                  }|j                  |       |r\|j                  |j                        x}r-|j                  ||t        |      |j                  z   dz          t        ||||||i      S g S )NTr@   r   )startoffset)r)   r*   r9   start_positions)r+   rT   r)   positionparentdelete_pluginget_last_pluginr*   _shift_plugin_positionslenr   )r=   r(   r*   source_pluginsrq   plugin_positionplugin_parenttarget_last_plugins           r4   detach_alias_pluginzAlias.detach_alias_plugin   s    11(t1T#// //((0%7%G%G%XX!X"::)~.1C1L1LLqP ;  /.!)!)? ;  	r6   c           
         t         |          t        d| j                  t        j
                        t        d| j                  t        j                        t        d| j                  t        j                        t        dt        j                  j                         t        j                        t        dt        j                  j                         t        j                        gz   S )Nzcreate-alias/)r$   zaliases/<int:pk>/usage/zdetach-alias/<int:plugin_pk>/zselect2/zcategory-select2/)superget_plugin_urlsr   create_alias_viewr   r   alias_usage_viewUSAGE_ALIAS_URL_NAMEdetach_alias_plugin_viewr   r   AliasSelect2Viewas_viewSELECT2_ALIAS_URL_NAMECategorySelect2ViewCATEGORY_SELECT2_URL_NAME)r1   	__class__s    r4   r   zAlias.get_plugin_urls   s    w&(&&44
 )%%33
 /--;;
 &&..055
 #))11388+,
 
 	
r6   c                 X   |j                   j                  st        t        |j                  xs d       }|j                         r|j                  nd }|j                  dk(  r|j                         st        d      S |j                   }t        |j                  xs d ||      }|j                         sddlm} | j                  }|j                  j                  ||i       }| j                   j"                  | _        |j&                  | _        t+        d      |d|j,                  |j.                  | j1                  |      g d}	| j3                  ||	dd	d 
      S |j5                         }
|
st        d      S |j                  j7                  d      }t8        j;                  ||
|      st        |j=                         \  }}}t?        |g       |rL|j                  j7                  d      }|j                  j7                  d      }| jA                  ||g||      S tC        |d      S )NGETzForm received unexpected values)initialrS   r   )adminrK   T)title	adminformis_popupmediaerrorspreserved_filtersinline_admin_formsetsF)rc   changeobjz'Plugins are required to create an aliasr'   r(   r)   )new_pluginssource_placeholdersource_pluginzadmin/cms/page/close_frame.html)"rS   is_staffr   r   r   is_validcleaned_datamethodr   r   POSTdjango.contribr   create_alias_fieldsethelpers	AdminFormmodel_metaoptsr%   
admin_siterQ   r   r   get_preserved_filtersrender_change_formrT   getr   rr   saver   render_replace_responser   )r1   rV   forminitial_datarS   create_formr   	fieldsets
admin_formr2   rp   r'   r+   rW   alias_pluginr(   r)   s                    r4   r   zAlias.create_alias_view   s
   ||$$"""7;;#6$7,0MMOt((>>U"4==?)*KLL||%LL D 
 ##%,22I00iLJ

((DI#jjDO>*' #))%,,%)%?%?%H)+G ** +   ))+)9  **..y9%%dGW=""-8-=-=-?*}l]O, --11(;F%2266}EK//)N#.$	 0    )JKKr6   c           	      0   |j                   j                  st        t        t        |      }|j
                  dk(  r]| j                  j                  }t        d      d|t        d      d|j                  t        d      |j                  d}t        |d|      S t        |      }|j                  j                  |d	      }| j                  |j                   |j                   |      }|st        | j#                  ||
      }	| j%                  ||	|      S )NrO   r   rH   Tindexr   )r   has_change_permissionr   	root_pathr   	app_labelobject_nameobjectz!djangocms_alias/detach_alias.htmlr@   rJ   )r   r   )rS   r   r   r   r!   r   r   r   rQ   r   r   r+   r   r
   rT   rR   r)   r   r   )
r1   rV   	plugin_pkr3   r   r2   r*   rp   rR   copied_pluginss
             r4   r   zAlias.detach_alias_plugin_view)  s
   ||$$""$[Y?>>U"::##D>*)-*73 !^^ z"..	G $G-PRYZZ,W5..,,X$,O__W\\83G3GQ
""11 2 

 ++&" , 
 	
r6   c                 r   g g }}|D ]  }|j                   r|j                   j                         n|}|gt        |j                               z   }	|j                  j                  |j                  |j                        }
t        ||	|      }t        |      }|
|d<   |j                  |       |j                  |       |j                  i t        |      d|i        ||dd}|r|j                  rt        |      |d<   |r|j                  r|j                  dd|d	<   | j                  |d d
|      S )N)	parent_id)target_pluginplugin_order	structureT)addedPluginsmovedPluginsr   replacedPlugin)placeholder_iddeletedreplacedPlaceholderALIAS_REPLACE)r   rF   
extra_data)rz   get_bound_pluginlistget_descendantsr)   get_plugin_tree_orderr*   r   r	   r   updaterP   rO   render_close_frame)r1   rV   r   r   r   move_pluginsadd_pluginsr(   rootrp   r   plugin_tree	move_datarZ   s                 r4   r   zAlias.render_replace_responseP  sO   $&k! 	^F7=}}6==113&DftD$8$8$:;;G!--CC ** D L *'7$OK/7I(4In%[)	*\"9&"A\;P[\]	^  ((

 ]--%<]%KD!""4"7"7"4"7"7+D&' &&wD]a&bbr6   c                 f   |j                   j                  st        t        t        |      }t        j
                  }t        d|       }d|t        d      d|j                  t        d      ||||j                  j                  d      t        |j                  d d      d	}t        |d
|      S )Nr   zObjects using alias: Tr   r   backc                 "    t        | t              S )N)r-   r   )r   s    r4   <lambda>z(Alias.alias_usage_view.<locals>.<lambda>  s    
3 5 r6   )keyreverse)r   r   r   r   r   r   r   r   originalshow_back_btnobjects_listz djangocms_alias/alias_usage.html)rS   r   r   r   rm   r   rQ   r   r   r   r   sortedobjects_usingr   )r1   rV   rO   r+   r   r   r2   s          r4   r   zAlias.alias_usage_viewo  s    ||$$""!*4)%12%)&w/W:$[[__V4"##5	
$  )KWUUr6   )NF)NN)rM   
__module____qualname__rQ   r$   r!   r   r   r   r   autocomplete_fieldsr5   classmethodr>   r]   ra   rr   rR   r   r   r   r   r   r   __classcell__)r   s   @r4   r   r   #   s    W:DED  
	
" #)A
 0 0
 2 2h  $ 
 
, 4 4  .
:ALF%
Nc>Vr6   N)5
cms.modelsr   cms.plugin_baser   r   cms.plugin_poolr   cms.toolbar.utilsr   r   r	   	cms.utilsr
   cms.utils.permissionsr   r   cms.utils.pluginsr   cms.utils.urlutilsr   r   django.core.exceptionsr   django.httpr   django.shortcutsr   django.template.responser   django.urlsr   django.utils.translationr   rQ   djangocms_aliasr   djangocms_alias.utilsr   rC   r   r   r   formsr   r   r   modelsr   rm   r    r!   __all__register_plugin r6   r4   <module>r      s     9 ' [ [ / : @ 3 . . 5  & 5  J H H ' - 
 dVM dV dVr6   