Changeset 9142 for src/django_gheat/gheat
- Timestamp:
- May 4, 2011, 11:04:37 AM (14 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
src/django_gheat/gheat/management/commands/import_csv.py
r9123 r9142 33 33 import csv 34 34 35 def import_file(location, meetrondje, gebruiker, email): 35 # Function that imports a csv and processes it. 36 def import_file(location, meetrondje, gebruiker, email, antenne, kaart): 36 37 38 # Creating some objects that have to be created only once. Uses 'get_or_create' to rule out duplicates. 39 # Creating user object. Depends on basecommand options. 37 40 g, created = Gebruiker.objects.get_or_create(naam=gebruiker , email=email) 38 a, created = Apparatuur.objects.get_or_create(antenne='test' , kaart='test') 41 # Creating equipment object. Depends on basecomand options. 42 a, created = Apparatuur.objects.get_or_create(antenne=antenne , kaart=kaart) 43 # Creating dataset object. Depends on baseommand options. 39 44 mr = MeetRondje.objects.create(datum=datetime.datetime.now() , naam=meetrondje , gebruiker=g , apparatuur=a) 40 45 46 # Open the csv. 41 47 csvfile = csv.reader(open(location, 'rb'), delimiter='\t') 48 # Read every row individually and extract the required data. 42 49 for row in csvfile: 50 # '.replace' & '.strip' are for replacing and stripping some unwished characters. Edit where necessary. 43 51 lat = row[0].replace('N ', '') 44 52 lon = row[1].replace('E ', '') … … 51 59 print lat, lon, ssid, bssid, enc, sig 52 60 61 # Creating accespoint objects. Avoiding duplicates. 53 62 ap, created = Accespoint.objects.get_or_create(mac=bssid, ssid=ssid, encryptie=enc) 63 # Creating the measurement objects. 54 64 m = Meting.objects.create(meetrondje=mr, accespoint=ap, latitude=lat, longitude=lon, signaal=sig) 55 65 56 66 # Basecommand with options. This gives the user some control over the execution of this script. 57 67 class Command(BaseCommand): 58 68 option_list = BaseCommand.option_list + ( … … 61 71 make_option('-g', '--gebruiker', dest='gebruiker', default='username'), 62 72 make_option('-e', '--email', dest='email', default='foo@bar.org'), 73 make_option('-a', '--antenne', dest='antenne', default='geen'), 74 make_option('-k', '--kaart', dest='kaart', default='interne kaart'), 63 75 ) 64 76 77 # The function 'import_file' will be executed with the default option values, or the values specified by the user. 65 78 def handle(self, *args, **options): 66 import_file(options['location'],options['meetrondje'],options['gebruiker'],options['email'] )79 import_file(options['location'],options['meetrondje'],options['gebruiker'],options['email'],options['antenne'],options['kaart'])
Note:
See TracChangeset
for help on using the changeset viewer.