forked from chb/indivo_server
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dump_documents.py
74 lines (56 loc) · 1.86 KB
/
dump_documents.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
"""
A script to dump documents for records created by
indivo-connector
"""
##
## DJANGO SETUP
##
from django.core.management import setup_environ
import settings, string
setup_environ(settings)
##
## constants
##
INFO = """<patient>
<last_name>%s</last_name>
<first_names>%s</first_names>
<mrn>%s</mrn>
<confirmation_code>%s</confirmation_code>
</patient>"""
##
## now the script
##
import sys, os
from indivo.models import *
from xml.etree import ElementTree
def dump_documents():
out_dir = sys.argv[1]
# go through the patients
p = Principal.objects.get(email='[email protected]')
records = Record.objects.filter(creator=p)
print "%s records" % len(records)
PREFIX = "http://indivo.org/vocab/xml/documents#"
for i, record in enumerate(records):
record_dir = out_dir + ("/patient_%s" % i)
# make the directory
os.mkdir(record_dir)
# we need basic info, first name, last name
contact = ElementTree.fromstring(record.contact.content)
last_name = contact.findtext("{%s}name/{%s}familyName" % (PREFIX,PREFIX))
first_name = contact.findtext("{%s}name/{%s}givenName" % (PREFIX,PREFIX))
# generate a random MRN
mrn = "%s-%s-%s" % (utils.random_string(3, choices=string.digits),
utils.random_string(3, choices=string.digits),
utils.random_string(3, choices=string.digits))
confirmation_code = utils.random_string(3, choices=string.digits)
info = INFO % (last_name, first_name, mrn, confirmation_code)
info_file = open(record_dir + "/info.xml", "w")
info_file.write(info)
info_file.close()
# go through documents
for i, doc in enumerate([r for r in record.documents.all()]):
doc_file = open(record_dir + "/doc_" + str(i), "w")
doc_file.write(doc.content)
doc_file.close()
if __name__ == '__main__':
dump_documents()