[9006] | 1 | from gheat import managers
|
---|
[9026] | 2 | import datetime
|
---|
[9184] | 3 | import binascii
|
---|
| 4 | import hashlib
|
---|
[9006] | 5 |
|
---|
[9184] | 6 | from django.core import validators
|
---|
| 7 | from django.db import models
|
---|
| 8 | from django.dispatch import receiver
|
---|
| 9 | from django.utils.encoding import smart_unicode
|
---|
| 10 | from django.utils.translation import ugettext_lazy as _
|
---|
| 11 |
|
---|
| 12 | class BinaryField(models.Field):
|
---|
| 13 | MAGIC = '###MAGIC###'
|
---|
| 14 | description = _('Binary object using base64 (up to %(max_length)s)')
|
---|
| 15 |
|
---|
| 16 | __metaclass__ = models.SubfieldBase
|
---|
| 17 |
|
---|
| 18 | def __init__(self, *args, **kwargs):
|
---|
| 19 | # base64 roughly max the binary size with with 4 times
|
---|
| 20 | kwargs['max_length'] = kwargs['max_length'] * 4 + len(self.MAGIC)
|
---|
| 21 | super(BinaryField, self).__init__(*args, **kwargs)
|
---|
| 22 | self.validators.append(validators.MaxLengthValidator(self.max_length))
|
---|
| 23 |
|
---|
| 24 | def to_python(self,value):
|
---|
| 25 | if value.startswith(self.MAGIC):
|
---|
| 26 | return binascii.a2b_base64(value[len(self.MAGIC):])
|
---|
| 27 | else:
|
---|
| 28 | return value
|
---|
| 29 |
|
---|
| 30 | def get_db_prep_value(self, value, connection, prepared=False):
|
---|
| 31 | target = self.MAGIC + binascii.b2a_base64(value)
|
---|
| 32 | if len(target) > self.max_length:
|
---|
| 33 | raise ValueError(len(target))
|
---|
| 34 | return target
|
---|
| 35 |
|
---|
| 36 | def get_prep_lookup(self, lookup_type, value):
|
---|
| 37 | raise TypeError('Lookup type %r not supported.' % lookup_type)
|
---|
| 38 |
|
---|
| 39 | def get_internal_type(self):
|
---|
| 40 | return 'TextField'
|
---|
| 41 |
|
---|
| 42 | class TileCache(models.Model):
|
---|
| 43 | key = models.CharField(max_length=34,unique=True)
|
---|
| 44 | data = BinaryField(max_length=10000)
|
---|
| 45 | creation_time = models.DateTimeField(auto_now_add=True)
|
---|
| 46 |
|
---|
| 47 | def __unicode__(self):
|
---|
| 48 | return self.key
|
---|
| 49 |
|
---|
| 50 | @staticmethod
|
---|
| 51 | def make_key(source):
|
---|
| 52 | return hashlib.md5(source).hexdigest()
|
---|
| 53 |
|
---|
[9562] | 54 | class WirelessClient(models.Model):
|
---|
| 55 | mac = models.CharField(max_length=17, unique=True)
|
---|
| 56 | def __unicode__(self):
|
---|
| 57 | return self.mac
|
---|
[9184] | 58 |
|
---|
[9579] | 59 |
|
---|
[9592] | 60 | class Organization(models.Model):
|
---|
| 61 | fullname = models.CharField(max_length=100,unique=True)
|
---|
| 62 | name = models.CharField(max_length=100,unique=True)
|
---|
| 63 | def __unicode__(self):
|
---|
| 64 | return self.fullname
|
---|
[9579] | 65 |
|
---|
[9592] | 66 | @staticmethod
|
---|
| 67 | def get_name_by_ssid(ssid):
|
---|
| 68 | """ Try to determine the organization via the SSID """
|
---|
[9593] | 69 | ssid = ssid.lower()
|
---|
[9592] | 70 | name = None
|
---|
| 71 | if ssid.startswith('ap') and ssid.endswith('wleiden.net'):
|
---|
| 72 | name = 'WirelessLeiden'
|
---|
[9593] | 73 | elif ssid.startswith('il-') and ssid.endswith('wleiden.net'):
|
---|
[9592] | 74 | name = 'WirelessLeiden'
|
---|
[9593] | 75 | elif ssid.startswith('ap') and 'wirelessleiden' in ssid:
|
---|
| 76 | name = 'WirelessLeiden'
|
---|
| 77 | elif ssid.startswith('ap-') and 'westeinder' in ssid:
|
---|
| 78 | name = 'WirelessPlassen'
|
---|
| 79 | elif ssid.endswith('walphen.net'):
|
---|
| 80 | name = 'WirelessAlphen'
|
---|
| 81 | elif 'wirelessarnhem' in ssid:
|
---|
| 82 | name = 'WirelessArnhem'
|
---|
[9592] | 83 | return name
|
---|
| 84 |
|
---|
[9593] | 85 | @staticmethod
|
---|
| 86 | def get_by_ssid(ssid):
|
---|
| 87 | name = Organization.get_name_by_ssid(ssid)
|
---|
| 88 | if not name:
|
---|
| 89 | return None
|
---|
| 90 | else:
|
---|
| 91 | return Organization.objects.get(name=name)
|
---|
| 92 |
|
---|
| 93 |
|
---|
[9590] | 94 | class Node(models.Model):
|
---|
[9593] | 95 | name = models.CharField(max_length=50, unique=True)
|
---|
[9590] | 96 | latitude = models.FloatField(null=True,blank=True)
|
---|
| 97 | longitude = models.FloatField(null=True,blank=True)
|
---|
[9592] | 98 | organization = models.ForeignKey(Organization,null=True, blank=True)
|
---|
[9590] | 99 |
|
---|
[9592] | 100 | def __unicode__(self):
|
---|
| 101 | return "%s - %s" % (self.name, self.organization)
|
---|
| 102 |
|
---|
[9026] | 103 | class Accespoint(models.Model):
|
---|
[9053] | 104 | mac = models.CharField(max_length=17)
|
---|
| 105 | ssid = models.CharField(max_length=64)
|
---|
[9592] | 106 | organization = models.ForeignKey(Organization,null=True, blank=True)
|
---|
[9053] | 107 | encryptie = models.BooleanField()
|
---|
[9562] | 108 | class Meta:
|
---|
| 109 | unique_together = ('mac', 'ssid')
|
---|
[9053] | 110 | def __unicode__(self):
|
---|
| 111 | return "%s - %s" % (self.mac, self.ssid)
|
---|
[9006] | 112 |
|
---|
[9579] | 113 |
|
---|
| 114 | def save(self, *args, **kwargs):
|
---|
| 115 | self.organization = self.get_organization(self.ssid)
|
---|
| 116 | super(Accespoint, self).save(*args, **kwargs)
|
---|
| 117 |
|
---|
[9026] | 118 | class Gebruiker(models.Model):
|
---|
[9053] | 119 | naam = models.CharField(max_length=64)
|
---|
| 120 | email = models.CharField(max_length=64)
|
---|
| 121 | def __unicode__(self):
|
---|
| 122 | return "%s - %s" % (self.naam, self.email)
|
---|
[9026] | 123 |
|
---|
[9041] | 124 | class Apparatuur(models.Model):
|
---|
[9053] | 125 | antenne = models.CharField(max_length=64)
|
---|
| 126 | kaart = models.CharField(max_length=64)
|
---|
| 127 | def __unicode__(self):
|
---|
| 128 | return "%s - %s" % (self.antenne, self.kaart)
|
---|
[9633] | 129 | class Meta:
|
---|
| 130 | verbose_name_plural = 'Apparatuur'
|
---|
[9026] | 131 |
|
---|
[9041] | 132 | class MeetRondje(models.Model):
|
---|
[9633] | 133 | datum = models.DateTimeField(blank=True,null=True,default=datetime.datetime.now)
|
---|
[9097] | 134 | naam = models.CharField(max_length=64)
|
---|
[9053] | 135 | gebruiker = models.ForeignKey(Gebruiker)
|
---|
| 136 | apparatuur = models.ForeignKey(Apparatuur)
|
---|
| 137 | def __unicode__(self):
|
---|
[9054] | 138 | return "%s - %s" % (self.gebruiker.naam, self.naam)
|
---|
[9633] | 139 | class Meta:
|
---|
| 140 | verbose_name_plural = 'MeetRondjes'
|
---|
[9041] | 141 |
|
---|
[9395] | 142 | class MeetBestand(models.Model):
|
---|
| 143 | meetrondje = models.ForeignKey(MeetRondje)
|
---|
[9633] | 144 | bestand = models.FileField(upload_to='scan-data/%Y/%m/%d')
|
---|
| 145 | is_imported = models.BooleanField(default=False)
|
---|
| 146 | class Meta:
|
---|
| 147 | verbose_name_plural = 'MeetBestanden'
|
---|
[9395] | 148 |
|
---|
| 149 |
|
---|
[9026] | 150 | class Meting(models.Model):
|
---|
[9053] | 151 | meetrondje = models.ForeignKey(MeetRondje)
|
---|
| 152 | accespoint = models.ForeignKey(Accespoint)
|
---|
[9633] | 153 | latitude = models.FloatField()
|
---|
| 154 | longitude = models.FloatField()
|
---|
[9053] | 155 | signaal = models.IntegerField(max_length=3)
|
---|
| 156 | objects = managers.MetingManager()
|
---|
[9054] | 157 | def __unicode__(self):
|
---|
[9164] | 158 | return "%s @ %.5f,%.5f : %s" % (self.accespoint.ssid, float(self.latitude), float(self.longitude), self.signaal)
|
---|
[9179] | 159 | class Meta:
|
---|
| 160 | # This implies that you cannot have multiple messurements on the same
|
---|
| 161 | # location for a single 'run', thus the data needs cleaned before to make
|
---|
| 162 | # this properly hold.
|
---|
| 163 | unique_together = ('meetrondje', 'accespoint', 'latitude', 'longitude'),
|
---|
[9633] | 164 | verbose_name_plural = 'Metingen'
|
---|
| 165 |
|
---|