This repository has been archived by the owner on Mar 8, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
import-people.py
68 lines (61 loc) · 1.9 KB
/
import-people.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
#!/usr/bin/env python
import json
import os
import re
import sys
sys.path.insert(0, os.path.join(os.path.dirname(__file__), '..'))
os.environ['DJANGO_SETTINGS_MODULE'] = 'sayit_philadelphia.settings'
from speeches.models import Speaker
from popolo.models import Organization, Membership
from instances.models import Instance
data = json.load(open(sys.argv[1]))
instance = Instance.objects.get(label='default')
for o in data['organizations']:
org, created = Organization.objects.get_or_create(
identifiers__identifier=o['id'],
defaults={
'name': o['name'],
'classification': o['classification'],
}
)
if created:
org.identifiers.create(
identifier=o['id'],
scheme='JSON',
)
org.links.create(
url=o['links'][0]['url'],
note=o['links'][0]['note'],
)
for person in data['persons']:
spkr, created = Speaker.objects.get_or_create(
instance=instance,
identifiers__identifier=person['id'],
defaults={
'name': person['name'],
'family_name': person['family_name']
}
)
if created:
spkr.identifiers.create(
identifier=person['id'],
scheme='JSON',
)
spkr.links.create(
url=person['links'][0]['url'],
note=person['links'][0]['note'],
)
for mship in data['memberships']:
person = Speaker.objects.get(identifiers__identifier=mship['person_id'])
org = Organization.objects.get(identifiers__identifier=mship['organization_id'])
data = {
'label': mship['label'],
'role': mship['role'],
'person': person,
'organization': org,
}
if 'start_date' in mship:
data['start_date'] = mship['start_date']
if 'end_date' in mship:
data['end_date'] = mship['end_date']
Membership.objects.get_or_create(**data)