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

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

Fix import of invalid datapoints, causing large dots in output as signal values of 100+ where imported.

File size: 4.6 KB
RevLine 
[9026]1import datetime
[9006]2
[9184]3from django.core import validators
4from django.db import models
5
[9664]6MIN_SIGNAL = 0
7MAX_SIGNAL = 100
[9638]8
[9664]9
10# http://stackoverflow.com/questions/849142/how-to-limit-the-maximum-value-of-a-numeric-field-in-a-django-model/849426#849426
11class 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
[9562]21class WirelessClient(models.Model):
22 mac = models.CharField(max_length=17, unique=True)
23 def __unicode__(self):
24 return self.mac
[9184]25
[9579]26
[9592]27class 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
[9579]32
[9592]33 @staticmethod
34 def get_name_by_ssid(ssid):
35 """ Try to determine the organization via the SSID """
[9593]36 ssid = ssid.lower()
[9592]37 name = None
38 if ssid.startswith('ap') and ssid.endswith('wleiden.net'):
39 name = 'WirelessLeiden'
[9593]40 elif ssid.startswith('il-') and ssid.endswith('wleiden.net'):
[9592]41 name = 'WirelessLeiden'
[9593]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'
[9592]50 return name
51
[9593]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
[9590]61class Node(models.Model):
[9593]62 name = models.CharField(max_length=50, unique=True)
[9590]63 latitude = models.FloatField(null=True,blank=True)
64 longitude = models.FloatField(null=True,blank=True)
[9592]65 organization = models.ForeignKey(Organization,null=True, blank=True)
[9590]66
[9592]67 def __unicode__(self):
68 return "%s - %s" % (self.name, self.organization)
69
[9026]70class Accespoint(models.Model):
[9053]71 mac = models.CharField(max_length=17)
72 ssid = models.CharField(max_length=64)
[9592]73 organization = models.ForeignKey(Organization,null=True, blank=True)
[9053]74 encryptie = models.BooleanField()
[9562]75 class Meta:
76 unique_together = ('mac', 'ssid')
[9053]77 def __unicode__(self):
78 return "%s - %s" % (self.mac, self.ssid)
[9006]79
[9579]80
81 def save(self, *args, **kwargs):
82 self.organization = self.get_organization(self.ssid)
83 super(Accespoint, self).save(*args, **kwargs)
84
[9026]85class Gebruiker(models.Model):
[9053]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)
[9026]90
[9041]91class Apparatuur(models.Model):
[9659]92 antenne = models.CharField(max_length=64, default="onbekend")
93 kaart = models.CharField(max_length=64, default="onbekend")
[9053]94 def __unicode__(self):
95 return "%s - %s" % (self.antenne, self.kaart)
[9633]96 class Meta:
97 verbose_name_plural = 'Apparatuur'
[9659]98 unique_together = ('antenne', 'kaart')
[9026]99
[9041]100class MeetRondje(models.Model):
[9633]101 datum = models.DateTimeField(blank=True,null=True,default=datetime.datetime.now)
[9097]102 naam = models.CharField(max_length=64)
[9053]103 gebruiker = models.ForeignKey(Gebruiker)
104 apparatuur = models.ForeignKey(Apparatuur)
105 def __unicode__(self):
[9054]106 return "%s - %s" % (self.gebruiker.naam, self.naam)
[9633]107 class Meta:
108 verbose_name_plural = 'MeetRondjes'
[9041]109
[9395]110class MeetBestand(models.Model):
111 meetrondje = models.ForeignKey(MeetRondje)
[9660]112 bestand = models.FileField(upload_to='scan-data/%Y/%m/%d')
[9633]113 is_imported = models.BooleanField(default=False)
114 class Meta:
115 verbose_name_plural = 'MeetBestanden'
[9660]116 unique_together = ('meetrondje', 'bestand')
[9637]117 def __unicode__(self):
118 return "%s - %s" % (self.meetrondje, self.bestand)
[9395]119
120
[9026]121class Meting(models.Model):
[9053]122 meetrondje = models.ForeignKey(MeetRondje)
123 accespoint = models.ForeignKey(Accespoint)
[9633]124 latitude = models.FloatField()
125 longitude = models.FloatField()
[9664]126 signaal = IntegerRangeField(max_length=3,min_value=MIN_SIGNAL,max_value=MAX_SIGNAL)
[9054]127 def __unicode__(self):
[9164]128 return "%s @ %.5f,%.5f : %s" % (self.accespoint.ssid, float(self.latitude), float(self.longitude), self.signaal)
[9179]129 class Meta:
130 # This implies that you cannot have multiple messurements on the same
131 # location for a single 'run', thus the data needs cleaned before to make
132 # this properly hold.
133 unique_together = ('meetrondje', 'accespoint', 'latitude', 'longitude'),
[9633]134 verbose_name_plural = 'Metingen'
135
Note: See TracBrowser for help on using the repository browser.