From 8f87319ed64b2ac0cfd87b219a3d01ddd806c8da Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20Bedna=C5=99=C3=ADk?= <jan.bednarik@gmail.com> Date: Mon, 19 Feb 2018 19:32:00 +0100 Subject: [PATCH] Add total reports to Author type. --- Dockerfile | 2 +- openlobby/core/api/schema.py | 10 +++-- openlobby/core/api/types.py | 7 ++- tests/schema/snapshots/snap_test_authors.py | 45 ++++++++++++------- tests/schema/snapshots/snap_test_node.py | 6 ++- .../snapshots/snap_test_search_reports.py | 18 +++++--- tests/schema/test_authors.py | 11 +++++ tests/schema/test_node.py | 2 + tests/schema/test_search_reports.py | 3 ++ 9 files changed, 75 insertions(+), 29 deletions(-) diff --git a/Dockerfile b/Dockerfile index afd26b7..430af53 100644 --- a/Dockerfile +++ b/Dockerfile @@ -9,4 +9,4 @@ ADD . /code/ EXPOSE 8010 -CMD ["sh", "-c", "make migrate && gunicorn -w 4 -b 0.0.0.0:8010 --access-logfile - --error-logfile - --capture-output openlobby.wsgi"] +CMD ["sh", "-c", "make migrate & gunicorn -w 4 -b 0.0.0.0:8010 --access-logfile - --error-logfile - --capture-output openlobby.wsgi"] diff --git a/openlobby/core/api/schema.py b/openlobby/core/api/schema.py index d206bdf..2f4d6c9 100644 --- a/openlobby/core/api/schema.py +++ b/openlobby/core/api/schema.py @@ -1,5 +1,6 @@ import graphene from graphene import relay +from django.db.models import Count from . import types from ..models import OpenIdClient @@ -24,8 +25,8 @@ class SearchReportsConnection(relay.Connection): def _get_authors_cache(ids): - users = User.objects.filter(id__in=ids) - return {u.id: types.User.from_db(u) for u in users} + authors = User.objects.filter(id__in=ids).annotate(total_reports=Count('report')) + return {a.id: types.Author.from_db(a) for a in authors} class Query: @@ -53,7 +54,10 @@ class Query: paginator = Paginator(**kwargs) total = User.objects.filter(is_author=True).count() - authors = User.objects.filter(is_author=True)[paginator.slice_from:paginator.slice_to] + authors = User.objects.filter(is_author=True)\ + .annotate(total_reports=Count('report'))\ + .order_by('last_name', 'first_name')[ + paginator.slice_from:paginator.slice_to] page_info = paginator.get_page_info(total) diff --git a/openlobby/core/api/types.py b/openlobby/core/api/types.py index 78640df..2e6432d 100644 --- a/openlobby/core/api/types.py +++ b/openlobby/core/api/types.py @@ -1,3 +1,4 @@ +from django.db.models import Count from elasticsearch import NotFoundError import graphene from graphene import relay @@ -117,6 +118,7 @@ class User(graphene.ObjectType): class Author(graphene.ObjectType): first_name = graphene.String() last_name = graphene.String() + total_reports = graphene.Int() extra = JSONString() reports = relay.ConnectionField(ReportConnection) @@ -130,12 +132,15 @@ class Author(graphene.ObjectType): first_name=user.first_name, last_name=user.last_name, extra=user.extra, + total_reports=user.total_reports, ) @classmethod def get_node(cls, info, id): try: - return cls.from_db(models.User.objects.get(id=id, is_author=True)) + author = models.User.objects.annotate(total_reports=Count('report'))\ + .get(id=id, is_author=True) + return cls.from_db(author) except models.User.DoesNotExist: return None diff --git a/tests/schema/snapshots/snap_test_authors.py b/tests/schema/snapshots/snap_test_authors.py index 88acda2..9f8b74d 100644 --- a/tests/schema/snapshots/snap_test_authors.py +++ b/tests/schema/snapshots/snap_test_authors.py @@ -14,10 +14,11 @@ snapshots['test_all 1'] = { { 'cursor': 'MQ==', 'node': { - 'extra': '{"movies": 1}', - 'firstName': 'Winston', - 'id': 'QXV0aG9yOjE=', - 'lastName': 'Wolfe' + 'extra': None, + 'firstName': 'Shaun', + 'id': 'QXV0aG9yOjM=', + 'lastName': 'Sheep', + 'totalReports': 0 } }, { @@ -26,16 +27,18 @@ snapshots['test_all 1'] = { 'extra': None, 'firstName': 'Spongebob', 'id': 'QXV0aG9yOjI=', - 'lastName': 'Squarepants' + 'lastName': 'Squarepants', + 'totalReports': 0 } }, { 'cursor': 'Mw==', 'node': { - 'extra': None, - 'firstName': 'Shaun', - 'id': 'QXV0aG9yOjM=', - 'lastName': 'Sheep' + 'extra': '{"movies": 1}', + 'firstName': 'Winston', + 'id': 'QXV0aG9yOjE=', + 'lastName': 'Wolfe', + 'totalReports': 0 } } ], @@ -57,17 +60,21 @@ snapshots['test_first 1'] = { { 'cursor': 'MQ==', 'node': { - 'firstName': 'Winston', - 'id': 'QXV0aG9yOjE=', - 'lastName': 'Wolfe' + 'extra': None, + 'firstName': 'Shaun', + 'id': 'QXV0aG9yOjM=', + 'lastName': 'Sheep', + 'totalReports': 0 } }, { 'cursor': 'Mg==', 'node': { + 'extra': None, 'firstName': 'Spongebob', 'id': 'QXV0aG9yOjI=', - 'lastName': 'Squarepants' + 'lastName': 'Squarepants', + 'totalReports': 0 } } ], @@ -89,9 +96,11 @@ snapshots['test_first_after 1'] = { { 'cursor': 'Mg==', 'node': { + 'extra': None, 'firstName': 'Spongebob', 'id': 'QXV0aG9yOjI=', - 'lastName': 'Squarepants' + 'lastName': 'Squarepants', + 'totalReports': 0 } } ], @@ -130,9 +139,11 @@ snapshots['test_last_before 1'] = { { 'cursor': 'Mg==', 'node': { + 'extra': None, 'firstName': 'Spongebob', 'id': 'QXV0aG9yOjI=', - 'lastName': 'Squarepants' + 'lastName': 'Squarepants', + 'totalReports': 0 } } ], @@ -153,6 +164,7 @@ snapshots['test_with_reports 1'] = { 'edges': [ { 'node': { + 'extra': '{"movies": 1}', 'firstName': 'Winston', 'id': 'QXV0aG9yOjE=', 'lastName': 'Wolfe', @@ -175,7 +187,8 @@ snapshots['test_with_reports 1'] = { } ], 'totalCount': 1 - } + }, + 'totalReports': 1 } } ] diff --git a/tests/schema/snapshots/snap_test_node.py b/tests/schema/snapshots/snap_test_node.py index d2b7936..dd7b1cf 100644 --- a/tests/schema/snapshots/snap_test_node.py +++ b/tests/schema/snapshots/snap_test_node.py @@ -22,7 +22,8 @@ snapshots['test_author 1'] = { 'extra': '{"x": 1}', 'firstName': 'Winston', 'id': 'QXV0aG9yOjU=', - 'lastName': 'Wolfe' + 'lastName': 'Wolfe', + 'totalReports': 0 } } } @@ -40,7 +41,8 @@ snapshots['test_report 1'] = { 'extra': '{"movies": 1}', 'firstName': 'Winston', 'id': 'QXV0aG9yOjE=', - 'lastName': 'Wolfe' + 'lastName': 'Wolfe', + 'totalReports': 1 }, 'body': 'Long story short: we got the Ring!', 'date': '2018-01-01 00:00:00+00:00', diff --git a/tests/schema/snapshots/snap_test_search_reports.py b/tests/schema/snapshots/snap_test_search_reports.py index 70f33ad..3dd6bcc 100644 --- a/tests/schema/snapshots/snap_test_search_reports.py +++ b/tests/schema/snapshots/snap_test_search_reports.py @@ -18,7 +18,8 @@ snapshots['test_all 1'] = { 'extra': None, 'firstName': 'Spongebob', 'id': 'QXV0aG9yOjI=', - 'lastName': 'Squarepants' + 'lastName': 'Squarepants', + 'totalReports': 1 }, 'body': 'Another long story.', 'date': '2018-01-05 00:00:00+00:00', @@ -39,7 +40,8 @@ snapshots['test_all 1'] = { 'extra': '{"movies": 1}', 'firstName': 'Winston', 'id': 'QXV0aG9yOjE=', - 'lastName': 'Wolfe' + 'lastName': 'Wolfe', + 'totalReports': 2 }, 'body': 'Aragorn is the King. And we have lost the Ring.', 'date': '2018-01-07 00:00:00+00:00', @@ -60,7 +62,8 @@ snapshots['test_all 1'] = { 'extra': '{"movies": 1}', 'firstName': 'Winston', 'id': 'QXV0aG9yOjE=', - 'lastName': 'Wolfe' + 'lastName': 'Wolfe', + 'totalReports': 2 }, 'body': 'Long story short: we got the Ring!', 'date': '2018-01-01 00:00:00+00:00', @@ -97,7 +100,8 @@ snapshots['test_query 1'] = { 'extra': None, 'firstName': 'Spongebob', 'id': 'QXV0aG9yOjI=', - 'lastName': 'Squarepants' + 'lastName': 'Squarepants', + 'totalReports': 1 }, 'body': 'Another long story.', 'date': '2018-01-05 00:00:00+00:00', @@ -128,7 +132,8 @@ snapshots['test_highlight 1'] = { 'extra': '{"movies": 1}', 'firstName': 'Winston', 'id': 'QXV0aG9yOjE=', - 'lastName': 'Wolfe' + 'lastName': 'Wolfe', + 'totalReports': 2 }, 'body': 'Aragorn is the King. And we have lost the <mark>Ring</mark>.', 'date': '2018-01-07 00:00:00+00:00', @@ -149,7 +154,8 @@ snapshots['test_highlight 1'] = { 'extra': '{"movies": 1}', 'firstName': 'Winston', 'id': 'QXV0aG9yOjE=', - 'lastName': 'Wolfe' + 'lastName': 'Wolfe', + 'totalReports': 2 }, 'body': 'Long story short: we got the <mark>Ring</mark>!', 'date': '2018-01-01 00:00:00+00:00', diff --git a/tests/schema/test_authors.py b/tests/schema/test_authors.py index e81d03e..b582d86 100644 --- a/tests/schema/test_authors.py +++ b/tests/schema/test_authors.py @@ -21,6 +21,7 @@ def test_all(client, snapshot): id firstName lastName + totalReports extra } } @@ -48,6 +49,8 @@ def test_first(client, snapshot): id firstName lastName + totalReports + extra } } pageInfo { @@ -74,6 +77,8 @@ def test_first_after(client, snapshot): id firstName lastName + totalReports + extra } } pageInfo { @@ -100,6 +105,8 @@ def test_last(client, snapshot): id firstName lastName + totalReports + extra } } pageInfo { @@ -126,6 +133,8 @@ def test_last_before(client, snapshot): id firstName lastName + totalReports + extra } } pageInfo { @@ -150,6 +159,8 @@ def test_with_reports(client, snapshot): id firstName lastName + totalReports + extra reports { totalCount edges { diff --git a/tests/schema/test_node.py b/tests/schema/test_node.py index 9b2adb7..02b12aa 100644 --- a/tests/schema/test_node.py +++ b/tests/schema/test_node.py @@ -41,6 +41,7 @@ def test_author(client, snapshot): id firstName lastName + totalReports extra }} }} @@ -83,6 +84,7 @@ def test_report(client, snapshot): id firstName lastName + totalReports extra }} }} diff --git a/tests/schema/test_search_reports.py b/tests/schema/test_search_reports.py index a980aa3..1309dda 100644 --- a/tests/schema/test_search_reports.py +++ b/tests/schema/test_search_reports.py @@ -29,6 +29,7 @@ def test_all(client, snapshot): id firstName lastName + totalReports extra } } @@ -68,6 +69,7 @@ def test_query(client, snapshot): id firstName lastName + totalReports extra } } @@ -101,6 +103,7 @@ def test_highlight(client, snapshot): id firstName lastName + totalReports extra } } -- GitLab