-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathadd-user.py
46 lines (34 loc) · 1.14 KB
/
add-user.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
"""
add a user to the database with
add user
python add-user.py -u USERNAME -p PASSWORD
delete user
python add-user.py -d USERNAME
"""
from getopt import getopt
import sys
import openmod.sh.schemas.oms as oms
from openmod.sh import web
args = sys.argv[1:]
options, remainder = getopt(args, 'u:p:d:')
options_dct = {opt: arg for opt, arg in options}
print(options_dct)
web.app.app_context().push()
if '-u' in options_dct:
username = options_dct['-u']
password = options_dct['-p']
user = oms.User(username, password)
oms.DB.session.add(user)
oms.DB.session.commit()
oms.DB.session.close()
print("User {} with password {} added to database.".format(username,
password))
if '-d' in options_dct:
username = options_dct['-d']
to_be_deleted = oms.DB.session.query(oms.User).filter_by(name=username).first()
if to_be_deleted is None:
raise Exception("User {} not in database.".format(username))
oms.DB.session.delete(to_be_deleted)
oms.DB.session.commit()
oms.DB.session.close()
print("User {} successfully deleteted.".format(username))