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, CodeCFTMEA
|
21
|
|
22
|
# Configuration
|
23
|
db_path = "./scripts/20121221-192258"
|
24
|
|
25
|
dbs = ["F_ST_ETIENNE_SESSAD_TED", "F_ST_ETIENNE_CMPP", "F_ST_ETIENNE_CAMSP", "F_ST_ETIENNE_SESSAD"]
|
26
|
|
27
|
# Global mappers. This dicts are used to map a Faure id with a calebasse object.
|
28
|
dossiers = {}
|
29
|
|
30
|
def _to_date(str_date):
|
31
|
if not str_date:
|
32
|
return None
|
33
|
return datetime.strptime(str_date[:-13], "%Y-%m-%d")
|
34
|
|
35
|
def _to_int(str_int):
|
36
|
if not str_int:
|
37
|
return None
|
38
|
return int(str_int)
|
39
|
|
40
|
def discipline_mapper(tables_data, service):
|
41
|
for line in tables_data['discipline']:
|
42
|
# Insert workertype
|
43
|
if not WorkerType.objects.filter(name=line['libelle']):
|
44
|
WorkerType.objects.create(name=line['libelle'])
|
45
|
|
46
|
|
47
|
def intervenants_mapper(tables_data, service):
|
48
|
for line in tables_data['intervenants']:
|
49
|
# Insert workers
|
50
|
for disp in tables_data['discipline']:
|
51
|
if disp['id'] == line['discipline']:
|
52
|
type = WorkerType.objects.get(name=disp['libelle'])
|
53
|
# TODO : import actif or not
|
54
|
worker, created = Worker.objects.get_or_create(
|
55
|
type=type,
|
56
|
last_name=line['nom'],
|
57
|
first_name=line['prenom'],
|
58
|
email=line['email'],
|
59
|
phone=line['tel'],
|
60
|
gender=int(line['titre']),
|
61
|
)
|
62
|
worker.services.add(service)
|
63
|
|
64
|
def dossiers_mapper(tables_data, service):
|
65
|
global dossiers
|
66
|
for line in tables_data['dossiers']:
|
67
|
status = Status.objects.filter(type="ACCUEIL").filter(services=service)
|
68
|
creator = User.objects.get(id=1)
|
69
|
gender = _to_int(line['nais_sexe'])
|
70
|
if gender == 0:
|
71
|
gender = None
|
72
|
# TODO: add more fields
|
73
|
patient, created = PatientRecord.objects.get_or_create(first_name=line['prenom'],
|
74
|
last_name=line['nom'], birthdate=_to_date(line['nais_date']),
|
75
|
twinning_rank=_to_int(line['nais_rang']),
|
76
|
gender=gender, service=service, creator=creator)
|
77
|
dossiers[line['id']] = patient
|
78
|
|
79
|
if not created:
|
80
|
if not line['ins_date']:
|
81
|
# Hack when there is no inscription date put 01/01/1970
|
82
|
line['ins_date'] = "1970-01-01 00:00:00.000"
|
83
|
fs = FileState.objects.create(status=status[0], author=creator,
|
84
|
date_selected=_to_date(line['ins_date']),
|
85
|
previous_state=None, patient=patient)
|
86
|
patient.last_state = fs
|
87
|
patient.save()
|
88
|
if line['sor_date']:
|
89
|
status = Status.objects.filter(type="CLOS").filter(services=service)
|
90
|
fs = FileState.objects.create(status=status[0], author=creator,
|
91
|
date_selected=_to_date(line['sor_date']),
|
92
|
previous_state=None, patient=patient)
|
93
|
patient.last_state = fs
|
94
|
patient.save()
|
95
|
|
96
|
def rs_mapper(tables_data, service):
|
97
|
global dossiers
|
98
|
|
99
|
event_type = EventType.objects.get(
|
100
|
label=u"Rendez-vous patient"
|
101
|
)
|
102
|
|
103
|
for line in tables_data['rs']:
|
104
|
if dossiers.has_key(line['enfant_id']):
|
105
|
patient = dossiers[line['enfant_id']]
|
106
|
strdate = line['date_rdv'][:-13] + ' ' + line['heure'][11:-4]
|
107
|
date = datetime.strptime(strdate, "%Y-%m-%d %H:%M:%S")
|
108
|
|
109
|
# TODO: add act_type
|
110
|
# act_event = EventAct.objects.get_or_create(
|
111
|
# title=line['libelle'],
|
112
|
# event_type=event_type,
|
113
|
# patient=patient,
|
114
|
# act_type=act_type,
|
115
|
# date=date
|
116
|
# )
|
117
|
else:
|
118
|
# TODO: if no patient add event
|
119
|
pass
|
120
|
|
121
|
|
122
|
def conge_mapper(tables_data, service):
|
123
|
""" """
|
124
|
for line in tables_data['conge']:
|
125
|
pass
|
126
|
|
127
|
def ev_mapper(tables_data, service):
|
128
|
""" """
|
129
|
pass
|
130
|
|
131
|
def notes_mapper(tables_data, service):
|
132
|
""" """
|
133
|
pass
|
134
|
|
135
|
def _get_dict(cols, line):
|
136
|
""""""
|
137
|
res = {}
|
138
|
for i, data in enumerate(line):
|
139
|
res[cols[i]] = data.decode('utf-8')
|
140
|
return res
|
141
|
|
142
|
tables_data = {}
|
143
|
|
144
|
def main():
|
145
|
""" """
|
146
|
csvfile = open('./scripts/20121221-192258/F_ST_ETIENNE_SESSAD_TED/mises.csv', 'rb')
|
147
|
csvlines = csv.reader(csvfile, delimiter=';', quotechar='|')
|
148
|
cols = csvlines.next()
|
149
|
for line in csvlines:
|
150
|
CodeCFTMEA(code=int(line[1]), axe=int(line[2]), name=line[3]).save()
|
151
|
|
152
|
if __name__ == "__main__":
|
153
|
main()
|