import re
from django import template
from django.conf import settings
from django.core import urlresolvers
from django.utils.translation import get_language
from yawdadmin import admin_site
from yawdadmin.conf import settings as ls

register = template.Library()

@register.inclusion_tag('admin/includes/topmenu.html', takes_context=True)
def admin_top_menu(context):        
    return {
        'perms' : context['perms'],
        'top_menu' : admin_site.top_menu(context['request']),
        'homeurl' : urlresolvers.reverse('admin:index'),
        'user' : context['user'],
        'langs' : context['langs'] if 'langs' in context else [],
        'default_lang': context['default_lang'] if 'default_lang' in context else None,
        'clean_url' : context['clean_url'] if 'clean_url' in context else '',
        'LANGUAGE_CODE' : get_language(),
        'optionset_labels' : admin_site.get_option_admin_urls(),
        'analytics' : context['user'].is_superuser and ls.ADMIN_GOOGLE_ANALYTICS_FLOW,
    }

@register.simple_tag
def clean_media(media):
    media._js = [i for i in media._js if not re.match(
        r'%sadmin/js/((jquery(\.init)?|collapse|admin/RelatedObjectLookups)(\.min)?\.)js' % settings.STATIC_URL, i)]
    return media

@@ -1050,16 +1050,50 @@
 media):%0A
+    if hasattr(media, '_js'):%0A    
     medi
@@ -1139,16 +1139,20 @@
 .match(%0A
+    
         

check for the existance of '_js' attribute in media, as some custom 3rd-party admin views may provide an empty media variable