Project

General

Profile

Download (3.14 KB) Statistics
| Branch: | Tag: | Revision:

calebasse / scripts / import_compagnies_transport.py @ bfe4851b

1
# -*- coding: utf-8 -*-
2
#!/usr/bin/env python
3

    
4
import os
5
import csv
6
import codecs
7
import string
8
import random
9
from datetime import datetime, time
10

    
11
import django.core.management
12
import calebasse.settings
13
django.core.management.setup_environ(calebasse.settings)
14

    
15
from django.contrib.auth.models import User
16
from calebasse.actes.models import EventAct
17
from calebasse.agenda.models import Event, EventType
18
from calebasse.dossiers.models import PatientRecord, Status, FileState
19
from calebasse.ressources.models import Service
20
from calebasse.personnes.models import Worker, Holiday, UserWorker
21
from calebasse.ressources.models import WorkerType, TransportCompany
22

    
23

    
24
f = "./scripts/transports.csv"
25

    
26
def _to_date(str_date):
27
    if not str_date:
28
        return None
29
    return datetime.strptime(str_date[:-13], "%Y-%m-%d")
30

    
31
def _to_int(str_int):
32
    if not str_int:
33
        return None
34
    return int(str_int)
35

    
36
def discipline_mapper(tables_data, service):
37
    for line in tables_data['discipline']:
38
        # Insert workertype
39
        if not WorkerType.objects.filter(name=line['libelle']):
40
            WorkerType.objects.create(name=line['libelle'])
41

    
42

    
43
def intervenants_mapper(tables_data, service):
44
    for line in tables_data['intervenants']:
45
        # Insert workers
46
        for disp in tables_data['discipline']:
47
            if disp['id'] == line['discipline']:
48
                type = WorkerType.objects.get(name=disp['libelle'])
49
        # TODO : import actif or not
50
        worker, created = Worker.objects.get_or_create(
51
                type=type,
52
                last_name=line['nom'],
53
                first_name=line['prenom'],
54
                email=line['email'],
55
                phone=line['tel'],
56
                gender=int(line['titre']),
57
                )
58
        worker.services.add(service)
59

    
60
class UTF8Recoder:
61
    """
62
    Iterator that reads an encoded stream and reencodes the input to UTF-8
63
    """
64
    def __init__(self, f, encoding):
65
        self.reader = codecs.getreader(encoding)(f)
66

    
67
    def __iter__(self):
68
        return self
69

    
70
    def next(self):
71
        return self.reader.next().encode("utf-8")
72

    
73
class UnicodeReader:
74
    """
75
    A CSV reader which will iterate over lines in the CSV file "f",
76
    which is encoded in the given encoding.
77
    """
78

    
79
    def __init__(self, f, dialect=csv.excel, encoding="iso8859-15", **kwds):
80
        f = UTF8Recoder(f, encoding)
81
        self.reader = csv.reader(f, dialect=dialect, **kwds)
82

    
83
    def next(self):
84
        row = self.reader.next()
85
        return [unicode(s, "utf-8") for s in row]
86

    
87
    def __iter__(self):
88
        return self
89

    
90
def main():
91
    csvfile = open(f, 'rb')
92
    csvlines = UnicodeReader(csvfile, delimiter=';', quotechar='|',encoding='utf-8')
93
    csvlines.next()
94
    for line in csvlines:
95
        TransportCompany(name=line[0],
96
        address = line[1],
97
        address_complement = line[2],
98
        zip_code = line[3],
99
        city = line[4],
100
        phone = line[5],
101
        fax = line[6],
102
        email = line[7],
103
        correspondant = line[8],
104
        old_camsp_id = line[9],
105
        old_cmpp_id = line[10],
106
        old_sessad_dys_id = line[11],
107
        old_sessad_ted_id = line[12]
108
        ).save()
109

    
110

    
111
if __name__ == "__main__":
112
    main()
(7-7/30)