-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
init_db.py
100 lines (89 loc) · 3.7 KB
/
init_db.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
from flask import Flask
from werkzeug.security import generate_password_hash
from flask_sqlalchemy import SQLAlchemy
from sqlalchemy_json import mutable_json_type
from modules.helpers import logger, generateToken
from modules.domoticz import saveJson
app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///db.sqlite'
db = SQLAlchemy(app)
class User(db.Model):
__tablename__ = "User"
id = db.Column(db.Integer, primary_key=True)
email = db.Column(db.String(100), unique=True)
username = db.Column(db.String(100), unique=True)
password = db.Column(db.String(100))
roomplan = db.Column(db.String(10))
domo_url = db.Column(db.String(255))
domouser = db.Column(db.String(100))
domopass = db.Column(db.String(100))
admin = db.Column(db.Boolean, default=False, nullable=False)
googleassistant = db.Column(db.Boolean, default=False, nullable=False)
authtoken = db.Column(db.String(100))
device_config = db.Column(mutable_json_type(dbtype=db.JSON, nested=True))
def __repr__(self):
return f"<User {self.id}>"
class Settings(db.Model):
__tablename__ = "Settings"
id = db.Column(db.Integer, primary_key=True)
client_id = db.Column(db.String(255))
client_secret = db.Column(db.String(255))
api_key = db.Column(db.String(255))
tempunit = db.Column(db.String(255))
use_ssl = db.Column(db.Boolean, default=False, nullable=False)
ssl_cert = db.Column(db.String(255))
ssl_key = db.Column(db.String(255))
language = db.Column(db.String(255))
armlevels = db.Column(mutable_json_type(dbtype=db.JSON, nested=True))
def __repr__(self):
return f"<Settings {self.id}>"
with app.app_context():
db.create_all()
email = 'admin@example.com'
username = 'admin'
new_user = User(email=email,
username=username,
# password=generate_password_hash('smarthome'),
password='smarthome',
roomplan='0',
domo_url='http://192.168.1.99:8080',
domouser='domoticz',
domopass='password',
admin=True,
googleassistant=False,
authtoken=generateToken(username),
device_config={}
)
db.session.add(new_user)
settings = Settings(client_id='sampleClientId',
client_secret='sampleClientSecret',
api_key='<aog_api_key>',
tempunit='C',
use_ssl=False,
ssl_cert='/home/cert/local.crt',
ssl_key='/home/cert/local.key',
language='en',
armlevels={'armhome': [
"armed home",
"low security",
"home and guarding",
"level 1",
"home",
"SL1"],
'armaway': [
"armed away",
"high security",
"away and guarding",
"level 2",
"away",
"SL2"]
}
)
db.session.add(settings)
try:
db.session.commit()
data = {}
saveJson(username, data)
logger.info("Database is created...")
except Exception:
logger.info('Database already created')