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