
    &g1                         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  G d de      Z G d	 d
ee      Z G d dee      Z G d dee      Zy# e$ r d dl	mZ d dlm
Z Y Ow xY w)    )apps)reverse)gettext_lazy)ugettext_lazy)	mark_safe)AppListElementMixinc                   J    e Zd ZdZdZdZdZdZdZdZ	dZ
dZddZd Zd	 Zd
 Zy)MenuItema  
    This is the base class for custom menu items.
    A menu item can have the following properties:

    ``title``
        String that contains the menu item title, make sure you use the
        django gettext functions if your application is multilingual.
        Default value: 'Untitled menu item'.

    ``url``
        String that contains the menu item URL.
        Default value: '#'.

    ``css_classes``
        A list of css classes to be added to the menu item ``li`` class
        attribute. Default value: [].

    ``accesskey``
        The menu item accesskey. Default value: None.

    ``description``
        An optional string that will be used as the ``title`` attribute of
        the menu-item ``a`` tag. Default value: None.

    ``enabled``
        Boolean that determines whether the menu item is enabled or not.
        Disabled items are displayed but are not clickable.
        Default value: True.

    ``template``
        The template to use to render the menu item.
        Default value: 'admin_tools/menu/item.html'.

    ``children``
        A list of children menu items. All children items must be instances of
        the ``MenuItem`` class.
    zUntitled menu item#NTzadmin_tools/menu/item.htmlc                     ||| _         ||| _        |D ])  }t        | j                  |      st	        | |||          + | j
                  xs g | _        | j                  xs g | _        y N)titleurlhasattr	__class__setattrchildrencss_classes)selfr   r   kwargskeys        O/var/www/html/djangosite/lib/python3.12/site-packages/admin_tools/menu/items.py__init__zMenuItem.__init__>   sm    DJ?DH 	0Ct~~s+c6#;/	0 +++1r    c                      y)a  
        Like for menus, menu items have a ``init_with_context`` method that is
        called with a ``django.template.RequestContext`` instance as unique
        argument.
        This gives you enough flexibility to build complex items, for example,
        let's build a "history" menu item, that will list the last ten visited
        pages::

            from admin_tools.menu.items import MenuItem

            class HistoryMenuItem(MenuItem):
                title = 'History'

                def init_with_context(self, context):
                    request = context['request']
                    # we use sessions to store the visited pages stack
                    history = request.session.get('history', [])
                    for item in history:
                        self.children.append(MenuItem(
                            title=item['title'],
                            url=item['url']
                        ))
                    # add the current page to the history
                    history.insert(0, {
                        'title': context['title'],
                        'url': request.META['PATH_INFO']
                    })
                    if len(history) > 10:
                        history = history[:10]
                    request.session['history'] = history

        Here's a screenshot of our history item:

        .. image:: images/history_menu_item.png
        N )r   contexts     r   init_with_contextzMenuItem.init_with_contextL   s    H 	r   c                     |j                         }| j                  |k(  xs8 t        | j                  D cg c]  }|j	                  |      s| c}      dkD  S c c}w )z
        Helper method that returns ``True`` if the menu item is active.
        A menu item is considered as active if it's URL or one of its
        descendants URL is equals to the current URL.
        r   )get_full_pathr   lenr   is_selected)r   requestcurrent_urlcs       r   r"   zMenuItem.is_selectedr   sT     ++-xx;& JDMMDqQ]]7-CDEI	JDs   AAc                      y)z
        Helper method that returns ``True`` if the menu item is empty.
        This method always returns ``False`` for basic items, but can return
        ``True`` if the item is an AppList.
        Fr   r   s    r   is_emptyzMenuItem.is_empty|   s     r   )NN)__name__
__module____qualname____doc__r   r   r   	accesskeydescriptionenabledtemplater   r   r   r"   r(   r   r   r   r
   r
      sH    $L !E
CKIKG+HH2$LJr   r
   c                   0     e Zd ZdZd fd	Zd Zd Z xZS )AppLista  
    A menu item that lists installed apps an their models.
    In addition to the parent :class:`~admin_tools.menu.items.MenuItem`
    properties, the ``AppList`` has two extra properties:

    ``models``
        A list of models to include, only models whose name (e.g.
        "blog.comments.Comment") match one of the strings (e.g. "blog.*")
        in the models list will appear in the menu item.

    ``exclude``
        A list of models to exclude, if a model name (e.g.
        "blog.comments.Comment") match an element of this list (e.g.
        "blog.comments.*") it won't appear in the menu item.


    If no models/exclude list is provided, **all apps** are shown.

    Here's a small example of building an app list menu item::

        from admin_tools.menu import items, Menu

        class MyMenu(Menu):
            def __init__(self, **kwargs):
                super(MyMenu, self).__init__(**kwargs)
                self.children.append(items.AppList(
                    title='Applications',
                    exclude_list=('django.contrib',)
                )

    The screenshot of what this code produces:

    .. image:: images/applist_menu_item.png

    .. note::

        Note that this menu takes into account user permissions, as a
        consequence, if a user has no rights to change or add a ``Group`` for
        example, the ``django.contrib.auth.Group model`` child item won't be
        displayed in the menu.
    c                 
   t        |j                  dg             | _        t        |j                  dg             | _        |j                  dg       | _        |j                  dg       | _        t        t        | "  |fi | y)z2
        ``AppListMenuItem`` constructor.
        modelsexcludeinclude_listexclude_listN)	listpopr4   r5   r6   r7   superr2   r   r   r   r   r   s      r   r   zAppList.__init__   sm     6::h34FJJy"56"JJ~r:"JJ~r:gt%e6v6r   c           	         | j                  |d         }i }|D ]  \  }}|d   s|j                  dd      s|j                  j                  }||vr6t	        j
                  |      j                  | j                  ||      g d||<   ||   d   j                  |j                  j                  | j                  ||      d        t        |j                               D ]  }||   }t        |d   |d	         }	||   d   j                  d
        ||   d   D ]&  }
|	j                  j                  t        di |
       ( | j                  j                  |	        y)
        Please refer to
        :meth:`~admin_tools.menu.items.MenuItem.init_with_context`
        documentation from :class:`~admin_tools.menu.items.MenuItem` class.
        r#   changeviewF)r   r   r4   r4   r   r   r   r   c                     | d   S )Nr   r   )xs    r   <lambda>z+AppList.init_with_context.<locals>.<lambda>   s
    1W: r   )r   Nr   )_visible_modelsget_meta	app_labeldjango_appsget_app_configverbose_name_get_admin_app_list_urlappendverbose_name_plural_get_admin_change_urlsortedkeysr
   sortr   )r   r   itemsr   modelpermsrG   appapp_dictitem
model_dicts              r   r   zAppList.init_with_context   sg    $$WY%78! 	LE5(Ouyy'?--I$ $229=JJ77wG 	#Y OH%,,8811%A. 	  $))+& 	'CCyH(7"3%IDIh$$)=$>"3i1 =
$$X%;
%;<=MM  &	'r   c                 2    t        | j                        dk(  S )a  
        Helper method that returns ``True`` if the applist menu item has no
        children.

        >>> from admin_tools.menu.items import MenuItem, AppList
        >>> item = AppList(title='My menu item')
        >>> item.is_empty()
        True
        >>> item.children.append(MenuItem(title='foo'))
        >>> item.is_empty()
        False
        >>> item.children = []
        >>> item.is_empty()
        True
        r   r!   r   r'   s    r   r(   zAppList.is_empty         4==!Q&&r   r   r)   r*   r+   r,   r   r   r(   __classcell__r   s   @r   r2   r2      s    (T7'B'r   r2   c                   0     e Zd ZdZd fd	Zd Zd Z xZS )	ModelLista  
    A menu item that lists a set of models.
    In addition to the parent :class:`~admin_tools.menu.items.MenuItem`
    properties, the ``ModelList`` has two extra properties:

    ``models``
        A list of models to include, only models whose name (e.g.
        "blog.comments.Comment") match one of the strings (e.g. "blog.*")
        in the include list will appear in the dashboard module.

    ``exclude``
        A list of models to exclude, if a model name (e.g.
        "blog.comments.Comment" match an element of this list (e.g.
        "blog.comments.*") it won't appear in the dashboard module.

    Here's a small example of building a model list menu item::

        from admin_tools.menu import items, Menu

        class MyMenu(Menu):
            def __init__(self, **kwargs):
                super(MyMenu, self).__init__(**kwargs)
                self.children += [
                    items.ModelList(
                        'Authentication', ['django.contrib.auth.*',]
                    )
                ]

    .. note::

        Note that this menu takes into account user permissions, as a
        consequence, if a user has no rights to change or add a ``Group`` for
        example, the ``django.contrib.auth.Group model`` item won't be
        displayed in the menu.
    c                     t        |xs g       | _        t        |xs g       | _        |j                  dg       | _        |j                  dg       | _        t        t        | "  |fi | y)z,
        ``ModelList`` constructor.
        r6   r7   N)	r8   r4   r5   r9   r6   r7   r:   r`   r   )r   r   r4   r5   r   r   s        r   r   zModelList.__init__  s_     6<R(GMr*"JJ~r:"JJ~r:i'88r   c                    | j                  |d         }|D ]m  \  }}|d   s|j                  dd      s|j                  j                  }| j	                  ||      }t        ||      }| j                  j                  |       o y)r=   r#   r>   r?   Fr@   N)rD   rE   rF   rM   rN   r
   r   rL   )r   r   rR   rS   rT   r   r   rW   s           r   r   zModelList.init_with_context  s     $$WY%78! 	'LE5(Ouyy'?KK33E,,UG<C%S1DMM  &	'r   c                 2    t        | j                        dk(  S )a  
        Helper method that returns ``True`` if the modellist menu item has no
        children.

        >>> from admin_tools.menu.items import MenuItem, ModelList
        >>> item = ModelList(title='My menu item')
        >>> item.is_empty()
        True
        >>> item.children.append(MenuItem(title='foo'))
        >>> item.is_empty()
        False
        >>> item.children = []
        >>> item.is_empty()
        True
        r   rZ   r'   s    r   r(   zModelList.is_empty-  r[   r   )NNNr\   r^   s   @r   r`   r`      s    "H	9''r   r`   c                   @     e Zd ZdZ ed       Zd fd	Zd Zd Z xZ	S )	Bookmarksa  
    A menu item that lists pages bookmarked by the user. This menu item also
    adds an extra button to the menu that allows the user to bookmark or
    un-bookmark the current page.

    Here's a small example of adding a bookmark menu item::

        from admin_tools.menu import items, Menu

        class MyMenu(Menu):
            def __init__(self, **kwargs):
                super(MyMenu, self).__init__(**kwargs)
                self.children.append(items.Bookmarks('My bookmarks'))

    c                     t        t        | 
  |fi | d| j                  vr| j                  j	                  d       y y )Nbookmark)r:   re   r   r   rL   r;   s      r   r   zBookmarks.__init__R  s>    i'88T---##J/ .r   c                 *   ddl m} |j                  j                  |d   j                        D ]D  }| j
                  j                  t        t        |j                        |j                               F t        | j
                        sd| _        yy)r=   r   )Bookmarkr#   )userFN)admin_tools.menu.modelsri   objectsfilterrj   r   rL   r
   r   r   r   r!   r/   )r   r   ri   bs       r   r   zBookmarks.init_with_contextW  sw     	5!!((gi.@.E.E(F 	FAMM  )AGG*<aee!DE	F 4==! DL "r   c                      y)zW
        A bookmark menu item is never considered as active, the real item is.
        Fr   )r   r#   s     r   r"   zBookmarks.is_selectede  s     r   r   )
r)   r*   r+   r,   _r   r   r   r"   r]   r^   s   @r   re   re   @  s"     kNE0
!r   re   N)django.appsr   rH   django.urlsr   django.utils.translationr   rp   ImportErrordjango.core.urlresolversr   django.utils.safestringr   admin_tools.utilsr   objectr
   r2   r`   re   r   r   r   <module>ry      sw    +< $: . 1tv tnf'h+ f'RO'- O'd)- )s	  <0;<s   A A$#A$