minor-fixes
This commit is contained in:
parent
a5ec1838b1
commit
0533863230
|
@ -24,13 +24,12 @@ from services.schema import query
|
||||||
from services.search import search_text
|
from services.search import search_text
|
||||||
from services.viewed import ViewedStorage
|
from services.viewed import ViewedStorage
|
||||||
|
|
||||||
|
|
||||||
def query_shouts():
|
def query_shouts():
|
||||||
"""
|
"""
|
||||||
Базовый запрос для получения публикаций с подзапросами статистики, авторов и тем,
|
Базовый запрос для получения публикаций с подзапросами статистики, авторов и тем,
|
||||||
с агрегацией в строку вместо JSON.
|
с агрегацией в строку.
|
||||||
|
|
||||||
:return: Запрос для получения публикаций.
|
:return: Запрос для получения публикаций, aliased_reaction:
|
||||||
"""
|
"""
|
||||||
# Создаем алиасы для таблиц для избежания конфликтов имен
|
# Создаем алиасы для таблиц для избежания конфликтов имен
|
||||||
aliased_reaction = aliased(Reaction)
|
aliased_reaction = aliased(Reaction)
|
||||||
|
@ -106,7 +105,6 @@ def query_shouts():
|
||||||
|
|
||||||
return q, aliased_reaction
|
return q, aliased_reaction
|
||||||
|
|
||||||
|
|
||||||
def parse_aggregated_string(aggregated_str):
|
def parse_aggregated_string(aggregated_str):
|
||||||
"""
|
"""
|
||||||
Преобразует строку, полученную из string_agg, обратно в список словарей.
|
Преобразует строку, полученную из string_agg, обратно в список словарей.
|
||||||
|
@ -126,14 +124,12 @@ def parse_aggregated_string(aggregated_str):
|
||||||
item_data[key] = value
|
item_data[key] = value
|
||||||
else:
|
else:
|
||||||
# Лог или обработка случаев, когда разделитель отсутствует
|
# Лог или обработка случаев, когда разделитель отсутствует
|
||||||
print(f"Некорректный формат поля: {field}")
|
logger.error(f"Некорректный формат поля: {field}")
|
||||||
continue
|
continue
|
||||||
items.append(item_data)
|
items.append(item_data)
|
||||||
|
|
||||||
return items
|
return items
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
def get_shouts_with_stats(q, limit, offset=0, author_id=None):
|
def get_shouts_with_stats(q, limit, offset=0, author_id=None):
|
||||||
"""
|
"""
|
||||||
Получение публикаций со статистикой, и подзапросами авторов и тем.
|
Получение публикаций со статистикой, и подзапросами авторов и тем.
|
||||||
|
@ -181,7 +177,6 @@ def get_shouts_with_stats(q, limit, offset=0, author_id=None):
|
||||||
|
|
||||||
return shouts
|
return shouts
|
||||||
|
|
||||||
|
|
||||||
def filter_my(info, session, q):
|
def filter_my(info, session, q):
|
||||||
"""
|
"""
|
||||||
Фильтрация публикаций, основанная на подписках пользователя.
|
Фильтрация публикаций, основанная на подписках пользователя.
|
||||||
|
@ -213,7 +208,6 @@ def filter_my(info, session, q):
|
||||||
q = q.filter(Shout.id.in_(subquery))
|
q = q.filter(Shout.id.in_(subquery))
|
||||||
return q, reader_id
|
return q, reader_id
|
||||||
|
|
||||||
|
|
||||||
def apply_filters(q, filters, author_id=None):
|
def apply_filters(q, filters, author_id=None):
|
||||||
"""
|
"""
|
||||||
Применение фильтров к запросу.
|
Применение фильтров к запросу.
|
||||||
|
@ -256,7 +250,6 @@ def apply_filters(q, filters, author_id=None):
|
||||||
|
|
||||||
return q
|
return q
|
||||||
|
|
||||||
|
|
||||||
@query.field("get_shout")
|
@query.field("get_shout")
|
||||||
async def get_shout(_, info, slug: str):
|
async def get_shout(_, info, slug: str):
|
||||||
"""
|
"""
|
||||||
|
@ -277,6 +270,7 @@ async def get_shout(_, info, slug: str):
|
||||||
[
|
[
|
||||||
shout,
|
shout,
|
||||||
commented_stat,
|
commented_stat,
|
||||||
|
followers_stat,
|
||||||
rating_stat,
|
rating_stat,
|
||||||
last_reaction_at,
|
last_reaction_at,
|
||||||
authors,
|
authors,
|
||||||
|
@ -289,8 +283,8 @@ async def get_shout(_, info, slug: str):
|
||||||
"rating": rating_stat,
|
"rating": rating_stat,
|
||||||
"last_reacted_at": last_reaction_at,
|
"last_reacted_at": last_reaction_at,
|
||||||
}
|
}
|
||||||
shout.authors = authors or []
|
shout.authors = parse_aggregated_string(authors)
|
||||||
shout.topics = topics or []
|
shout.topics = parse_aggregated_string(topics)
|
||||||
|
|
||||||
for author_caption in (
|
for author_caption in (
|
||||||
session.query(ShoutAuthor)
|
session.query(ShoutAuthor)
|
||||||
|
@ -304,8 +298,8 @@ async def get_shout(_, info, slug: str):
|
||||||
)
|
)
|
||||||
):
|
):
|
||||||
for author in shout.authors:
|
for author in shout.authors:
|
||||||
if author.id == author_caption.author:
|
if author["id"] == str(author_caption.author):
|
||||||
author.caption = author_caption.caption
|
author["caption"] = author_caption.caption
|
||||||
main_topic = (
|
main_topic = (
|
||||||
session.query(Topic.slug)
|
session.query(Topic.slug)
|
||||||
.join(
|
.join(
|
||||||
|
@ -324,9 +318,8 @@ async def get_shout(_, info, slug: str):
|
||||||
return shout
|
return shout
|
||||||
except Exception as _exc:
|
except Exception as _exc:
|
||||||
import traceback
|
import traceback
|
||||||
|
|
||||||
logger.error(traceback.format_exc())
|
logger.error(traceback.format_exc())
|
||||||
|
return None
|
||||||
|
|
||||||
@query.field("load_shouts_by")
|
@query.field("load_shouts_by")
|
||||||
async def load_shouts_by(_, _info, options):
|
async def load_shouts_by(_, _info, options):
|
||||||
|
@ -347,7 +340,6 @@ async def load_shouts_by(_, _info, options):
|
||||||
order_by = Shout.featured_at if filters.get("featured") else Shout.published_at
|
order_by = Shout.featured_at if filters.get("featured") else Shout.published_at
|
||||||
order_str = options.get("order_by")
|
order_str = options.get("order_by")
|
||||||
if order_str in ["rating", "followers", "comments", "last_reacted_at"]:
|
if order_str in ["rating", "followers", "comments", "last_reacted_at"]:
|
||||||
# TODO: implement followers_stat
|
|
||||||
q = q.order_by(desc(text(f"{order_str}_stat")))
|
q = q.order_by(desc(text(f"{order_str}_stat")))
|
||||||
query_order_by = desc(order_by) if options.get("order_by_desc", True) else asc(order_by)
|
query_order_by = desc(order_by) if options.get("order_by_desc", True) else asc(order_by)
|
||||||
q = q.order_by(nulls_last(query_order_by))
|
q = q.order_by(nulls_last(query_order_by))
|
||||||
|
@ -419,7 +411,6 @@ async def load_shouts_search(_, _info, text, limit=50, offset=0):
|
||||||
shouts = get_shouts_with_stats(q, limit, offset)
|
shouts = get_shouts_with_stats(q, limit, offset)
|
||||||
for shout in shouts:
|
for shout in shouts:
|
||||||
shout.score = scores[f"{shout.id}"]
|
shout.score = scores[f"{shout.id}"]
|
||||||
shouts.append(shout)
|
|
||||||
shouts.sort(key=lambda x: x.score, reverse=True)
|
shouts.sort(key=lambda x: x.score, reverse=True)
|
||||||
return shouts
|
return shouts
|
||||||
return []
|
return []
|
||||||
|
@ -536,7 +527,7 @@ async def load_shouts_coauthored(_, info, limit=50, offset=0):
|
||||||
author_id = info.context.get("author", {}).get("id")
|
author_id = info.context.get("author", {}).get("id")
|
||||||
if not author_id:
|
if not author_id:
|
||||||
return []
|
return []
|
||||||
q, aliaed_reaction = query_shouts()
|
q, aliased_reaction = query_shouts()
|
||||||
q = q.filter(Shout.authors.any(id=author_id))
|
q = q.filter(Shout.authors.any(id=author_id))
|
||||||
return get_shouts_with_stats(q, limit, offset=offset)
|
return get_shouts_with_stats(q, limit, offset=offset)
|
||||||
|
|
||||||
|
@ -555,18 +546,17 @@ async def load_shouts_discussed(_, info, limit=50, offset=0):
|
||||||
author_id = info.context.get("author", {}).get("id")
|
author_id = info.context.get("author", {}).get("id")
|
||||||
if not author_id:
|
if not author_id:
|
||||||
return []
|
return []
|
||||||
# Subquery to find shout IDs that the author has commented on
|
# Подзапрос для поиска идентификаторов публикаций, которые комментировал автор
|
||||||
reaction_subquery = (
|
reaction_subquery = (
|
||||||
select(Reaction.shout)
|
select(Reaction.shout)
|
||||||
.distinct() # Ensure distinct shout IDs
|
.distinct() # Убедитесь, что получены уникальные идентификаторы публикаций
|
||||||
.filter(and_(Reaction.created_by == author_id, Reaction.body.is_not(None)))
|
.filter(and_(Reaction.created_by == author_id, Reaction.body.is_not(None)))
|
||||||
.correlate(Shout) # Ensure proper correlation with the main query
|
.correlate(Shout) # Убедитесь, что подзапрос правильно связан с основным запросом
|
||||||
)
|
)
|
||||||
q, aliased_reaction = query_shouts()
|
q, aliased_reaction = query_shouts()
|
||||||
q = q.filter(Shout.id.in_(reaction_subquery))
|
q = q.filter(Shout.id.in_(reaction_subquery))
|
||||||
return get_shouts_with_stats(q, limit, offset=offset)
|
return get_shouts_with_stats(q, limit, offset=offset)
|
||||||
|
|
||||||
|
|
||||||
async def reacted_shouts_updates(follower_id: int, limit=50, offset=0) -> List[Shout]:
|
async def reacted_shouts_updates(follower_id: int, limit=50, offset=0) -> List[Shout]:
|
||||||
"""
|
"""
|
||||||
Обновляет публикации, на которые подписан автор, с учетом реакций.
|
Обновляет публикации, на которые подписан автор, с учетом реакций.
|
||||||
|
@ -597,7 +587,6 @@ async def reacted_shouts_updates(follower_id: int, limit=50, offset=0) -> List[S
|
||||||
|
|
||||||
return shouts
|
return shouts
|
||||||
|
|
||||||
|
|
||||||
@query.field("load_shouts_followed")
|
@query.field("load_shouts_followed")
|
||||||
@login_required
|
@login_required
|
||||||
async def load_shouts_followed(_, info, limit=50, offset=0) -> List[Shout]:
|
async def load_shouts_followed(_, info, limit=50, offset=0) -> List[Shout]:
|
||||||
|
@ -621,7 +610,6 @@ async def load_shouts_followed(_, info, limit=50, offset=0) -> List[Shout]:
|
||||||
logger.debug(error)
|
logger.debug(error)
|
||||||
return []
|
return []
|
||||||
|
|
||||||
|
|
||||||
@query.field("load_shouts_followed_by")
|
@query.field("load_shouts_followed_by")
|
||||||
async def load_shouts_followed_by(_, info, slug: str, limit=50, offset=0) -> List[Shout]:
|
async def load_shouts_followed_by(_, info, slug: str, limit=50, offset=0) -> List[Shout]:
|
||||||
"""
|
"""
|
||||||
|
|
Loading…
Reference in New Issue
Block a user