~ris/loco-team-portal/fix-552762

« back to all changes in this revision

Viewing changes to loco_directory/common/templatetags/list_to_columns.py

  • Committer: Daniel Holbach
  • Date: 2009-12-21 11:03:49 UTC
  • Revision ID: daniel.holbach@canonical.com-20091221110349-bo2zmme5i2e4egif
start at revision 1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# -*- coding: utf-8 -*-
 
2
from django import template
 
3
 
 
4
register = template.Library()
 
5
 
 
6
class SplitListNode(template.Node):
 
7
    def __init__(self, list, cols, new_list):
 
8
        self.list = list
 
9
        self.cols = cols
 
10
        self.new_list = new_list
 
11
 
 
12
    def split_seq(self, list, cols=2):
 
13
        start = 0
 
14
        for i in xrange(cols):
 
15
            stop = start + len(list[i::cols])
 
16
            yield list[start:stop]
 
17
            start = stop
 
18
 
 
19
    def render(self, context):
 
20
        context[self.new_list] = self.split_seq(context[self.list],
 
21
            int(self.cols))
 
22
        return ''
 
23
 
 
24
def list_to_columns(parser, token):
 
25
    bits = token.contents.split()
 
26
    if len(bits) != 5:
 
27
        raise template.TemplateSyntaxError, "list_to_columns list as new_list 2"
 
28
    if bits[2] != 'as':
 
29
        raise template.TemplateSyntaxError, "second argument to the list_to_columns tag must be 'as'"
 
30
    return SplitListNode(bits[1], bits[4], bits[3])
 
31
list_to_columns = register.tag(list_to_columns)