3
(h`                 @   s   d dl 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mZ d dlmZ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dddZdS )    N)import_module)	AppConfigapps)reverse)post_migrate)TemplateTemplateDoesNotExist)get_templateselect_template)module_has_submodule   )modules)ModuleURLResolverc               @   sl   e Zd ZdZdZdZedd Zedd Zdd	 Z	d
d Z
dd Zedd Zdd Zdd Zdd ZdS )ModuleMixina  Extension for the django AppConfig. Makes django app pluggable at runtime.

    The application have to have <app_module>/urls.py file, with
    a single no-parametrized url with name='index', ex::

        urlpatterns = [
            url('^$', generic.TemplateView.as_view(template_name="sales/index.html"), name="index"),
        ]

    All AppConfigs urls will be included into material.frontend.urls automatically under /<app_label>/ prefix
    The AppConfig.label, used for the urls namespace.

    The menu.html sample::

        <ul>
            <li><a href="{% url 'sales:index' %}">Dashboard</a></li>
            <li><a href="{% url 'sales:customers' %}">Customers</a></li>
            {% if perms.sales.can_add_lead %}<li><a href="{% url 'sales:leads' %}">Leads</a></li>{% endif %}
        </ul>

    In all application templates, the current application config
    instance would be available as `current_module` template variable

    :keyword order: The relative module order priority. Modules in the
                    site menu would be listed according its priorities.

    :keyword icon: The module icon.

    Example::

        class Sales(ModuleMixin, AppConfig):
            name = 'sales'
            icon = '<i class="material-icons">call</i>'

    
   z%<i class="material-icons">receipt</i>c             C   s
   | j j S )zModule name.)labeltitle)self r   C/tmp/pip-install-q3hcpn_q/django-material/material/frontend/apps.pyverbose_name7   s    zModuleMixin.verbose_namec             C   s   ddl m} |jj| jS )zCheck the module installation state.

        Default implementation store installed state in the database
        `frontend_dbmodule` table.
        r   )Module)modelsr   objects	installedr   )r   DbModuler   r   r   r   <   s    zModuleMixin.installedc             C   s   | j pdj S )zQModule description.

        By default taken from the module docstring.
         )__doc__strip)r   r   r   r   descriptionF   s    zModuleMixin.descriptionc             C   s   dS )z6Check is user have permission to access to the module.Tr   )r   userr   r   r   has_permM   s    zModuleMixin.has_permc             C   sH   t | jdr2d| jdf }t|}t|dr2|jS tjdj| j	 g S )Nurlsz%s.%surlpatternsz;Module {} have not urls.py submodule or `urlpatterns` in it)
r   modulenamer   hasattrr#   warningswarnformatr   )r   Zurls_module_nameZurls_moduler   r   r   get_urlsQ   s    
zModuleMixin.get_urlsc             C   s&   dj | j}t|| j | | j| jdS )zWModule url config.

        By default it would be loaded from '<app>/urls.py'
        z^{}/)r$   Zapp_name	namespace)r)   r   r   r*   )r   base_urlr   r   r   r"   [   s    zModuleMixin.urlsc             C   s   t dj| jS )zEntry url for a module.z{}:index)r   r)   r   )r   r   r   r   	index_urld   s    zModuleMixin.index_urlc             C   s.   yt dj| jS  tk
r(   tdS X dS )zLoad module menu template.

        Template should be located in `<app_label>/menu.html`

        If no template exists, no exception raised.

        Intendent to use with {% include %} template tag::

            {% include module.menu %}
        z{}/menu.htmlr   N)r	   r)   r   r   r   )r   r   r   r   menuh   s    zModuleMixin.menuc             C   s   t dj| jdgS )a  Base template for a module.

        If  <app_label>/base_module.html exists it would be used.
        The default is 'material/frontend/base_module.html'

        Intendent to use in modules generic tmeplates. Ex::

            {% extends current_module.base_template %}
        z{}/base_module.htmlz"material/frontend/base_module.html)r
   r)   r   )r   r   r   r   base_templatex   s    

zModuleMixin.base_templateN)__name__
__module____qualname__r   ordericonpropertyr   r   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ZdZdd ZdS )MaterialFrontendConfigz%Default condif for Material Frontend.zmaterial.frontendzSite Modulesz)<i class="material-icons">view_module</i>c             C   s8   x$t j D ]}t|tr
tj| q
W tjt| d dS )zRegister all available modules.)ZsenderN)	r   Zget_app_configs
isinstancer   modules_registryregisterr   connectupdate_modules)r   
app_configr   r   r   ready   s    
zMaterialFrontendConfig.readyN)r0   r1   r2   r   r%   r   r4   r=   r   r   r   r   r6      s
   r6      Tc       
      K   s   ddl m} x@tj D ]4}|jj|jd\}}|r|dkrtdj|j qW |jj	dd tj D d}|j
 r|rtd	jd
jdd |D }	nd}	|	dkr|j  td n|dks|rtd dS )z,Sync installed modules and database records.r   )r   )r   r>   zAdding module {}c             S   s   g | ]
}|j qS r   )r   ).0r$   r   r   r   
<listcomp>   s    z"update_modules.<locals>.<listcomp>)Z	label__inzThe following modules are stale and need to be deleted:
 {}
Are you sure you want to delete these modules entries?

Type 'yes' to continue, or 'no' to cancel: z    %sc             s   s   | ]}|j V  qd S )N)r   )r?   r$   r   r   r   	<genexpr>   s    z!update_modules.<locals>.<genexpr>yeszStale modules deleted.zStale modules remain.N)r   r   r8   r   r   Zget_or_creater   printr)   excludeexistsinputjoindelete)
r<   	verbosityinteractivekwargsr   r$   _createdZstale_modulesZok_to_deleter   r   r   r;      s$    
r;   )r>   T)r'   	importlibr   Zdjango.appsr   r   Zdjango.core.urlresolversr   Zdjango.db.models.signalsr   Zdjango.templater   r   Zdjango.template.loaderr	   r
   Zdjango.utils.module_loadingr   registryr   r8   Zurlconfr   objectr   r6   r;   r   r   r   r   <module>   s   y