diff --git a/CHANGELOG.md b/CHANGELOG.md index 60e38b29..d2ee30d2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,7 +1,5 @@ -#### [0.4.10] - 2025-02-10 -- `add_author_stat_columns` fixed -- `Draft` orm and schema tuning and fixes -- `create_draft` and `update_draft` mutations and resolvers fixed +#### [0.4.11] - 2025-02-12 +- `create_draft` resolver requires draft_id fixed #### [0.4.9] - 2025-02-09 diff --git a/resolvers/draft.py b/resolvers/draft.py index 21dd9e2c..4fb60126 100644 --- a/resolvers/draft.py +++ b/resolvers/draft.py @@ -62,21 +62,50 @@ async def load_drafts(_, info): @mutation.field("create_draft") @login_required async def create_draft(_, info, draft_input): + """Create a new draft. + + Args: + info: GraphQL context + draft_input (dict): Draft data including optional fields: + - title (str) + - body (str) + - slug (str) + - etc. + + Returns: + dict: Contains either: + - draft: The created draft object + - error: Error message if creation failed + + Example: + >>> async def test_create(): + ... context = {'user_id': '123', 'author': {'id': 1}} + ... info = type('Info', (), {'context': context})() + ... result = await create_draft(None, info, {'title': 'Test'}) + ... assert result.get('error') is None + ... assert result['draft'].title == 'Test' + ... return result + """ user_id = info.context.get("user_id") author_dict = info.context.get("author", {}) author_id = author_dict.get("id") - draft_id = draft_input.get("id") - if not draft_id: - return {"error": "Draft ID is required"} if not user_id or not author_id: - return {"error": "Author ID are required"} + return {"error": "Author ID is required"} - with local_session() as session: - draft = Draft(created_by=author_id, **draft_input) - session.add(draft) - session.commit() - return {"draft": draft} + try: + with local_session() as session: + # Remove id from input if present since it's auto-generated + if "id" in draft_input: + del draft_input["id"] + + draft = Draft(created_by=author_id, **draft_input) + session.add(draft) + session.commit() + return {"draft": draft} + except Exception as e: + logger.error(f"Failed to create draft: {e}", exc_info=True) + return {"error": f"Failed to create draft: {str(e)}"} @mutation.field("update_draft") diff --git a/resolvers/editor.py b/resolvers/editor.py index 0330a9f6..89c6e4b2 100644 --- a/resolvers/editor.py +++ b/resolvers/editor.py @@ -636,27 +636,26 @@ async def delete_shout(_, info, shout_id: int): def get_main_topic(topics): """Get the main topic from a list of ShoutTopic objects.""" logger.info(f"Starting get_main_topic with {len(topics) if topics else 0} topics") - logger.debug(f"Topics data: {[(t.topic.slug if t.topic else 'no-topic', t.main) for t in topics] if topics else []}") + logger.debug( + f"Topics data: {[(t.topic.slug if t.topic else 'no-topic', t.main) for t in topics] if topics else []}" + ) if not topics: logger.warning("No topics provided to get_main_topic") - return { - "id": 0, - "title": "no topic", - "slug": "notopic", - "is_main": True - } + return {"id": 0, "title": "no topic", "slug": "notopic", "is_main": True} # Find first main topic in original order main_topic_rel = next((st for st in topics if st.main), None) - logger.debug(f"Found main topic relation: {main_topic_rel.topic.slug if main_topic_rel and main_topic_rel.topic else None}") + logger.debug( + f"Found main topic relation: {main_topic_rel.topic.slug if main_topic_rel and main_topic_rel.topic else None}" + ) if main_topic_rel and main_topic_rel.topic: result = { "slug": main_topic_rel.topic.slug, "title": main_topic_rel.topic.title, "id": main_topic_rel.topic.id, - "is_main": True + "is_main": True, } logger.info(f"Returning main topic: {result}") return result @@ -668,14 +667,9 @@ def get_main_topic(topics): "slug": topics[0].topic.slug, "title": topics[0].topic.title, "id": topics[0].topic.id, - "is_main": True + "is_main": True, } return result logger.warning("No valid topics found, returning default") - return { - "slug": "notopic", - "title": "no topic", - "id": 0, - "is_main": True - } + return {"slug": "notopic", "title": "no topic", "id": 0, "is_main": True} diff --git a/resolvers/reader.py b/resolvers/reader.py index b82a7c1e..57f27bd7 100644 --- a/resolvers/reader.py +++ b/resolvers/reader.py @@ -239,7 +239,9 @@ def get_shouts_with_links(info, q, limit=20, offset=0): main_topic = None if hasattr(row, "main_topic"): logger.debug(f"Raw main_topic for shout#{shout_id}: {row.main_topic}") - main_topic = json.loads(row.main_topic) if isinstance(row.main_topic, str) else row.main_topic + main_topic = ( + json.loads(row.main_topic) if isinstance(row.main_topic, str) else row.main_topic + ) logger.debug(f"Parsed main_topic for shout#{shout_id}: {main_topic}") if not main_topic and topics and len(topics) > 0: @@ -248,16 +250,11 @@ def get_shouts_with_links(info, q, limit=20, offset=0): "id": topics[0]["id"], "title": topics[0]["title"], "slug": topics[0]["slug"], - "is_main": True + "is_main": True, } elif not main_topic: logger.warning(f"No main_topic and no topics found for shout#{shout_id}") - main_topic = { - "id": 0, - "title": "no topic", - "slug": "notopic", - "is_main": True - } + main_topic = {"id": 0, "title": "no topic", "slug": "notopic", "is_main": True} shout_dict["main_topic"] = main_topic logger.debug(f"Final main_topic for shout#{shout_id}: {main_topic}")