Add post create cli

This commit is contained in:
Tim Van Baak 2021-09-20 21:25:10 -07:00
parent a6399e7e22
commit 1ca46d47f5
3 changed files with 35 additions and 2 deletions

View File

@ -2,7 +2,7 @@
Post query interface Post query interface
""" """
import re from typing import Optional
from sqlalchemy import select from sqlalchemy import select
@ -14,7 +14,7 @@ from amanuensis.errors import ArgumentError, BackendArgumentTypeError
def create( def create(
db: DbContext, db: DbContext,
lexicon_id: int, lexicon_id: int,
user_id: int, user_id: Optional[int],
body: str, body: str,
) -> Post: ) -> Post:
""" """

View File

@ -8,6 +8,7 @@ import amanuensis.cli.admin
import amanuensis.cli.character import amanuensis.cli.character
import amanuensis.cli.index import amanuensis.cli.index
import amanuensis.cli.lexicon import amanuensis.cli.lexicon
import amanuensis.cli.post
import amanuensis.cli.user import amanuensis.cli.user
from amanuensis.db import DbContext from amanuensis.db import DbContext
@ -113,6 +114,7 @@ def main():
add_subcommand(subparsers, amanuensis.cli.character) add_subcommand(subparsers, amanuensis.cli.character)
add_subcommand(subparsers, amanuensis.cli.index) add_subcommand(subparsers, amanuensis.cli.index)
add_subcommand(subparsers, amanuensis.cli.lexicon) add_subcommand(subparsers, amanuensis.cli.lexicon)
add_subcommand(subparsers, amanuensis.cli.post)
add_subcommand(subparsers, amanuensis.cli.user) add_subcommand(subparsers, amanuensis.cli.user)
# Parse args and perform top-level arg processing # Parse args and perform top-level arg processing

31
amanuensis/cli/post.py Normal file
View File

@ -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