source: src/django_gheat/gheat/management/commands/import_csv.py@ 9123

Last change on this file since 9123 was 9123, checked in by dennisw, 14 years ago

Fixed encryption, problem with duplicate accespoints solved somehow (local problem I guess).

  • Property svn:executable set to *
File size: 2.5 KB
Line 
1#!/usr/bin/env python
2#
3###########################################
4#
5# Script for importing .csv files
6#
7# In theory, only the -f option is needed, but for overview's sake, please use the others aswell.
8# -f = location of the .netxml, e.g. '/home/test.csv'
9# -m = name of the dataset, e.g. 'Walk in park' or 'Trip with boat'
10# -g = your name
11# -e = your email address
12#
13# (Run from project root)
14# ./manage.py import_csv -f <file location> -m <dataset name> -g <username> -e <email>
15#
16# Make sure the variables in this script match the column numbers in your file e.g.;
17# Lat is read from the first column [0], if the lat in your file is in the 4th column, change
18# 'lat = row[0]' to 'lat = row[3]'.
19# Also, take note of the 'replace()' and 'strip()' functions. These should probably be edited aswell.
20#
21# Dennis Wagenaar
22# d.wagenaar@gmail.com
23#
24###########################################
25
26from django.core.management import setup_environ
27from django.core.management.base import BaseCommand
28from optparse import OptionParser, make_option
29import settings
30setup_environ(settings)
31from gheat.models import *
32import datetime
33import csv
34
35def import_file(location, meetrondje, gebruiker, email):
36
37 g, created = Gebruiker.objects.get_or_create(naam=gebruiker , email=email)
38 a, created = Apparatuur.objects.get_or_create(antenne='test' , kaart='test')
39 mr = MeetRondje.objects.create(datum=datetime.datetime.now() , naam=meetrondje , gebruiker=g , apparatuur=a)
40
41 csvfile = csv.reader(open(location, 'rb'), delimiter='\t')
42 for row in csvfile:
43 lat = row[0].replace('N ', '')
44 lon = row[1].replace('E ', '')
45 ssid = row[2].strip('( )')
46 bssid = row[4].strip('( )')
47 enc = row[8]
48 if enc[2] == '1': enc = True
49 else: enc = False
50 sig = 100 # Get's fix soon
51 print lat, lon, ssid, bssid, enc, sig
52
53 ap, created = Accespoint.objects.get_or_create(mac=bssid, ssid=ssid, encryptie=enc)
54 m = Meting.objects.create(meetrondje=mr, accespoint=ap, latitude=lat, longitude=lon, signaal=sig)
55
56
57class Command(BaseCommand):
58 option_list = BaseCommand.option_list + (
59 make_option('-f', '--location', dest='location', default='location'),
60 make_option('-m', '--meetrondje', dest='meetrondje', default='rondje'),
61 make_option('-g', '--gebruiker', dest='gebruiker', default='username'),
62 make_option('-e', '--email', dest='email', default='foo@bar.org'),
63 )
64
65 def handle(self, *args, **options):
66 import_file(options['location'],options['meetrondje'],options['gebruiker'],options['email'])
Note: See TracBrowser for help on using the repository browser.