Skip to content

Commit

Permalink
Supports Pagintor from recent django 1.4 alpha
Browse files Browse the repository at this point in the history
BoundRows retuns another BoundRows when sliced.
  • Loading branch information
koledennix committed Nov 11, 2011
1 parent 4852e9c commit 2c0ed12
Showing 1 changed file with 17 additions and 7 deletions.
24 changes: 17 additions & 7 deletions django_tables2/rows.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# -*- coding: utf-8 -*-
import inspect
from itertools import imap, ifilter
from itertools import ifilter
from django.db import models
from django.db.models.fields import FieldDoesNotExist
from django.utils.functional import curry
Expand Down Expand Up @@ -174,27 +174,37 @@ class BoundRows(object):
:param table: the table in which the rows exist.
"""
def __init__(self, table):
def __init__(self, table, slice=None):
self.table = table
if slice is not None:
self._data = table.data[slice]
else:
self._data = None

@property
def data(self):
if self._data:
return self._data
else:
return self.table.data

def __iter__(self):
"""Convience method for :meth:`.BoundRows.all`"""
for record in self.table.data:
for record in self.data:
yield BoundRow(self.table, record)

def __len__(self):
"""Returns the number of rows in the table."""
return len(self.table.data)
return len(self.data)

# for compatibility with QuerySetPaginator
count = __len__

def __getitem__(self, key):
"""Allows normal list slicing syntax to be used."""
if isinstance(key, slice):
return imap(lambda record: BoundRow(self.table, record),
self.table.data[key])
return BoundRows(self.table, key)
elif isinstance(key, int):
return BoundRow(self.table, self.table.data[key])
return BoundRow(self.table, self.data[key])
else:
raise TypeError('Key must be a slice or integer.')

0 comments on commit 2c0ed12

Please sign in to comment.