source: src/django_gheat/gheat/random_db_init.py@ 9047

Last change on this file since 9047 was 9047, checked in by rick, 14 years ago

Hack and example how-to interface with Django and Python. And also some way to
get rid of the damm php script going to the database.

  • Property svn:executable set to *
File size: 2.0 KB
Line 
1#!/usr/bin/env python
2#
3# Example code for random inserting points into the database.
4#
5# Rick van der Zwet <info@rickvanderzwet.nl>
6import settings
7from django.core.management import setup_environ
8setup_environ(settings)
9
10
11from django.db import connection, transaction
12from gheat.models import *
13import datetime
14import os
15import random
16import sys
17
18def add_random_measurements(count):
19 # Create User/Equipment Objects
20 username = os.getenv('USER', 'foobar')
21 user, created = Gebruiker.objects.get_or_create(naam=username, email=username + '@example.org')
22 equipment, created = Apparatuur.objects.get_or_create(antenne='itern', kaart='device')
23
24 accespoint, created = Accespoint.objects.get_or_create(mac='00:11:22:33:44',
25 ssid='ap-WirelessLeiden-Test', encryptie=True)
26
27 # Mesurement run
28 rondje = MeetRondje.objects.create(datum=datetime.datetime.now(), naam='Test Ronde',
29 gebruiker=user, apparatuur=equipment)
30 rondje.save()
31
32 sql_insert = "INSERT INTO gheat_meting (meetrondje_id, accespoint_id, lat, lng, signaal) VALUES ";
33 sql_insert_values = []
34 for i in range(0,count):
35 latitude = random.randint(140252,167846)
36 longitude = random.randint(459019,514637)
37 signal = random.randint(0,100)
38 # This takes roughly 1 minute on 10000 items which logically cause it does
39 # 10000 sql commits! So use the SQL INSERT hack, instead.
40 #if (i % 1000) == 0:
41 # print "#Importing Random mesurements: %s/%s" % (i,count)
42 #meting = Meting.objects.create(meetrondje=rondje,
43 # accespoint=accespoint,latitude=latitude,
44 # longitude=longitude,signaal=signal)
45 #meting.save()
46 sql_insert_values.append("('" + "','".join(map(str,[rondje.id, accespoint.id, latitude, longitude, signal])) + "')")
47
48 sql_insert += ','.join(sql_insert_values) + ';';
49 cursor = connection.cursor()
50 cursor.execute(sql_insert)
51 transaction.commit_unless_managed()
52
53
54if __name__ == "__main__":
55 # Awefull hack for argument parsing
56 count = int(sys.argv[0]) if len(sys.argv) > 1 else 10000
57 add_random_measurements(count)
58
Note: See TracBrowser for help on using the repository browser.