-
Notifications
You must be signed in to change notification settings - Fork 4
/
mass_import.py
56 lines (46 loc) · 1.52 KB
/
mass_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
import csv
from dracobot2.config import SessionLocal
from dracobot2.models import User, UserDetails
session = SessionLocal()
f = open("import.csv")
f_reader = csv.reader(f)
header = next(f_reader)
def get_row_info(cur_row):
index = int(cur_row[0])
name = cur_row[1]
handle = cur_row[2]
likes = cur_row[3]
dislikes = cur_row[4]
room_number = cur_row[5]
requests = cur_row[6]
level = int(cur_row[7])
dragon_no = int(cur_row[8])
return {
'index': index,
'name': name,
'handle': handle,
'likes': likes,
'dislikes': dislikes,
'room_number': room_number,
'requests': requests,
'level': level,
'dragon_no': dragon_no,
}
users_list = {}
for row in f_reader:
user_obj = get_row_info(row)
user_db = User(id=user_obj['index'], tele_handle=user_obj['handle'])
user_details_db = UserDetails(user=user_db,
name=user_obj['name'],
likes=user_obj['likes'],
dislikes=user_obj['dislikes'],
room_number=user_obj['room_number'],
requests=user_obj['requests'],
level=user_obj['level'])
users_list[user_obj['index']] = (user_db, user_obj['dragon_no'])
session.add(user_details_db)
session.commit()
for user_id in users_list:
user_db, dragon_id = users_list[user_id]
user_db.dragon = users_list[dragon_id][0]
session.commit()