1 | from gheat import managers
|
---|
2 | import datetime
|
---|
3 | import binascii
|
---|
4 | import hashlib
|
---|
5 |
|
---|
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 |
|
---|
54 | class WirelessClient(models.Model):
|
---|
55 | mac = models.CharField(max_length=17, unique=True)
|
---|
56 | def __unicode__(self):
|
---|
57 | return self.mac
|
---|
58 |
|
---|
59 |
|
---|
60 | ORGANIZATION_CHOICES = (
|
---|
61 | ('WirelessLeiden', 'Wireless Leiden'),
|
---|
62 | )
|
---|
63 |
|
---|
64 | class Node(models.Model):
|
---|
65 | name = models.CharField(max_length=50)
|
---|
66 | latitude = models.FloatField(null=True,blank=True)
|
---|
67 | longitude = models.FloatField(null=True,blank=True)
|
---|
68 | organization = models.CharField(max_length="50",choices=ORGANIZATION_CHOICES,null=True,blank=True)
|
---|
69 |
|
---|
70 | class Accespoint(models.Model):
|
---|
71 | mac = models.CharField(max_length=17)
|
---|
72 | ssid = models.CharField(max_length=64)
|
---|
73 | organization = models.CharField(max_length="50",choices=ORGANIZATION_CHOICES,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 | @staticmethod
|
---|
81 | def get_organization(ssid):
|
---|
82 | """ Try to determine the organization via the SSID """
|
---|
83 | organization = ''
|
---|
84 | if ssid.startswith('ap') and ssid.endswith('wleiden.net'):
|
---|
85 | organization = 'WirelessLeiden'
|
---|
86 | elif ssid.startswith('ap') and 'WirelessLeiden' in ssid:
|
---|
87 | organization = 'WirelessLeiden'
|
---|
88 | return organization
|
---|
89 |
|
---|
90 | def save(self, *args, **kwargs):
|
---|
91 | self.organization = self.get_organization(self.ssid)
|
---|
92 | super(Accespoint, self).save(*args, **kwargs)
|
---|
93 |
|
---|
94 | class Gebruiker(models.Model):
|
---|
95 | naam = models.CharField(max_length=64)
|
---|
96 | email = models.CharField(max_length=64)
|
---|
97 | def __unicode__(self):
|
---|
98 | return "%s - %s" % (self.naam, self.email)
|
---|
99 |
|
---|
100 | class Apparatuur(models.Model):
|
---|
101 | antenne = models.CharField(max_length=64)
|
---|
102 | kaart = models.CharField(max_length=64)
|
---|
103 | def __unicode__(self):
|
---|
104 | return "%s - %s" % (self.antenne, self.kaart)
|
---|
105 |
|
---|
106 | class MeetRondje(models.Model):
|
---|
107 | datum = models.DateTimeField(blank=True,null=True)
|
---|
108 | naam = models.CharField(max_length=64)
|
---|
109 | gebruiker = models.ForeignKey(Gebruiker)
|
---|
110 | apparatuur = models.ForeignKey(Apparatuur)
|
---|
111 | def __unicode__(self):
|
---|
112 | return "%s - %s" % (self.gebruiker.naam, self.naam)
|
---|
113 |
|
---|
114 | class MeetBestand(models.Model):
|
---|
115 | meetrondje = models.ForeignKey(MeetRondje)
|
---|
116 | bestand = models.FileField(upload_to='meet-bestand/%Y/%m/%d')
|
---|
117 |
|
---|
118 |
|
---|
119 | class Meting(models.Model):
|
---|
120 | meetrondje = models.ForeignKey(MeetRondje)
|
---|
121 | accespoint = models.ForeignKey(Accespoint)
|
---|
122 | latitude = models.FloatField(name='Latitude', db_column='lat')
|
---|
123 | longitude = models.FloatField(name='Longitude', db_column='lng')
|
---|
124 | signaal = models.IntegerField(max_length=3)
|
---|
125 | objects = managers.MetingManager()
|
---|
126 | def __unicode__(self):
|
---|
127 | return "%s @ %.5f,%.5f : %s" % (self.accespoint.ssid, float(self.latitude), float(self.longitude), self.signaal)
|
---|
128 | class Meta:
|
---|
129 | # This implies that you cannot have multiple messurements on the same
|
---|
130 | # location for a single 'run', thus the data needs cleaned before to make
|
---|
131 | # this properly hold.
|
---|
132 | unique_together = ('meetrondje', 'accespoint', 'latitude', 'longitude'),
|
---|