
    &gI                         d 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mZ ddlmZ dd	lmZ  ej&                  d
      Z G d dej*                        Z G d dej.                        Zy)z
Additional menu items.
    N)items)ContentType)ObjectDoesNotExist)capfirst)NoReverseMatchresolve)gettext_lazy)sort_cms_modelsz(.+)_([^_]+)_changec                       e Zd ZdZd Zd Zy)CmsModelListz
    A custom :class:`~admin_tools.menu.items.ModelList` that displays menu items for each model.
    It has a strong bias towards sorting CMS apps on top.
    c                    | j                  |d         }|D cg c]s  \  }}| j                  ||      r\|j                  j                  |j                  j                  t        |j                  j                        | j                  ||      du }}}t        |       |D ]8  }| j                  j                  t        j                  |d   |d                : yc c}}w )z&
        Initialize the menu.
        request)nameapp_nametitleurlr   r   )r   r   N)_visible_modelsis_item_visible_meta
model_name	app_labelr   verbose_name_plural_get_admin_change_urlr
   childrenappendr   MenuItem)selfcontext	listitemsmodelpermsmodelss         O/var/www/html/djangosite/lib/python3.12/site-packages/fluent_dashboard/items.pyinit_with_contextzCmsModelList.init_with_context   s    
 (();<	 !*	
 u##E51 ..!KK11!%++"A"AB11%A		
 	
 	  	YEMM  eGn%PU,!WX	Y	
s   A8Cc                     |d   S )a  
        Return whether the model should be displayed in the menu.
        By default it checks for the ``perms['change']`` value; only items with change permission will be displayed.
        This function can be extended to support "view permissions" for example.

        :param model: The model class
        :param perms: The permissions from :func:`ModelAdmin.get_model_perms()<django.contrib.admin.ModelAdmin.get_model_perms>`.
        change )r   r    r!   s      r#   r   zCmsModelList.is_item_visible4   s     X    N)__name__
__module____qualname____doc__r$   r   r'   r(   r#   r   r      s    
Y6	r(   r   c                   H     e Zd ZdZ ed      ZdZdgZ fdZd Z	d Z
 xZS )ReturnToSiteItema  
    A "Return to site" button for the menu.
    It redirects the user back to the frontend pages.

    By default, it attempts to find the current frontend URL that corresponds
    with the model that's being edited in the admin 'change' page.
    If this is not possible, the default URL (``/``) will be used instead.

    The menu item has a custom ``returntosite`` CSS class to be distinguishable between the other menu items.
    zReturn to site/returntositec                     t         |   |       | j                  |d         }|r	 |j                         }|r|| _        yyy# t
        t        f$ r
}Y d}~yd}~ww xY w)z
        Find the current URL based on the context.
        It uses :func:`get_edited_object` to find the model,
        and calls ``get_absolute_url()`` to get the frontend URL.
        r   N)superr$   get_edited_objectget_absolute_urlr   AttributeErrorr   )r   r   edited_modelr   e	__class__s        r#   r$   z"ReturnToSiteItem.init_with_contextS   so     	!'* --gi.@A#"335 "DH   #N3 s   A AAc                    t        |j                        }|j                  dk(  r|j                  r~|j                  j	                  d      rct
        j                  |j                        }|sy	 |j                  d   }| j                  |j                  d      |j                  d      |      S y# t        $ r  |j                  sY y|j                  d   }Y Zw xY w)z}
        Return the object which is currently being edited.
        Returns ``None`` if the match could not be made.
        admin_changeN	object_idr         )r   	path_info	namespaceurl_nameendswithRE_CHANGE_URLmatchkwargsKeyErrorargsget_object_by_natural_keygroup)r   r   resolvermatchrD   r<   s        r#   r3   z"ReturnToSiteItem.get_edited_objectf   s    
   1 12##w.&&&&//	:
 "''(>(>?E2)00=	 11%++a.%++a.R[\\  2$)))..q1	2s   /B0 0CCCc                     	 t         j                  j                  ||      }|j	                         }t        |d      sy	 |j                  |      S # t         j                  $ r Y yw xY w# t        $ r Y yw xY w)zz
        Return a model based on a natural key.
        This is a utility function for :func:`get_edited_object`.
        Nr4   )pk)r   objectsget_by_natural_keyDoesNotExistmodel_classhasattrget_object_for_this_typer   )r   r   r   r<   
model_type
ModelClasss         r#   rH   z*ReturnToSiteItem.get_object_by_natural_key   s    
	$,,??	:VJ  ++-
z#56	66)6DD '' 		 " 		s#    A  A+ A('A(+	A76A7)r)   r*   r+   r,   _r   r   css_classesr$   r3   rH   __classcell__)r8   s   @r#   r.   r.   @   s1    	 E
C!"K#&:r(   r.   )r,   redjangoadmin_tools.menur   "django.contrib.contenttypes.modelsr   django.core.exceptionsr   django.template.defaultfiltersr   django.urlsr   r   django.utils.translationr	   rU   fluent_dashboard.appgroupsr
   compilerC   	ModelListr   r   r.   r'   r(   r#   <module>rc      s[    
  " : 5 3 / 6 6

01*5?? *ZVu~~ Vr(   