Commits

David Jean Louis committed 6c39780

changed how media files are rendered, to play well with other apps, output is still xhtml strict valid

Comments (0)

Files changed (13)

admin_tools/dashboard/models.py

 
         class MyDashboard(Dashboard):
             class Media:
-                css = {'screen': '/media/css/mydashboard.css'}
+                css = ('/media/css/mydashboard.css',)
                 js = ('/media/js/mydashboard.js',)
 
     Here's an example of a custom dashboard::
     .. image:: images/dashboard_example.png
     """
     class Media:
-        css = {
-            'all': 'dashboard.css',
-            'ie': 'dashboard-ie.css',
-        }
-        js  = (
-            'jquery/jquery-1.4.1.min.js',
-            'jquery/jquery-ui-1.8rc1.custom.min.js',
-            'jquery/jquery.cookie.min.js',
-            'json.min.js',
-            'jquery/jquery.dashboard.js',
-        )
+        css = ()
+        js  = ()
 
     def __init__(self, *args, **kwargs):
         """

admin_tools/dashboard/templates/admin/index.html

 {% extends "admin/base_site.html" %}
 {% load i18n dashboard_tags %}
 
-{% block extrahead %}
-{{ block.super }}
-{% render_dashboard_js %}
-{% endblock %}
-
 {% block extrastyle %}
 {{ block.super }}
 {% render_dashboard_css %}

admin_tools/dashboard/templates/dashboard/css.html

+<link rel="stylesheet" href="{{ media_url }}/admin_tools/css/dashboard.css" type="text/css" media="screen, projection"/>
+<!--[if lt IE 8]>
+<link rel="stylesheet" href="{{ media_url }}/admin_tools/css/dashboard-ie.css" type="text/css" media="screen, projection"/>
+<![endif]-->
+{% for css in css_files %}
+<link rel="stylesheet" href="{{ media_url }}/{{ css }}" type="text/css" media="screen, projection"/>{% endfor %}

admin_tools/dashboard/templates/dashboard/dashboard.html

 {% load i18n dashboard_tags %}
+
+<script type="text/javascript" charset="utf-8">
+    // When jQuery is sourced, it's going to overwrite whatever might be in the
+    // '$' variable, so store a reference of it in a temporary variable...
+    var _$ = window.$;
+    // load jquery if it's not loaded yet
+    if (typeof jQuery == 'undefined') {
+        var jquery_url = '{{ media_url }}/admin_tools/js/jquery/jquery-1.4.1.min.js';
+        document.write(unescape('%3Cscript src="' + jquery_url + '" type="text/javascript"%3E%3C/script%3E'));
+    }
+</script>
+<script type="text/javascript" charset="utf-8">
+    // load jquery ui if it's not loaded yet
+    if (typeof jQuery.ui == 'undefined') {
+        var jquery_ui_url = '{{ media_url }}/admin_tools/js/jquery/jquery-ui-1.8rc1.custom.min.js';
+        document.write(unescape('%3Cscript src="' + jquery_ui_url + '" type="text/javascript"%3E%3C/script%3E'));
+    }
+</script>
+<script type="text/javascript" src="{{ media_url }}/admin_tools/js/json.min.js"></script>
+<script type="text/javascript" src="{{ media_url }}/admin_tools/js/jquery/jquery.cookie.min.js"></script>
+<script type="text/javascript" src="{{ media_url }}/admin_tools/js/jquery/jquery.dashboard.js"></script>
+{% if dashboard.Media.js %}
+{% for js in dashboard.Media.js %}
+<script type="text/javascript" src="{{ media_url }}/{{ js }}"></script>{% endfor %}
+{% endif %}
+
 {% if dashboard.title %}
 <h1 class="dashboard-title">{{ dashboard.title }}</h1>
 {% endif %}
 
 <div id="dashboard-panel">
     <h3><a href="#">{% trans "Modules" %}</a></h3>
+    {% if has_disabled_modules %}
     <ul>
         {% spaceless %}
         {% for module in dashboard %}
         {% endfor %}
         {% endspaceless %}
     </ul>
+    {% endif %}
 </div>
 <div id="{{ dashboard.get_id }}" class="dashboard-container">
     {% for module in dashboard %}

admin_tools/dashboard/templatetags/dashboard_tags.py

 Dashboard template tags, the following dashboard tags are available:
 * ``{% render_dashboard %}``
 * ``{% render_dashboard_module %}``
-* ``{% render_dashboard_js %}``
-* ``{% render_dashboard_css %}``
 
 To load the dashboard tags just do: ``{% load dashboard_tags %}``.
 """
 
 import math
 from django import template
-from admin_tools.utils import render_media
+from django.conf import settings
 from admin_tools.dashboard.utils import get_dashboard_from_context
 
 register = template.Library()
     context.update({
         'template': dashboard.template,
         'dashboard': dashboard,
-        'split_at': math.ceil(float(len(dashboard))/float(dashboard.columns))
+        'split_at': math.ceil(float(len(dashboard))/float(dashboard.columns)),
+        'media_url': settings.MEDIA_URL.rstrip('/'),
+        'has_disabled_modules': len([m for m in dashboard if not m.enabled]) > 0,
     })
     return context
 render_dashboard = register.inclusion_tag(
 )(render_dashboard_module)
 
 
-def render_dashboard_js(dashboard=None):
+def render_dashboard_css(context, dashboard=None):
     """
-    Template tag that renders the needed js files for the dashboard.
-    It relies on the ``Media`` inner class of the ``Dashboard`` instance.
+    Template tag that renders the dashboard css files.
     """
     if dashboard is None:
-        dashboard = get_dashboard_from_context({})
-    tpl = '<script type="text/javascript" src="%sadmin_tools/js/%s"></script>'
-    return render_media('js', tpl, dashboard)
-register.simple_tag(render_dashboard_js)
+        dashboard = get_dashboard_from_context(context)
 
-
-def render_dashboard_css(dashboard=None):
-    """
-    Template tag that renders the needed css files for the dashboard.
-    It relies on the ``Media`` inner class of the ``Dashboard`` instance.
-    """
-    if dashboard is None:
-        dashboard = get_dashboard_from_context({})
-    tpl = '<link rel="stylesheet" type="text/css" media="%s" href="%sadmin_tools/css/%s" />'
-    return render_media('css', tpl, dashboard)
-register.simple_tag(render_dashboard_css)
+    context.update({
+        'css_files': dashboard.Media.css,
+        'media_url': settings.MEDIA_URL.rstrip('/'),
+    })
+    return context
+render_dashboard_css = register.inclusion_tag(
+    'dashboard/css.html',
+    takes_context=True
+)(render_dashboard_css)

admin_tools/dashboard/utils.py

 from admin_tools.dashboard.models import *
 
 
+
 def get_dashboard_from_context(context):
     try:
         request = context['request']
         request = HttpRequest()
     if request.META.get('REQUEST_URI') == reverse('admin:index'):
         return get_index_dashboard(request)
-    try:
-        app = context['app_list'][0]
-        models = []
-        app_label = None
-        app_title = app['name']
-        for model, model_admin in admin.site._registry.items():
-            if app['name'] == model._meta.app_label.title():
-                split = model.__module__.find(model._meta.app_label)
-                app_label = model.__module__[0:split] + model._meta.app_label
-                app_title = model._meta.app_label.title
-                for m in app['models']:
-                    if m['name'] == capfirst(model._meta.verbose_name_plural):
-                        mod = '%s.%s' % (model.__module__, model.__name__)
-                        models.append(mod)
-        return get_app_index_dashboard(request, app_label, app_title, models)
-    except KeyError:
-        return get_app_index_dashboard(request, '', [])
+    app = context['app_list'][0]
+    models = []
+    app_label = None
+    app_title = app['name']
+    for model, model_admin in admin.site._registry.items():
+        if app['name'] == model._meta.app_label.title():
+            split = model.__module__.find(model._meta.app_label)
+            app_label = model.__module__[0:split] + model._meta.app_label
+            app_title = model._meta.app_label.title
+            for m in app['models']:
+                if m['name'] == capfirst(model._meta.verbose_name_plural):
+                    mod = '%s.%s' % (model.__module__, model.__name__)
+                    models.append(mod)
+    return get_app_index_dashboard(request, app_label, app_title, models)
 
 
 def get_index_dashboard(request):

admin_tools/media/admin_tools/js/jquery/jquery.dashboard.js

     var _delete_element = function(elt, options, save_preference) {
         var existing = $('#'+options.panel_id).find('li a[rel='+elt.attr('id')+']');
         if (!existing.length) {
-            $('#' + options.panel_id).find('ul').append(
+            var panel_ul = $('#' + options.panel_id).find('ul');
+            if (!panel_ul.length) {
+                $('#' + options.panel_id).append('<ul/>');
+                panel_ul = $('#' + options.panel_id).find('ul');
+            }
+            panel_ul.append(
                 '<li><a href="#" rel="' 
                 + elt.attr('id') 
                 + '" class="addlink dashboard-module-add">'

admin_tools/menu/models.py

 
         class MyMenu(Menu):
             class Media:
-                css = {'screen': '/media/css/mymenu.css'}
+                css = ('/media/css/mymenu.css',)
                 js = ('/media/js/mymenu.js',)
 
     Here's an example of a custom menu::
     """
 
     class Media:
-        css = {
-            'all': 'menu.css',
-            'ie' : 'menu-ie.css'
-        }
+        css = ()
         js  = ()
 
     def __init__(self, *args, **kwargs):

admin_tools/menu/templates/admin/base_site.html

 {% extends "admin/base.html" %}
 {% load i18n menu_tags %}
+{% block title %}{{ title }} | {% trans 'Django site admin' %}{% endblock %}
+
 {% block extrastyle %}
+{{ block.super }}
 {% render_menu_css %}
 {% endblock %}
-{% block title %}{{ title }} | {% trans 'Django site admin' %}{% endblock %}
 
 {% block branding %}
 <h1 id="site-name">{% trans 'Django administration' %}</h1>

admin_tools/menu/templates/menu/css.html

+<link rel="stylesheet" href="{{ media_url }}/admin_tools/css/menu.css" type="text/css" media="screen, projection"/>
+<!--[if lt IE 8]>
+<link rel="stylesheet" href="{{ media_url }}/admin_tools/css/menu-ie.css" type="text/css" media="screen, projection"/>
+<![endif]-->
+{% for css in css_files %}
+<link rel="stylesheet" href="{{ media_url }}/{{ css }}" type="text/css" media="screen, projection"/>{% endfor %}

admin_tools/menu/templates/menu/menu.html

 {% load menu_tags %}
+{% if menu %}
 <!--[if IE 6]>
 <script type="text/javascript">
 var hover_ie6 = function() {
 window.onload = hover_ie6;
 </script>
 <![endif]-->
-{% if menu %}
+
 <ul id="navigation-menu">
     {% for item in menu %}{% render_menu_item item forloop.counter %}{% endfor %}
-</div>
+</ul>
 {% endif %}

admin_tools/menu/templatetags/menu_tags.py

 Menu template tags, the following menu tags are available:
 * ``{% render_menu %}``
 * ``{% render_menu_item %}``
-* ``{% render_menu_js %}``
-* ``{% render_menu_css %}``
 
 To load the menu tags just do: ``{% load menu_tags %}``.
 """
 
 from django import template
+from django.conf import settings
 from django.http import HttpRequest
-from admin_tools.utils import render_media
 from admin_tools.menu.utils import get_admin_menu
 
 register = template.Library()
     """
     if menu is None:
         menu = get_admin_menu(context['request'])
+
     menu.render(context['request'])
     context.update({
         'template': menu.template,
         'menu': menu,
+        'media_url': settings.MEDIA_URL.rstrip('/'),
     })
     return context
 render_menu = register.inclusion_tag(
 )(render_menu_item)
 
 
-def render_menu_js(menu=None):
+def render_menu_css(context, menu=None):
     """
-    Template tag that renders the needed js files for the menu.
-    It relies on the ``Media`` inner class of the ``Menu`` instance.
+    Template tag that renders the menu css files.
     """
     if menu is None:
-        menu = get_admin_menu(None)
-    tpl = '<script type="text/javascript" src="%sadmin_tools/js/%s"></script>'
-    return render_media('js', tpl, menu)
-register.simple_tag(render_menu_js)
+        menu = get_admin_menu(context['request'])
 
-
-def render_menu_css(menu=None):
-    """
-    Template tag that renders the needed css files for the menu.
-    It relies on the ``Media`` inner class of the ``Menu`` instance.
-    """
-    if menu is None:
-        menu = get_admin_menu(None)
-    tpl = '<link rel="stylesheet" type="text/css" media="%s" href="%sadmin_tools/css/%s" />'
-    return render_media('css', tpl, menu)
-register.simple_tag(render_menu_css)
+    context.update({
+        'css_files': menu.Media.css,
+        'media_url': settings.MEDIA_URL.rstrip('/'),
+    })
+    return context
+render_menu_css = register.inclusion_tag(
+    'menu/css.html',
+    takes_context=True
+)(render_menu_css)

admin_tools/utils.py

     def _get_admin_add_url(self, model):
         app_label = model._meta.app_label
         return reverse('admin:%s_%s_add' % (app_label, model.__name__.lower()))
-
-
-# todo: refactor how menu and dashboard media are rendered
-def render_media(type, tpl, obj):
-    """
-    Helper method used by the render_menu_css/js and render_dashboard_css/js 
-    template tags.
-    """
-    p = getattr(settings, 'ADMIN_TOOLS_MEDIA_URL', getattr(settings, 'MEDIA_URL'))
-    cache = []
-
-    def get_css_include(o):
-        ret = []
-        for t, f in o.Media.css.items():
-            if (t,f) not in cache:
-                if t == 'ie6':
-                    template = "<!--[if IE 6]>\n" + tpl + "\n<![endif]-->"
-                    t = 'screen'
-                elif t == 'ie7':
-                    template = "<!--[if IE 7]>\n" + tpl + "\n<![endif]-->"
-                    t = 'screen'
-                elif t == 'ie':
-                    template = "<!--[if lt IE 8]>\n" + tpl + "\n<![endif]-->"
-                    t = 'screen'
-                else:
-                    template = tpl
-                ret.append(template % (t, p, f))
-                cache.append((t,f))
-        return ret
-
-    def get_js_include(o):
-        ret = []
-        for f in o.Media.js:
-            if f not in cache:
-                ret.append(tpl % (p, f))
-                cache.append(f)
-        return ret
-
-    return "\n".join(locals()['get_%s_include' % type](obj))
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.