Revision d7005bbe
Added by Mikaël Ates over 12 years ago
scripts/check_double.py | ||
---|---|---|
1 |
import sys |
|
1 | 2 |
import calebasse.settings |
2 | 3 |
import django.core.management |
4 |
from datetime import datetime |
|
3 | 5 |
|
4 | 6 |
django.core.management.setup_environ(calebasse.settings) |
5 | 7 |
|
6 | 8 |
from calebasse.dossiers.models import PatientRecord |
7 | 9 |
from calebasse.actes.models import Act |
8 | 10 |
|
11 |
from django.db import transaction |
|
12 |
|
|
13 |
@transaction.commit_manually |
|
9 | 14 |
def main(): |
15 |
print datetime.now() |
|
16 |
same_acts_set = [] |
|
10 | 17 |
seen = [] |
11 | 18 |
i = 0 |
19 |
total = PatientRecord.objects.all().count() |
|
12 | 20 |
for patient in PatientRecord.objects.all(): |
21 |
i += 1 |
|
13 | 22 |
acts = Act.objects.filter(patient=patient) |
14 | 23 |
for act in acts: |
15 |
if not (patient.id, act.date, act.time) in seen: |
|
16 |
seen.append((patient.id, act.date, act.time)) |
|
17 |
same_acts = Act.objects.filter(patient=patient, date=act.date, time=act.time) |
|
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)
|
|
18 | 27 |
nb = same_acts.count() |
19 | 28 |
if nb > 1: |
20 |
keep = None |
|
21 |
for a in same_acts: |
|
22 |
state = a.get_state() |
|
23 |
if state and state.state_name != 'NON_VALIDE' and a.validation_locked == True: |
|
24 |
keep = a |
|
25 |
break |
|
26 |
if not keep: |
|
27 |
lockeds = same_acts.filter(validation_locked=True) |
|
28 |
if lockeds.count() >= 1: |
|
29 |
keep = lockeds[0] |
|
30 |
else: |
|
31 |
for a in same_acts: |
|
32 |
state = a.get_state() |
|
33 |
if state and state.state_name != 'NON_VALIDE': |
|
34 |
keep = a |
|
35 |
break |
|
36 |
if not keep: |
|
37 |
keep = same_acts[0] |
|
38 |
same_acts.exclude(pk=keep.pk).delete() |
|
39 |
i += 1 |
|
40 |
print "Nb actes redondants traites: %d" % i |
|
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() |
|
41 | 76 |
|
42 | 77 |
if __name__ == "__main__": |
43 | 78 |
main() |
Also available in: Unified diff
scripts: update script to remove acts in double.