calebasse/scripts/find_duplicate_acts.py @ 48091f78
| 3b25faf6 | Benjamin Dauvergne | #!/usr/bin/env python
|
|
| 63c8283b | Benjamin Dauvergne | import sys
|
|
| 3b25faf6 | Benjamin Dauvergne | import os
|
|
import datetime as dt
|
|||
from collections import defaultdict
|
|||
| 63c8283b | Benjamin Dauvergne | os.environ.setdefault("DJANGO_SETTINGS_MODULE", "calebasse.settings")
|
|
from django.db import transaction
|
|||
@transaction.commit_on_success
|
|||
def main():
|
|||
| 3b25faf6 | Benjamin Dauvergne | from calebasse.actes.validation import get_days_with_acts_not_locked
|
|
from calebasse.actes.models import Act
|
|||
doubles = defaultdict(lambda: set())
|
|||
days_with_acts_not_locked = get_days_with_acts_not_locked(dt.date(2010,1,1), dt.date.today())
|
|||
acts = Act.objects.filter(date__in=days_with_acts_not_locked) \
|
|||
.order_by('time') \
|
|||
| 63c8283b | Benjamin Dauvergne | .prefetch_related('doctors', 'patient__service')
|
|
| 3b25faf6 | Benjamin Dauvergne | for act in acts:
|
|
participants_id = [doctor.id for doctor in act.doctors.all()]
|
|||
key = (act.date, act.patient_id, act.act_type_id, tuple(sorted(participants_id)))
|
|||
doubles[key].add(act)
|
|||
for key in doubles.keys():
|
|||
if len(doubles[key]) < 2:
|
|||
del doubles[key]
|
|||
date = None
|
|||
total = 0
|
|||
for key in sorted(doubles.iterkeys()):
|
|||
for act in doubles[key]:
|
|||
if not act.validation_locked:
|
|||
break
|
|||
else:
|
|||
continue
|
|||
if key[0] != date:
|
|||
if date is not None:
|
|||
print
|
|||
date = key[0]
|
|||
print ' = Acte en double le ', date, '='
|
|||
| 2efc8977 | Benjamin Dauvergne | print '{0:>6} {1:>6} {2:>6} {3:>6} {4:>6} {5:>6} {6:>6}'.format('act_id', 'ev_id', 'exc_id', 'old_id', 'rr_id', 'rs_id', 'heure')
|
|
| 3b25faf6 | Benjamin Dauvergne | for act in sorted(doubles[key]):
|
|
total += 1
|
|||
exception_to = ''
|
|||
| 63c8283b | Benjamin Dauvergne | rr_id = ''
|
|
rs_id = ''
|
|||
| 3b25faf6 | Benjamin Dauvergne | if act.parent_event:
|
|
exception_to = act.parent_event.exception_to_id
|
|||
| 63c8283b | Benjamin Dauvergne | rr_id = act.parent_event.old_rr_id or ''
|
|
rs_id = act.parent_event.old_rs_id or ''
|
|||
print '%06d' % act.id, '%6s' % act.parent_event_id, '%6s' % exception_to, '%6s' % act.old_id, '%6s' % rr_id, '%6s' % rs_id, \
|
|||
'%6s' % act.time.strftime('%H:%M'), act, act.validation_locked, act.actvalidationstate_set.all(), act.patient.service.slug
|
|||
| 3b25faf6 | Benjamin Dauvergne | print
|
|
| ac12103e | Benjamin Dauvergne | if '--delete' in sys.argv:
|
|
| 63c8283b | Benjamin Dauvergne | x = raw_input('Delete non locked acts ?')
|
|
if x.lower() == 'y':
|
|||
for act in doubles[key]:
|
|||
if not act.validation_locked:
|
|||
print 'Deleted act', act.id
|
|||
act.delete()
|
|||
| 3b25faf6 | Benjamin Dauvergne | print 'Total', total, 'actes'
|
|
| 63c8283b | Benjamin Dauvergne | ||
if __name__ == "__main__":
|
|||
main()
|