
#dV1                 @   s   d  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 Gd d   d e  Z Gd	 d
   d
 e  Z Gd d   d e  Z Gd d   d e e  Z Gd d   d e e  Z Gd d   d e  Z Gd d   d e  Z d S)z?
Module where grappelli dashboard modules classes are defined.
    )capfirst)ContentType)ugettext_lazy)apps)AppListElementMixinc               @   s   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   s   | d  k	 r | |  _  n  x7 | D]/ } t |  j |  r t |  | | |  q q W|  j p^ g  |  _ |  j pp g  |  _ d |  _ d  S)NF)titlehasattr	__class__setattrchildrencss_classes_initialized)selfr	   kwargskey r   G/tmp/pip-build-0jahl3lb/django-grappelli/grappelli/dashboard/modules.py__init__C   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_contextN   s    z!DashboardModule.init_with_contextc             C   s1   |  j  d k o0 |  j d k o0 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_emptyo   s    zDashboardModule.is_emptyc             C   sj   d g } |  j  rP | j d  d |  j k rP d |  j k rP | j d  qP n  | |  j 7} d j |  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_classesv   s    		z"DashboardModule.render_css_classes)__name__
__module____qualname____doc__templater   columnZ
show_titler	   Z	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   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  Sx |  j D] } | j |  q Wd |  _  d  S)NT)r   r   r   )r   r   moduler   r   r   r      s
    	zGroup.init_with_contextc                sA   t  t |   j   r d Sx! |  j D] } | j   s# d Sq# Wd 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   r   r   )r   r   r(      s   r(   c               @   s4   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.
    ZLinksz*grappelli/dashboard/modules/link_list.htmlc             C   s   |  j  r d  Sg  } x |  j D] } t | t t f  r i | d d 6| d d 6} t |  d k rw | d | d <n  t |  d k r | d | d	 <n  | j |  q | j |  q W| |  _ d
 |  _  d  S)Nr   r	      url      Zexternal   descriptionT)r   r   
isinstancetuplelistr   r   )r   r   Znew_childrenlinkZ	link_dictr   r   r   r      s    		zLinkList.init_with_contextN)r"   r#   r$   r%   _r	   r&   r   r   r   r   r   r,      s   r,   c                   sU   e  Z d  Z d Z e d  Z d Z d Z d Z d   f d d  Z	 d d   Z
   S)	AppListz<
    Module that lists installed apps and their models.
    ZApplicationsz)grappelli/dashboard/modules/app_list.htmlNc                sS   t  | j d g    |  _ t  | j d g    |  _ t t |   j | |  d  S)Nmodelsexclude)r5   popr9   r:   r*   r8   r   )r   r	   r   )r   r   r   r      s    zAppList.__init__c       
      C   s  |  j  r d  S|  j | d  } i  } x | D] \ } } | j j } | | k r i t j |  j d 6t | j    d 6|  j	 | |  d 6g  d 6| | <n  i  } t | j j
  | d <| d r |  j | |  | d <n  | d r |  j | |  | d	 <n  | | d j |  q- Wt | j    } | j   x@ | D]8 }	 | |	 d j d
 d d    |  j j | |	  q<Wd |  _  d  S)Nrequestnamer	   r.   r9   change	admin_urladdadd_urlr   c             S   s   |  d S)Nr	   r   )ir   r   r   <lambda>   s    z+AppList.init_with_context.<locals>.<lambda>T)r   _visible_models_meta	app_labeldjango_appsZget_app_configZverbose_namer   r	   Z_get_admin_app_list_urlverbose_name_plural_get_admin_change_url_get_admin_add_urlr   r5   keyssortr   )
r   r   itemsr   modelpermsrF   
model_dictZapps_sortedZappr   r   r   r      s2    	


zAppList.init_with_context)r"   r#   r$   r%   r7   r	   r&   r9   r:   r   r   r   r   )r   r   r8      s   r8   c                   sO   e  Z d  Z d Z d Z d Z d Z d d d   f d d  Z d d   Z   S)	ModelListz,
    Module that lists a set of models.
    z+grappelli/dashboard/modules/model_list.htmlNc                sG   t  | p g   |  _ t  | p! g   |  _ t t |   j | |  d  S)N)r5   r9   r:   r*   rQ   r   )r   r	   r9   r:   r   )r   r   r   r     s    zModelList.__init__c             C   s   |  j  r d  S|  j | d  } | s* d  Sx | D]~ \ } } i  } t | j j  | d <| d r| |  j | |  | d <n  | d r |  j | |  | d <n  |  j j |  q1 Wd |  _  d  S)Nr<   r	   r>   r?   r@   rA   T)	r   rD   r   rE   rH   rI   rJ   r   r   )r   r   rM   rN   rO   rP   r   r   r   r     s    	

zModelList.init_with_context)	r"   r#   r$   r%   r&   r9   r:   r   r   r   r   )r   r   rQ      s   rQ   c                   sd   e  Z d  Z d Z e d  Z d Z d Z d Z d Z	 d d d d   f d d  Z
 d d	   Z   S)
RecentActionszD
    Module that lists the recent actions for the current user.
    zRecent Actionsz/grappelli/dashboard/modules/recent_actions.html
   Nc                sO   | p	 g  |  _  | p g  |  _ | j i | d 6 t t |   j | |  d  S)Nlimit)include_listexclude_listupdater*   rR   r   )r   r	   rT   rU   rV   r   )r   r   r   r   (  s    zRecentActions.__init__c                s   |  j  r d  Sd d l m   d d l m } | d }   f d d   } | j d  k rj | j j   } n | j j d | j j	  } |  j
 r | j | |  j
   } n  |  j r | j | |  j   } n  | j d d	  d  |  j  |  _ d
 |  _  d  S)Nr   )Q)LogEntryr<   c                s   d  } x |  D] } t  | t  r7   d | j  } nI y | j d  \ } } Wn t d |   Yn X  d | d |  } | d  k r | } q | | B} q W| S)NZcontent_type__id.zInvalid contenttype: "%s"Zcontent_type__app_labelZcontent_type__model)r3   r   idsplit
ValueError)r5   ZqsetZcontenttypeZcurrent_qsetrF   rN   )rX   r   r   get_qset7  s    		z1RecentActions.init_with_context.<locals>.get_qsetZuser__pk__exactcontent_typeuserT)r   Zdjango.db.modelsrX   Zdjango.contrib.admin.modelsrY   r`   objectsallfilterpkrU   rV   r:   Zselect_relatedrT   r   )r   r   rY   r<   r^   qsr   )rX   r   r   /  s    	
		"zRecentActions.init_with_context)r"   r#   r$   r%   r7   r	   r&   rT   rU   rV   r   r   r   r   )r   r   rR     s   	rR   c                   s[   e  Z d  Z d Z e d  Z d Z d Z d Z d d d   f d d  Z	 d d   Z
   S)	Feedz8
    Class that represents a feed dashboard module.
    zRSS Feedz%grappelli/dashboard/modules/feed.htmlNc                s8   | j  i | d 6| d 6 t t |   j | |  d  S)Nfeed_urlrT   )rW   r*   rf   r   )r   r	   rg   rT   r   )r   r   r   r   c  s    zFeed.__init__c             C   s+  |  j  r d  Sd d  l } |  j d  k r7 t d   n  y d d  l } Wn4 t k
 r} |  j j i d d 6d d 6 d  SYn X| j |  j  } |  j	 d  k	 r | d d  |  j	  } n
 | d } xX | D]P } | j
 | _ y# | j | j d d    | _ Wn Yn X|  j j |  q Wd |  _  d  S)	Nr   z!You must provide a valid feed URLz-You must install the FeedParser python moduler	   Twarningentriesr/   )r   datetimerg   r]   
feedparserImportErrorr   r   parserT   r6   r.   dateZupdated_parsed)r   r   rj   rk   feedri   entryr   r   r   r   g  s0    		
#zFeed.init_with_context)r"   r#   r$   r%   r7   r	   r&   rg   rT   r   r   r   r   )r   r   rf   Y  s   rf   N)r%   Zdjango.utils.textr   Z"django.contrib.contenttypes.modelsr   Zdjango.utils.translationr   r7   Zdjango.appsr   rG   Zgrappelli.dashboard.utilsr   objectr   r(   r,   r8   rQ   rR   rf   r   r   r   r   <module>   s   s2.<