<commit_before>import logging

from django.conf import settings
from django import template
from django.db import models
from django.utils.encoding import smart_str
from django.utils.safestring import mark_safe
from django.template.defaultfilters import stringfilter
from django.contrib.contenttypes.models import ContentType

from ella.core.models import Listing, Category
from ella.core.managers import ListingHandler
from ella.core.cache.utils import get_cached_object
from ella.core.box import Box


log = logging.getLogger('ella.core.templatetags')
register = template.Library()

class ListingNode(template.Node):
    def __init__(self, var_name, parameters):
        self.var_name = var_name
        self.parameters = parameters

    def render(self, context):
        params = {}
        for key, value in self.parameters.items():
            if isinstance(value, template.Variable):
                value = value.resolve(context)
            params[key] = value

        if 'category' in params and isinstance(params['category'], basestring):
            params['category'] = get_cached_object(Category, tree_path=self.resolved_parameters['category'], site__id=settings.SITE_ID)

        limits = {}
        if 'offset' in params:
            # templates are 1-based, compensate
            limits['offset'] = params.pop('offset') - 1

        if 'count' in params:
            limits['count'] = params.pop('count')

        lh = Listing.objects.get_queryset_wrapper(**params)

        context[self.var_name] = lh.get_listings(**limits)
        return ''

@register.tag
def listing(parser, token):
    """
    Tag that will obtain listing of top objects for a given category and store them in context under given name.<commit_msg>forgotten rename<commit_after>import logging

from django.conf import settings
from django import template
from django.db import models
from django.utils.encoding import smart_str
from django.utils.safestring import mark_safe
from django.template.defaultfilters import stringfilter
from django.contrib.contenttypes.models import ContentType

from ella.core.models import Listing, Category
from ella.core.managers import ListingHandler
from ella.core.cache.utils import get_cached_object
from ella.core.box import Box


log = logging.getLogger('ella.core.templatetags')
register = template.Library()

class ListingNode(template.Node):
    def __init__(self, var_name, parameters):
        self.var_name = var_name
        self.parameters = parameters

    def render(self, context):
        params = {}
        for key, value in self.parameters.items():
            if isinstance(value, template.Variable):
                value = value.resolve(context)
            params[key] = value

        if 'category' in params and isinstance(params['category'], basestring):
            params['category'] = get_cached_object(Category, tree_path=params['category'], site__id=settings.SITE_ID)

        limits = {}
        if 'offset' in params:
            # templates are 1-based, compensate
            limits['offset'] = params.pop('offset') - 1

        if 'count' in params:
            limits['count'] = params.pop('count')

        lh = Listing.objects.get_queryset_wrapper(**params)

        context[self.var_name] = lh.get_listings(**limits)
        return ''

@register.tag
def listing(parser, token):
    """
    Tag that will obtain listing of top objects for a given category and store them in context under given name.
{'fraction_non_alphanumeric': 0.09332562843108928, 'fraction_numerical': 0.0011557353366079169, 'mean_word_length': 3.3220973782771535, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '7366322', 'n_tokens_mistral': 936, 'n_tokens_neox': 908, 'n_words': 270}