Initial implementation of bin/nntp-tiny-adduser
This commit is contained in:
parent
06b2d3a36e
commit
b0cedd492f
1 changed files with 40 additions and 0 deletions
40
bin/nntp-tiny-adduser
Executable file
40
bin/nntp-tiny-adduser
Executable file
|
@ -0,0 +1,40 @@
|
|||
#! /usr/bin/env python3
|
||||
|
||||
import sys
|
||||
import os
|
||||
import argparse
|
||||
|
||||
from nntp.tiny.config import Config
|
||||
from nntp.tiny.db import Database
|
||||
from nntp.tiny.user import User
|
||||
|
||||
parser = argparse.ArgumentParser(description='Create new account')
|
||||
parser.add_argument('--config-file', '-f', type=str, help='Specify a configuration file location')
|
||||
parser.add_argument('username', type=str, help='Username of account')
|
||||
|
||||
args = parser.parse_args()
|
||||
|
||||
config = Config.load(args.config_file)
|
||||
db = Database.connect(config.get('database', 'path'))
|
||||
|
||||
user = db.get(User, {'username': args.username})
|
||||
|
||||
if user is not None:
|
||||
print(f"{sys.argv[0]}: user '{args.username}' already exists", file=sys.stderr)
|
||||
exit(1)
|
||||
|
||||
user = User()
|
||||
user.active = True
|
||||
user.username = args.username
|
||||
user.password = '*'
|
||||
user.fullname = input('Full name: ')
|
||||
user.mail = input('Email address: ')
|
||||
|
||||
try:
|
||||
db.add(user)
|
||||
db.commit()
|
||||
except Exception as e:
|
||||
print(f"Failed to create user '{args.username}': {e}", file=sys.stderr)
|
||||
exit(1)
|
||||
|
||||
print(f"Created user '{args.username}' successfully.")
|
Loading…
Add table
Reference in a new issue