offset-limit

This commit is contained in:
tonyrewin 2022-09-14 14:09:28 +03:00
parent 66cd20514e
commit 1fc46bb450
3 changed files with 5 additions and 4 deletions

View File

@ -91,6 +91,6 @@ async def topics_random(_, info, amount=12):
# #if topic_stat["shouts"] > 2: # #if topic_stat["shouts"] > 2:
# normalized_topics.append(topic) # normalized_topics.append(topic)
topic.stat = topic_stat topic.stat = topic_stat
normalized_topics = topics normalized_topics.append(topic)
sample_length = min(len(normalized_topics), amount) sample_length = min(len(normalized_topics), amount)
return random.sample(normalized_topics, sample_length) return random.sample(normalized_topics, sample_length)

View File

@ -4,7 +4,7 @@ from orm.topic import Topic
from base.orm import local_session from base.orm import local_session
from base.resolvers import mutation, query from base.resolvers import mutation, query
from services.zine.shoutauthor import ShoutAuthorStorage from services.zine.shoutauthor import ShoutAuthorStorage
from services.zine.shoutscache import ShoutsCache, prepare_shouts from services.zine.shoutscache import ShoutsCache
from services.stat.viewed import ViewedStorage from services.stat.viewed import ViewedStorage
from resolvers.profile import author_follow, author_unfollow from resolvers.profile import author_follow, author_unfollow
from resolvers.topics import topic_follow, topic_unfollow from resolvers.topics import topic_follow, topic_unfollow
@ -112,7 +112,7 @@ async def get_search_results(_, _info, query, offset, limit):
@query.field("shoutsByTopics") @query.field("shoutsByTopics")
async def shouts_by_topics(_, _info, slugs, offset, limit): async def shouts_by_topics(_, _info, slugs, offset, limit):
with local_session() as session: with local_session() as session:
shouts = prepare_shouts( shouts = (
session.query(Shout) session.query(Shout)
.join(ShoutTopic) .join(ShoutTopic)
.where(and_(ShoutTopic.topic.in_(slugs), bool(Shout.publishedAt))) .where(and_(ShoutTopic.topic.in_(slugs), bool(Shout.publishedAt)))
@ -130,7 +130,7 @@ async def shouts_by_topics(_, _info, slugs, offset, limit):
@query.field("shoutsByCollection") @query.field("shoutsByCollection")
async def shouts_by_collection(_, _info, collection, offset, limit): async def shouts_by_collection(_, _info, collection, offset, limit):
with local_session() as session: with local_session() as session:
shouts = prepare_shouts( shouts = (
session.query(Shout) session.query(Shout)
.join(ShoutCollection, ShoutCollection.collection == collection) .join(ShoutCollection, ShoutCollection.collection == collection)
.where(and_(ShoutCollection.shout == Shout.slug, bool(Shout.publishedAt))) .where(and_(ShoutCollection.shout == Shout.slug, bool(Shout.publishedAt)))

View File

@ -39,6 +39,7 @@ class ShoutsCache:
.options(selectinload(Shout.authors), selectinload(Shout.topics)) .options(selectinload(Shout.authors), selectinload(Shout.topics))
.where(bool(Shout.publishedAt)) .where(bool(Shout.publishedAt))
.order_by(desc("publishedAt")) .order_by(desc("publishedAt"))
.order_by(desc("createdAt"))
.limit(ShoutsCache.limit) .limit(ShoutsCache.limit)
)) ))
async with ShoutsCache.lock: async with ShoutsCache.lock: