Fill out user-add and add user-list

This commit is contained in:
Tim Van Baak 2020-01-08 22:48:00 -08:00
parent 4bd7330229
commit 9b97768438
3 changed files with 53 additions and 13 deletions

View File

@ -121,6 +121,8 @@ def command_run(args):
@add_argument("--email", help="User's email") @add_argument("--email", help="User's email")
def command_user_add(args): def command_user_add(args):
"""Creates a user""" """Creates a user"""
import json
import user import user
import config import config
@ -139,6 +141,24 @@ def command_user_add(args):
return -1 return -1
# Create user # Create user
new_user = user.create_user(args.username, args.displayname, args.email) new_user = user.create_user(args.username, args.displayname, args.email)
with config.json_ro(new_user.config) as js:
print(json.dumps(js, indent=2))
@no_argument
def command_user_list(args):
"""Lists users"""
import os
import config
user_dirs = os.listdir(config.prepend('user'))
users = []
for uid in user_dirs:
with config.json_ro('user', uid, 'config.json') as user:
users.append(user)
users.sort(key=lambda u: u['username'])
for user in users:
print("{0} {1} ({2})".format(user['uid'], user['displayname'], user['username']))
@add_argument("--foo", action="store_true") @add_argument("--foo", action="store_true")
def command_dump(args): def command_dump(args):

View File

@ -40,8 +40,8 @@ def init_config(args):
def get(key): def get(key):
return GLOBAL_CONFIG[key] return GLOBAL_CONFIG[key]
def prepend(path): def prepend(*path):
return os.path.join(CONFIG_DIR, path) return os.path.join(CONFIG_DIR, *path)
def open_sh(path, mode): def open_sh(path, mode):
return config.loader.open_sh(prepend(path), mode) return config.loader.open_sh(prepend(path), mode)
@ -49,14 +49,14 @@ def open_sh(path, mode):
def open_ex(path, mode): def open_ex(path, mode):
return config.loader.open_ex(prepend(path), mode) return config.loader.open_ex(prepend(path), mode)
def json_ro(path): def json_ro(*path):
return config.loader.json_ro(prepend(path)) return config.loader.json_ro(prepend(*path))
def json_rw(path): def json_rw(*path):
return config.loader.json_rw(prepend(path)) return config.loader.json_rw(prepend(*path))
def json(*args, mode='r'):
if not args[-1].endswith(".json"):
args[-1] = args[-1] + ".json"
path = os.path.join(CONFIG_DIR, *args)
def new_user(user_json):
user_dir = prepend("user", user_json['uid'])
os.mkdir(user_dir)
with config.loader.open_ex(os.path.join(user_dir, "config.json"), 'w') as f:
json.dump(user_json, f, allow_nan=False, indent='\t')

View File

@ -1,7 +1,16 @@
import os
import re import re
import time
import uuid
from werkzeug.security import generate_password_hash, check_password_hash
import config
class User(): class User():
pass def __init__(self, uid):
self.uid = uid
self.config = os.path.join('user', uid, 'config.json')
def valid_username(username): def valid_username(username):
return re.match(r"^[A-Za-z0-9-_]{3,}$", username) is not None return re.match(r"^[A-Za-z0-9-_]{3,}$", username) is not None
@ -14,4 +23,15 @@ def valid_email(email):
return re.match(addrspec, email) return re.match(addrspec, email)
def create_user(username, displayname, email): def create_user(username, displayname, email):
pass uid = uuid.uuid4().hex
now = int(time.time())
user_json = {
'uid': uid,
'username': username,
'displayname': displayname,
'email': email,
'password': None,
'created': now,
}
config.new_user(user_json)
return User(uid)