Skip to content
Snippets Groups Projects
Commit 11849324 authored by jan.bednarik's avatar jan.bednarik
Browse files

Exclude draft Reports from search and totals.

parent 60ee176e
Branches
No related tags found
No related merge requests found
import graphene import graphene
from graphene import relay from graphene import relay
from django.db.models import Count from django.db.models import Count, Q
from . import types from . import types
from ..models import OpenIdClient from ..models import OpenIdClient
...@@ -25,7 +25,8 @@ class SearchReportsConnection(relay.Connection): ...@@ -25,7 +25,8 @@ class SearchReportsConnection(relay.Connection):
def _get_authors_cache(ids): def _get_authors_cache(ids):
authors = User.objects.filter(id__in=ids).annotate(total_reports=Count('report')) authors = User.objects.filter(id__in=ids)\
.annotate(total_reports=Count('report', filter=Q(report__is_draft=False)))
return {a.id: types.Author.from_db(a) for a in authors} return {a.id: types.Author.from_db(a) for a in authors}
...@@ -55,7 +56,7 @@ class Query: ...@@ -55,7 +56,7 @@ class Query:
total = User.objects.filter(is_author=True).count() total = User.objects.filter(is_author=True).count()
authors = User.objects.filter(is_author=True)\ authors = User.objects.filter(is_author=True)\
.annotate(total_reports=Count('report'))\ .annotate(total_reports=Count('report', filter=Q(report__is_draft=False)))\
.order_by('last_name', 'first_name')[ .order_by('last_name', 'first_name')[
paginator.slice_from:paginator.slice_to] paginator.slice_from:paginator.slice_to]
......
from django.db.models import Count from django.db.models import Count, Q
from elasticsearch import NotFoundError from elasticsearch import NotFoundError
import graphene import graphene
from graphene import relay from graphene import relay
...@@ -142,7 +142,8 @@ class Author(graphene.ObjectType): ...@@ -142,7 +142,8 @@ class Author(graphene.ObjectType):
@classmethod @classmethod
def get_node(cls, info, id): def get_node(cls, info, id):
try: try:
author = models.User.objects.annotate(total_reports=Count('report'))\ author = models.User.objects\
.annotate(total_reports=Count('report', filter=Q(report__is_draft=False)))\
.get(id=id, is_author=True) .get(id=id, is_author=True)
return cls.from_db(author) return cls.from_db(author)
except models.User.DoesNotExist: except models.User.DoesNotExist:
......
...@@ -12,6 +12,7 @@ def query_reports(query, paginator, *, highlight=False): ...@@ -12,6 +12,7 @@ def query_reports(query, paginator, *, highlight=False):
fields = ['title', 'body', 'received_benefit', 'provided_benefit', fields = ['title', 'body', 'received_benefit', 'provided_benefit',
'our_participants', 'other_participants'] 'our_participants', 'other_participants']
s = ReportDoc.search() s = ReportDoc.search()
s = s.exclude('term', is_draft=True)
if query != '': if query != '':
s = s.query('multi_match', query=query, fields=fields) s = s.query('multi_match', query=query, fields=fields)
if highlight: if highlight:
...@@ -23,6 +24,7 @@ def query_reports(query, paginator, *, highlight=False): ...@@ -23,6 +24,7 @@ def query_reports(query, paginator, *, highlight=False):
def reports_by_author(author_id, paginator): def reports_by_author(author_id, paginator):
s = ReportDoc.search() s = ReportDoc.search()
s = s.exclude('term', is_draft=True)
s = s.filter('term', author_id=author_id) s = s.filter('term', author_id=author_id)
s = s.sort('-published') s = s.sort('-published')
s = s[paginator.slice_from:paginator.slice_to] s = s[paginator.slice_from:paginator.slice_to]
......
...@@ -63,6 +63,18 @@ reports = [ ...@@ -63,6 +63,18 @@ reports = [
'our_participants': 'Aragorn', 'our_participants': 'Aragorn',
'other_participants': 'Sauron', 'other_participants': 'Sauron',
}, },
{
'id': 4,
'date': arrow.get(2018, 1, 9).datetime,
'published': arrow.get(2018, 1, 11).datetime,
'title': 'The Silmarillion',
'body': 'Not finished yet.',
'received_benefit': '',
'provided_benefit': '',
'our_participants': '',
'other_participants': '',
'is_draft': True,
},
] ]
...@@ -72,13 +84,5 @@ def prepare_reports(): ...@@ -72,13 +84,5 @@ def prepare_reports():
Report.objects.create(author=author1, **reports[0]) Report.objects.create(author=author1, **reports[0])
Report.objects.create(author=author2, **reports[1]) Report.objects.create(author=author2, **reports[1])
Report.objects.create(author=author1, **reports[2]) Report.objects.create(author=author1, **reports[2])
Report.objects.create(author=author1, **reports[3])
User.objects.create(**authors[2])
def prepare_report():
author = User.objects.create(**authors[0])
Report.objects.create(author=author, **reports[0])
def prepare_authors():
for author in authors:
User.objects.create(**author)
...@@ -30,7 +30,7 @@ snapshots['test_all 1'] = { ...@@ -30,7 +30,7 @@ snapshots['test_all 1'] = {
'hasCollidingName': False, 'hasCollidingName': False,
'id': 'QXV0aG9yOjI=', 'id': 'QXV0aG9yOjI=',
'lastName': 'Squarepants', 'lastName': 'Squarepants',
'totalReports': 0 'totalReports': 1
} }
}, },
{ {
...@@ -41,7 +41,7 @@ snapshots['test_all 1'] = { ...@@ -41,7 +41,7 @@ snapshots['test_all 1'] = {
'hasCollidingName': False, 'hasCollidingName': False,
'id': 'QXV0aG9yOjE=', 'id': 'QXV0aG9yOjE=',
'lastName': 'Wolfe', 'lastName': 'Wolfe',
'totalReports': 0 'totalReports': 2
} }
} }
], ],
...@@ -79,7 +79,7 @@ snapshots['test_first 1'] = { ...@@ -79,7 +79,7 @@ snapshots['test_first 1'] = {
'hasCollidingName': False, 'hasCollidingName': False,
'id': 'QXV0aG9yOjI=', 'id': 'QXV0aG9yOjI=',
'lastName': 'Squarepants', 'lastName': 'Squarepants',
'totalReports': 0 'totalReports': 1
} }
} }
], ],
...@@ -106,7 +106,7 @@ snapshots['test_first_after 1'] = { ...@@ -106,7 +106,7 @@ snapshots['test_first_after 1'] = {
'hasCollidingName': False, 'hasCollidingName': False,
'id': 'QXV0aG9yOjI=', 'id': 'QXV0aG9yOjI=',
'lastName': 'Squarepants', 'lastName': 'Squarepants',
'totalReports': 0 'totalReports': 1
} }
} }
], ],
...@@ -150,7 +150,7 @@ snapshots['test_last_before 1'] = { ...@@ -150,7 +150,7 @@ snapshots['test_last_before 1'] = {
'hasCollidingName': False, 'hasCollidingName': False,
'id': 'QXV0aG9yOjI=', 'id': 'QXV0aG9yOjI=',
'lastName': 'Squarepants', 'lastName': 'Squarepants',
'totalReports': 0 'totalReports': 1
} }
} }
], ],
...@@ -169,6 +169,51 @@ snapshots['test_with_reports 1'] = { ...@@ -169,6 +169,51 @@ snapshots['test_with_reports 1'] = {
'data': { 'data': {
'authors': { 'authors': {
'edges': [ 'edges': [
{
'node': {
'extra': None,
'firstName': 'Shaun',
'hasCollidingName': False,
'id': 'QXV0aG9yOjM=',
'lastName': 'Sheep',
'reports': {
'edges': [
],
'totalCount': 0
},
'totalReports': 0
}
},
{
'node': {
'extra': None,
'firstName': 'Spongebob',
'hasCollidingName': False,
'id': 'QXV0aG9yOjI=',
'lastName': 'Squarepants',
'reports': {
'edges': [
{
'cursor': 'MQ==',
'node': {
'body': 'Another long story.',
'date': '2018-01-05 00:00:00+00:00',
'extra': '{"rings": 1}',
'id': 'UmVwb3J0OjI=',
'otherParticipants': 'Saruman, Sauron',
'ourParticipants': 'Frodo, Gimli, Legolas',
'providedBenefit': '',
'published': '2018-01-10 00:00:00+00:00',
'receivedBenefit': 'Mithrill Jacket',
'title': 'The Two Towers'
}
}
],
'totalCount': 1
},
'totalReports': 1
}
},
{ {
'node': { 'node': {
'extra': '{"movies": 1}', 'extra': '{"movies": 1}',
...@@ -180,6 +225,21 @@ snapshots['test_with_reports 1'] = { ...@@ -180,6 +225,21 @@ snapshots['test_with_reports 1'] = {
'edges': [ 'edges': [
{ {
'cursor': 'MQ==', 'cursor': 'MQ==',
'node': {
'body': 'Aragorn is the King. And we have lost the Ring.',
'date': '2018-01-07 00:00:00+00:00',
'extra': None,
'id': 'UmVwb3J0OjM=',
'otherParticipants': 'Sauron',
'ourParticipants': 'Aragorn',
'providedBenefit': 'The Ring',
'published': '2018-01-08 00:00:00+00:00',
'receivedBenefit': '',
'title': 'The Return of the King'
}
},
{
'cursor': 'Mg==',
'node': { 'node': {
'body': 'Long story short: we got the Ring!', 'body': 'Long story short: we got the Ring!',
'date': '2018-01-01 00:00:00+00:00', 'date': '2018-01-01 00:00:00+00:00',
...@@ -194,9 +254,9 @@ snapshots['test_with_reports 1'] = { ...@@ -194,9 +254,9 @@ snapshots['test_with_reports 1'] = {
} }
} }
], ],
'totalCount': 1 'totalCount': 2
}, },
'totalReports': 1 'totalReports': 2
} }
} }
] ]
......
...@@ -19,12 +19,12 @@ snapshots['test_login_shortcut 1'] = { ...@@ -19,12 +19,12 @@ snapshots['test_login_shortcut 1'] = {
snapshots['test_author 1'] = { snapshots['test_author 1'] = {
'data': { 'data': {
'node': { 'node': {
'extra': '{"x": 1}', 'extra': '{"movies": 1}',
'firstName': 'Winston', 'firstName': 'Winston',
'hasCollidingName': False, 'hasCollidingName': False,
'id': 'QXV0aG9yOjU=', 'id': 'QXV0aG9yOjE=',
'lastName': 'Wolfe', 'lastName': 'Wolfe',
'totalReports': 0 'totalReports': 2
} }
} }
} }
...@@ -44,7 +44,7 @@ snapshots['test_report 1'] = { ...@@ -44,7 +44,7 @@ snapshots['test_report 1'] = {
'hasCollidingName': False, 'hasCollidingName': False,
'id': 'QXV0aG9yOjE=', 'id': 'QXV0aG9yOjE=',
'lastName': 'Wolfe', 'lastName': 'Wolfe',
'totalReports': 1 'totalReports': 2
}, },
'body': 'Long story short: we got the Ring!', 'body': 'Long story short: we got the Ring!',
'date': '2018-01-01 00:00:00+00:00', 'date': '2018-01-01 00:00:00+00:00',
......
...@@ -2,14 +2,14 @@ import pytest ...@@ -2,14 +2,14 @@ import pytest
from openlobby.core.models import User from openlobby.core.models import User
from ..dummy import prepare_authors, prepare_report from ..dummy import prepare_reports
pytestmark = [pytest.mark.django_db, pytest.mark.usefixtures('django_es')] pytestmark = [pytest.mark.django_db, pytest.mark.usefixtures('django_es')]
def test_all(client, snapshot): def test_all(client, snapshot):
prepare_authors() prepare_reports()
User.objects.create(id=4, is_author=False, username='x') User.objects.create(id=4, is_author=False, username='x')
res = client.post('/graphql', {'query': """ res = client.post('/graphql', {'query': """
query { query {
...@@ -39,7 +39,7 @@ def test_all(client, snapshot): ...@@ -39,7 +39,7 @@ def test_all(client, snapshot):
def test_first(client, snapshot): def test_first(client, snapshot):
prepare_authors() prepare_reports()
res = client.post('/graphql', {'query': """ res = client.post('/graphql', {'query': """
query { query {
authors (first: 2) { authors (first: 2) {
...@@ -68,7 +68,7 @@ def test_first(client, snapshot): ...@@ -68,7 +68,7 @@ def test_first(client, snapshot):
def test_first_after(client, snapshot): def test_first_after(client, snapshot):
prepare_authors() prepare_reports()
res = client.post('/graphql', {'query': """ res = client.post('/graphql', {'query': """
query { query {
authors (first: 1, after: "MQ==") { authors (first: 1, after: "MQ==") {
...@@ -97,7 +97,7 @@ def test_first_after(client, snapshot): ...@@ -97,7 +97,7 @@ def test_first_after(client, snapshot):
def test_last(client, snapshot): def test_last(client, snapshot):
prepare_authors() prepare_reports()
res = client.post('/graphql', {'query': """ res = client.post('/graphql', {'query': """
query { query {
authors (last: 2) { authors (last: 2) {
...@@ -126,7 +126,7 @@ def test_last(client, snapshot): ...@@ -126,7 +126,7 @@ def test_last(client, snapshot):
def test_last_before(client, snapshot): def test_last_before(client, snapshot):
prepare_authors() prepare_reports()
res = client.post('/graphql', {'query': """ res = client.post('/graphql', {'query': """
query { query {
authors (last: 1, before: "Mw==") { authors (last: 1, before: "Mw==") {
...@@ -155,7 +155,7 @@ def test_last_before(client, snapshot): ...@@ -155,7 +155,7 @@ def test_last_before(client, snapshot):
def test_with_reports(client, snapshot): def test_with_reports(client, snapshot):
prepare_report() prepare_reports()
res = client.post('/graphql', {'query': """ res = client.post('/graphql', {'query': """
query { query {
authors { authors {
......
...@@ -4,7 +4,7 @@ from graphql_relay import to_global_id ...@@ -4,7 +4,7 @@ from graphql_relay import to_global_id
from openlobby.core.auth import create_access_token from openlobby.core.auth import create_access_token
from openlobby.core.models import OpenIdClient, User from openlobby.core.models import OpenIdClient, User
from ..dummy import prepare_report from ..dummy import prepare_reports
pytestmark = [pytest.mark.django_db, pytest.mark.usefixtures('django_es')] pytestmark = [pytest.mark.django_db, pytest.mark.usefixtures('django_es')]
...@@ -26,14 +26,7 @@ def test_login_shortcut(client, snapshot): ...@@ -26,14 +26,7 @@ def test_login_shortcut(client, snapshot):
def test_author(client, snapshot): def test_author(client, snapshot):
User.objects.create( prepare_reports()
id=5,
is_author=True,
openid_uid='TheWolf',
first_name='Winston',
last_name='Wolfe',
extra={'x': 1},
)
res = client.post('/graphql', {'query': """ res = client.post('/graphql', {'query': """
query {{ query {{
node (id:"{id}") {{ node (id:"{id}") {{
...@@ -47,7 +40,7 @@ def test_author(client, snapshot): ...@@ -47,7 +40,7 @@ def test_author(client, snapshot):
}} }}
}} }}
}} }}
""".format(id=to_global_id('Author', 5))}) """.format(id=to_global_id('Author', 1))})
snapshot.assert_match(res.json()) snapshot.assert_match(res.json())
...@@ -66,7 +59,7 @@ def test_author__returns_only_if_is_author(client, snapshot): ...@@ -66,7 +59,7 @@ def test_author__returns_only_if_is_author(client, snapshot):
def test_report(client, snapshot): def test_report(client, snapshot):
prepare_report() prepare_reports()
res = client.post('/graphql', {'query': """ res = client.post('/graphql', {'query': """
query {{ query {{
node (id:"{id}") {{ node (id:"{id}") {{
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment