draft-seo-handling
All checks were successful
Deploy on push / deploy (push) Successful in 1m10s

This commit is contained in:
2025-04-15 20:16:01 +03:00
parent bd129efde6
commit eb216a5f36
5 changed files with 30 additions and 30 deletions

View File

@@ -1,8 +1,8 @@
import time
from operator import or_
from sqlalchemy.sql import and_
import trafilatura
from sqlalchemy.sql import and_
from cache.cache import (
cache_author,
@@ -104,7 +104,7 @@ async def create_draft(_, info, draft_input):
if "title" not in draft_input or not draft_input["title"]:
draft_input["title"] = "" # Пустая строка вместо NULL
# Проверяем slug - он должен быть или не пустым, или не передаваться вообще
if "slug" in draft_input and (draft_input["slug"] is None or draft_input["slug"] == ""):
# При создании черновика удаляем пустой slug из входных данных
@@ -115,9 +115,9 @@ async def create_draft(_, info, draft_input):
# Remove id from input if present since it's auto-generated
if "id" in draft_input:
del draft_input["id"]
if "seo" not in draft_input and not draft_input["seo"]:
body_teaser = draft_input.get("body", "")[:300].split('\n')[:-1].join("\n")
body_teaser = draft_input.get("body", "")[:300].split("\n")[:-1].join("\n")
draft_input["seo"] = draft_input.get("lead", body_teaser)
# Добавляем текущее время создания
@@ -164,13 +164,13 @@ async def update_draft(_, info, draft_id: int, draft_input):
draft = session.query(Draft).filter(Draft.id == draft_id).first()
if not draft:
return {"error": "Draft not found"}
if "seo" not in draft_input and not draft.seo:
body_src = draft_input["body"] if "body" in draft_input else draft.body
body_text = trafilatura.extract(body_src)
lead_src = draft_input["lead"] if "lead" in draft_input else draft.lead
lead_text = trafilatura.extract(lead_src)
body_teaser = body_text[:300].split('. ')[:-1].join(".\n")
body_teaser = body_text[:300].split(". ")[:-1].join(".\n")
draft_input["seo"] = lead_text or body_teaser
Draft.update(draft, draft_input)
@@ -178,7 +178,7 @@ async def update_draft(_, info, draft_id: int, draft_input):
current_time = int(time.time())
draft.updated_at = current_time
draft.updated_by = author_id
session.commit()
return {"draft": draft}

View File

@@ -1,6 +1,7 @@
import time
import orjson
import trafilatura
from sqlalchemy import and_, desc, select
from sqlalchemy.orm import joinedload
from sqlalchemy.sql.functions import coalesce
@@ -22,7 +23,6 @@ from services.notify import notify_shout
from services.schema import query
from services.search import search_service
from utils.logger import root_logger as logger
import trafilatura
async def cache_by_id(entity, entity_id: int, cache_method):
@@ -181,7 +181,7 @@ async def create_shout(_, info, inp):
lead = inp.get("lead", "")
body_text = trafilatura.extract(body)
lead_text = trafilatura.extract(lead)
seo = inp.get("seo", lead_text or body_text[:300].split('. ')[:-1].join(". "))
seo = inp.get("seo", lead_text or body_text[:300].split(". ")[:-1].join(". "))
new_shout = Shout(
slug=slug,
body=body,
@@ -388,7 +388,7 @@ def patch_topics(session, shout, topics_input):
# @login_required
async def update_shout(_, info, shout_id: int, shout_input=None, publish=False):
logger.info(f"Starting update_shout with id={shout_id}, publish={publish}")
logger.debug(f"Full shout_input: {shout_input}") # DraftInput
logger.debug(f"Full shout_input: {shout_input}") # DraftInput
user_id = info.context.get("user_id")
roles = info.context.get("roles", [])

View File

@@ -6,7 +6,7 @@ from cache.cache import (
get_cached_topic_authors,
get_cached_topic_by_slug,
get_cached_topic_followers,
invalidate_cache_by_prefix
invalidate_cache_by_prefix,
)
from orm.author import Author
from orm.topic import Topic
@@ -126,7 +126,7 @@ async def get_topics_with_stats(limit=100, offset=0, community_id=None, by=None)
GROUP BY topic
"""
followers_stats = {row[0]: row[1] for row in session.execute(text(followers_stats_query))}
# Запрос на получение статистики авторов для выбранных тем
authors_stats_query = f"""
SELECT st.topic, COUNT(DISTINCT sa.author) as authors_count
@@ -149,7 +149,6 @@ async def get_topics_with_stats(limit=100, offset=0, community_id=None, by=None)
"""
comments_stats = {row[0]: row[1] for row in session.execute(text(comments_stats_query))}
# Формируем результат с добавлением статистики
result = []
for topic in topics:
@@ -158,7 +157,7 @@ async def get_topics_with_stats(limit=100, offset=0, community_id=None, by=None)
"shouts": shouts_stats.get(topic.id, 0),
"followers": followers_stats.get(topic.id, 0),
"authors": authors_stats.get(topic.id, 0),
"comments": comments_stats.get(topic.id, 0)
"comments": comments_stats.get(topic.id, 0),
}
result.append(topic_dict)