debug-update-shout

This commit is contained in:
Untone 2025-01-16 05:34:43 +03:00
parent 5e1021a18e
commit 576e1ea152
2 changed files with 102 additions and 47 deletions

View File

@ -88,7 +88,7 @@ server {
add_header Cache-Control "public, no-transform"; add_header Cache-Control "public, no-transform";
} }
location ~* \.(mp3)$ { location ~* \.(mp3|wav|ogg|flac|aac|aif|webm)$ {
if ($request_method = 'GET') { if ($request_method = 'GET') {
add_header 'Access-Control-Allow-Origin' $allow_origin always; add_header 'Access-Control-Allow-Origin' $allow_origin always;
add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS' always; add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS' always;

View File

@ -94,67 +94,122 @@ async def get_shouts_drafts(_, info):
@mutation.field("create_shout") @mutation.field("create_shout")
@login_required @login_required
async def create_shout(_, info, inp): async def create_shout(_, info, inp):
logger.info(f"Starting create_shout with input: {inp}")
user_id = info.context.get("user_id") user_id = info.context.get("user_id")
author_dict = info.context.get("author") author_dict = info.context.get("author")
logger.debug(f"Context user_id: {user_id}, author: {author_dict}")
if not author_dict: if not author_dict:
logger.error("Author profile not found in context")
return {"error": "author profile was not found"} return {"error": "author profile was not found"}
author_id = author_dict.get("id") author_id = author_dict.get("id")
if user_id and author_id: if user_id and author_id:
with local_session() as session: try:
author_id = int(author_id) with local_session() as session:
current_time = int(time.time()) author_id = int(author_id)
slug = inp.get("slug") or f"draft-{current_time}" current_time = int(time.time())
shout_dict = { slug = inp.get("slug") or f"draft-{current_time}"
"title": inp.get("title", ""),
"subtitle": inp.get("subtitle", ""), logger.info(f"Creating shout with slug: {slug}")
"lead": inp.get("lead", ""),
"description": inp.get("description", ""), shout_dict = {
"body": inp.get("body", ""), "title": inp.get("title", ""),
"layout": inp.get("layout", "article"), "subtitle": inp.get("subtitle", ""),
"created_by": author_id, "lead": inp.get("lead", ""),
"authors": [], "description": inp.get("description", ""),
"slug": slug, "body": inp.get("body", ""),
"topics": inp.get("topics", []), "layout": inp.get("layout", "article"),
"published_at": None, "created_by": author_id,
"community": 1, "authors": [],
"created_at": current_time, # Set created_at as Unix timestamp "slug": slug,
} "topics": inp.get("topics", []),
same_slug_shout = session.query(Shout).filter(Shout.slug == shout_dict.get("slug")).first() "published_at": None,
c = 1 "community": 1,
while same_slug_shout is not None: "created_at": current_time,
}
# Check for duplicate 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 == shout_dict.get("slug")).first()
c += 1 c = 1
shout_dict["slug"] += f"-{c}" while same_slug_shout is not None:
new_shout = Shout(**shout_dict) logger.debug(f"Found duplicate slug, trying iteration {c}")
session.add(new_shout) same_slug_shout = session.query(Shout).filter(Shout.slug == shout_dict.get("slug")).first()
session.commit() c += 1
shout_dict["slug"] += f"-{c}"
# NOTE: requesting new shout back try:
shout = session.query(Shout).where(Shout.slug == slug).first() logger.info("Creating new shout object")
if shout: new_shout = Shout(**shout_dict)
# Проверка на существование записи session.add(new_shout)
existing_sa = session.query(ShoutAuthor).filter_by(shout=shout.id, author=author_id).first() session.commit()
if not existing_sa: logger.info(f"Created shout with ID: {new_shout.id}")
sa = ShoutAuthor(shout=shout.id, author=author_id) except Exception as e:
session.add(sa) logger.error(f"Error creating shout object: {e}", exc_info=True)
return {"error": f"Database error: {str(e)}"}
topics = session.query(Topic).filter(Topic.slug.in_(inp.get("topics", []))).all() # Get created shout
for topic in topics: try:
existing_st = session.query(ShoutTopic).filter_by(shout=shout.id, author=topic.id).first() logger.debug(f"Retrieving created shout with slug: {slug}")
if not existing_st: shout = session.query(Shout).where(Shout.slug == slug).first()
t = ShoutTopic(topic=topic.id, shout=shout.id) if not shout:
session.add(t) logger.error("Created shout not found in database")
return {"error": "Shout creation failed - not found after commit"}
except Exception as e:
logger.error(f"Error retrieving created shout: {e}", exc_info=True)
return {"error": f"Error retrieving created shout: {str(e)}"}
session.commit() # Link author
try:
logger.debug(f"Linking author {author_id} to shout {shout.id}")
existing_sa = session.query(ShoutAuthor).filter_by(shout=shout.id, author=author_id).first()
if not existing_sa:
sa = ShoutAuthor(shout=shout.id, author=author_id)
session.add(sa)
logger.info(f"Added author {author_id} to shout {shout.id}")
except Exception as e:
logger.error(f"Error linking author: {e}", exc_info=True)
return {"error": f"Error linking author: {str(e)}"}
follow(None, info, "shout", shout.slug) # Link topics
try:
logger.debug(f"Linking topics: {inp.get('topics', [])}")
topics = session.query(Topic).filter(Topic.slug.in_(inp.get("topics", []))).all()
for topic in topics:
existing_st = session.query(ShoutTopic).filter_by(shout=shout.id, topic=topic.id).first()
if not existing_st:
t = ShoutTopic(topic=topic.id, shout=shout.id)
session.add(t)
logger.info(f"Added topic {topic.id} to shout {shout.id}")
except Exception as e:
logger.error(f"Error linking topics: {e}", exc_info=True)
return {"error": f"Error linking topics: {str(e)}"}
# notifier try:
# await notify_shout(shout_dict, 'create') session.commit()
logger.info("Final commit successful")
except Exception as e:
logger.error(f"Error in final commit: {e}", exc_info=True)
return {"error": f"Error in final commit: {str(e)}"}
try:
logger.debug("Following created shout")
await follow(None, info, "shout", shout.slug)
except Exception as e:
logger.warning(f"Error following shout: {e}", exc_info=True)
# Don't return error as this is not critical
logger.info(f"Successfully created shout {shout.id}")
return {"shout": shout} return {"shout": shout}
return {"error": "cant create shout" if user_id else "unauthorized"} except Exception as e:
logger.error(f"Unexpected error in create_shout: {e}", exc_info=True)
return {"error": f"Unexpected error: {str(e)}"}
error_msg = "cant create shout" if user_id else "unauthorized"
logger.error(f"Create shout failed: {error_msg}")
return {"error": error_msg}
def patch_main_topic(session, main_topic, shout): def patch_main_topic(session, main_topic, shout):