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

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

Import script for csv added. Some problems with signal strength and duplicate accespoints.

  • Property svn:executable set to *
File size: 2.4 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 enc = enc[2]
49 sig = 100 # Get's fix soon
50 print lat, lon, ssid, bssid, enc, sig
51
52 ap, created = Accespoint.objects.get_or_create(mac=bssid, ssid=ssid, encryptie=enc)
53 m = Meting.objects.create(meetrondje=mr, accespoint=ap, latitude=lat, longitude=lon, signaal=sig)
54
55
56class Command(BaseCommand):
57 option_list = BaseCommand.option_list + (
58 make_option('-f', '--location', dest='location', default='location'),
59 make_option('-m', '--meetrondje', dest='meetrondje', default='rondje'),
60 make_option('-g', '--gebruiker', dest='gebruiker', default='username'),
61 make_option('-e', '--email', dest='email', default='foo@bar.org'),
62 )
63
64 def handle(self, *args, **options):
65 import_file(options['location'],options['meetrondje'],options['gebruiker'],options['email'])
Note: See TracBrowser for help on using the repository browser.