source: src/django_gheat/gheat/models.py@ 9819

Last change on this file since 9819 was 9819, checked in by rick, 13 years ago

Correct annoying typo in naming of the acces*s* point.

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