From 677c2168e445d8029ba63796937047fd31ac630d Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Jan=20Bedna=C5=99=C3=ADk?= <jan.bednarik@gmail.com>
Date: Sat, 27 Jan 2018 14:27:25 +0100
Subject: [PATCH] Simplify User type and split name to first and last name for
 User and Author types.

---
 openlobby/core/api/types.py                   | 30 ++++++-------------
 .../core/tests/snapshots/snap_test_schema.py  |  2 +-
 openlobby/core/tests/test_schema.py           |  8 ++---
 3 files changed, 13 insertions(+), 27 deletions(-)

diff --git a/openlobby/core/api/types.py b/openlobby/core/api/types.py
index d9d64e9..3b8b317 100644
--- a/openlobby/core/api/types.py
+++ b/openlobby/core/api/types.py
@@ -67,11 +67,10 @@ class ReportConnection(relay.Connection):
 
 
 class User(graphene.ObjectType):
-    name = graphene.String()
     openid_uid = graphene.String()
+    first_name = graphene.String()
+    last_name = graphene.String()
     email = graphene.String()
-    extra = JSONString()
-    reports = relay.ConnectionField(ReportConnection)
 
     class Meta:
         interfaces = (relay.Node, )
@@ -80,36 +79,24 @@ class User(graphene.ObjectType):
     def from_db(cls, user):
         return cls(
             id=user.id,
-            name=user.get_full_name(),
             openid_uid=user.openid_uid,
+            first_name=user.first_name,
+            last_name=user.last_name,
             email=user.email,
-            extra=user.extra,
         )
 
     @classmethod
     def get_node(cls, info, id):
+        # TODO return only viewer
         try:
             return cls.from_db(models.User.objects.get(id=id))
         except models.User.DoesNotExist:
             return None
 
-    def resolve_reports(self, info, **kwargs):
-        paginator = Paginator(**kwargs)
-        response = search.reports_by_author(self.id, paginator, **info.context)
-        total = response.hits.total
-        page_info = paginator.get_page_info(total)
-
-        edges = []
-        for i, report in enumerate(response):
-            cursor = paginator.get_edge_cursor(i + 1)
-            node = Report.from_es(report, author=self)
-            edges.append(ReportConnection.Edge(node=node, cursor=cursor))
-
-        return ReportConnection(page_info=page_info, edges=edges, total_count=total)
-
 
 class Author(graphene.ObjectType):
-    name = graphene.String()
+    first_name = graphene.String()
+    last_name = graphene.String()
     openid_uid = graphene.String()
     extra = JSONString()
     # TODO
@@ -122,7 +109,8 @@ class Author(graphene.ObjectType):
     def from_db(cls, user):
         return cls(
             id=user.id,
-            name=user.get_full_name(),
+            first_name=user.first_name,
+            last_name=user.last_name,
             openid_uid=user.openid_uid,
             extra=user.extra,
         )
diff --git a/openlobby/core/tests/snapshots/snap_test_schema.py b/openlobby/core/tests/snapshots/snap_test_schema.py
index d5d9f5a..ccd937c 100644
--- a/openlobby/core/tests/snapshots/snap_test_schema.py
+++ b/openlobby/core/tests/snapshots/snap_test_schema.py
@@ -13,6 +13,6 @@ snapshots['test_login_shortcuts 1'] = b'{"data":{"loginShortcuts":[{"id":"TG9naW
 
 snapshots['test_node__login_shortcut 1'] = b'{"data":{"node":{"id":"TG9naW5TaG9ydGN1dDoxMA==","name":"foo"}}}'
 
-snapshots['test_node__author 1'] = b'{"data":{"node":{"id":"QXV0aG9yOjU=","name":"Winston Wolfe","openidUid":"TheWolf","extra":"{\\"x\\": 1}"}}}'
+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}}'
diff --git a/openlobby/core/tests/test_schema.py b/openlobby/core/tests/test_schema.py
index 469ac14..949b9b7 100644
--- a/openlobby/core/tests/test_schema.py
+++ b/openlobby/core/tests/test_schema.py
@@ -64,7 +64,8 @@ def test_node__author(client, snapshot):
         node (id:"{id}") {{
             ... on Author {{
                 id
-                name
+                firstName
+                lastName
                 openidUid
                 extra
             }}
@@ -76,10 +77,7 @@ def test_node__author(client, snapshot):
 
 @pytest.mark.django_db
 def test_node__author__only_if_is_author(client, snapshot):
-    User.objects.create(
-        id=7,
-        is_author=False,
-    )
+    User.objects.create(id=7, is_author=False)
     res = client.post('/graphql', {'query': """
     query {{
         node (id:"{id}") {{
-- 
GitLab