-
Notifications
You must be signed in to change notification settings - Fork 27
/
import_lookup_values.py
132 lines (113 loc) · 4.94 KB
/
import_lookup_values.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
import json
import os
from django.core.management import BaseCommand
from core.common.constants import HEAD
from core.concepts.models import Concept
from core.orgs.models import Organization
from core.sources.models import Source
from core.users.models import UserProfile
class Command(BaseCommand):
help = 'import lookup values'
def handle(self, *args, **options):
ocladmin = UserProfile.objects.filter(username='ocladmin').get()
org_OCL = Organization.objects.get(mnemonic='OCL')
org_ISO = Organization.objects.filter(mnemonic='ISO').first()
if not org_ISO:
org_ISO = Organization(name='International Organization for Standardization (ISO)', mnemonic='ISO')
org_ISO.save()
org_ISO.members.add(ocladmin)
sources = self.get_or_create(org_OCL, ocladmin)
iso_source = self.get_or_create_iso_source(org_ISO, ocladmin)
current_path = os.path.dirname(__file__)
importer_confs = [
{
'source': sources['Classes'],
'file': os.path.join(current_path, "../../../lookup_fixtures/concept_classes.json")
},
{
'source': sources['Locales'],
'file': os.path.join(current_path, "../../../lookup_fixtures/locales.json")
},
{
'source': sources['Datatypes'],
'file': os.path.join(current_path, "../../../lookup_fixtures/datatypes_fixed.json")
},
{
'source': sources['NameTypes'],
'file': os.path.join(current_path, "../../../lookup_fixtures/nametypes_fixed.json")
},
{
'source': sources['DescriptionTypes'],
'file': os.path.join(current_path, "../../../lookup_fixtures/description_types.json")
},
{
'source': sources['MapTypes'],
'file': os.path.join(current_path, "../../../lookup_fixtures/maptypes_fixed.json")
},
]
for conf in importer_confs:
source = conf['source']
self.create_concepts(source, conf['file'], ocladmin)
self.create_concepts(
iso_source, os.path.join(current_path, "../../../lookup_fixtures/iso_639_1_locales.json"), ocladmin
)
@staticmethod
def get_or_create(org, user):
sources = {}
kwargs = {
'parent_resource': org
}
for source_name in ['Locales', 'Classes', 'Datatypes', 'DescriptionTypes', 'NameTypes', 'MapTypes']:
if Source.objects.filter(organization=org, mnemonic=source_name, version=HEAD).exists():
source = Source.objects.get(organization=org, mnemonic=source_name, version=HEAD)
else:
source = Source(
name=source_name, mnemonic=source_name, full_name=source_name, organization=org, created_by=user,
default_locale='en', supported_locales=['en'], updated_by=user, version=HEAD
)
Source.persist_new(source, user, **kwargs)
sources[source_name] = source
return sources
@staticmethod
def get_or_create_iso_source(org, user):
ISO_SOURCE_ID = 'iso639-1'
kwargs = {
'parent_resource': org
}
source = Source.objects.filter(organization=org, mnemonic='iso639-1').first()
if not source:
source = Source(
name='Iso6391',
mnemonic=ISO_SOURCE_ID,
full_name='ISO 639-1: Codes for the representation of names of languages -- Part 1: Alpha-2 code',
description='Codes for the Representation of Names of Languages Part 1: Alpha-2 Code.'
' Used as part of the IETF 3066 specification for languages '
'throughout the HL7 specification.',
canonical_url='http://terminology.hl7.org/CodeSystem/iso639-1',
organization=org,
default_locale='en',
version=HEAD,
created_by=user,
updated_by=user,
active_mappings=0,
)
Source.persist_new(source, user, **kwargs)
return source
@staticmethod
def create_concepts(source, file, user):
file = open(file, 'r')
lines = file.readlines()
created = False
for line in lines:
data = json.loads(line)
mnemonic = data.pop('id', None)
if not Concept.objects.filter(parent=source, mnemonic=mnemonic, is_latest_version=True).exists():
data['mnemonic'] = mnemonic
data['parent'] = source
Concept.persist_new(data, user)
if not created:
created = True
if created:
source.active_concepts = source.concepts_set.filter(is_latest_version=True).count()
source.save()
return created