<commit_before>import copy
import inspect
from django import template
from django.conf import settings
from django.core.exceptions import ImproperlyConfigured
from django.core.urlresolvers import reverse
from django.utils.translation import ugettext_lazy as _
from django.utils.importlib import import_module

register = template.Library()


def make_url(shortcut, request):

    permission = shortcut.get('app_permission')
    if permission and \
            isinstance(permission, (list, tuple)) and len(permission) >= 2:
        shortcut['can_add'] = request.user.has_perm(
            '{0}.add_{1}'.format(*permission)
        )
        shortcut['can_change'] = request.user.has_perm(
            '{0}.change_{1}'.format(*permission)
        )

    if not shortcut.get('url'):
        try:
            url_name = shortcut['url_name']
        except KeyError:
            raise ImproperlyConfigured(
                _('settings.ADMIN_SHORTCUTS is improperly configured. '
                  'Please supply either a "url" or a "url_name" '
                  'for each shortcut.')
            )
        current_app = shortcut.get('app_name')
        if isinstance(url_name, list):
            shortcut['url'] = reverse(
                url_name[0], args=url_name[1:], current_app=current_app
            )
        else:
            shortcut['url'] = reverse(
                url_name, current_app=current_app)

        shortcut['url'] += shortcut.get('url_extra', '')

    if not shortcut.get('class'):
        shortcut['class'] = get_shortcut_class(
            shortcut.get('url_name', shortcut['url']))

    if shortcut.get('count'):
        shortcut['count'] = eval_func(shortcut['count'], request)

    if shortcut.get('count_new'):
        shortcut['count_new'] = eval_func(
            shortcut['count_new'], request)


def add_permission(parent, child):
    if child.get('can_change', None):
        parent['can_change'] = True


@register.inclusion_tag('admin_shortcuts/base.html', takes_context=True)
def admin_shortcuts(context):
    if 'ADMIN_SHORTCUTS' in context:
        admin_shortcuts = copy.deepcopy(context['ADMIN_SHORTCUTS'])
    else:
        admin_shortcuts = copy.deepcopy(
            getattr(settings, 'ADMIN_SHORTCUTS', []))
    admin_shortcuts_settings = copy.deepcopy(
        getattr(settings, 'ADMIN_SHORTCUTS_SETTINGS', {}))
    request = context.get('request', None)
    if not admin_shortcuts:
        return ''

    for group in admin_shortcuts:
        if not group.get('shortcuts'):
            raise ImproperlyConfigured(
                'settings.ADMIN_SHORTCUTS is improperly configured.')

        for shortcut in group.get('shortcuts', []):
            make_url(shortcut, request)
            add_permission(group, shortcut)

            for child in shortcut.get('children', []):
                make_url(child, request)
                add_permission(shortcut, child)
                # TODO: make it recursive for submenus

    return {
        'admin_shortcuts': admin_shortcuts,
        'settings': admin_shortcuts_settings,
        'context': context
    }


@register.inclusion_tag('admin_shortcuts/style.css')
def admin_shortcuts_css():
    return {
        'classes': [value for key, value in CLASS_MAPPINGS],<commit_msg>fix bug NoReverseMatch, if set module not exist on shortcuts admin<commit_after>import copy
import inspect
from django import template
from django.conf import settings
from django.core.exceptions import ImproperlyConfigured
from django.core.urlresolvers import reverse, NoReverseMatch
from django.utils.translation import ugettext_lazy as _
from django.utils.importlib import import_module

register = template.Library()


def make_url(shortcut, request):

    permission = shortcut.get('app_permission')
    if permission and \
            isinstance(permission, (list, tuple)) and len(permission) >= 2:
        shortcut['can_add'] = request.user.has_perm(
            '{0}.add_{1}'.format(*permission)
        )
        shortcut['can_change'] = request.user.has_perm(
            '{0}.change_{1}'.format(*permission)
        )

    if not shortcut.get('url'):
        try:
            url_name = shortcut['url_name']
        except KeyError:
            raise ImproperlyConfigured(
                _('settings.ADMIN_SHORTCUTS is improperly configured. '
                  'Please supply either a "url" or a "url_name" '
                  'for each shortcut.')
            )
        current_app = shortcut.get('app_name')
        if isinstance(url_name, list):
            try:
                shortcut['url'] = reverse(
                    url_name[0], args=url_name[1:], current_app=current_app)
            except NoReverseMatch:
                shortcut['url'] = ''
        else:
            try:
                shortcut['url'] = reverse(
                    url_name, current_app=current_app)
            except NoReverseMatch:
                shortcut['url'] = ''

        shortcut['url'] += shortcut.get('url_extra', '')

    if not shortcut.get('class'):
        shortcut['class'] = get_shortcut_class(
            shortcut.get('url_name', shortcut['url']))

    if shortcut.get('count'):
        shortcut['count'] = eval_func(shortcut['count'], request)

    if shortcut.get('count_new'):
        shortcut['count_new'] = eval_func(
            shortcut['count_new'], request)


def add_permission(parent, child):
    if child.get('can_change', None):
        parent['can_change'] = True


@register.inclusion_tag('admin_shortcuts/base.html', takes_context=True)
def admin_shortcuts(context):
    if 'ADMIN_SHORTCUTS' in context:
        admin_shortcuts = copy.deepcopy(context['ADMIN_SHORTCUTS'])
    else:
        admin_shortcuts = copy.deepcopy(
            getattr(settings, 'ADMIN_SHORTCUTS', []))
    admin_shortcuts_settings = copy.deepcopy(
        getattr(settings, 'ADMIN_SHORTCUTS_SETTINGS', {}))
    request = context.get('request', None)
    if not admin_shortcuts:
        return ''

    for group in admin_shortcuts:
        if not group.get('shortcuts'):
            raise ImproperlyConfigured(
                'settings.ADMIN_SHORTCUTS is improperly configured.')

        for shortcut in group.get('shortcuts', []):
            make_url(shortcut, request)
            add_permission(group, shortcut)

            for child in shortcut.get('children', []):
                make_url(child, request)
                add_permission(shortcut, child)
                # TODO: make it recursive for submenus


    return {
        'admin_shortcuts': admin_shortcuts,
        'settings': admin_shortcuts_settings,
        'context': context
    }


@register.inclusion_tag('admin_shortcuts/style.css')
def admin_shortcuts_css():
    return {
        'classes': [value for key, value in CLASS_MAPPINGS],
{'fraction_non_alphanumeric': 0.11161831836253866, 'fraction_numerical': 0.002061552054189368, 'mean_word_length': 2.5822784810126582, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6885315', 'n_tokens_mistral': 2037, 'n_tokens_neox': 1920, 'n_words': 414}