diff mbox

[Branch,~linaro-validation/lava-server/trunk] Rev 387: provide a hook to recompute the queryset after the table columns have changed

Message ID 20120818070713.29152.46268.launchpad@ackee.canonical.com
State Accepted
Headers show

Commit Message

Michael-Doyle Hudson Aug. 18, 2012, 7:07 a.m. UTC
------------------------------------------------------------
revno: 387
committer: Michael Hudson-Doyle <michael.hudson@linaro.org>
branch nick: trunk
timestamp: Sat 2012-08-18 12:06:05 +1200
message:
  provide a hook to recompute the queryset after the table columns have changed
modified:
  lava/utils/data_tables/backends.py
  lava/utils/data_tables/tables.py


--
lp:lava-server
https://code.launchpad.net/~linaro-validation/lava-server/trunk

You are subscribed to branch lp:lava-server.
To unsubscribe from this branch go to https://code.launchpad.net/~linaro-validation/lava-server/trunk/+edit-subscription
diff mbox

Patch

=== modified file 'lava/utils/data_tables/backends.py'
--- lava/utils/data_tables/backends.py	2012-08-17 05:52:39 +0000
+++ lava/utils/data_tables/backends.py	2012-08-18 00:06:05 +0000
@@ -240,6 +240,7 @@ 
                 "{asc_desc}{column}".format(
                     asc_desc="-" if order == 'desc' else '',
                     column=accessor.replace('.', '__')))
+        print order_by
         return queryset.order_by(*order_by)
 
     def process(self, query):

=== modified file 'lava/utils/data_tables/tables.py'
--- lava/utils/data_tables/tables.py	2012-05-29 01:33:39 +0000
+++ lava/utils/data_tables/tables.py	2012-08-18 00:06:05 +0000
@@ -152,15 +152,9 @@ 
         super(DataTablesTable, self).__init__(
             data=data, sortable=sortable, empty_text=empty_text, attrs=attrs,
             template=template)
+        self._full_length = None
         if not data_backed_table:
-            self.full_queryset = self.get_queryset(*params)
-            self.full_length = self.full_queryset.count()
-            ordering = self.datatable_opts.get('aaSorting', [[0, 'asc']])
-            sorted_queryset = TableBackend(self).apply_sorting_columns(
-                self.full_queryset, ordering)
-            display_length = self.datatable_opts.get('iDisplayLength', 10)
-            del self.data.list
-            self.data.queryset = sorted_queryset[:display_length]
+            self._compute_queryset(params)
         # We are careful about modifying the attrs here -- if it comes from
         # class Meta:-type options, we don't want to modify the original
         # value!
@@ -175,6 +169,22 @@ 
             })
         self.attrs = attrs
 
+    def _compute_queryset(self, params):
+        self.full_queryset = self.get_queryset(*params)
+        ordering = self.datatable_opts.get('aaSorting', [[0, 'asc']])
+        sorted_queryset = TableBackend(self).apply_sorting_columns(
+            self.full_queryset, ordering)
+        display_length = self.datatable_opts.get('iDisplayLength', 10)
+        if getattr(self.data, 'list', None) is not None:
+            del self.data.list
+        self.data.queryset = sorted_queryset[:display_length]
+
+    @property
+    def full_length(self):
+        if self._full_length is None:
+            self._full_length = self.full_queryset.count()
+        return self._full_length
+
     @classmethod
     def json(cls, request, params=()):
         """Format table data according to request.
@@ -211,7 +221,7 @@ 
                 'bServerSide': True,
                 'sAjaxSource': self.source,
                 'bFilter': bool(self.searchable_columns),
-                'iDeferLoading': self.full_length,
+                'iDeferLoading': self.full_queryset.count(),
                 })
         aoColumnDefs = opts['aoColumnDefs'] = []
         for col in self.columns: