shout-create-fix

This commit is contained in:
Untone 2025-01-18 10:57:34 +03:00
parent ae7580252b
commit 49be05d4db

View File

@ -4,7 +4,7 @@ from sqlalchemy import and_, desc, select
from sqlalchemy.orm import joinedload from sqlalchemy.orm import joinedload
from sqlalchemy.sql.functions import coalesce from sqlalchemy.sql.functions import coalesce
from cache.cache import cache_author, cache_topic, invalidate_shouts_cache, cache_related_entities from cache.cache import cache_author, cache_topic, invalidate_shouts_cache
from orm.author import Author from orm.author import Author
from orm.shout import Shout, ShoutAuthor, ShoutTopic from orm.shout import Shout, ShoutAuthor, ShoutTopic
from orm.topic import Topic from orm.topic import Topic
@ -113,35 +113,33 @@ async def create_shout(_, info, inp):
logger.info(f"Creating shout with slug: {slug}") logger.info(f"Creating shout with slug: {slug}")
shout_dict = { # Создаем объект Shout напрямую, без промежуточного словаря
"title": inp.get("title", ""), new_shout = Shout(
"subtitle": inp.get("subtitle", ""), title=inp.get("title", ""),
"lead": inp.get("lead", ""), subtitle=inp.get("subtitle", ""),
"description": inp.get("description", ""), lead=inp.get("lead", ""),
"body": inp.get("body", ""), description=inp.get("description", ""),
"layout": inp.get("layout", "article"), body=inp.get("body", ""),
"created_by": author_id, layout=inp.get("layout", "article"),
"authors": [], created_by=author_id,
"slug": slug, slug=slug,
"topics": inp.get("topics", []), published_at=None,
"published_at": None, community=1,
"community": 1, created_at=current_time
"created_at": current_time, )
}
# Check for duplicate slug # Check for duplicate slug
logger.debug(f"Checking for existing slug: {slug}") logger.debug(f"Checking for existing slug: {slug}")
same_slug_shout = session.query(Shout).filter(Shout.slug == shout_dict.get("slug")).first() same_slug_shout = session.query(Shout).filter(Shout.slug == new_shout.slug).first()
c = 1 c = 1
while same_slug_shout is not None: while same_slug_shout is not None:
logger.debug(f"Found duplicate slug, trying iteration {c}") logger.debug(f"Found duplicate slug, trying iteration {c}")
same_slug_shout = session.query(Shout).filter(Shout.slug == shout_dict.get("slug")).first() new_shout.slug = f"{slug}-{c}"
same_slug_shout = session.query(Shout).filter(Shout.slug == new_shout.slug).first()
c += 1 c += 1
shout_dict["slug"] += f"-{c}"
try: try:
logger.info("Creating new shout object") logger.info("Creating new shout object")
new_shout = Shout(**shout_dict)
session.add(new_shout) session.add(new_shout)
session.commit() session.commit()
logger.info(f"Created shout with ID: {new_shout.id}") logger.info(f"Created shout with ID: {new_shout.id}")