<commit_before>from django import template

from ella.series.models import Serie, SeriePart

register = template.Library()

@register.tag
def get_serie(parser, token):
    """
    Template tag get_serie.

    Usage::
        {% get_serie for <placement>[ limit <X>] as <result> %}

    Examples::

        {% get_serie for placement as serie %}
        {% get_serie for placement limit 10 as serie %}
    """
    params = get_serie_parse(token.split_contents())
    return SerieNode(params)

def get_serie_parse(input):
    if len(input) < 5:
        raise template.TemplateSyntaxError("Two or more arguments are expected in %r tag" % input[0])
    params = {}

    o = 1
    # for
    if input[o] == 'for':
        params['for'] = input[o+1]
    else:
        raise template.TemplateSyntaxError("Unknown argument %r in tag %r" % (input[o], input[0]))
    o = 3
    # limit
    if input[o] == 'limit':
        params['limit'] = input[o+1]
        o = o + 2
    # as
    if input[o] == 'as':
        params['as'] = input[o+1]
    else:
        raise template.TemplateSyntaxError("%r tag requires 'as' argument" % (input[0]))

    return params

class SerieNode(template.Node):
    def __init__(self, params):
        self.params = params

    def render(self, context):
        # TODO: caching

        # Get placement
        placement = template.Variable(self.params['for']).resolve(context)

        try:
            if isinstance(placement.target, Serie):
                parts = placement.target.parts
            else:
                current_part = SeriePart.objects.get(placement=placement) # Asi pujde vzit primo z placement.target
                parts = current_part.serie.parts

                # Shall I hide newer parts?
                if current_part.serie.hide_newer_parts:
                    parts = parts.filter(placement__publish_from__lte=current_part.placement.publish_from)

                # TODO: limit

        except SeriePart.DoesNotExist:
            return ''

        context[self.params['as']] = parts
        return ''<commit_msg>Update get_serie template tag Refs #373<commit_after>from django import template

from ella.series.models import Serie, SeriePart

register = template.Library()

@register.tag
def get_serie(parser, token):
    """
    Template tag get_serie.

    Usage::
        {% get_serie for <placement>[ limit <X>] as <result> %}

    Examples::

        {% get_serie for placement as serie %}
        {% get_serie for placement limit 10 as serie %}
    """
    params = get_serie_parse(token.split_contents())
    return SerieNode(params)

def get_serie_parse(input):
    if len(input) < 5:
        raise template.TemplateSyntaxError("Two or more arguments are expected in %r tag" % input[0])
    params = {}

    o = 1
    # for
    if input[o] == 'for':
        params['for'] = input[o+1]
    else:
        raise template.TemplateSyntaxError("Unknown argument %r in tag %r" % (input[o], input[0]))
    o = 3
    # limit
    if input[o] == 'limit':
        params['limit'] = input[o+1]
        o = o + 2
    # as
    if input[o] == 'as':
        params['as'] = input[o+1]
    else:
        raise template.TemplateSyntaxError("%r tag requires 'as' argument" % (input[0]))

    return params

class SerieNode(template.Node):
    def __init__(self, params):
        self.params = params

    def render(self, context):
        # TODO: caching

        # Get placement
        placement = template.Variable(self.params['for']).resolve(context)

        try:
            if isinstance(placement.target, Serie):
                parts = placement.target.parts
            else:
                current_part = SeriePart.objects.get(placement=placement)
                parts = current_part.serie.parts

                # Shall I hide newer parts?
                if current_part.serie.hide_newer_parts:
                    parts = parts.filter(placement__publish_from__lte=current_part.placement.publish_from)

        except SeriePart.DoesNotExist:
            return ''

        # Limit
        if self.params.has_key('limit'):
            parts = parts[:int(self.params['limit'])]

        context[self.params['as']] = parts
        return ''
{'fraction_non_alphanumeric': 0.11548556430446194, 'fraction_numerical': 0.006442376521116679, 'mean_word_length': 2.2981904012588514, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 9, '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': '2344522', 'n_tokens_mistral': 1252, 'n_tokens_neox': 1216, 'n_words': 312}