|
123
|
1 |
# -*- coding: utf-8 -*- |
|
|
2 |
''' |
|
693
|
3 |
Importe les code INSEE dans les fiches HDA (objets :class:`hdabo.models.datasheet`). |
|
|
4 |
|
|
|
5 |
**Usage**: ``django-admin import_hda_insee_csv [options] [args [args ...]]`` |
|
|
6 |
|
|
|
7 |
**Arguments**: <chemin_vers_csv_insee> |
|
|
8 |
|
|
123
|
9 |
''' |
|
|
10 |
import csv |
|
|
11 |
import re |
|
443
|
12 |
|
|
|
13 |
from django.core.management.base import BaseCommand, CommandError |
|
|
14 |
|
|
|
15 |
from hdabo.models import Datasheet |
|
|
16 |
from hdalab.models import InseeCoords, DatasheetExtras |
|
|
17 |
|
|
123
|
18 |
|
|
|
19 |
class Command(BaseCommand): |
|
|
20 |
''' |
|
|
21 |
Command to export tags |
|
|
22 |
''' |
|
|
23 |
args = '<path_to_csv_file>' |
|
|
24 |
options = '' |
|
|
25 |
help = """Imports HDA Lab INSEE codes from a csv file""" |
|
693
|
26 |
|
|
123
|
27 |
def handle(self, *args, **options): |
|
|
28 |
|
|
|
29 |
if len(args) == 0 or not args[0]: |
|
|
30 |
raise CommandError("Give a CSV File to import") |
|
693
|
31 |
|
|
123
|
32 |
filename = args[0] |
|
693
|
33 |
|
|
123
|
34 |
csvfile = open(filename, "rb") |
|
|
35 |
dialect = csv.Sniffer().sniff(csvfile.read(1024)) |
|
|
36 |
csvfile.seek(0) |
|
|
37 |
reader = csv.reader(csvfile, dialect) |
|
|
38 |
fieldstoget = [ 'id', 'insee' ] |
|
693
|
39 |
|
|
|
40 |
|
|
123
|
41 |
for i,line in enumerate(reader): |
|
|
42 |
if i == 0: |
|
|
43 |
fields = {} |
|
|
44 |
minlength = 0 |
|
|
45 |
for j,field in enumerate(line): |
|
|
46 |
for fieldname in fieldstoget: |
|
|
47 |
if re.search('(?i)%s' % fieldname, field): |
|
|
48 |
fields[fieldname] = j |
|
|
49 |
minlength = max(j,minlength) |
|
|
50 |
else: |
|
|
51 |
if len(line) > minlength: |
|
231
|
52 |
rawdata = dict([(k,line[v].strip()) for k,v in fields.iteritems()]) |
|
123
|
53 |
# Traitement special pour la Corse |
|
|
54 |
insee = int(re.sub('^2(A|B)','20',rawdata['insee'])) |
|
693
|
55 |
|
|
123
|
56 |
hda_id = int(rawdata['id']) |
|
693
|
57 |
|
|
123
|
58 |
try: |
|
|
59 |
datasheet = Datasheet.objects.get(hda_id=hda_id) |
|
|
60 |
except Datasheet.DoesNotExist: |
|
|
61 |
#print "Datasheet for id %d does not exist" % hda_id |
|
|
62 |
datasheet = None |
|
|
63 |
try: |
|
|
64 |
inseecoord = InseeCoords.objects.get(insee=insee) |
|
|
65 |
except InseeCoords.DoesNotExist: |
|
124
|
66 |
print "INSEE entry for Insee Code %d does not exist" % insee |
|
|
67 |
inseecoord = None |
|
693
|
68 |
|
|
123
|
69 |
if datasheet is not None and inseecoord is not None: |
|
|
70 |
dsextra, created = DatasheetExtras.objects.get_or_create(datasheet=datasheet, defaults={'insee':inseecoord}) |
|
|
71 |
if not created: |
|
|
72 |
dsextra.insee = inseecoord |
|
|
73 |
dsextra.save() |
|
693
|
74 |
|
|
|
75 |
csvfile.close() |