o
    ["<f03                     @   s   d Z ddlmZ ddlmZ ddlmZ ddl	m
Z
 G dd deZG dd	 d	eZG d
d deZG dd dee
ZG dd dee
ZG dd deZG dd deZdS )z?
Module where grappelli dashboard modules classes are defined.
    )apps)capfirst)gettext_lazy)AppListElementMixinc                   @   sZ   e Zd ZdZdZdZdZdZdZdZ	dZ
dZdZdZdddZdd	 Zd
d Zdd ZdS )DashboardModulea  
    Base class for all dashboard modules.
    Dashboard modules have the following properties:

    ``collapsible``
        Boolean that determines whether the module is collapsible, this
        allows users to show/hide module content. Default: ``True``.

    ``column``
        Integer that corresponds to the column.
        Default: None.

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

    ``title_url``
        String that contains the module title URL. If given the module
        title will be a link to this URL. Default value: ``None``.

    ``css_classes``
        A list of css classes to be added to the module ``div`` class
        attribute. Default value: ``None``.

    ``pre_content``
        Text or HTML content to display above the module content.
        Default value: ``None``.

    ``post_content``
        Text or HTML content to display under the module content.
        Default value: ``None``.

    ``template``
        The template to use to render the module.
        Default value: 'grappelli/dashboard/module.html'.
    zgrappelli/dashboard/module.htmlTN c                 K   sV   |d ur|| _ |D ]}t| j|rt| |||  q	| jpg | _| jp$g | _d| _d S )NF)titlehasattr	__class__setattrchildrencss_classes_initialized)selfr   kwargskey r   R/var/www/html/kck/venv/lib/python3.10/site-packages/grappelli/dashboard/modules.py__init__@   s   
zDashboardModule.__init__c                 C   s   dS )a  
        Like for the :class:`~grappelli.dashboard.Dashboard` class, dashboard
        modules 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 modules, for
        example, let's build a "history" dashboard module, that will list the
        last ten visited pages::

            from grappelli.dashboard import modules

            class HistoryDashboardModule(modules.LinkList):
                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(item)
                    # 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

        Nr   )r   contextr   r   r   init_with_contextK   s   z!DashboardModule.init_with_contextc                 C   s"   | j du o| jdu ot| jdkS )zO
        Return True if the module has no content and False otherwise.
        Nr   )pre_contentpost_contentlenr   )r   r   r   r   is_emptyl   s   "zDashboardModule.is_emptyc                 C   sH   dg}| j r|d d| jvrd| jvr|d || j7 }d|S )zL
        Return a string containing the css classes for the module.
        zgrp-dashboard-modulezgrp-collapsezgrp-openz
grp-closed )collapsibleappendr   join)r   retr   r   r   render_css_classess   s   



z"DashboardModule.render_css_classesN)__name__
__module____qualname____doc__templater   column
show_titler   	title_urlr   r   r   r   r   r   r   r    r   r   r   r   r      s     &
!r   c                       s,   e Zd ZdZdZdd Z fddZ  ZS )Groupa  
    Represents a group of modules.

    Here's an example of modules group::

        from grappelli.dashboard import modules, Dashboard

        class MyDashboard(Dashboard):
            def __init__(self, **kwargs):
                Dashboard.__init__(self, **kwargs)
                self.children.append(modules.Group(
                    title="My group",
                    children=[
                        modules.AppList(
                            title='Administration',
                            models=('django.contrib.*',)
                        ),
                        modules.AppList(
                            title='Applications',
                            exclude=('django.contrib.*',)
                        )
                    ]
                ))

    z&grappelli/dashboard/modules/group.htmlc                 C   s*   | j rd S | jD ]}|| qd| _ d S )NT)r   r   r   )r   r   moduler   r   r   r      s
   

zGroup.init_with_contextc                    s0   t t|  r	dS | jD ]	}| s dS qdS )zx
        A group of modules is considered empty if it has no children or if
        all its children are empty.
        TF)superr*   r   r   )r   childr
   r   r   r      s   
zGroup.is_empty)r"   r#   r$   r%   r&   r   r   __classcell__r   r   r.   r   r*      s
    r*   c                   @   s$   e Zd ZdZedZdZdd ZdS )LinkListz1
    A module that displays a list of links.
    Linksz*grappelli/dashboard/modules/link_list.htmlc                 C   s   | j rd S g }| jD ]R}t|ttfrW|d |d d}t|dkr(|d |d< t|dkr4|d |d< t|d	krQ|d }t|trI|rGd
nd }|rQt||d< || q
|| q
|| _d| _ d S )Nr      )r   url      external   description   _blanktargetT)	r   r   
isinstancetuplelistr   boolstrr   )r   r   new_childrenlink	link_dictr;   r   r   r   r      s(   


zLinkList.init_with_contextN)r"   r#   r$   r%   _r   r&   r   r   r   r   r   r0      s
    r0   c                       >   e Zd ZdZedZdZdZdZd	 fdd	Z	dd Z
  ZS )
AppListz<
    Module that lists installed apps and their models.
    Applicationsz)grappelli/dashboard/modules/app_list.htmlNc                    s@   t |dg | _t |dg | _tt| j|fi | d S )Nmodelsexclude)r>   poprH   rI   r,   rF   r   )r   r   r   r.   r   r   r      s   zAppList.__init__c           
      C   s   | j rd S | |d }i }|D ]W\}}|jj}||vr1t|jt| | 	||g d||< i }t|jj
|d< |d sC|d rR|d  |d< | |||d< |d r^| |||d	< || d
 | qt| }|  |D ]}	||	 d
 jdd d | j||	  qtd| _ d S )Nrequest)namer   r3   rH   r   changeview	view_only	admin_urladdadd_urlrH   c                 S   s   | d S )Nr   r   )ir   r   r   <lambda>   s    z+AppList.init_with_context.<locals>.<lambda>)r   T)r   _visible_models_meta	app_labeldjango_appsget_app_configverbose_namer   r   _get_admin_app_list_urlverbose_name_plural_get_admin_change_url_get_admin_add_urlr   r>   keyssortr   )
r   r   itemsr   modelpermsrW   
model_dictapps_sortedappr   r   r   r      s4   




zAppList.init_with_contextr!   )r"   r#   r$   r%   rD   r   r&   rH   rI   r   r   r/   r   r   r.   r   rF      s    rF   c                       s6   e Zd ZdZdZdZdZd fdd	Zdd Z  Z	S )		ModelListz,
    Module that lists a set of models.
    z+grappelli/dashboard/modules/model_list.htmlNc                    s8   t |pg | _t |pg | _tt| j|fi | d S r!   )r>   rH   rI   r,   rg   r   )r   r   rH   rI   r   r.   r   r   r     s   zModelList.__init__c                 C   s   | j rd S | |d }|sd S |D ]7\}}i }t|jj|d< |d s(|d r7|d  |d< | |||d< |d rC| |||d< | j| qd	| _ d S )
NrK   r   rM   rN   rO   rP   rQ   rR   T)	r   rU   r   rV   r\   r]   r^   r   r   )r   r   ra   rb   rc   rd   r   r   r   r     s   
zModelList.init_with_contextNNN)
r"   r#   r$   r%   r&   rH   rI   r   r   r/   r   r   r.   r   rg     s    rg   c                       sF   e Zd ZdZedZdZdZdZdZ			d
 fdd	Z
dd	 Z  ZS )RecentActionszD
    Module that lists the recent actions for the current user.
    zRecent actionsz/grappelli/dashboard/modules/recent_actions.html
   Nc                    s>   |pg | _ |pg | _|d|i tt| j|fi | d S )Nlimit)include_listexclude_listupdater,   ri   r   )r   r   rk   rl   rm   r   r.   r   r   r   -  s   

zRecentActions.__init__c                    s   | j rd S ddlm  ddlm} |d } fdd}|jd u r&|j }n	|jj|jj	d}| j
r:||| j
}| jrE||| j}|dd	d | j | _d
| _ d S )Nr   Q)LogEntryrK   c                    s~   ddl m} d }| D ]2}t||r |jd}nz	|d\}}W n	   td|  ||d}|d u r8|}q
||B }q
|S )Nr   )ContentType)content_type__id.zInvalid contenttype: "%s")content_type__app_labelcontent_type__model)"django.contrib.contenttypes.modelsrr   r<   idsplit
ValueError)r>   rr   qsetcontenttypecurrent_qsetrW   rb   ro   r   r   get_qset<  s"   

z1RecentActions.init_with_context.<locals>.get_qset)user__pk__exactcontent_typeuserT)r   django.db.modelsrp   django.contrib.admin.modelsrq   r   objectsallfilterpkrl   rm   rI   select_relatedrk   r   )r   r   rq   rK   r~   qsr   ro   r   r   4  s   

zRecentActions.init_with_context)Nrj   NN)r"   r#   r$   r%   rD   r   r&   rk   rl   rm   r   r   r/   r   r   r.   r   ri   "  s    ri   c                       rE   )
Feedz8
    Class that represents a feed dashboard module.
    zRSS Feedz%grappelli/dashboard/modules/feed.htmlNc                    s,   | ||d tt| j|fi | d S )N)feed_urlrk   )rn   r,   r   r   )r   r   r   rk   r   r.   r   r   r   i  s   zFeed.__init__c                 C   s   | j rd S dd l}| jd u rtdzdd l}W n ty+   | jddd Y d S w || j}| j	d urA|d d | j	 }n|d }|D ]}|j
|_z|j|jdd  |_W n   Y | j| qGd| _ d S )Nr   z!You must provide a valid feed URLz-You must install the FeedParser python moduleT)r   warningentriesr4   )r   datetimer   rz   
feedparserImportErrorr   r   parserk   rB   r3   dateupdated_parsed)r   r   r   r   feedr   entryr   r   r   r   m  s4   


zFeed.init_with_contextrh   )r"   r#   r$   r%   rD   r   r&   r   rk   r   r   r/   r   r   r.   r   r   _  s    r   N)r%   django.appsr   rX   django.utils.textr   django.utils.translationr   rD   grappelli.dashboard.utilsr   objectr   r*   r0   rF   rg   ri   r   r   r   r   r   <module>   s   s2 / =