-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdatascript.py
149 lines (128 loc) · 4.5 KB
/
datascript.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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
import os
from openpyxl import load_workbook
from openpyxl.workbook import Workbook
from openpyxl.compat import range
from openpyxl.cell import get_column_letter
from jllPortal import init_db
from array import *
import sqlite3
# configuration
DATA_FOLDER = os.path.join(os.getcwd(), 'data')
DATABASE = os.path.join(os.getcwd(), 'jll.db')
def connect_db():
return sqlite3.connect(DATABASE)
def get_filepath(filename):
return os.path.join(DATA_FOLDER, filename)
def insert_into(table, fields, values, data):
cur.execute('insert into '+table+' '+fields+' values '+values, data)
cur.execute('select max(id) FROM '+table)
return cur.fetchone()[0]
def read_from(filename, sheet):
wb = load_workbook(filename = get_filepath(filename), read_only=True)
return wb[sheet]
def write_to(filename):
return load_workbook(filename=get_filepath(filename))
def create(table, fields, values, worksheet, header):
ids = array('i')
for row in worksheet.rows:
data = []
if row[0].value != header:
for cell in row:
data.append(cell.value)
ids.append(insert_into(table, fields, values, data))
return ids
def write_column(worksheet, data, column, rowOffset):
for i, entry in enumerate(data):
worksheet.cell(row=i+rowOffset, column=column).value = entry
def create_clients():
table = 'client'
fields = '(name, imgFilename, industry)'
values = '(?, ?, ?)'
ws = read_from('Company.xlsx', 'Sheet1')
return create(table, fields, values, ws, 'name')
def create_client_contacts(clientIds):
table = 'clientContact'
fields = '(firstName, lastName, email, phone, address, clientId)'
values = '(?,?,?,?,?,?)'
wb = write_to('CompanyContact.xlsx')
ws = wb['Sheet1']
write_column(ws, clientIds, 6, 2)
wb.save(get_filepath('CompanyContact.xlsx'))
return create(table, fields, values, ws, 'firstName')
def create_client_requirements(clientIds):
table = 'clientRequirements'
fields = '(RSF, budget, employees, market, clientId)'
values = '(?,?,?,?,?)'
wb = write_to('CompanyRequirements.xlsx')
ws = wb['Sheet1']
write_column(ws, clientIds, 5, 2)
wb.save(get_filepath('CompanyRequirements.xlsx'))
return create(table, fields, values, ws, 'RSF')
def create_property():
table = 'property'
fields = '(name, imagepath, address, description, size, rent, employees, market )'
values = '(?,?,?,?,?,?,?,?)'
ws = read_from('Property.xlsx', 'Sheet1')
return create(table, fields, values, ws, 'name')
def create_client_property(clientIds, propertyIds):
table = 'client_property'
fields = '(clientId, propertyId, comment, rating, showing)'
values = '(?,?,?,?,?)'
wb = write_to('CompanyProperty.xlsx')
ws = wb['Sheet1']
i = 0
for clientId in clientIds:
for propertyId in propertyIds:
ws.cell(row=i+2, column=1).value = clientId
ws.cell(row=i+2, column=2).value = propertyId
ws.cell(row=i+2, column=5).value = 0
i = i+1
wb.save(get_filepath('CompanyProperty.xlsx'))
return create(table, fields, values, ws, 'clientId')
conn = connect_db()
cur = conn.cursor()
init_db()
clientIds = create_clients()
create_client_contacts(clientIds)
create_client_requirements(clientIds)
propertyIds = create_property()
create_client_property(clientIds, propertyIds)
# Save (commit) the changes
conn.commit()
# We can also close the connection if we are done with it.
# Just be sure any changes have been committed or they will be lost.
conn.close()
def show_tables():
conn = connect_db()
cur = conn.cursor()
print '****************************************'
print '*****************Client*****************'
print '****************************************'
cur.execute('select * from client')
for row in cur.fetchall():
print row
print '****************************************'
print '*************Client Contact*************'
print '****************************************'
cur.execute('select * from clientContact')
for row in cur.fetchall():
print row
print '****************************************'
print '**********Client Requirements***********'
print '****************************************'
cur.execute('select * from clientRequirements')
for row in cur.fetchall():
print row
print '****************************************'
print '****************Property****************'
print '****************************************'
cur.execute('select * from property')
for row in cur.fetchall():
print row
print '****************************************'
print '************Client Property*************'
print '****************************************'
cur.execute('select * from client_property')
for row in cur.fetchall():
print row
conn.close()