<commit_before>from samklang_menu.models import Menu
from django import template
from django.core.exceptions import ObjectDoesNotExist
from django.contrib.sites.models import Site

register = template.Library()

def html_menu(menu, active_link=None):
    menu_contents = []
    for item in menu:
        if item==active_link:
            class_expr = 'class="active"'
        else:
            class_expr = ''
        menu_contents.append('<li><a href="%s" %s>%s</a></li>' % (item.url, class_expr, item.name))
    return "<ul>" + "".join(menu_contents) + "</ul>"


@register.simple_tag
def simple_menu(request, default_url="/"):

    if hasattr(request, 'site'):
        site = request.site
    else:
        site = Site.objects.get(pk=1)

    # find the closest parent to the active url
    active_link = Menu.find_active(site, request.path_info)

    # find the menu closest to the active_link
    menu = active_link.submenu(request.user)

    return html_menu(menu, active_link)

@register.simple_tag
def menu(request, default_url="/"):
    return simple_menu(request, default_url)


@register.simple_tag
def breadcrumbs(request, extra=[]):
    """Get the menu item for the page you are at. Find all ancestors in the menu hierarchy. Start at top level and break if permissions are not ok.

    You can pass extra elements using the "extra" parameter"""

    if hasattr(request, 'site'):
        site_id = request.site.id
    else:
        site_id = 1
    user = request.user
    path = request.path_info
    try:
        link = Menu.objects.get(tree_id=site_id, url=path)
    except Menu.DoesNotExist:
        link = Menu.objects.get(tree_id=site_id, url='/')

    ancestors = []
    for ancestor in link.get_ancestors():
        if not ancestor.group or ancestor.group in user.groups.all():
            ancestors.append(ancestor)
        else:
            break
    ancestors.append(link)

    return " > ".join([ '<a href="%s">%s</a>' % (ancestor.url, ancestor.name) for ancestor in ancestors ] + [ '<a href="%s">%s</a>' % (t[0], t[1]) for t in extra ])

@register.simple_tag
def widgets(request, name):
    return "".join([ widget.widget().render(request) for widget in request.active.widgets.all() ])<commit_msg>Make more failproof active menu item searching<commit_after>from samklang_menu.models import Menu
from django import template
from django.core.exceptions import ObjectDoesNotExist
from django.contrib.sites.models import Site

register = template.Library()

def html_menu(menu, active_link=None):
    menu_contents = []
    for item in menu:
        if item==active_link:
            class_expr = 'class="active"'
        else:
            class_expr = ''
        menu_contents.append('<li><a href="%s" %s>%s</a></li>' % (item.url, class_expr, item.name))
    return "<ul>" + "".join(menu_contents) + "</ul>"


@register.simple_tag
def simple_menu(request, default_url="/"):

    if hasattr(request, 'site'):
        site = request.site
    else:
        site = Site.objects.get(pk=1)

    # find the closest parent to the active url
    active_link = Menu.find_active(site, request.path_info)

    # find the menu closest to the active_link
    if active_link:
        menu = active_link.submenu(request.user)
    else:
        menu = Menu.objects.get(tree_id=site.id, url="/").submenu(request.user)

    return html_menu(menu, active_link)

@register.simple_tag
def menu(request, default_url="/"):
    return simple_menu(request, default_url)


@register.simple_tag
def breadcrumbs(request, extra=[]):
    """Get the menu item for the page you are at. Find all ancestors in the menu hierarchy. Start at top level and break if permissions are not ok.

    You can pass extra elements using the "extra" parameter"""

    if hasattr(request, 'site'):
        site_id = request.site.id
    else:
        site_id = 1
    user = request.user
    path = request.path_info
    try:
        link = Menu.objects.get(tree_id=site_id, url=path)
    except Menu.DoesNotExist:
        link = Menu.objects.get(tree_id=site_id, url='/')

    ancestors = []
    for ancestor in link.get_ancestors():
        if not ancestor.group or ancestor.group in user.groups.all():
            ancestors.append(ancestor)
        else:
            break
    ancestors.append(link)

    return " > ".join([ '<a href="%s">%s</a>' % (ancestor.url, ancestor.name) for ancestor in ancestors ] + [ '<a href="%s">%s</a>' % (t[0], t[1]) for t in extra ])

@register.simple_tag
def widgets(request, name):
    if request.active:
        return "".join([ widget.widget().render(request) for widget in request.active.widgets.all() ])
    else:
        return ""
{'fraction_non_alphanumeric': 0.12737068965517243, 'fraction_numerical': 0.0017241379310344827, 'mean_word_length': 3.1363636363636362, 'pattern_counts': {'":': 0, '<': 23, '<?xml version=': 0, '>': 25, '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': '2543481', 'n_tokens_mistral': 1464, 'n_tokens_neox': 1445, 'n_words': 398}