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