-
Notifications
You must be signed in to change notification settings - Fork 1
/
data_alchemy.py
105 lines (90 loc) · 3.72 KB
/
data_alchemy.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
from base import Session, engine, Base
import kazoo_put as kp
from user import User
from device import Device
from account import Account
from datetime import datetime
from call import Call
def insert_data(item_type, account_id, item_id, auth, **users):
Base.metadata.create_all(engine)
session = Session()
# def __init__(self, id_id, name, create_date, no_devices, email, account):
def create_user():
item_user = kp.get_items(item_type=item_type, account_id=account_id, item_id=item_id, auth=auth)
print('in data_alchemy', item_user)
data = item_user['data']
if data.get('priv_level') == 'admin':
user_name = 'admin'
email = '[email protected]'
else:
user_name = data['caller_id']['internal']['name']
email = data.get('email')
account = session.query(Account).get(account_id)
user = User(item_id, user_name, datetime.now(), 1, email, account)
session.add(user)
# def __init__(self, id_1, name, device_type):
def create_device():
item_device = kp.get_items(item_type=item_type, account_id=account_id, item_id=item_id, auth=auth)
print(item_device)
data = item_device['data']
device_type = data.get('device_type')
owner_id = data.get('owner_id')
name = data.get('name')
device = Device(item_id, name, device_type)
user = session.query(User).get(owner_id)
device.user = [user]
session.add(device)
# def __init__(self, id_1, name, create_date):
def create_account():
item_account = kp.get_items(item_type=item_type, account_id=account_id, item_id=item_id, auth=auth)
print(item_account)
data = item_account['data']
account_name = data.get('name')
account = Account(item_id, account_name, datetime.now())
session.add(account)
# def __init__(self, id_i, create_date, to, from_m, duration):
def create_call():
print('inserting calls')
outbound = users.get('outbound')
print(outbound)
inbound = users.get('inbound')
print(inbound)
to_name = users.get('callee')
from_name = users.get('caller')
duration = users.get('duration')
if outbound is not None:
p = session.query(Call.id).filter(Call.id == item_id)
if not session.query(p.exists()).scalar():
to = session.query(User).get(users.get('outbound'))
call = Call(item_id, datetime.now(), to_name, from_name, duration)
call.user = [to]
session.add(call)
else:
to = session.query(User).get(users.get('outbound'))
call = session.query(Call).get(item_id)
call.user.append(to)
elif inbound is not None:
p = session.query(Call.id).filter(Call.id == item_id)
if not session.query(p.exists()).scalar():
from_u = session.query(User).get(users.get('inbound'))
call = Call(item_id, datetime.now(), to_name, from_name, duration)
call.user = [from_u]
session.add(call)
else:
from_u = session.query(User).get(users.get('inbound'))
call = session.query(Call).get(item_id)
call.user.append(from_u)
if item_type == 'user':
print('inserting users')
q = session.query(User.id).filter(User.id == item_id)
print(q)
if not session.query(q.exists()).scalar():
create_user()
if item_type == 'account':
create_account()
if item_type == 'device':
create_device()
if item_type == 'call':
create_call()
session.commit()
session.close()