diff --git a/Dockerfile b/Dockerfile index afd26b74272499014196602428245d81178185c3..430af538d4ba7f659c670406dda72ea92c1fcc1d 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 d206bdf7b124773d6cfd9e54ed0fc97cfd09f151..2f4d6c9b34562d36b21f3f440931ec024b440613 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 78640dfe3934d6e250ffc0b9bb7f7e903b4e7aaa..2e6432daa1f810c3d5ca0e378c433b2ba49eb574 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 88acda2ab25a1e728a38899b4661f6fc0c4237e7..9f8b74d06acc3d7d253802a719deb9bad622a542 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 d2b7936a3346d1c9f3281455a745a5833b6ae853..dd7b1cff55545c0ea4002c5c57f82b004bfca38d 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 70f33adda263e829d0e417f46a2e563d2f827ee3..3dd6bcc6e5360945ed20f1bf29d4f9b87a307a5e 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 e81d03e9e50f0705766b85951359fe7a707ff352..b582d865b2dd7ef67555b3593d893af1ea050939 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 9b2adb7f818672f6fff5a3361e147ed1afa684b9..02b12aada13852404e1de7743b9e204f4f357e2c 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 a980aa37a4a31eb166716a9d9b1a486d61704be5..1309dda7ac5217c8090f1d3793a57bc010ae085b 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 } }