1
|
import sys
|
2
|
import calebasse.settings
|
3
|
import django.core.management
|
4
|
from datetime import datetime
|
5
|
|
6
|
django.core.management.setup_environ(calebasse.settings)
|
7
|
|
8
|
from calebasse.dossiers.models import PatientRecord
|
9
|
from calebasse.actes.models import Act
|
10
|
|
11
|
from django.db import transaction
|
12
|
|
13
|
@transaction.commit_manually
|
14
|
def main():
|
15
|
print datetime.now()
|
16
|
same_acts_set = []
|
17
|
seen = []
|
18
|
i = 0
|
19
|
total = PatientRecord.objects.all().count()
|
20
|
for patient in PatientRecord.objects.all():
|
21
|
i += 1
|
22
|
acts = Act.objects.filter(patient=patient)
|
23
|
for act in acts:
|
24
|
if not (patient.id, act.date, act.time, act.act_type) in seen:
|
25
|
seen.append((patient.id, act.date, act.time, act.act_type))
|
26
|
same_acts = Act.objects.filter(patient=patient, date=act.date, time=act.time, act_type=act.act_type)
|
27
|
nb = same_acts.count()
|
28
|
if nb > 1:
|
29
|
same_acts_set.append(same_acts)
|
30
|
if not i % 100:
|
31
|
percent = int(round((float(i) / float(total)) * 100))
|
32
|
out = '\r %20s [%s%s] %3d %%' % ("Recherche des doublons : ", '=' * percent, ' ' * (100 - percent), percent)
|
33
|
sys.stdout.write(out)
|
34
|
sys.stdout.flush()
|
35
|
total = len(same_acts_set)
|
36
|
i = 0
|
37
|
for same_acts in same_acts_set:
|
38
|
i += 1
|
39
|
# Recherche du parent event
|
40
|
parent_event_id = None
|
41
|
for a in same_acts:
|
42
|
if a.parent_event:
|
43
|
if parent_event_id and parent_event_id != a.parent_event.id:
|
44
|
print "Il y a plusieurs evenement parent, bizarre"
|
45
|
parent_event_id = a.parent_event.id
|
46
|
keep = None
|
47
|
for a in same_acts:
|
48
|
state = a.get_state()
|
49
|
if state and state.state_name != 'NON_VALIDE' and a.validation_locked == True:
|
50
|
keep = a
|
51
|
break
|
52
|
if not keep:
|
53
|
lockeds = same_acts.filter(validation_locked=True)
|
54
|
if lockeds.count() >= 1:
|
55
|
keep = lockeds[0]
|
56
|
else:
|
57
|
for a in same_acts:
|
58
|
state = a.get_state()
|
59
|
if state and state.state_name != 'NON_VALIDE':
|
60
|
keep = a
|
61
|
break
|
62
|
if not keep:
|
63
|
keep = same_acts[0]
|
64
|
if parent_event_id and not keep.parent_event:
|
65
|
keep.parent_event_id = parent_event_id
|
66
|
keep.save()
|
67
|
same_acts.exclude(pk=keep.pk).delete()
|
68
|
if not i % 100:
|
69
|
percent = int(round((float(i) / float(total)) * 100))
|
70
|
out = '\r %20s [%s%s] %3d %%' % ("Traitement des doublons : ", '=' * percent, ' ' * (100 - percent), percent)
|
71
|
sys.stdout.write(out)
|
72
|
sys.stdout.flush()
|
73
|
transaction.commit()
|
74
|
print "Nb de doublons traites: %d" % total
|
75
|
print datetime.now()
|
76
|
|
77
|
if __name__ == "__main__":
|
78
|
main()
|