From 8e01f9f667f0407c35aca18b34498fa349bc33e4 Mon Sep 17 00:00:00 2001 From: Omar Diaz Date: Thu, 13 Jul 2017 16:35:31 +0200 Subject: [PATCH 1/6] Pagination --- .gitignore | 1 + carbon14/django.py | 21 +++++++++++++++++++++ 2 files changed, 22 insertions(+) diff --git a/.gitignore b/.gitignore index 25efed5..1fcae76 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ *.egg-info/ .cache/ __pycache__/ +/.idea/ \ No newline at end of file diff --git a/carbon14/django.py b/carbon14/django.py index d693050..f9f6682 100644 --- a/carbon14/django.py +++ b/carbon14/django.py @@ -86,3 +86,24 @@ def serialize(self, instance, *args, **kwargs): if isinstance(value, Point): value = tuple(value) return value + + +class PaginatedModelCollection(ModelCollection): + def _resolve( + self, + level, + instances, + ctx, + ids=None, + offset=0, + limit=0, + **kwargs + ): + return super()._resolve( + level, + instances, + ctx, + ids, + **kwargs + )[offset:limit] + From 5a38b4ff7e4760bc9724584f44fc765b168c10f7 Mon Sep 17 00:00:00 2001 From: Omar Diaz Date: Thu, 13 Jul 2017 16:58:07 +0200 Subject: [PATCH 2/6] Pagination --- carbon14/django.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/carbon14/django.py b/carbon14/django.py index f9f6682..66a8312 100644 --- a/carbon14/django.py +++ b/carbon14/django.py @@ -105,5 +105,5 @@ def _resolve( ctx, ids, **kwargs - )[offset:limit] + )[offset:offset + limit] From 7374e09b9e4982c1685d361bc27428fae6bcb6fe Mon Sep 17 00:00:00 2001 From: Omar Diaz Date: Thu, 13 Jul 2017 17:10:24 +0200 Subject: [PATCH 3/6] Pagination --- carbon14/django.py | 27 ++++++++++++++++++++++++--- 1 file changed, 24 insertions(+), 3 deletions(-) diff --git a/carbon14/django.py b/carbon14/django.py index 66a8312..a8f5a25 100644 --- a/carbon14/django.py +++ b/carbon14/django.py @@ -44,13 +44,34 @@ class ModelCollection(Collection): _auth_required = True - def _resolve(self, level, instances, ctx, ids=None, **kwargs): + def _resolve( + self, + level, + instances, + ctx, + ids=None, + limit=None, + offset=None, + **kwargs + ): if self._auth_required and not ctx.user.is_authenticated(): instances = instances.none() else: if ids is not None: instances = instances.filter(id__in=ids) - return instances.all() + + instances = instances.all() + + if limit and offset: + instances = instances[offset:offset + limit] + + if offset and not limit: + instances = instances[offset:] + + if limit and not offset: + instances = instances[:limit] + + return instances class GraphQLView(APIView): @@ -105,5 +126,5 @@ def _resolve( ctx, ids, **kwargs - )[offset:offset + limit] + ) From f32ddd0a85af3a97ad25eb56d47a328d34587143 Mon Sep 17 00:00:00 2001 From: Omar Diaz Date: Thu, 13 Jul 2017 17:13:19 +0200 Subject: [PATCH 4/6] Pagination --- carbon14/django.py | 21 --------------------- 1 file changed, 21 deletions(-) diff --git a/carbon14/django.py b/carbon14/django.py index a8f5a25..441e30c 100644 --- a/carbon14/django.py +++ b/carbon14/django.py @@ -107,24 +107,3 @@ def serialize(self, instance, *args, **kwargs): if isinstance(value, Point): value = tuple(value) return value - - -class PaginatedModelCollection(ModelCollection): - def _resolve( - self, - level, - instances, - ctx, - ids=None, - offset=0, - limit=0, - **kwargs - ): - return super()._resolve( - level, - instances, - ctx, - ids, - **kwargs - ) - From d1d3f952929434082a6f5909fdd1688ada0826d0 Mon Sep 17 00:00:00 2001 From: Omar Diaz Date: Thu, 13 Jul 2017 17:59:42 +0200 Subject: [PATCH 5/6] Pagination --- carbon14/django.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/carbon14/django.py b/carbon14/django.py index 441e30c..9922b60 100644 --- a/carbon14/django.py +++ b/carbon14/django.py @@ -69,7 +69,7 @@ def _resolve( instances = instances[offset:] if limit and not offset: - instances = instances[:limit] + instances = instances[0:limit] return instances From 67469a694ee221ad4763256e26f20124ebcfd082 Mon Sep 17 00:00:00 2001 From: Omar Diaz Date: Fri, 14 Jul 2017 09:40:02 +0200 Subject: [PATCH 6/6] Pagination --- .gitignore | 2 +- carbon14/django.py | 14 ++++---------- 2 files changed, 5 insertions(+), 11 deletions(-) diff --git a/.gitignore b/.gitignore index 1fcae76..9efe7a8 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,4 @@ *.egg-info/ .cache/ __pycache__/ -/.idea/ \ No newline at end of file +/.idea/ diff --git a/carbon14/django.py b/carbon14/django.py index 9922b60..e7783c4 100644 --- a/carbon14/django.py +++ b/carbon14/django.py @@ -51,7 +51,7 @@ def _resolve( ctx, ids=None, limit=None, - offset=None, + offset=0, **kwargs ): if self._auth_required and not ctx.user.is_authenticated(): @@ -60,16 +60,10 @@ def _resolve( if ids is not None: instances = instances.filter(id__in=ids) - instances = instances.all() + instances = instances.all()[offset:] - if limit and offset: - instances = instances[offset:offset + limit] - - if offset and not limit: - instances = instances[offset:] - - if limit and not offset: - instances = instances[0:limit] + if limit: + instances = instances[:limit] return instances