-
Notifications
You must be signed in to change notification settings - Fork 1
/
importa_data.py
executable file
·275 lines (255 loc) · 7.81 KB
/
importa_data.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
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
#!/usr/bin/env python
import sys, os
from json import dumps, loads, JSONEncoder
sys.path.append('/var/www/fusolab/')
os.environ['DJANGO_SETTINGS_MODULE'] = 'fusolab2_0.settings'
from django.contrib.auth.models import User, Group
from bar.models import *
from base.models import *
from ingresso.models import *
from registration.models import *
from reports.models import *
from bar.managers import *
from salutatore.models import *
import time
import iso8601
from copy import deepcopy
musers = {}
# USERS
def import_user():
print "Importo USER"
f = open('users.json', 'r')
for o in loads(f.read()):
u = User()
#u.pk = o['pk']
musers[o['pk']] = {'old_fields': o['fields']}
print "PK:", o['pk']
print "nome:", o['fields']['first_name'], o['fields']['last_name']
try:
g = Group.objects.get(name = "turnisti")
except:
g = None
if not g:
g = Group.objects.create(name = "turnisti")
g.save()
for k,v in o['fields'].items():
toadd = False
if (k == "is_superuser" or k == 'is_staff') and v == True:
v = False
toadd = True
#add user in turnisti
# problema con 3584
#if k== "user_permissions" and o['pk'] == 3584:
# v = []
print k,v
if k != 'groups' and k != "user_permissions":
setattr(u, k, v)
print "Aggiungo: " , u
if u.username == 'fusoci':
u.is_superuser = u.is_staff = True
if u.username != 'root':
#u.save()
u = User.objects.get(username = o['fields']['username'])
musers[o['pk']]['new_pk'] = u.pk
if toadd:
#u.groups.add(g)
#u.save()
pass
f.close()
muserprofile = {}
#USERPROFILES
def import_userprofile():
print "Importo USERPROFILE"
f = open('userprofiles.json', 'r')
for o in loads(f.read()):
print "pk: " , o['pk']
try:
u = UserProfile.objects.get(user__pk = musers[o['fields']['user']]['new_pk'])
except:
u = UserProfile()
for k,v in o['fields'].items():
if k == 'user':
#k = 'user_id'
v = User.objects.get(pk = musers[v]['new_pk'] )
setattr(u, k, v)
print "Aggiungo: " , u
#u.full_clean()
#u.save()
muserprofile[o['pk']] = u.pk
f.close()
#CARD
def import_card():
print "Importo CARD"
f = open('cards.json', 'r')
for o in loads(f.read()):
u = Card()
for k,v in o['fields'].items():
if k == 'user':
v = UserProfile.objects.get(pk = muserprofile[v])
#k = 'user_id'
setattr(u, k, v)
print "Aggiungo card: "# , u
#u.full_clean()
u.save()
f.close()
#GREETINGS
def import_greeting():
print "Importo GREETING"
f = open('greetings.json', 'r')
for o in loads(f.read()):
u = Greeting()
for k,v in o['fields'].items():
if k == 'user':
#k = 'user_id'
v = UserProfile.objects.get(pk = muserprofile[v])
setattr(u, k, v)
print "Aggiungo greeting: "# , u
#u.full_clean()
u.save()
f.close()
#PRODUCT
def import_product():
print "Importo PRODUCTS"
f = open('products.json', 'r')
for o in loads(f.read()):
u = Product()
for k,v in o['fields'].items():
if k != 'internal_cost':
setattr(u, k, v)
print "Aggiungo products: "# , u
#u.full_clean()
u.save()
f.close()
#RECEIPT
mreceipts = {}
def import_receipt():
print "Importo RECEIPT"
f = open('receipts.json', 'r')
for o in loads(f.read()):
u = Receipt()
for k,v in o['fields'].items():
if k == 'cashier':
v = UserProfile.objects.get(pk = muserprofile[v])
#k = 'cashier_id'
setattr(u, k, v)
print "Aggiungo purchased products: "# , u
#u.full_clean()
u.save()
mreceipts[o['pk']] = u.pk
f.close()
#PURCHASEDPRODUCT
def import_purchasedproduct():
print "Importo PURCHASEDPRODUCTS"
f = open('purchasedproducts.json', 'r')
for o in loads(f.read()):
u = PurchasedProduct()
for k,v in o['fields'].items():
if k == 'receipt':
v = Receipt.objects.get(pk = mreceipts[v])
#k = 'receipt_id'
setattr(u, k, v)
print "Aggiungo purchased products: "# , u
#u.full_clean()
u.save()
f.close()
#BarCashBalance
def import_barcashbalance():
print "Importo BARCASHBALANCE"
f = open('barcashbalances.json', 'r')
for o in loads(f.read()):
u = BarBalance()
print o
fields = o['fields']
#u.date = iso8601.parse_date(fields['date']) if type(fields['date']) is unicode else fields['date']
u.date = datetime.strptime(fields['date'], "%Y-%m-%d %H:%M:%S" ) #2012-11-10 02:44:51
u.date.replace(tzinfo=None)
u.cashier = UserProfile.objects.get(pk = muserprofile[fields['cashier']])
u.note = fields['note']
# opening
u.operation = OPENING #OPENING, CLOSING, PAYMENT, DEPOSIT, WITHDRAW, CASHPOINT
u.amount = fields['initial_cash']
u.save()
u.parent = u
u.save()
#withdraw
if fields['withdraw'] > 0:
u2 = deepcopy(u)
u2.operation = WITHDRAW
u2.pk = None
u2.parent = u
u2.amount = fields['withdraw']
u2.save()
#deposit
if fields['deposit'] >0:
u3 = deepcopy(u)
u3.operation = DEPOSIT
u3.pk = None
u3.parent = u
u3.amount = fields['deposit']
u3.save()
# closing
u4 = deepcopy(u)
u4.operation = CLOSING
u4.pk = None
u4.parent = u
u4.amount = fields['final_cash']
u4.save()
print "Aggiungo bar balance : "# , u
f.close()
#EntranceCashBalance
def import_entrancecashbalance():
f = open('entrancecashbalances.json', 'r')
for o in loads(f.read()):
u = EntranceBalance()
fields = o['fields']
u.date = datetime.strptime(fields['date'], "%Y-%m-%d %H:%M:%S" ) #2012-11-10 02:44:51
u.date.replace(tzinfo=None)
u.cashier = UserProfile.objects.get(pk = muserprofile[fields['cashier']])
u.note = fields['note']
# opening
u.operation = OPENING #OPENING, CLOSING, PAYMENT, DEPOSIT, WITHDRAW, CASHPOINT
u.amount = fields['initial_cash']
u.save()
u.parent = u
u.save()
#withdraw
if fields['withdraw'] > 0:
u2 = deepcopy(u)
u2.operation = WITHDRAW
u2.pk = None
u2.parent = u
u2.amount = fields['withdraw']
u2.save()
#deposit
if fields['deposit'] >0:
u3 = deepcopy(u)
u3.operation = DEPOSIT
u3.pk = None
u3.parent = u
u3.amount = fields['deposit']
u3.save()
# closing
u4 = deepcopy(u)
u4.operation = CLOSING
u4.pk = None
u4.parent = u
u4.amount = fields['final_cash']
u4.save()
print "Aggiungo entrance balance : "# , u
#u.full_clean()
#u.save()
f.close()
def main():
import_user()
import_userprofile()
#import_card()
#import_product()
#import_greeting()
#import_receipt()
#import_purchasedproduct()
# problemi sul parent dei primi valori
import_barcashbalance()
import_entrancecashbalance()
if __name__ =='__main__':main()
#TODO ripristinare add now in receipt
# ripristinare email tesoriere