1
|
# -*- coding: utf-8 -*-
|
2
|
#!/usr/bin/env python
|
3
|
|
4
|
import os
|
5
|
import csv
|
6
|
|
7
|
from datetime import datetime
|
8
|
|
9
|
import calebasse.settings
|
10
|
import django.core.management
|
11
|
|
12
|
django.core.management.setup_environ(calebasse.settings)
|
13
|
|
14
|
from django.db import transaction
|
15
|
from calebasse.agenda.models import Event
|
16
|
from calebasse.ressources.models import Service
|
17
|
|
18
|
import utils
|
19
|
|
20
|
# Configuration
|
21
|
db_path = "./scripts/20121221-192258"
|
22
|
|
23
|
dbs = ["F_ST_ETIENNE_SESSAD_TED", "F_ST_ETIENNE_CMPP", "F_ST_ETIENNE_CAMSP", "F_ST_ETIENNE_SESSAD"]
|
24
|
# dbs = ["F_ST_ETIENNE_SESSAD_TED"]
|
25
|
|
26
|
def _to_datetime(str_date):
|
27
|
if not str_date:
|
28
|
return None
|
29
|
return datetime.strptime(str_date[:19], "%Y-%m-%d %H:%M:%S")
|
30
|
|
31
|
def _to_date(str_date):
|
32
|
dt = _to_datetime(str_date)
|
33
|
return dt and dt.date()
|
34
|
|
35
|
def _to_time(str_date):
|
36
|
dt = _to_datetime(str_date)
|
37
|
return dt and dt.time()
|
38
|
|
39
|
def _to_duration(str_date):
|
40
|
dt = _to_datetime(str_date)
|
41
|
if dt is None:
|
42
|
return None
|
43
|
return dt - datetime(1900, 01, 01, 0, 0)
|
44
|
|
45
|
def _to_int(str_int):
|
46
|
if not str_int:
|
47
|
return None
|
48
|
return int(str_int)
|
49
|
|
50
|
def _get_dict(cols, line):
|
51
|
""""""
|
52
|
res = {}
|
53
|
for i, data in enumerate(line):
|
54
|
res[cols[i]] = data.decode('utf-8')
|
55
|
return res
|
56
|
|
57
|
def load_csv(db, name, offset=0, limit=9999999, id_column=0):
|
58
|
records = []
|
59
|
idx = {}
|
60
|
|
61
|
csvfile = open(os.path.join(db_path, db, name + '.csv'), 'rb')
|
62
|
csvlines = csv.reader(csvfile, delimiter=';', quotechar='|')
|
63
|
cols = csvlines.next()
|
64
|
i = 0
|
65
|
for line in csvlines:
|
66
|
if not (offset <= int(line[id_column]) < offset+limit):
|
67
|
continue
|
68
|
data = _get_dict(cols, line)
|
69
|
records.append(data)
|
70
|
idx[data['id']] = i
|
71
|
i += 1
|
72
|
csvfile.close()
|
73
|
return records, idx, cols
|
74
|
|
75
|
def add_invalid(d, reason):
|
76
|
d.setdefault('invalid', '')
|
77
|
if d['invalid']:
|
78
|
d['invalid'] += ', '
|
79
|
d['invalid'] += reason
|
80
|
|
81
|
@transaction.commit_on_success
|
82
|
def main():
|
83
|
""" """
|
84
|
|
85
|
for db in dbs:
|
86
|
if "F_ST_ETIENNE_CMPP" == db:
|
87
|
service = Service.objects.get(name="CMPP")
|
88
|
elif "F_ST_ETIENNE_CAMSP" == db:
|
89
|
service = Service.objects.get(name="CAMSP")
|
90
|
elif "F_ST_ETIENNE_SESSAD_TED" == db:
|
91
|
service = Service.objects.get(name="SESSAD TED")
|
92
|
elif "F_ST_ETIENNE_SESSAD" == db:
|
93
|
service = Service.objects.get(name="SESSAD DYS")
|
94
|
|
95
|
print '===', service.name, '==='
|
96
|
print datetime.now()
|
97
|
limit = 20000
|
98
|
|
99
|
event_indexes = utils.QuerysetIndex(
|
100
|
Event.objects.filter(services=service),
|
101
|
'old_rs_id')
|
102
|
|
103
|
|
104
|
offset = 0
|
105
|
while True:
|
106
|
rs_data, rs_idx, rs_cols = load_csv(db, 'rs', offset=offset,
|
107
|
limit=limit)
|
108
|
if not rs_data:
|
109
|
break
|
110
|
print 'Loading cancellation flag for', len(rs_data), 'events'
|
111
|
events_id_to_cancel = []
|
112
|
for line in rs_data:
|
113
|
if line['id'] not in event_indexes.by_old_rs_id:
|
114
|
continue
|
115
|
if line['desactivate'] == '-1':
|
116
|
event_id = event_indexes.by_old_rs_id[line['id']].id
|
117
|
events_id_to_cancel.append(event_id)
|
118
|
print 'Updating', len(events_id_to_cancel), 'flags'
|
119
|
Event.objects.filter(id__in=events_id_to_cancel) \
|
120
|
.update(canceled=True)
|
121
|
offset += limit
|
122
|
|
123
|
|
124
|
if __name__ == "__main__":
|
125
|
main()
|