<commit_before># -*- coding: utf-8 -*-

from django.template import Library, Node, TemplateSyntaxError, TemplateDoesNotExist, Variable
from django.template.loader import select_template
import operator

from Kraggne.models import MenuItem

register = Library()

################################################################
############## COMMUN FUNCTIONS ################################
################################################################

def next_bit_for(bits, key, if_none=None):
    try:
        res = bits[bits.index(key)+1]
        if res in ('as','for','with','into') or "=" in res:
            return if_none
        return res
    except:
        return if_none

def get_val_for(bits,key,if_none=None,type=bool):
    for u in bits:
        if u.startswith(key+"="):
            res = u.split('=')[1]
            if type == bool:
                return res=='True'
            return type(res)
    return if_none


def resolve(var, context):
    """Resolves a variable out of context if it's not in quotes"""
    if var[0] in ('"', "'") and var[-1] == var[0]:
        return var[1:-1]
    else:
        try:
            return Variable(var).resolve(context)
        except:
            return var

def GetMenuBySlug(slug):
    try:
        return MenuItem.objects.get(slug=slug)
    except:
        return None

################################################################
#######################" BREADCRUMB ############################
################################################################

class breadcumbNode(Node):

    def __init__(self, slug=None,template_path=None,store_in_object=None,variable_name=None,include_self=True):
        self.slug = slug
        self.template_path = template_path
        self.store_in_object = store_in_object
        self.variable_name = variable_name
        self.include_self = include_self

    def render(self, context):
        if not self.slug:
            try:
                menu = context["page"]
            except:
                return ''
        else:
            menu = GetMenuBySlug(resolve(self.slug,context))

        if not menu:
            return ''

        breadcrumb = menu.get_ancestors(include_self=self.include_self).filter(is_visible = True)

        if self.store_in_object:
            context[resolve(self.store_in_object,context)] = breadcrumb
            return ''

        template_paths = []
        if self.template_path:
            template_paths.append(resolve(self.template_path, context))
        template_paths.append('Kraggne/breadcrumb.html')
            
        try:
            t = select_template(template_paths)
        except:
            return ''

        # Set content as variable inside context, if variable_name is given
        if self.variable_name:
            context[resolve(self.variable_name, context)] = breadcrumb
        else:
            context['object_list'] = breadcrumb

        content = t.render(context)

        return content


def do_breadcrumb(parser, token):
    """
    {% breadcrumb ["slug"] [include_self=True] %}
    {% breadcrumb ["slug"] into "slug_object" [include_self=True] %}
    {% breadcrumb ["slug"] with "templatename.html" [include_self=True] %}
    {% breadcrumb ["slug"] with "templatename.html" as "variable" [include_self=True] %}
    """

    bits = token.contents.split()
    args = {
        'slug': next_bit_for(bits, 'breadcrumb'),
        'store_in_object': next_bit_for(bits, 'into'),
        'template_path': next_bit_for(bits, 'with'),
        'variable_name': next_bit_for(bits, 'as'),
        'include_self' : get_val_for(bits,'include_self',if_none=True,type=bool)
    }
    return breadcumbNode(**args)

register.tag('breadcrumb', do_breadcrumb)

##################################################################
####################### SHOW MENU ################################
##################################################################

class menuNode(Node):

    def __init__(self, slug,template_path=None,store_in_object=None,variable_name=None,include_self=True,level_min=0,level_nb=0):
        self.slug = slug
        self.template_path = template_path
        self.store_in_object = store_in_object
        self.variable_name = variable_name
        self.include_self = include_self
        self.level_min = level_min
        self.level_nb = level_nb

    def render(self, context):

        if not self.slug:
            try:
                menu = context["page"]
            except:
                return ''
        else:
            menu = GetMenuBySlug(resolve(self.slug,context))

        if not menu:
            return ''

        if self.level_min > 0:
            tree = menu.get_descendants(include_self=self.include_self).filter(is_visible=True,level__gte=menu.level+self.level_min)
        else:
            if self.level_min < 0:
                menu = menu.get_ancestors(include_self=True).filter(level__gte=menu.level+self.level_min-1)[0]
                tree = menu.get_descendants(include_self=False).filter(is_visible=True)
            else:
                tree = menu.get_descendants(include_self=self.include_self).filter(is_visible=True)
<commit_msg>updtate to make sheated menu<commit_after># -*- coding: utf-8 -*-

from django.template import Library, Node, TemplateSyntaxError, TemplateDoesNotExist, Variable
from django.template.loader import select_template
import operator

from Kraggne.models import MenuItem

register = Library()

################################################################
############## COMMUN FUNCTIONS ################################
################################################################

def next_bit_for(bits, key, if_none=None):
    try:
        res = bits[bits.index(key)+1]
        if res in ('as','for','with','into') or "=" in res:
            return if_none
        return res
    except:
        return if_none

def get_val_for(bits,key,if_none=None,type=bool):
    for u in bits:
        if u.startswith(key+"="):
            res = u.split('=')[1]
            if type == bool:
                return res=='True'
            return type(res)
    return if_none


def resolve(var, context):
    """Resolves a variable out of context if it's not in quotes"""
    if var[0] in ('"', "'") and var[-1] == var[0]:
        return var[1:-1]
    else:
        try:
            return Variable(var).resolve(context)
        except:
            return var

def GetMenuBySlug(slug):
    try:
        return MenuItem.objects.get(slug=slug)
    except:
        return None

################################################################
#######################" BREADCRUMB ############################
################################################################

class breadcumbNode(Node):

    def __init__(self, slug=None,template_path=None,store_in_object=None,variable_name=None,include_self=True):
        self.slug = slug
        self.template_path = template_path
        self.store_in_object = store_in_object
        self.variable_name = variable_name
        self.include_self = include_self

    def render(self, context):
        if not self.slug:
            try:
                menu = context["page"]
            except:
                return ''
        else:
            menu = GetMenuBySlug(resolve(self.slug,context))

        if not menu:
            return ''

        breadcrumb = menu.get_ancestors(include_self=self.include_self).filter(is_visible = True)

        if self.store_in_object:
            context[resolve(self.store_in_object,context)] = breadcrumb
            return ''

        template_paths = []
        if self.template_path:
            template_paths.append(resolve(self.template_path, context))
        template_paths.append('Kraggne/breadcrumb.html')
            
        try:
            t = select_template(template_paths)
        except:
            return ''

        # Set content as variable inside context, if variable_name is given
        if self.variable_name:
            context[resolve(self.variable_name, context)] = breadcrumb
        else:
            context['object_list'] = breadcrumb

        content = t.render(context)

        return content


def do_breadcrumb(parser, token):
    """
    {% breadcrumb ["slug"] [include_self=True] %}
    {% breadcrumb ["slug"] into "slug_object" [include_self=True] %}
    {% breadcrumb ["slug"] with "templatename.html" [include_self=True] %}
    {% breadcrumb ["slug"] with "templatename.html" as "variable" [include_self=True] %}
    """

    bits = token.contents.split()
    args = {
        'slug': next_bit_for(bits, 'breadcrumb'),
        'store_in_object': next_bit_for(bits, 'into'),
        'template_path': next_bit_for(bits, 'with'),
        'variable_name': next_bit_for(bits, 'as'),
        'include_self' : get_val_for(bits,'include_self',if_none=True,type=bool)
    }
    return breadcumbNode(**args)

register.tag('breadcrumb', do_breadcrumb)

##################################################################
####################### SHOW MENU ################################
##################################################################

class menuNode(Node):

    def __init__(self, slug,template_path=None,store_in_object=None,variable_name=None,include_self=True,level_min=0,level_nb=0):
        self.slug = slug
        self.template_path = template_path
        self.store_in_object = store_in_object
        self.variable_name = variable_name
        self.include_self = include_self
        self.level_min = level_min
        self.level_nb = level_nb

    def render(self, context):

        if not self.slug:
            try:
                menu = context["page"]
            except:
                return ''
        else:
            menu = GetMenuBySlug(resolve(self.slug,context))

        if not menu:
            return ''

        if self.level_min > 0:
            tree = menu.get_descendants(include_self=self.include_self).filter(is_visible=True,level__gte=menu.level+self.level_min)
        elif self.level_min < 0:
            tmp = menu
            menu = menu.get_ancestors(include_self=self.include_self).filter(level__gte=menu.level+self.level_min-1)[0]
            tree = menu.get_descendants(include_self=False).filter(is_visible=True)
            if self.include_self == False:
                tree=tree.exclude(pk__in=tmp.get_descendants(include_self=True))
        else:
            tree = menu.get_descendants(include_self=self.include_self).filter(is_visible=True)


{'fraction_non_alphanumeric': 0.20095147478591818, 'fraction_numerical': 0.002664129400570885, 'mean_word_length': 2.8572477064220183, 'pattern_counts': {'":': 0, '<': 5, '<?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': '4106132', 'n_tokens_mistral': 3050, 'n_tokens_neox': 2954, 'n_words': 561}