From c0292c03e10cb9e5b8ea1c5cd3195a4de54c7068 Mon Sep 17 00:00:00 2001 From: Bradley Ayers Date: Sun, 27 Feb 2011 23:45:20 +1000 Subject: [PATCH] using QuerySets actually works now, added a relevant test that now uses django-attest --- django_tables/tables.py | 35 +++++++++++++++++++++-------------- django_tables/tests/models.py | 31 +++++++++++++------------------ 2 files changed, 34 insertions(+), 32 deletions(-) diff --git a/django_tables/tables.py b/django_tables/tables.py index 5b4f8d3..09062b8 100644 --- a/django_tables/tables.py +++ b/django_tables/tables.py @@ -22,33 +22,40 @@ class TableData(object): """ def __init__(self, data, table): from django.db.models.query import QuerySet - self._data = data if not isinstance(data, QuerySet) else None - self._queryset = data if isinstance(data, QuerySet) else None + if isinstance(data, QuerySet): + self.queryset = data + elif isinstance(data, list): + self.list = data + else: + raise ValueError('data must be a list or QuerySet object, not %s' + % data.__class__.__name__) self._table = table # work with a copy of the data that has missing values populated with # defaults. - if self._data: - self._data = copy.copy(self._data) - self._populate_missing_values(self._data) + if hasattr(self, 'list'): + self.list = copy.copy(self.list) + self._populate_missing_values(self.list) def __len__(self): # Use the queryset count() method to get the length, instead of # loading all results into memory. This allows, for example, # smart paginators that use len() to perform better. - return self._queryset.count() if self._queryset else len(self._data) + return (self.queryset.count() if hasattr(self, 'queryset') + else len(self.list)) def order_by(self, order_by): """Order the data based on column names in the table.""" # translate order_by to something suitable for this data order_by = self._translate_order_by(order_by) - if self._queryset: + if hasattr(self, 'queryset'): # need to convert the '.' separators to '__' (filter syntax) - order_by = order_by.replace(Accessor.SEPARATOR, - QUERYSET_ACCESSOR_SEPARATOR) - self._queryset = self._queryset.order_by(**order_by) + order_by = [o.replace(Accessor.SEPARATOR, + QUERYSET_ACCESSOR_SEPARATOR) + for o in order_by] + self.queryset = self.queryset.order_by(*order_by) else: - self._data.sort(cmp=order_by.cmp) + self.list.sort(cmp=order_by.cmp) def _translate_order_by(self, order_by): """Translate from column names to column accessors""" @@ -113,8 +120,8 @@ class TableData(object): value = bound_column.formatter(value) return value - def __getitem__(self, key): - return self._data[key] + def __getitem__(self, index): + return (self.list if hasattr(self, 'list') else self.queryset)[index] class DeclarativeColumnsMetaclass(type): @@ -193,7 +200,7 @@ class Table(StrAndUnicode): the perfect fit for this. Instead, ``base_colums`` is copied to table instances, so modifying that will not touch the class-wide column list. - + """ self._rows = Rows(self) # bound rows self._columns = Columns(self) # bound columns diff --git a/django_tables/tests/models.py b/django_tables/tests/models.py index 4148822..4bc010b 100644 --- a/django_tables/tests/models.py +++ b/django_tables/tests/models.py @@ -2,28 +2,18 @@ from django.contrib.auth.models import User from django.conf import settings from django.core.paginator import * import django_tables as tables -import django_tables.models as django_tables_models +from django_attest import TestContext from attest import Tests -# we're going to test against User, so let's create a few -User.objects.create_user('fake-user-1', 'fake-1@example.com', 'password') -User.objects.create_user('fake-user-2', 'fake-2@example.com', 'password') -User.objects.create_user('fake-user-3', 'fake-3@example.com', 'password') -User.objects.create_user('fake-user-4', 'fake-4@example.com', 'password') - - models = Tests() -@models.context -def transaction(): - print 't1' - yield - print 't2' +models.context(TestContext()) + @models.context -def context(): +def samples(): class Context(object): class UserTable(tables.Table): username = tables.Column() @@ -37,13 +27,18 @@ def context(): last_login = tables.Column() date_joined = tables.Column() - print 'c1' + # we're going to test against User, so let's create a few + User.objects.create_user('fake-user-1', 'fake-1@example.com', 'password') + User.objects.create_user('fake-user-2', 'fake-2@example.com', 'password') + User.objects.create_user('fake-user-3', 'fake-3@example.com', 'password') + User.objects.create_user('fake-user-4', 'fake-4@example.com', 'password') + yield Context - print 'c2' + @models.test -def simple(context): - table = context.UserTable(User.objects.all()) +def simple(dj, samples): + table = samples.UserTable(User.objects.all()) ''' -- 2.26.2