from django import template

register = template.Library()


@register.simple_tag(takes_context=True)
def get_values(context, attribute_path, set_index='*', index='*'):
    if set_index == '*' and index == '*':
        try:
            return context['values'][attribute_path]
        except KeyError:
            return []

    elif set_index == '*':
        try:
            value_sets = context['values'][attribute_path]
        except KeyError:
            return []

        values = []
        for value_set in value_sets:
            try:
                values.append(value_set[index])
            except IndexError:
                values.append('')
        values = sorted(values, reverse=False, key=lambda x: x.id)
        return values

    elif index == '*':
        try:
            return context['values'][attribute_path][set_index]
        except (KeyError, IndexError):
            return []
    else:
        try:
            return context['values'][attribute_path][set_index][index]
        except (KeyError, IndexError):
            return ''


@register.simple_tag(takes_context=True)
def get_numbers(context, attribute_path, set_index='*', index='*'):
    numbers = []
    for value_set in get_values(context, attribute_path, set_index, index):
        numbers.append([value.as_number for value in value_set])
    return numbers


@register.simple_tag(takes_context=True)
def get_set_values(context, set, attribute_path, index='*'):
    return get_values(context, attribute_path, set.set_index, index)


@register.simple_tag(takes_context=True)
def get_set_numbers(context, set, attribute_path, index='*'):
    return get_numbers(context, attribute_path, set.set_index, index)


@register.simple_tag(takes_context=True)
def get_set_sum(context, set, attribute_path, index='*'):
    numbers = get_set_numbers(context, set, attribute_path, index)
    return sum(numbers)


@register.simple_tag(takes_context=True)
def get_value(context, attribute_path, set_index=0, index=0):
    return get_values(context, attribute_path, set_index, index)


@register.simple_tag(takes_context=True)
def get_number(context, attribute_path, set_index=0, index=0):
    return get_numbers(context, attribute_path, set_index, index)


@register.simple_tag(takes_context=True)
def get_set_value(context, set, attribute_path, index=0):
    return get_set_values(context, set, attribute_path, index)


@register.simple_tag(takes_context=True)
def get_set_number(context, set, attribute_path, index=0):
    return get_set_numbers(context, set, attribute_path, index)


@register.simple_tag(takes_context=True)
def get_set(context, attribute_path):
    id_path = attribute_path.rstrip('/') + '/id'
    return get_values(context, id_path, index=0)


@register.inclusion_tag('views/tags/value.html', takes_context=True)
def render_value(context, attribute_path, set_index=0, index=0):
    try:
        return {'value': get_values(context, attribute_path, set_index, index)}
    except KeyError:
        return None


@register.inclusion_tag('views/tags/value.html', takes_context=True)
def render_set_value(context, set, attribute_path, index=0):
    try:
        return {'value': get_values(context, attribute_path, set.set_index, index)}
    except KeyError:
        return None


@register.inclusion_tag('views/tags/value_list.html', takes_context=True)
def render_value_list(context, attribute_path, set_index=0):
    try:
        return {'values': get_values(context, attribute_path, set_index)}
    except KeyError:
        return None


@register.inclusion_tag('views/tags/value_list.html', takes_context=True)
def render_set_value_list(context, set, attribute_path):
    try:
        return {'values': get_values(context, attribute_path, set.set_index)}
    except KeyError:
        return None


@register.inclusion_tag('views/tags/value_inline_list.html', takes_context=True)
def render_value_inline_list(context, attribute_path, set_index=0):
    try:
        return {'values': get_values(context, attribute_path, set_index)}
    except KeyError:
        return None


@register.inclusion_tag('views/tags/value_inline_list.html', takes_context=True)
def render_set_value_inline_list(context, set, attribute_path):
    try:
        return {'values': get_values(context, attribute_path, set.set_index)}
    except KeyError:
        return None

@@ -1113,1457 +1113,461 @@
 get_
-numbers(context, attribute_path, set_index='*', index='*'):%0A    numbers = %5B%5D%0A    for value_set in get_values(context, attribute_path, set_index, index):%0A        numbers.append(%5Bvalue.as_number for value in value_set%5D)%0A    return numbers%0A%0A%0A@register.simple_tag(takes_context=True)%0Adef get_set_values(context, set, attribute_path, index='*'):%0A    return get_values(context, attribute_path, set.set_index, index)%0A%0A%0A@register.simple_tag(takes_context=True)%0Adef get_set_numbers(context, set, attribute_path, index='*'):%0A    return get_numbers(context, attribute_path, set.set_index, index)%0A%0A%0A@register.simple_tag(takes_context=True)%0Adef get_set_sum(context, set, attribute_path, index='*'):%0A    numbers = get_set_numbers(context, set, attribute_path, index)%0A    return sum(numbers)%0A%0A%0A@register.simple_tag(takes_context=True)%0Adef get_value(context, attribute_path, set_index=0, index=0):%0A    return get_values(context, attribute_path, set_index, index)%0A%0A%0A@register.simple_tag(takes_context=True)%0Adef get_number(context, attribute_path, set_index=0, index=0):%0A    return get_numbers(context, attribute_path, set_index, index)%0A%0A%0A@register.simple_tag(takes_context=True)%0Adef get_set_value(context, set, attribute_path, index=0):%0A    return get_set_values(context, set, attribute_path, index)%0A%0A%0A@register.simple_tag(takes_context=True)%0Adef get_set_number(context, set, attribute_path, index=0):%0A    return get_set_numbers(context, set, attribute_path, index)%0A
+set_values(context, set, attribute_path, index='*'):%0A    return get_values(context, attribute_path, set.set_index, index)%0A%0A@register.simple_tag(takes_context=True)%0Adef get_value(context, attribute_path, set_index=0, index=0):%0A    return get_values(context, attribute_path, set_index, index)%0A%0A@register.simple_tag(takes_context=True)%0Adef get_set_value(context, set, attribute_path, index=0):%0A    return get_set_values(context, set, attribute_path, index)
 %0A%0A@r

Remove ununsed tags