From: Michael Elsdörfer Date: Wed, 24 Mar 2010 17:10:03 +0000 (+0100) Subject: More refactoring: Custom table classes now don't need to assign to the _snapshot... X-Git-Tag: 0.2~21 X-Git-Url: http://git.tremily.us/?a=commitdiff_plain;h=3c71f8dd338d0116408bebb58358f6edf9df7c59;p=django-tables2.git More refactoring: Custom table classes now don't need to assign to the _snapshot argument directly, but can simply return their snapshot. --- diff --git a/django_tables/base.py b/django_tables/base.py index 540ab63..bdb97bb 100644 --- a/django_tables/base.py +++ b/django_tables/base.py @@ -443,6 +443,15 @@ class BaseTable(object): # ``Table`` class docstring for more information. self.base_columns = copy.deepcopy(type(self).base_columns) + def _reset_snapshot(self, reason): + """Called to reset the current snaptshot, for example when + options change that could affect it. + + ``reason`` is given so that subclasses can decide that a + given change may not affect their snaptshot. + """ + self._snapshot = None + def _build_snapshot(self): """Rebuild the table for the current set of options. @@ -452,11 +461,11 @@ class BaseTable(object): Subclasses should override this. """ - self._snapshot = copy.copy(self._data) + return self._data def _get_data(self): if self._snapshot is None: - self._build_snapshot() + self._snapshot = self._build_snapshot() return self._snapshot data = property(lambda s: s._get_data()) @@ -516,8 +525,7 @@ class BaseTable(object): return True def _set_order_by(self, value): - if self._snapshot is not None: - self._snapshot = None + self._reset_snapshot('order_by') # accept both string and tuple instructions order_by = (isinstance(value, basestring) \ and [value.split(',')] \ diff --git a/django_tables/memory.py b/django_tables/memory.py index cd2118b..35ccac6 100644 --- a/django_tables/memory.py +++ b/django_tables/memory.py @@ -79,7 +79,7 @@ class MemoryTable(BaseTable): if self.order_by: actual_order_by = self._resolve_sort_directions(self.order_by) sort_table(snapshot, self._cols_to_fields(actual_order_by)) - self._snapshot = snapshot + return snapshot class Table(MemoryTable): diff --git a/django_tables/models.py b/django_tables/models.py index b7bf76c..4a24ad5 100644 --- a/django_tables/models.py +++ b/django_tables/models.py @@ -245,4 +245,4 @@ class ModelTable(BaseTable): if self.order_by: actual_order_by = self._resolve_sort_directions(self.order_by) queryset = queryset.order_by(*self._cols_to_fields(actual_order_by)) - self._snapshot = queryset + return queryset