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

from django import template
from django.conf import settings
from django.utils import timezone
from django.utils.safestring import mark_safe
from django.template.defaultfilters import linebreaksbr
from django.core.cache import cache

from opps.containers.models import Container, ContainerBox, Mirror
from opps.channels.models import Channel

from magicdate import magicdate


register = template.Library()
logger = logging.getLogger()


@register.assignment_tag
def get_recommendations(query_slice, child_class, container):
    """
    Takes the container object and get recommendations and filters by given
    child_class and limit the result by given slice.

    Sample usage::

        {% get_recommendations ":3" "post" container as context_var %}

    query_slice:
        A string with slice notation to limit the queryset result
    child_class:
        Name of child class
    container:
        A Container object
    """

    if not query_slice:
        query_slice = ":"

    bits = []
    for x in query_slice.split(':'):
        if len(x) == 0:
            bits.append(None)
        else:
            bits.append(int(x))

    return container.recommendation(child_class, bits)


@register.simple_tag(takes_context=True)
def get_containerbox(context, slug, template_name=None, **extra_context):

    request = context['request']
    is_mobile = getattr(request, 'is_mobile', False)

    cachekey = "ContainerBox-{}-{}-{}-{}".format(
        slug,
        template_name,
        is_mobile,
        request.site.id
    )

    render = cache.get(cachekey)
    if render:
        return render

    filters = {}
    filters['site'] = request.site
    filters['slug'] = slug
    filters['date_available__lte'] = timezone.now()
    filters['published'] = True

    master_site = settings.OPPS_CONTAINERS_SITE_ID or 1

    try:
        box = ContainerBox.objects.get(**filters)
    except ContainerBox.DoesNotExist:
        box = None

    if request.site.id != master_site and \
       not box or not getattr(box, 'has_content', False):
        filters['site'] = master_site
        try:
            box = ContainerBox.objects.get(**filters)
        except ContainerBox.DoesNotExist:
            box = None

    if not box:
        box = ContainerBox.objects.none()

    t = template.loader.get_template('articles/articlebox_detail.html')
    if template_name:
        t = template.loader.get_template(template_name)

    context = {<commit_msg>Fix AttributeError when there is no request.site (we should always add .site to request)<commit_after># -*- coding: utf-8 -*-
import logging

from django import template
from django.conf import settings
from django.utils import timezone
from django.utils.safestring import mark_safe
from django.template.defaultfilters import linebreaksbr
from django.core.cache import cache
from django.contrib.sites.models import Site

from opps.containers.models import Container, ContainerBox, Mirror
from opps.channels.models import Channel

from magicdate import magicdate


register = template.Library()
logger = logging.getLogger()


@register.assignment_tag
def get_recommendations(query_slice, child_class, container):
    """
    Takes the container object and get recommendations and filters by given
    child_class and limit the result by given slice.

    Sample usage::

        {% get_recommendations ":3" "post" container as context_var %}

    query_slice:
        A string with slice notation to limit the queryset result
    child_class:
        Name of child class
    container:
        A Container object
    """

    if not query_slice:
        query_slice = ":"

    bits = []
    for x in query_slice.split(':'):
        if len(x) == 0:
            bits.append(None)
        else:
            bits.append(int(x))

    return container.recommendation(child_class, bits)


@register.simple_tag(takes_context=True)
def get_containerbox(context, slug, template_name=None, **extra_context):

    request = context['request']
    current_site = getattr(
        request,
        'site',
        Site.objects.get(pk=settings.SITE_ID)
    )
    is_mobile = getattr(request, 'is_mobile', False)

    cachekey = "ContainerBox-{}-{}-{}-{}".format(
        slug,
        template_name,
        is_mobile,
        current_site.id
    )

    render = cache.get(cachekey)
    if render:
        return render

    filters = {}
    filters['site'] = current_site
    filters['slug'] = slug
    filters['date_available__lte'] = timezone.now()
    filters['published'] = True

    master_site = settings.OPPS_CONTAINERS_SITE_ID or 1

    try:
        box = ContainerBox.objects.get(**filters)
    except ContainerBox.DoesNotExist:
        box = None

    if current_site.id != master_site and \
       not box or not getattr(box, 'has_content', False):
        filters['site'] = master_site
        try:
            box = ContainerBox.objects.get(**filters)
        except ContainerBox.DoesNotExist:
            box = None

    if not box:
        box = ContainerBox.objects.none()

    t = template.loader.get_template('articles/articlebox_detail.html')
    if template_name:
        t = template.loader.get_template(template_name)

    context = {
{'fraction_non_alphanumeric': 0.09468470184797104, 'fraction_numerical': 0.0015240998285387694, 'mean_word_length': 2.917910447761194, 'pattern_counts': {'":': 4, '<': 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': '7141922', 'n_tokens_mistral': 1546, 'n_tokens_neox': 1479, 'n_words': 408}