From 04cb0cb69642c8a4cb1c6fbad196a844dad8b09b Mon Sep 17 00:00:00 2001 From: Tim Van Baak Date: Fri, 3 Jan 2020 17:07:28 -0800 Subject: [PATCH] Add basic user creation command --- amanuensis/cli.py | 23 +++++++++++++++++++++++ amanuensis/user.py | 17 +++++++++++++++++ 2 files changed, 40 insertions(+) create mode 100644 amanuensis/user.py diff --git a/amanuensis/cli.py b/amanuensis/cli.py index 710f7d8..8855607 100644 --- a/amanuensis/cli.py +++ b/amanuensis/cli.py @@ -117,6 +117,29 @@ def command_run(args): return -1 app.app.run(host=args.address, port=args.port) +@add_argument("--username", help="User's login handle") +@add_argument("--displayname", help="User's publicly displayed name") +@add_argument("--email", help="User's email") +def command_user_add(args): + """Creates a user""" + import user + import config + + # Verify or query parameters + if not args.username: + args.username = input("username: ").strip() + if not user.valid_username(args.username): + config.logger.error("Invalid username: usernames may only contain alphanumeric characters, dashes, and underscores") + return -1 + if not args.displayname: + args.displayname = args.username + if not args.email: + args.email = input("email: ").strip() + if not user.valid_email(args.email): + config.logger.error("Invalid email") + return -1 + # Create user + new_user = user.create_user(args.username, args.displayname, args.email) @add_argument("--foo", action="store_true") def command_dump(args): diff --git a/amanuensis/user.py b/amanuensis/user.py new file mode 100644 index 0000000..a682674 --- /dev/null +++ b/amanuensis/user.py @@ -0,0 +1,17 @@ +import re + +class User(): + pass + +def valid_username(username): + return re.match(r"^[A-Za-z0-9-_]{3,}$", username) is not None + +def valid_email(email): + """Vaguely RFC2822 email verifier""" + atom = r"[0-9A-Za-z!#$%&'*+-/=?^_`{|}~]{1,}" + dotatom = atom + r"(\." + atom + r")*" + addrspec = "^" + dotatom + "@" + dotatom + "$" + return re.match(addrspec, email) + +def create_user(username, displayname, email): + pass