This commit is contained in:
parent
fc930a539b
commit
1ccc5fb9e7
|
@ -1,3 +1,4 @@
|
||||||
|
import json
|
||||||
from typing import List
|
from typing import List
|
||||||
|
|
||||||
from sqlalchemy.orm import aliased, joinedload
|
from sqlalchemy.orm import aliased, joinedload
|
||||||
|
@ -82,6 +83,24 @@ def get_shouts_with_links(info, q, limit=20, offset=0, author_id=None):
|
||||||
includes_authors = has_field(info, "authors")
|
includes_authors = has_field(info, "authors")
|
||||||
includes_topics = has_field(info, "topics")
|
includes_topics = has_field(info, "topics")
|
||||||
includes_stat = has_field(info, "stat")
|
includes_stat = has_field(info, "stat")
|
||||||
|
includes_media = has_field(info, "media")
|
||||||
|
|
||||||
|
# created_by и main_topic
|
||||||
|
if has_field(info, "created_by"):
|
||||||
|
q = q.outerjoin(Author, Shout.created_by == Author.id).add_columns(
|
||||||
|
Author.id.label("main_author_id"),
|
||||||
|
Author.name.label("main_author_name"),
|
||||||
|
Author.slug.label("main_author_slug"),
|
||||||
|
Author.pic.label("main_author_pic"),
|
||||||
|
Author.caption.label("main_author_caption"),
|
||||||
|
)
|
||||||
|
if has_field(info, "main_topic"):
|
||||||
|
q = q.outerjoin(Topic, Shout.main_topic == Topic.id).add_columns(
|
||||||
|
Topic.id.label("main_topic_id"),
|
||||||
|
Topic.title.label("main_topic_title"),
|
||||||
|
Topic.slug.label("main_topic_slug"),
|
||||||
|
func.literal(True).label("main_topic_is_main"),
|
||||||
|
)
|
||||||
|
|
||||||
with local_session() as session:
|
with local_session() as session:
|
||||||
shouts_result = session.execute(q).scalars().all()
|
shouts_result = session.execute(q).scalars().all()
|
||||||
|
@ -111,11 +130,7 @@ def get_shouts_with_links(info, q, limit=20, offset=0, author_id=None):
|
||||||
)
|
)
|
||||||
authors_and_topics = session.execute(query).all()
|
authors_and_topics = session.execute(query).all()
|
||||||
|
|
||||||
shouts_data = {
|
shouts_data = {shout.id: {**shout.dict(), "authors": [], "topics": set()} for shout in shouts_result}
|
||||||
shout.id: {**shout.dict(),
|
|
||||||
"authors": [],
|
|
||||||
"topics": set()} for shout in shouts_result
|
|
||||||
}
|
|
||||||
|
|
||||||
for row in authors_and_topics:
|
for row in authors_and_topics:
|
||||||
shout_data = shouts_data[row.shout_id]
|
shout_data = shouts_data[row.shout_id]
|
||||||
|
@ -140,6 +155,8 @@ def get_shouts_with_links(info, q, limit=20, offset=0, author_id=None):
|
||||||
shout_data["topics"].add(tuple(topic.items()))
|
shout_data["topics"].add(tuple(topic.items()))
|
||||||
|
|
||||||
for shout in shouts_data.values():
|
for shout in shouts_data.values():
|
||||||
|
if includes_media:
|
||||||
|
shout["media"] = json.dumps(shout.get("media", []))
|
||||||
if includes_stat:
|
if includes_stat:
|
||||||
shout_id = shout["id"]
|
shout_id = shout["id"]
|
||||||
viewed_stat = ViewedStorage.get_shout(shout_id=shout_id) or 0
|
viewed_stat = ViewedStorage.get_shout(shout_id=shout_id) or 0
|
||||||
|
@ -150,10 +167,7 @@ def get_shouts_with_links(info, q, limit=20, offset=0, author_id=None):
|
||||||
"last_reacted_at": shout.get("last_reacted_at"),
|
"last_reacted_at": shout.get("last_reacted_at"),
|
||||||
}
|
}
|
||||||
|
|
||||||
shout["topics"] = sorted(
|
shout["topics"] = sorted([dict(t) for t in shout["topics"]], key=lambda x: (not x["is_main"], x["id"]))
|
||||||
[dict(t) for t in shout["topics"]],
|
|
||||||
key=lambda x: (not x["is_main"], x["id"])
|
|
||||||
)
|
|
||||||
|
|
||||||
return list(shouts_data.values())
|
return list(shouts_data.values())
|
||||||
|
|
||||||
|
@ -240,7 +254,7 @@ async def get_shout(_, info, slug="", shout_id=0):
|
||||||
Получение публикации по slug или id.
|
Получение публикации по slug или id.
|
||||||
|
|
||||||
:param _: Корневой объект запроса (не используется)
|
:param _: Корневой объект запроса (не используется)
|
||||||
:param _info: Информация о контексте GraphQL
|
:param info: Информация о контексте GraphQL
|
||||||
:param slug: Уникальный идентификатор публикации
|
:param slug: Уникальный идентификатор публикации
|
||||||
:param shout_id: ID публикации
|
:param shout_id: ID публикации
|
||||||
:return: Данные публикации с включенной статистикой
|
:return: Данные публикации с включенной статистикой
|
||||||
|
|
|
@ -57,26 +57,35 @@ type Reaction {
|
||||||
# old_thread: String
|
# old_thread: String
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type Media {
|
||||||
|
url: String
|
||||||
|
pic: String
|
||||||
|
title: String
|
||||||
|
body: String
|
||||||
|
artist: String
|
||||||
|
}
|
||||||
|
|
||||||
type Shout {
|
type Shout {
|
||||||
id: Int!
|
id: Int!
|
||||||
|
title: String!
|
||||||
slug: String!
|
slug: String!
|
||||||
body: String!
|
body: String!
|
||||||
|
layout: String!
|
||||||
|
|
||||||
lead: String
|
lead: String
|
||||||
description: String
|
description: String
|
||||||
main_topic: String
|
|
||||||
topics: [Topic]
|
|
||||||
created_by: Author!
|
|
||||||
updated_by: Author
|
|
||||||
deleted_by: Author
|
|
||||||
authors: [Author]
|
|
||||||
communities: [Community]
|
|
||||||
title: String!
|
|
||||||
subtitle: String
|
subtitle: String
|
||||||
lang: String
|
lang: String
|
||||||
community: String
|
|
||||||
cover: String
|
cover: String
|
||||||
cover_caption: String
|
cover_caption: String
|
||||||
layout: String!
|
|
||||||
|
community: Community!
|
||||||
|
main_topic: Topic!
|
||||||
|
created_by: Author!
|
||||||
|
topics: [Topic]
|
||||||
|
authors: [Author]
|
||||||
|
updated_by: Author
|
||||||
|
deleted_by: Author
|
||||||
|
|
||||||
created_at: Int!
|
created_at: Int!
|
||||||
updated_at: Int
|
updated_at: Int
|
||||||
|
@ -86,7 +95,7 @@ type Shout {
|
||||||
|
|
||||||
version_of: Shout # TODO: use version_of somewhere
|
version_of: Shout # TODO: use version_of somewhere
|
||||||
|
|
||||||
media: String
|
media: [Media]
|
||||||
stat: Stat
|
stat: Stat
|
||||||
score: Float
|
score: Float
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user