From 1ca46d47f588bc649bcf56257b44ff4a8fcbab6a Mon Sep 17 00:00:00 2001 From: Tim Van Baak Date: Mon, 20 Sep 2021 21:25:10 -0700 Subject: [PATCH] Add post create cli --- amanuensis/backend/post.py | 4 ++-- amanuensis/cli/__init__.py | 2 ++ amanuensis/cli/post.py | 31 +++++++++++++++++++++++++++++++ 3 files changed, 35 insertions(+), 2 deletions(-) create mode 100644 amanuensis/cli/post.py diff --git a/amanuensis/backend/post.py b/amanuensis/backend/post.py index 8a4d59d..8a170fb 100644 --- a/amanuensis/backend/post.py +++ b/amanuensis/backend/post.py @@ -2,7 +2,7 @@ Post query interface """ -import re +from typing import Optional from sqlalchemy import select @@ -14,7 +14,7 @@ from amanuensis.errors import ArgumentError, BackendArgumentTypeError def create( db: DbContext, lexicon_id: int, - user_id: int, + user_id: Optional[int], body: str, ) -> Post: """ diff --git a/amanuensis/cli/__init__.py b/amanuensis/cli/__init__.py index e457db4..56af06e 100644 --- a/amanuensis/cli/__init__.py +++ b/amanuensis/cli/__init__.py @@ -8,6 +8,7 @@ import amanuensis.cli.admin import amanuensis.cli.character import amanuensis.cli.index import amanuensis.cli.lexicon +import amanuensis.cli.post import amanuensis.cli.user from amanuensis.db import DbContext @@ -113,6 +114,7 @@ def main(): add_subcommand(subparsers, amanuensis.cli.character) add_subcommand(subparsers, amanuensis.cli.index) add_subcommand(subparsers, amanuensis.cli.lexicon) + add_subcommand(subparsers, amanuensis.cli.post) add_subcommand(subparsers, amanuensis.cli.user) # Parse args and perform top-level arg processing diff --git a/amanuensis/cli/post.py b/amanuensis/cli/post.py new file mode 100644 index 0000000..1dfd7f7 --- /dev/null +++ b/amanuensis/cli/post.py @@ -0,0 +1,31 @@ +import logging + +from amanuensis.backend import * +from amanuensis.db import * + +from .helpers import add_argument + + +COMMAND_NAME = "post" +COMMAND_HELP = "Interact with posts." + +LOG = logging.getLogger(__name__) + + +@add_argument("--lexicon", required=True, help="The lexicon's name") +@add_argument("--by", help="The character's public id") +@add_argument("--text", help="The text of the post") +def command_create(args) -> int: + """ + Create a post in a lexicon. + """ + db: DbContext = args.get_db() + lexicon = lexiq.try_from_name(db, args.lexicon) + if not lexicon: + raise ValueError("Lexicon does not exist") + user = userq.try_from_username(db, args.by) + user_id = user.id if user else None + post: Post = postq.create(db, lexicon.id, user_id, args.text) + preview = post.body[:20] + "..." if len(post.body) > 20 else post.body + LOG.info(f"Posted '{preview}' in {lexicon.full_title}") + return 0