<commit_before>#!/usr/bin/env python
import os
import datetime as dt
from collections import defaultdict

if __name__ == "__main__":
    os.environ.setdefault("DJANGO_SETTINGS_MODULE", "calebasse.settings")
    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') \
            .prefetch_related('doctors')
    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, '='
            print '{:>6} {:>6} {:>6} {:>6} {:>6}'.format('act_id', 'ev_id', 'exc_id', 'old_id', 'heure')
            for act in sorted(doubles[key]):
                total += 1
                exception_to = ''
                if act.parent_event:
                    exception_to = act.parent_event.exception_to_id
                print '%06d' % act.id, '%6s' % act.parent_event_id, '%6s' % exception_to, '%6s' % act.old_id, '%6s' % act.time.strftime('%H:%M'), act, act.validation_locked, act.actvalidationstate_set.all()
            print
    print 'Total', total, 'actes'<commit_msg>scripts: add a --delete mode to the find_dupliacte_acts script<commit_after>#!/usr/bin/env python
import sys
import os
import datetime as dt
from collections import defaultdict

os.environ.setdefault("DJANGO_SETTINGS_MODULE", "calebasse.settings")

from django.db import transaction

@transaction.commit_on_success
def main():
    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') \
            .prefetch_related('doctors', 'patient__service')
    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, '='
            print '{:>6} {:>6} {:>6} {:>6} {:>6} {:>6} {:>6}'.format('act_id', 'ev_id', 'exc_id', 'old_id', 'rr_id', 'rs_id', 'heure')
            for act in sorted(doubles[key]):
                total += 1
                exception_to = ''
                rr_id = ''
                rs_id = ''
                if act.parent_event:
                    exception_to = act.parent_event.exception_to_id
                    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
            print
            if len([act for act in doubles[key] if act.validation_locked]) == 1 and '--delete' in sys.argv:
                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()
    print 'Total', total, 'actes'

if __name__ == "__main__":
    main()
{'fraction_non_alphanumeric': 0.1258741258741259, 'fraction_numerical': 0.010708041958041958, 'mean_word_length': 2.1392318244170094, 'pattern_counts': {'":': 2, '<': 5, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6250312', 'n_tokens_mistral': 1516, 'n_tokens_neox': 1446, 'n_words': 338}