This repository has been archived by the owner on Mar 5, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
init.py
104 lines (92 loc) · 3.99 KB
/
init.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
# from flask import Flask
# from flask_sqlalchemy import SQLAlchemy
import csv
import os
import sys
# import random
from base64 import b64encode
from flask_security.utils import encrypt_password
from homeautomation import create_app
from homeautomation.models import db, StockProductCategory, StockProduct,\
user_datastore
app = create_app()
with app.app_context():
app.logger.info('Recreating DB')
db.drop_all()
db.create_all()
app.logger.info('Filling DB with test data')
# Create the default roles
app.logger.info('Filling DB with users')
try:
basic = user_datastore.find_or_create_role(name='User',
description="Basic user")
admin = user_datastore.find_or_create_role(name='Admin',
description='API Administrator')
# Create the default users
user_datastore.create_user(username='papa',
email='[email protected]',
password=encrypt_password('apap'),
first_name="Papa",
last_name="Family",
api_key=b64encode(os.urandom(48)).decode('ascii'))
user_datastore.create_user(username='mama',
email='[email protected]',
password=encrypt_password('amam'),
first_name="Mama",
last_name="Family",
api_key=b64encode(os.urandom(48)).decode('ascii'))
user_datastore.create_user(username='child',
email='[email protected]',
password=encrypt_password('dlihc'),
first_name="Child",
last_name="Family")
papa = user_datastore.find_user(username='papa')
mama = user_datastore.find_user(username='mama')
child = user_datastore.find_user(username='child')
user_datastore.activate_user(papa)
user_datastore.activate_user(mama)
user_datastore.activate_user(child)
user_datastore.add_role_to_user(papa, admin)
user_datastore.add_role_to_user(mama, admin)
user_datastore.add_role_to_user(child, basic)
# Save users
db.session.commit()
except:
app.logger.exception('Exception occured during installing users',
sys.exc_info()[0])
raise
try:
app.logger.info('Filling DB with categories')
with open('categories.csv', encoding='utf-8') as f:
# assume first line is header
cf = csv.DictReader(f, delimiter=',')
for row in cf:
parent_id = row.get('Parent_id')
db.session.add(
StockProductCategory(
id=int(row.get('Id')),
parent_id=int(parent_id) if parent_id != '' else None,
name=row.get('Name'),
prio=row.get('Prio')
)
)
db.session.commit()
app.logger.info('Filling DB with products')
with open('products.csv', encoding='utf-8') as f:
# assume first line is header
cf = csv.DictReader(f, delimiter=',')
for row in cf:
db.session.add(
StockProduct(
id=int(row.get('Id')),
category_id=int(row.get('Category_id')),
name=row.get('Name'),
volume=row.get('Volume'),
sum_amounts=row.get('Sum_amounts')
)
)
db.session.commit()
except:
app.logger.exception('Exception occured during installing data',
sys.exc_info()[0])
raise