diff --git a/openlobby/core/api/schema.py b/openlobby/core/api/schema.py index fc04960a705048d3e614a8d1b68da8c4f13a4364..035594fadc2693624eac5af060ae39d38be6e1b7 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 a88d833a4ec85babe5f44b4ff7badf2ff67fb60b..76049fc15854928d779a95203c74430cdb633655 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 ccd937c5da3fac15fd18eec4b26f0e1a9d189a9b..50b4228282f4443073b66b1a83b37208c3573b3c 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 949b9b7aa620f7cc1a62c0926f00690404cdbedf..e79e87b009ee8ed93a61e4f8685a8ef1533a0153 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)