forked from IATI/CSV2IATI
-
Notifications
You must be signed in to change notification settings - Fork 0
/
migrate_transactions.py
27 lines (23 loc) · 965 Bytes
/
migrate_transactions.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
import json
from csviatimodeleditor import IATIModel, db
import sys
import collections
dec = json.JSONDecoder(object_pairs_hook=collections.OrderedDict).decode
for m in IATIModel.query.all():
if m.model_content:
model = dec(m.model_content)
for k,v in model['mapping'].items():
if v['datatype'] == 'transaction':
v['datatype'] = 'compound'
v['fields'] = v['tdatafields']
del v['tdatafields']
if 'transaction_type' in v['fields']:
v['fields']['transaction-type'] = v['fields']['transaction_type']
del v['fields']['transaction_type']
for k,field in v['fields'].items():
assert k == field['iati-field']
del field['iati-field']
field['datatype'] = 'compound'
m.model_content = json.dumps(model)
db.session.add(m)
db.session.commit()