1
|
#!/usr/bin/env python
|
2
|
|
3
|
import os
|
4
|
import csv
|
5
|
|
6
|
from datetime import datetime, time
|
7
|
|
8
|
import calebasse.settings
|
9
|
import django.core.management
|
10
|
|
11
|
django.core.management.setup_environ(calebasse.settings)
|
12
|
|
13
|
from django.contrib.auth.models import User
|
14
|
|
15
|
#from calebasse.actes.models import EventAct
|
16
|
from calebasse.agenda.models import Event, EventType
|
17
|
from calebasse.dossiers.models import PatientRecord, Status, FileState
|
18
|
from calebasse.ressources.models import Service
|
19
|
from calebasse.personnes.models import Worker, Holiday
|
20
|
from calebasse.ressources.models import WorkerType
|
21
|
|
22
|
# Configuration
|
23
|
db_path = "/home/jschneider/apps/calebasse/scripts/20130104-213225/"
|
24
|
|
25
|
dbs = ["F_ST_ETIENNE_SESSAD_TED", "F_ST_ETIENNE_CMPP", "F_ST_ETIENNE_CAMSP", "F_ST_ETIENNE_SESSAD"]
|
26
|
tables = ["rs", "rr"]
|
27
|
|
28
|
|
29
|
# Global mappers. This dicts are used to map a Faure id with a calebasse object.
|
30
|
dossiers = {}
|
31
|
|
32
|
def _to_date(str_date):
|
33
|
if not str_date:
|
34
|
return None
|
35
|
return datetime.strptime(str_date[:-13], "%Y-%m-%d")
|
36
|
|
37
|
def _to_int(str_int):
|
38
|
if not str_int:
|
39
|
return None
|
40
|
return int(str_int)
|
41
|
|
42
|
def discipline_mapper(tables_data, service):
|
43
|
for line in tables_data['discipline']:
|
44
|
# Insert workertype
|
45
|
if not WorkerType.objects.filter(name=line['libelle']):
|
46
|
WorkerType.objects.create(name=line['libelle'])
|
47
|
|
48
|
|
49
|
def intervenants_mapper(tables_data, service):
|
50
|
for line in tables_data['intervenants']:
|
51
|
# Insert workers
|
52
|
for disp in tables_data['discipline']:
|
53
|
if disp['id'] == line['discipline']:
|
54
|
type = WorkerType.objects.get(name=disp['libelle'])
|
55
|
# TODO : import actif or not
|
56
|
worker, created = Worker.objects.get_or_create(
|
57
|
type=type,
|
58
|
last_name=line['nom'],
|
59
|
first_name=line['prenom'],
|
60
|
email=line['email'],
|
61
|
phone=line['tel'],
|
62
|
gender=int(line['titre']),
|
63
|
)
|
64
|
worker.services.add(service)
|
65
|
|
66
|
def dossiers_mapper(tables_data, service):
|
67
|
global dossiers
|
68
|
for line in tables_data['dossiers']:
|
69
|
status = Status.objects.filter(type="ACCUEIL").filter(services=service)
|
70
|
creator = User.objects.get(id=1)
|
71
|
gender = _to_int(line['nais_sexe'])
|
72
|
if gender == 0:
|
73
|
gender = None
|
74
|
# TODO: add more fields
|
75
|
patient, created = PatientRecord.objects.get_or_create(first_name=line['prenom'],
|
76
|
last_name=line['nom'], birthdate=_to_date(line['nais_date']),
|
77
|
twinning_rank=_to_int(line['nais_rang']),
|
78
|
gender=gender, service=service, creator=creator)
|
79
|
dossiers[line['id']] = patient
|
80
|
|
81
|
if not created:
|
82
|
if not line['ins_date']:
|
83
|
# Hack when there is no inscription date put 01/01/1970
|
84
|
line['ins_date'] = "1970-01-01 00:00:00.000"
|
85
|
fs = FileState.objects.create(status=status[0], author=creator,
|
86
|
date_selected=_to_date(line['ins_date']),
|
87
|
previous_state=None, patient=patient)
|
88
|
patient.last_state = fs
|
89
|
patient.save()
|
90
|
if line['sor_date']:
|
91
|
status = Status.objects.filter(type="CLOS").filter(services=service)
|
92
|
fs = FileState.objects.create(status=status[0], author=creator,
|
93
|
date_selected=_to_date(line['sor_date']),
|
94
|
previous_state=None, patient=patient)
|
95
|
patient.last_state = fs
|
96
|
patient.save()
|
97
|
|
98
|
def rs_mapper(tables_data, service):
|
99
|
if service.name == "CMPP":
|
100
|
for line in tables_data['rs']:
|
101
|
if line['id'] == '32436':
|
102
|
print line
|
103
|
|
104
|
def rr_mapper(tables_data, service):
|
105
|
if service.name == "CMPP":
|
106
|
for line in tables_data['rr']:
|
107
|
if line['id'] == '650':
|
108
|
print line
|
109
|
|
110
|
|
111
|
def conge_mapper(tables_data, service):
|
112
|
""" """
|
113
|
for line in tables_data['conge']:
|
114
|
pass
|
115
|
|
116
|
def ev_mapper(tables_data, service):
|
117
|
""" """
|
118
|
pass
|
119
|
|
120
|
def notes_mapper(tables_data, service):
|
121
|
""" """
|
122
|
pass
|
123
|
|
124
|
def _get_dict(cols, line):
|
125
|
""""""
|
126
|
res = {}
|
127
|
for i, data in enumerate(line):
|
128
|
res[cols[i]] = data.decode('utf-8')
|
129
|
return res
|
130
|
|
131
|
tables_data = {}
|
132
|
|
133
|
def main():
|
134
|
""" """
|
135
|
for db in dbs:
|
136
|
if "F_ST_ETIENNE_CMPP" == db:
|
137
|
service = Service.objects.get(name="CMPP")
|
138
|
elif "F_ST_ETIENNE_CAMSP" == db:
|
139
|
service = Service.objects.get(name="CAMSP")
|
140
|
elif "F_ST_ETIENNE_SESSAD_TED" == db:
|
141
|
service = Service.objects.get(name="SESSAD TED")
|
142
|
elif "F_ST_ETIENNE_SESSAD" == db:
|
143
|
service = Service.objects.get(name="SESSAD DYS")
|
144
|
for table in tables:
|
145
|
# TODO: rewrite this part and treat only line by line
|
146
|
tables_data[table] = None
|
147
|
csvfile = open(os.path.join(db_path, db, '%s.csv' % table), 'rb')
|
148
|
csvlines = csv.reader(csvfile, delimiter=';', quotechar='|')
|
149
|
cols = csvlines.next()
|
150
|
tables_data[table] = []
|
151
|
for line in csvlines:
|
152
|
data = _get_dict(cols, line)
|
153
|
tables_data[table].append(data)
|
154
|
func = eval("%s_mapper" % table)
|
155
|
func(tables_data, service)
|
156
|
csvfile.close()
|
157
|
|
158
|
|
159
|
if __name__ == "__main__":
|
160
|
main()
|
161
|
|