Commits

Waldemar Kornewald committed 674aa35

IMPORTANT: set_cursor() now returns the modified queryset. update your code! What changed: improved cursor API to be less hacky. Now the cursor won't get lost if you add filters after setting the cursor.

Comments (0)

Files changed (2)

 from google.appengine.datastore.datastore_query import Cursor
 
+class CursorQueryMixin(object):
+    def clone(self, *args, **kwargs):
+        kwargs['_gae_cursor'] = getattr(self, '_gae_cursor', None)
+        kwargs['_gae_start_cursor'] = getattr(self, '_gae_start_cursor', None)
+        kwargs['_gae_end_cursor'] = getattr(self, '_gae_end_cursor', None)
+        return super(CursorQueryMixin, self).clone(*args, **kwargs)
+
 def get_cursor(queryset):
     # Evaluate QuerySet
     len(queryset)
     return Cursor.to_websafe_string(cursor)
 
 def set_cursor(queryset, start=None, end=None):
+    queryset = queryset.all()
+    class CursorQuery(CursorQueryMixin, queryset.query.__class__):
+        pass
+    queryset.query = queryset.query.clone(klass=CursorQuery)
     if start is not None:
         start = Cursor.from_websafe_string(start)
-        queryset.query._gae_start_cursor = start
+    queryset.query._gae_start_cursor = start
     if end is not None:
         end = Cursor.from_websafe_string(end)
-        queryset.query._gae_end_cursor = end
+    queryset.query._gae_end_cursor = end
     # Evaluate QuerySet
-    len(queryset)
+    return queryset
         for item in results:
             query = FieldsWithOptionsModel.objects.all()[:1]
             if cursor is not None:
-                set_cursor(query, cursor)
+                query = set_cursor(query, cursor)
             next = query[0]
             self.assertEqual(next.pk, item.pk)
             cursor = get_cursor(query)
-        query = FieldsWithOptionsModel.objects.all()[:1]
-        set_cursor(query, cursor)
-        self.assertEqual(list(query), [])
+        query = set_cursor(FieldsWithOptionsModel.objects.all(), cursor)
+        self.assertEqual(list(query[:1]), [])
 
     def test_Q_objects(self):
         self.assertEquals([entity.email for entity in