forked from MoreliaTalk/morelia_server
-
Notifications
You must be signed in to change notification settings - Fork 0
/
manage.py
115 lines (93 loc) · 3.9 KB
/
manage.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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
"""
Copyright (c) 2020 - present NekrodNIK, Stepan Skriabin, rus-ai and other.
Look at the file AUTHORS.md(located at the root of the project) to get the
full list.
This file is part of Morelia Server.
Morelia Server is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Morelia Server is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with Morelia Server. If not, see <https://www.gnu.org/licenses/>.
"""
from time import time
from time import process_time
from uuid import uuid4
import click
from mod import lib
from mod.db import dbhandler
from mod.db.dbhandler import DatabaseWriteError
from mod.db.dbhandler import DatabaseReadError
from mod.db.dbhandler import DatabaseAccessError
from mod.config import DATABASE
from mod.config import SUPERUSER
@click.group()
def cli():
pass
@cli.command("db-create", help="Create all table with all data")
def db_create():
start_time = process_time()
db = dbhandler.DBHandler(uri=DATABASE.get('uri'))
db.create_table()
click.echo(f'Table is created at: '
f'{process_time() - start_time} sec.')
@cli.command("db-delete", help="Delete all table with all data")
def db_delete():
start_time = process_time()
db = dbhandler.DBHandler(uri=DATABASE.get('uri'))
db.delete_table()
click.echo(f'Table is deleted at: '
f'{process_time() - start_time} sec.')
@cli.command("superuser-create", help="Create superuser in database")
def create_superuser():
db = dbhandler.DBHandler(uri=DATABASE.get('uri'))
user_uuid = str(123456789)
hash_password = SUPERUSER.get('hash_password')
try:
db.add_user(uuid=user_uuid,
login="login",
password="password",
hash_password=hash_password,
username="superuser",
salt=b"salt",
key=b"key")
click.echo("Superuser created")
except DatabaseWriteError as error:
click.echo(f'Failed to create a user. Error text: {error}')
@cli.command("flow-create", help="Create flow type group in database")
def create_flow():
db = dbhandler.DBHandler(uri=DATABASE.get('uri'))
user_uuid = str(123456789)
try:
new_user = db.get_user_by_uuid(uuid=user_uuid)
new_flow = db.add_flow(uuid=str(uuid4().hex),
users=[user_uuid],
time_created=int(time()),
flow_type="group",
title="Test",
info="Test flow",
owner=user_uuid)
new_flow.addUserConfig(new_user)
click.echo("Flow created")
except (DatabaseReadError,
DatabaseAccessError,
DatabaseWriteError) as error:
click.echo(f'Failed to create a flow. Error text: {error}')
@cli.command("admin-create", help="Create user in admin panel")
@click.option("--username", help="username admin")
@click.option("--password", help="password admin")
def admin_create_user(username, password):
db = dbhandler.DBHandler(uri=DATABASE.get('uri'))
generator = lib.Hash(password,
str(uuid4().hex),
key=b"key",
salt=b"salt")
db.add_admin(username=username,
hash_password=generator.password_hash())
click.echo(f"Admin created\nusername: {username}\npassword: {password}")
if __name__ == "__main__":
cli()