Commits

Anonymous committed 208069d

Added support for the `sort` and `dir` parameters according to ExtJS Ext.data.Store needs.

  • Participants
  • Parent commits b024c52

Comments (0)

Files changed (1)

File piston/handler.py

     max_resources_per_page = 20
     resource_name = None
     allowed_filters = ()
+    allowed_order_by = ()
     
     def read(self, request,  start = None, count=None):
         '''
             except KeyError:
                 raise KeyError("Paginated resources must be passed a 'start' parameter.")
         if count is None:
-            count = request.GET.get("count", PaginatedCollectionBaseHandler.max_resources_per_page)
+            count = request.GET.get("limit", PaginatedCollectionBaseHandler.max_resources_per_page)
         # just make sure we have ints, and no insane values 
         start = int(start)
         count = min(int(count), PaginatedCollectionBaseHandler.max_resources_per_page)
         if slices_in_querystring:
             query_dict = dict([part.split('=') for part in request.META["QUERY_STRING"].split('&')]) 
 
-
         queryset = self.model.objects.all()
 
+        # filter conditions in the queryset using the `allowed_filters` attribute
         for key in self.allowed_filters:
             if key in query_dict:
                 queryset = queryset.filter(**{key: query_dict[key]})
-                
-            
+
+        # order by conditions in the queryset using the `allowed_order_by` attribute 
+        if query_dict.has_key('sort') and query_dict['sort'] in self.allowed_order_by:
+            if query_dict.has_key('dir') and query_dict['dir'] == 'DESC':
+                sort_key = "-%s" % query_dict['sort']
+            else:
+                sort_key = query_dict['sort']
+            queryset = queryset.order_by(sort_key)
+
         # the queryset proper
         total = queryset.count()
         end = min (total, start + count)