1 | import datetime
|
---|
2 |
|
---|
3 | from django.core import validators
|
---|
4 | from django.db import models
|
---|
5 |
|
---|
6 | MIN_SIGNAL = 0
|
---|
7 | MAX_SIGNAL = 100
|
---|
8 |
|
---|
9 |
|
---|
10 | # http://stackoverflow.com/questions/849142/how-to-limit-the-maximum-value-of-a-numeric-field-in-a-django-model/849426#849426
|
---|
11 | class IntegerRangeField(models.IntegerField):
|
---|
12 | def __init__(self, verbose_name=None, name=None, min_value=None, max_value=None, **kwargs):
|
---|
13 | self.min_value, self.max_value = min_value, max_value
|
---|
14 | models.IntegerField.__init__(self, verbose_name, name, **kwargs)
|
---|
15 | def formfield(self, **kwargs):
|
---|
16 | defaults = {'min_value': self.min_value, 'max_value':self.max_value}
|
---|
17 | defaults.update(kwargs)
|
---|
18 | return super(IntegerRangeField, self).formfield(**defaults)
|
---|
19 |
|
---|
20 |
|
---|
21 | class WirelessClient(models.Model):
|
---|
22 | mac = models.CharField(max_length=17, unique=True)
|
---|
23 | def __unicode__(self):
|
---|
24 | return self.mac
|
---|
25 |
|
---|
26 |
|
---|
27 | class Organization(models.Model):
|
---|
28 | fullname = models.CharField(max_length=100,unique=True)
|
---|
29 | name = models.CharField(max_length=100,unique=True)
|
---|
30 | def __unicode__(self):
|
---|
31 | return self.fullname
|
---|
32 |
|
---|
33 | @staticmethod
|
---|
34 | def get_name_by_ssid(ssid):
|
---|
35 | """ Try to determine the organization via the SSID """
|
---|
36 | ssid = ssid.lower()
|
---|
37 | name = None
|
---|
38 | if ssid.startswith('ap') and ssid.endswith('wleiden.net'):
|
---|
39 | name = 'WirelessLeiden'
|
---|
40 | elif ssid.startswith('il-') and ssid.endswith('wleiden.net'):
|
---|
41 | name = 'WirelessLeiden'
|
---|
42 | elif ssid.startswith('ap') and 'wirelessleiden' in ssid:
|
---|
43 | name = 'WirelessLeiden'
|
---|
44 | elif ssid.startswith('ap-') and 'westeinder' in ssid:
|
---|
45 | name = 'WirelessPlassen'
|
---|
46 | elif ssid.endswith('walphen.net'):
|
---|
47 | name = 'WirelessAlphen'
|
---|
48 | elif 'wirelessarnhem' in ssid:
|
---|
49 | name = 'WirelessArnhem'
|
---|
50 | return name
|
---|
51 |
|
---|
52 | @staticmethod
|
---|
53 | def get_by_ssid(ssid):
|
---|
54 | name = Organization.get_name_by_ssid(ssid)
|
---|
55 | if not name:
|
---|
56 | return None
|
---|
57 | else:
|
---|
58 | return Organization.objects.get(name=name)
|
---|
59 |
|
---|
60 |
|
---|
61 | class Node(models.Model):
|
---|
62 | name = models.CharField(max_length=50, unique=True)
|
---|
63 | latitude = models.FloatField(null=True,blank=True)
|
---|
64 | longitude = models.FloatField(null=True,blank=True)
|
---|
65 | organization = models.ForeignKey(Organization,null=True, blank=True)
|
---|
66 |
|
---|
67 | def __unicode__(self):
|
---|
68 | return "%s - %s" % (self.name, self.organization)
|
---|
69 |
|
---|
70 | class Accespoint(models.Model):
|
---|
71 | mac = models.CharField(max_length=17)
|
---|
72 | ssid = models.CharField(max_length=64)
|
---|
73 | organization = models.ForeignKey(Organization,null=True, blank=True)
|
---|
74 | encryptie = models.BooleanField()
|
---|
75 | class Meta:
|
---|
76 | unique_together = ('mac', 'ssid')
|
---|
77 | def __unicode__(self):
|
---|
78 | return "%s - %s" % (self.mac, self.ssid)
|
---|
79 |
|
---|
80 |
|
---|
81 | def save(self, *args, **kwargs):
|
---|
82 | self.organization = self.get_organization(self.ssid)
|
---|
83 | super(Accespoint, self).save(*args, **kwargs)
|
---|
84 |
|
---|
85 | class Gebruiker(models.Model):
|
---|
86 | naam = models.CharField(max_length=64)
|
---|
87 | email = models.CharField(max_length=64)
|
---|
88 | def __unicode__(self):
|
---|
89 | return "%s - %s" % (self.naam, self.email)
|
---|
90 |
|
---|
91 | class Apparatuur(models.Model):
|
---|
92 | antenne = models.CharField(max_length=64, default="onbekend")
|
---|
93 | kaart = models.CharField(max_length=64, default="onbekend")
|
---|
94 | def __unicode__(self):
|
---|
95 | return "%s - %s" % (self.antenne, self.kaart)
|
---|
96 | class Meta:
|
---|
97 | verbose_name_plural = 'Apparatuur'
|
---|
98 | unique_together = ('antenne', 'kaart')
|
---|
99 |
|
---|
100 | class MeetRondje(models.Model):
|
---|
101 | datum = models.DateTimeField(blank=True,null=True,default=datetime.datetime.now)
|
---|
102 | naam = models.CharField(max_length=64)
|
---|
103 | gebruiker = models.ForeignKey(Gebruiker)
|
---|
104 | apparatuur = models.ForeignKey(Apparatuur)
|
---|
105 | def __unicode__(self):
|
---|
106 | return "%s - %s" % (self.gebruiker.naam, self.naam)
|
---|
107 | class Meta:
|
---|
108 | verbose_name_plural = 'MeetRondjes'
|
---|
109 |
|
---|
110 | FILE_TYPES = (
|
---|
111 | ( 'netxml', 'Netstumbler NetXML'),
|
---|
112 | ( 'gpsxml', 'Netstumbler GPSXML'),
|
---|
113 | ( 'droidcsv', 'DroidStumbler CSV'),
|
---|
114 | )
|
---|
115 |
|
---|
116 | class MeetBestand(models.Model):
|
---|
117 | meetrondje = models.ForeignKey(MeetRondje)
|
---|
118 | bestand = models.FileField(upload_to='scan-data/%Y/%m/%d')
|
---|
119 | filetype = models.CharField(max_length=64,choices=FILE_TYPES,default='netxml')
|
---|
120 | is_imported = models.BooleanField(default=False,help_text="This field get set the moment the data is processed into the database")
|
---|
121 | class Meta:
|
---|
122 | verbose_name_plural = 'MeetBestanden'
|
---|
123 | unique_together = ('meetrondje', 'bestand')
|
---|
124 | def __unicode__(self):
|
---|
125 | return "%s - %s" % (self.meetrondje, self.bestand)
|
---|
126 |
|
---|
127 |
|
---|
128 | class Meting(models.Model):
|
---|
129 | meetrondje = models.ForeignKey(MeetRondje)
|
---|
130 | accespoint = models.ForeignKey(Accespoint)
|
---|
131 | latitude = models.FloatField()
|
---|
132 | longitude = models.FloatField()
|
---|
133 | signaal = IntegerRangeField(max_length=3,min_value=MIN_SIGNAL,max_value=MAX_SIGNAL)
|
---|
134 | def __unicode__(self):
|
---|
135 | return "%s @ %.5f,%.5f : %s" % (self.accespoint.ssid, float(self.latitude), float(self.longitude), self.signaal)
|
---|
136 | class Meta:
|
---|
137 | # This implies that you cannot have multiple messurements on the same
|
---|
138 | # location for a single 'run', thus the data needs cleaned before to make
|
---|
139 | # this properly hold.
|
---|
140 | unique_together = ('meetrondje', 'accespoint', 'latitude', 'longitude'),
|
---|
141 | verbose_name_plural = 'Metingen'
|
---|
142 |
|
---|