-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmete_import.py
95 lines (79 loc) · 2.12 KB
/
mete_import.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
"""
Import script from mete to frauk
you need to delete the frauk database manually
then you need to call:
$ python mete_import.py /path/to/mete.db
and if the script does not explode, you should now have everything you need.
"""
import sqlite3
import sys
import datetime
from backend import db
from backend.user.model import User
from backend.product.model import Product
from backend.audit.model import Audit
db.create_all()
con = sqlite3.connect(sys.argv[1])
cur = con.cursor()
res = cur.execute('''SELECT
id,
name,
email,
created_at,
updated_at,
balance,
active,
audit FROM users;''')
rows = res.fetchall()
for row in rows:
user = User()
user.id = row[0]
user.name = row[1]
user.email = row[2]
user.created_at = datetime.datetime.strptime(row[3], "%Y-%m-%d %H:%M:%S.%f")
user.updated_at = datetime.datetime.strptime(row[4], "%Y-%m-%d %H:%M:%S.%f")
user.balance = int(row[5] * 100)
user.active = row[6]
user.audit = (row[7] == 't')
db.session.add(user)
db.session.commit()
res = cur.execute('''SELECT
id,
name,
bottle_size,
caffeine,
price,
logo_file_name,
created_at,
updated_at,
logo_content_type,
logo_file_size,
logo_updated_at FROM drinks;''')
rows = res.fetchall()
for row in rows:
product = Product()
product.id = row[0]
product.name = row[1]
product.bottle_size_l = row[2]
product.caffeine_mg = row[3]
product.price = int(row[4] * 100)
product.created_at = datetime.datetime.strptime(row[6], "%Y-%m-%d %H:%M:%S.%f")
product.updated_at = datetime.datetime.strptime(row[7], "%Y-%m-%d %H:%M:%S.%f")
db.session.add(product)
db.session.commit()
res = cur.execute('''
SELECT
id, created_at, difference, drink, user from audits;
''')
rows = res.fetchall()
for row in rows:
audit = Audit()
uid = row[4]
if not uid: uid = 0
audit.id = row[0]
audit.created_at = datetime.datetime.strptime(row[1], "%Y-%m-%d %H:%M:%S.%f")
audit.difference = row[2] * 100
audit.product_id = row[3]
audit.user_id = uid
db.session.add(audit)
db.session.commit()