forked from heroku/python-getting-started
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathload_test_data.py
57 lines (40 loc) · 1.95 KB
/
load_test_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
import importlib
import csv
def run():
def clean(string):
newString = ''
for c in string:
if 64 < ord(c) < 123 or c == ' ':
newString += c
return newString
hello_models = importlib.import_module('hello.models')
with open('test-data/create-instructions.txt') as instructions:
for entry in instructions:
file, model_name = entry.rstrip().split(' ')
constructor = getattr(hello_models, model_name)
try:
with open(file) as csvfile:
reader = csv.reader(csvfile, delimiter=';')
header = next(reader)
header = [clean(entry) for entry in header]
def make_kwargs(values):
tuple = {}
assert len(values) == len(header), [header, file]
for attribute_name, attribute_value in zip(header, values):
if attribute_name.startswith('ref'):
assert len(attribute_name.split(' ')) == 3, [attribute_name, header, file]
attrName, refName = attribute_name.split(' ')[1:]
refModelClass = getattr(hello_models, refName)
refGetter = getattr(getattr(refModelClass, 'objects'), 'get')
tuple[attrName] = refGetter(pk=attribute_value)
else:
tuple[attribute_name] = attribute_value
return tuple
for data_row in reader:
kwargs = make_kwargs(data_row)
instance = constructor(**kwargs)
instance.save()
except Exception:
print(file)
print(entry)
raise Exception