From 5dc9dfeb0ddc21f6190edb12c20efad91bfb76e7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20Bedna=C5=99=C3=ADk?= <jan.bednarik@gmail.com> Date: Sun, 28 Jan 2018 11:04:18 +0100 Subject: [PATCH] Authors field in schema. --- openlobby/core/api/schema.py | 24 ++++++++++++++ openlobby/core/models.py | 1 + .../core/tests/snapshots/snap_test_schema.py | 2 ++ openlobby/core/tests/test_schema.py | 31 +++++++++++++++++++ 4 files changed, 58 insertions(+) diff --git a/openlobby/core/api/schema.py b/openlobby/core/api/schema.py index fc04960..035594f 100644 --- a/openlobby/core/api/schema.py +++ b/openlobby/core/api/schema.py @@ -9,6 +9,13 @@ from .. import search from ..models import User +class AuthorsConnection(relay.Connection): + total_count = graphene.Int() + + class Meta: + node = types.Author + + class SearchReportsConnection(relay.Connection): total_count = graphene.Int() @@ -26,6 +33,7 @@ class Query: ' Default: false') node = relay.Node.Field() + authors = relay.ConnectionField(AuthorsConnection) search_reports = relay.ConnectionField( SearchReportsConnection, description='Fulltext search in Reports.', @@ -38,6 +46,22 @@ class Query: description='Shortcuts for login. Use with LoginByShortcut mutation.', ) + def resolve_authors(self, info, **kwargs): + paginator = Paginator(**kwargs) + + total = User.objects.filter(is_author=True).count() + authors = User.objects.filter(is_author=True)[paginator.slice_from:paginator.slice_to] + + page_info = paginator.get_page_info(total) + + edges = [] + for i, author in enumerate(authors): + cursor = paginator.get_edge_cursor(i + 1) + node = types.Author.from_db(author) + edges.append(AuthorsConnection.Edge(node=node, cursor=cursor)) + + return AuthorsConnection(page_info=page_info, edges=edges, total_count=total) + def resolve_search_reports(self, info, **kwargs): paginator = Paginator(**kwargs) query = kwargs.get('query', '') diff --git a/openlobby/core/models.py b/openlobby/core/models.py index a88d833..76049fc 100644 --- a/openlobby/core/models.py +++ b/openlobby/core/models.py @@ -5,6 +5,7 @@ from django.contrib.postgres.fields import JSONField class User(AbstractUser): + # TODO remove username, set different login field openid_uid = models.CharField(max_length=255, unique=True, db_index=True) extra = JSONField(null=True, blank=True) is_author = models.BooleanField(default=False) diff --git a/openlobby/core/tests/snapshots/snap_test_schema.py b/openlobby/core/tests/snapshots/snap_test_schema.py index ccd937c..50b4228 100644 --- a/openlobby/core/tests/snapshots/snap_test_schema.py +++ b/openlobby/core/tests/snapshots/snap_test_schema.py @@ -16,3 +16,5 @@ snapshots['test_node__login_shortcut 1'] = b'{"data":{"node":{"id":"TG9naW5TaG9y snapshots['test_node__author 1'] = b'{"data":{"node":{"id":"QXV0aG9yOjU=","firstName":"Winston","lastName":"Wolfe","openidUid":"TheWolf","extra":"{\\"x\\": 1}"}}}' snapshots['test_node__author__only_if_is_author 1'] = b'{"data":{"node":null}}' + +snapshots['test_authors 1'] = b'{"data":{"authors":{"totalCount":1,"edges":[{"node":{"id":"QXV0aG9yOjU=","firstName":"Winston","lastName":"Wolfe","openidUid":"TheWolf","extra":"{\\"x\\": 1}"}}]}}}' diff --git a/openlobby/core/tests/test_schema.py b/openlobby/core/tests/test_schema.py index 949b9b7..e79e87b 100644 --- a/openlobby/core/tests/test_schema.py +++ b/openlobby/core/tests/test_schema.py @@ -88,3 +88,34 @@ def test_node__author__only_if_is_author(client, snapshot): }} """.format(id=to_global_id('Author', 7))}) snapshot.assert_match(res.content) + + +@pytest.mark.django_db +def test_authors(client, snapshot): + User.objects.create( + id=5, + username='a', + is_author=True, + openid_uid='TheWolf', + first_name='Winston', + last_name='Wolfe', + extra={'x': 1}, + ) + User.objects.create(id=7, is_author=False, username='b') + res = client.post('/graphql', {'query': """ + query { + authors { + totalCount + edges { + node { + id + firstName + lastName + openidUid + extra + } + } + } + } + """}) + snapshot.assert_match(res.content) -- GitLab