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

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

Value check for csv import.

  • Property svn:executable set to *
File size: 3.8 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 .csv, 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# -a = antenna
13# -k = network card
14#
15# (Run from project root)
16# ./manage.py import_csv -f <file location> -m <dataset name> -g <username> -e <email> - a <antenna> -k <network card>
17#
18# Make sure the variables in this script match the column numbers in your file e.g.;
19# Lat is read from the first column [0], if the lat in your file is in the 4th column, change
20# 'lat = row[0]' to 'lat = row[3]'.
21# Also, take note of the 'replace()' and 'strip()' functions. These should probably be edited aswell.
22#
23# Dennis Wagenaar
24# d.wagenaar@gmail.com
25#
26###########################################
27
28from django.core.management import setup_environ
29from django.core.management.base import BaseCommand
30from optparse import OptionParser, make_option
31import settings
32setup_environ(settings)
33from gheat.models import *
34import datetime
35import csv
36
37# Function that imports a csv and processes it.
38def import_file(location, meetrondje, gebruiker, email, antenne, kaart):
39
40 # Creating some objects that have to be created only once. Uses 'get_or_create' to rule out duplicates.
41 # Creating user object. Depends on basecommand options.
42 g, created = Gebruiker.objects.get_or_create(naam=gebruiker , email=email)
43 # Creating equipment object. Depends on basecomand options.
44 a, created = Apparatuur.objects.get_or_create(antenne=antenne , kaart=kaart)
45 # Creating dataset object. Depends on baseommand options.
46 mr = MeetRondje.objects.create(datum=datetime.datetime.now() , naam=meetrondje , gebruiker=g , apparatuur=a)
47
48 # Open the csv.
49 csvfile = csv.reader(open(location, 'rb'), delimiter='\t')
50 # Read every row individually and extract the required data.
51 for row in csvfile:
52 # '.replace' & '.strip' are for replacing and stripping some unwished characters. Edit where necessary.
53 lat = row[0].replace('N ', '')
54 lon = row[1].replace('E ', '')
55 ssid = row[2].strip('( )')
56 bssid = row[4].strip('( )')
57 enc = row[8]
58 if enc[2] == '1': enc = True
59 else: enc = False
60 sig = 100 # Get's fix soon
61 print lat, lon, ssid, bssid, enc, sig
62
63 # Simple check for bad values
64 if lat.startswith('0.'):
65 print 'Bad lat'
66 continue
67 elif lon.startswith('0.'):
68 print 'Bad lon'
69 continue
70 elif sig not in range(1,101):
71 print 'Bad sig'
72 continue
73 else:
74 print 'No bad values'
75
76 # Creating accespoint objects. Avoiding duplicates.
77 ap, created = Accespoint.objects.get_or_create(mac=bssid, ssid=ssid, encryptie=enc)
78 # Creating the measurement objects.
79 m = Meting.objects.create(meetrondje=mr, accespoint=ap, latitude=lat, longitude=lon, signaal=sig)
80
81# Basecommand with options. This gives the user some control over the execution of this script.
82class Command(BaseCommand):
83 option_list = BaseCommand.option_list + (
84 make_option('-f', '--location', dest='location', default='location'),
85 make_option('-m', '--meetrondje', dest='meetrondje', default='rondje'),
86 make_option('-g', '--gebruiker', dest='gebruiker', default='username'),
87 make_option('-e', '--email', dest='email', default='foo@bar.org'),
88 make_option('-a', '--antenne', dest='antenne', default='geen'),
89 make_option('-k', '--kaart', dest='kaart', default='interne kaart'),
90 )
91
92 # The function 'import_file' will be executed with the default option values, or the values specified by the user.
93 def handle(self, *args, **options):
94 import_file(options['location'],options['meetrondje'],options['gebruiker'],options['email'],options['antenne'],options['kaart'])
Note: See TracBrowser for help on using the repository browser.