-
Notifications
You must be signed in to change notification settings - Fork 0
/
migrate-items.py
88 lines (64 loc) · 1.78 KB
/
migrate-items.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
from dbfpy import dbf
import os, sys
os.environ["DJANGO_SETTINGS_MODULE"] = "settings"
from django.db import models
from django.core.management import call_command
from newspaper.models import Item
from newspaper.models import Price
import datetime
def saveItem(item):
itemObj = Item()
itemObj.name = item['DIENST']
itemObj.description = unicode(item['OMSCHR'], errors='replace')
itemObj.days = getDaysItem(item, itemObj)
itemObj.freq = getFreq(item)
itemObj.save()
savePrices(item, itemObj.id)
def savePrices(item, itemId):
enddate = '2999-12-31'
if item['PRIJS2'] > 0:
print "Newer price"
price = float(item['PRIJS2'])/100
begindate = item['PRIJS2DA']
savePrice(itemId, price, begindate, enddate)
enddate = item['PRIJS2DA']
delta = datetime.timedelta(days=1)
enddate = enddate - delta
begindate = '1990-01-01'
price = float(item['PRIJS'])/100
savePrice(itemId, price, begindate, enddate)
def savePrice(itemId, price, begindate, enddate):
priceObj = Price()
priceObj.item_id = itemId
priceObj.price = price
priceObj.begindate = begindate
priceObj.enddate = enddate
priceObj.save()
def getDaysItem(item, itemObj):
days = 0
if item['FREKW'] == 'D':
for i in range(2, 7):
if item['LEV' + str(i)] == 'Y':
days = days + itemObj.getDayBit(i-2)
return days
elif item['FREKW'] == 'W':
return itemObj.getDayBitByDate(item['LEVOP'])
return days
def getFreq(item):
if item['FREKW'] == 'D':
return 1
elif item['FREKW'] == 'W':
return 2
elif item['FREKW'] == 'V':
return 3
elif item['FREKW'] == 'M':
return 4
else:
print item
return 0
db = dbf.Dbf("migrate/KR_DIENS.DBF")
print "Migrate items"
print "============="
Item.objects.all().delete()
for item in db:
saveItem(item)