From e368ebf4e9600f20e9387ef4db93093ae1181a62 Mon Sep 17 00:00:00 2001 From: knst-kotov Date: Fri, 17 Dec 2021 19:14:31 +0100 Subject: [PATCH] add top shouts pagination --- migration/bson2json.py | 39 +++++++++++++++++++-------------------- resolvers/zine.py | 16 ++++++++-------- schema.graphql | 8 ++++---- 3 files changed, 31 insertions(+), 32 deletions(-) diff --git a/migration/bson2json.py b/migration/bson2json.py index 1f2f94ed..bbe909f7 100644 --- a/migration/bson2json.py +++ b/migration/bson2json.py @@ -6,24 +6,23 @@ import importlib from migration.utils import DateTimeEncoder def json_tables(): - print('creating json files at migration/data/') - data = { - "content_items": [], - "content_item_categories": [], - "tags": [], - "email_subscriptions": [], - "users": [], - "comments": [] - } - for table in data.keys(): - lc = [] - with open('migration/data/'+table+'.bson', 'rb') as f: - bs = f.read() - base = 0 - while base < len(bs): - base, d = bson.decode_document(bs, base) - lc.append(d) - data[table] = lc - open('migration/data/'+table+'.json', 'w').write(json.dumps(lc,cls=DateTimeEncoder)) - return data + print('creating json files at migration/data/') + data = { + "content_items": [], + "content_item_categories": [], + "tags": [], + "email_subscriptions": [], + "users": [], + "comments": [] + } + for table in data.keys(): + lc = [] + with open('migration/data/'+table+'.bson', 'rb') as f: + bs = f.read() + base = 0 + while base < len(bs): + base, d = bson.decode_document(bs, base) + lc.append(d) + data[table] = lc + open('migration/data/'+table+'.json', 'w').write(json.dumps(lc,cls=DateTimeEncoder)) diff --git a/resolvers/zine.py b/resolvers/zine.py index c7240508..58eefa91 100644 --- a/resolvers/zine.py +++ b/resolvers/zine.py @@ -191,24 +191,24 @@ class ShoutSubscriptions: subs.put_nowait(shout) @query.field("topViewed") -async def top_viewed(_, info, limit): +async def top_viewed(_, info, page, size): async with ShoutsCache.lock: - return ShoutsCache.top_viewed[:limit] + return ShoutsCache.top_viewed[page * size : (page + 1) * size] @query.field("topMonth") -async def top_month(_, info, limit): +async def top_month(_, info, page, size): async with ShoutsCache.lock: - return ShoutsCache.top_month[:limit] + return ShoutsCache.top_month[page * size : (page + 1) * size] @query.field("topOverall") -async def top_overall(_, info, limit): +async def top_overall(_, info, page, size): async with ShoutsCache.lock: - return ShoutsCache.top_overall[:limit] + return ShoutsCache.top_overall[page * size : (page + 1) * size] @query.field("recents") -async def recent_shouts(_, info, limit): +async def recent_shouts(_, info, page, size): async with ShoutsCache.lock: - return ShoutsCache.recent_shouts[:limit] + return ShoutsCache.recent_shouts[page * size : (page + 1) * size] @mutation.field("createShout") @login_required diff --git a/schema.graphql b/schema.graphql index dbfa0fa0..b5da2c63 100644 --- a/schema.graphql +++ b/schema.graphql @@ -160,10 +160,10 @@ type Query { getShoutComments(slug: String!): [Comment]! # mainpage - topViewed(limit: Int): [Shout]! - topMonth(limit: Int): [Shout]! - topOverall(limit: Int): [Shout]! - recents(limit: Int): [Shout]! + topViewed(page: Int!, size: Int!): [Shout]! + topMonth(page: Int!, size: Int!): [Shout]! + topOverall(page: Int!, size: Int!): [Shout]! + recents(page: Int!, size: Int!): [Shout]! # topics topicsBySlugs(slugs: [String]): [Topic]!