-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpopulatedb.py
More file actions
430 lines (371 loc) · 12.7 KB
/
populatedb.py
File metadata and controls
430 lines (371 loc) · 12.7 KB
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
from pyramid.paster import (
get_appsettings,
setup_logging,
)
from server.models import (
DBSession,
Base,
Node,
Project,
BudgetGroup,
BudgetItem,
Component,
ComponentType,
)
from sqlalchemy import (
Column,
Index,
Integer,
Text,
ForeignKey,
)
from random import randint
import sys
import transaction
from sqlalchemy import exc
from sqlalchemy.sql import exists
from pyramid.scripts.common import parse_vars
from sqlalchemy import engine_from_config
from sqlalchemy.orm import sessionmaker
import xlrd
import uuid
import os
from sqlalchemy import exc
# delete the database
try:
os.remove("server.sqlite")
except OSError, o:
pass
config_uri = 'development.ini'
options = {}
settings = {'pyramid.includes': '\npyramid_debugtoolbar\npyramid_tm',
'sqlalchemy.url': 'sqlite:////home/niel/projects/optimatesql/optimatesql/server.sqlite',
'__file__': '/home/niel/projects/optimatesql/optimatesql/development.ini',
'pyramid.default_locale_name': 'en', 'pyramid.reload_templates': 'true',
'here': '/home/niel/projects/optimatesql/optimatesql',
'pyramid.debug_notfound': 'false', 'pyramid.debug_routematch': 'false',
'pyramid.debug_authorization': 'false'}
engine = engine_from_config(settings, 'sqlalchemy.')
session = DBSession
session.configure(bind=engine)
Base.metadata.create_all(engine)
# session.expire_on_commit = False
with transaction.manager:
"""
Open and read an Excel file
"""
# project = Project(ID=1, Name="test", Description="Desc", ParentID=0)
# bg = BudgetGroup(ID=2, Name="testbg", Description="bgdesc", ParentID=1)
# session.add(project)
# session.add(bg)
# qry = session.query(Node).all()
# for n in qry:
# print n
# print session.query(BudgetGroup).filter_by(ID=2).update({'ID': 4})
# transaction.commit()
# qry = session.query(Node).all()
# for n in qry:
# print n
root = Node(ID=0)
session.add(root)
projectbook = xlrd.open_workbook("/home/niel/projects/exceldata/Projects.xls")
sheet = projectbook.sheet_by_index(0)
codeindex = 0
nameindex = 1
descriptionindex = 2
budgetcostindex = 12
ordercostindex = 13
claimedcostindex = 15
print "Converting Project table"
# # build the projects
for x in range (1, sheet.nrows):
code = int(sheet.cell(x,codeindex).value)
name = sheet.cell(x, nameindex).value
description = sheet.cell(x, descriptionindex).value
try:
budgetcost = float(sheet.cell(x, budgetcostindex).value)
except ValueError, e:
budgetcost = 0
try:
ordercost = float(sheet.cell(x, ordercostindex).value)
except ValueError, e:
ordercost = 0
try:
claimedcost = float(sheet.cell(x, claimedcostindex).value)
except ValueError, e:
claimedcost = 0
project = Project(ID=code, Name=name,
Description=description,
ParentID=0)
session.add(project)
project.Total=budgetcost
project.Ordered=ordercost
project.Claimed=claimedcost
transaction.commit()
#build the budgetgroups
budgetgroupbook = xlrd.open_workbook("/home/niel/projects/exceldata/BudgetGroups.xls")
sheet = budgetgroupbook.sheet_by_index(0)
codeindex = 0
nameindex = 1
parentindex = 2
descriptionindex = 3
budgetcostindex = 4
ordercostindex = 5
claimedcostindex = 7
newcode = 150000
changedbgcodes = {}
# correct negative codes and circular dependancies
for x in range (1, sheet.nrows):
code = int(sheet.cell(x,codeindex).value)
try:
pid = int(sheet.cell(x,parentindex).value)
except ValueError, e:
pid = 0
if pid<0:
pid = -pid
if pid not in changedbgcodes:
newcode+=1
changedbgcodes[pid] = newcode
if code == pid:
changedbgcodes[pid] = 0
print "Converting BudgetGroups table"
# build the budgetgroups
for x in range (1, sheet.nrows):
code = int(sheet.cell(x,codeindex).value)
name = sheet.cell(x, nameindex).value
description = sheet.cell(x, descriptionindex).value
try:
budgetcost = float(sheet.cell(x, budgetcostindex).value)
except ValueError, e:
budgetcost = 0
try:
ordercost = float(sheet.cell(x, ordercostindex).value)
except ValueError, e:
ordercost = 0
try:
claimedcost = float(sheet.cell(x, claimedcostindex).value)
except ValueError, e:
claimedcost = 0
try:
parentcode = int(sheet.cell(x,parentindex).value)
except ValueError, e:
parentcode = 0
# if the code has been changed assign it here
if code in changedbgcodes.keys():
if changedbgcodes[code] != 0:
code = changedbgcodes[code]
# if it is negative it refers to a parent in the same table
if parentcode < 0:
parentcode = abs(parentcode)
parentcode = changedbgcodes[parentcode]
bg = BudgetGroup(ID=code,
Name=name,
Description=description,
ParentID=parentcode)
session.add(bg)
bg.Total=budgetcost
bg.Ordered=ordercost
bg.Claimed=claimedcost
transaction.commit()
#build the budgetitems
budgetitembook = xlrd.open_workbook("/home/niel/projects/exceldata/BudgetItems.xls")
sheet = budgetitembook.sheet_by_index(0)
codeindex = 0
nameindex = 1
parentindex = 2
descriptionindex = 3
quantityindex = 13
rateindex = 14
unitindex = 17
budgetcostindex = 5
ordercostindex = 6
claimedcostindex = 9
changedbicodes = {}
# correct negative codes and circular dependancies
for x in range (1, sheet.nrows):
code = int(sheet.cell(x,codeindex).value)
try:
pid = int(sheet.cell(x,parentindex).value)
except ValueError, e:
pid = 0
if pid<0:
pid = -pid
if pid not in changedbicodes:
newcode+=1
changedbicodes[pid] = newcode
if code == pid:
changedbicodes[pid] = 0
if code in changedbgcodes.keys():
newcode+=1
changedbicodes[code] = newcode
if code == pid:
changedbicodes[pid] = 0
if session.query(Node).filter_by(ID=code).first():
newcode+=1
changedbicodes[code] = newcode
print "Converting Budgetitems table"
# build the budgetitems
for x in range (1, sheet.nrows):
code = int(sheet.cell(x,codeindex).value)
name = sheet.cell(x, nameindex).value
description = sheet.cell(x, descriptionindex).value
measureunit = sheet.cell(x, unitindex).value
try:
claimedcost = float(sheet.cell(x, claimedcostindex).value)
except ValueError, e:
claimedcost = 0
try:
ordercost = float(sheet.cell(x, ordercostindex).value)
except ValueError, e:
ordercost = 0
try:
parentcode = int(sheet.cell(x,parentindex).value)
except ValueError, e:
parentcode = 0
try:
budgetcost = float(sheet.cell(x, budgetcostindex).value)
except ValueError, e:
budgetcost = 0
try:
quantity = float(sheet.cell(x,quantityindex).value)
except ValueError, e:
quantity = 0
try:
rate = float(sheet.cell(x,rateindex).value)
except ValueError, e:
rate = 0
# if the code has been changed assign it here
if code in changedbicodes.keys():
if changedbicodes[code] != 0:
code = changedbicodes[code]
# if it is negative it refers to a parent in the same table
if parentcode < 0:
parentcode = abs(parentcode)
parentcode = changedbicodes[parentcode]
# otherwise check if the parent code has changed
elif parentcode in changedbgcodes:
parentcode = changedbgcodes[parentcode]
bi = BudgetItem(ID=code, Name=name,
Description=description,
ParentID=parentcode,
Unit=measureunit)
session.add(bi)
bi.Total=budgetcost
bi.Ordered=ordercost
bi.Claimed=claimedcost
bi.Quantity=quantity
bi.Rate=rate
transaction.commit()
#build the components
componentbook = xlrd.open_workbook("/home/niel/projects/exceldata/Components.xls")
sheet = componentbook.sheet_by_index(0)
codeindex = 0
nameindex = 1
parentindex = 2
descriptionindex = 3
typeindex = 4
rateindex = 14
quantityindex = 13
unitindex = 19
budgetcostindex = 5
ordercostindex = 6
claimedcostindex = 8
# change the codes to unique ID's
changedcocodes = {}
# correct negative codes and circular dependancies
for x in range (1, sheet.nrows):
code = int(sheet.cell(x,codeindex).value)
try:
pid = int(sheet.cell(x,parentindex).value)
except ValueError, e:
pid = 0
if pid<0:
pid = -pid
if pid not in changedcocodes:
newcode+=1
changedcocodes[pid] = newcode
if code == pid:
changedcocodes[pid] = 0
if code in changedbgcodes.keys():
newcode+=1
changedcocodes[code] = newcode
if code == pid:
changedcocodes[pid] = 0
elif code in changedbicodes.keys():
newcode+=1
changedcocodes[code] = newcode
if code == pid:
changedcocodes[pid] = 0
if session.query(Node).filter_by(ID=code).first():
newcode+=1
changedcocodes[code] = newcode
print "Converting Components table"
# build the components
for x in range (1, sheet.nrows):
code = int(sheet.cell(x,codeindex).value)
name = sheet.cell(x, nameindex).value
description = sheet.cell(x, descriptionindex).value
cotype = sheet.cell(x, typeindex).value
measureunit = sheet.cell(x, unitindex).value
try:
budgetcost = float(sheet.cell(x, budgetcostindex).value)
except ValueError, e:
budgetcost = 0
try:
ordercost = float(sheet.cell(x, ordercostindex).value)
except ValueError, e:
ordercost = 0
try:
claimedcost = float(sheet.cell(x, claimedcostindex).value)
except ValueError, e:
claimedcost = 0
try:
parentcode = int(sheet.cell(x,parentindex).value)
except ValueError, e:
parentcode = 0
try:
quantity = float(sheet.cell(x,quantityindex).value)
except ValueError, e:
quantity = 0
try:
rate = float(sheet.cell(x,rateindex).value)
except ValueError, e:
rate = 0
# if the code has been changed assign it here
if code in changedcocodes.keys():
if changedcocodes[code] != 0:
code = changedcocodes[code]
# if it is negative it refers to a parent in the same table
if parentcode < 0:
parentcode = abs(parentcode)
parentcode = changedcocodes[parentcode]
# otherwise check if the parent code has changed
elif parentcode in changedbicodes:
parentcode = changedbicodes[parentcode]
elif parentcode in changedbgcodes:
parentcode = changedbgcodes[parentcode]
co = Component(ID=code, Name=name,
Description=description,
Type=cotype,
Unit=measureunit,
ParentID=parentcode)
session.add(co)
co.Total=budgetcost
co.Ordered=ordercost
co.Claimed=claimedcost
co.Quantity=quantity
co.Rate=rate
transaction.commit()
cotypebook = xlrd.open_workbook("/home/niel/projects/exceldata/CompTypes.xls")
sheet = cotypebook.sheet_by_index(0)
codeindex = 0
nameindex = 1
print "Converting Component Type table"
# # build the componenttypes
for x in range (1, sheet.nrows):
code = int(sheet.cell(x,codeindex).value)
name = sheet.cell(x, nameindex).value
cotype = ComponentType(ID=code, Name=name)
session.add(cotype)
transaction.commit()
print "done"