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 | 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
|
---|
65 |
|
---|
66 | @staticmethod
|
---|
67 | def get_name_by_ssid(ssid):
|
---|
68 | """ Try to determine the organization via the SSID """
|
---|
69 | ssid = ssid.lower()
|
---|
70 | name = None
|
---|
71 | if ssid.startswith('ap') and ssid.endswith('wleiden.net'):
|
---|
72 | name = 'WirelessLeiden'
|
---|
73 | elif ssid.startswith('il-') and ssid.endswith('wleiden.net'):
|
---|
74 | name = 'WirelessLeiden'
|
---|
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'
|
---|
83 | return name
|
---|
84 |
|
---|
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 |
|
---|
94 | class Node(models.Model):
|
---|
95 | name = models.CharField(max_length=50, unique=True)
|
---|
96 | latitude = models.FloatField(null=True,blank=True)
|
---|
97 | longitude = models.FloatField(null=True,blank=True)
|
---|
98 | organization = models.ForeignKey(Organization,null=True, blank=True)
|
---|
99 |
|
---|
100 | def __unicode__(self):
|
---|
101 | return "%s - %s" % (self.name, self.organization)
|
---|
102 |
|
---|
103 | class Accespoint(models.Model):
|
---|
104 | mac = models.CharField(max_length=17)
|
---|
105 | ssid = models.CharField(max_length=64)
|
---|
106 | organization = models.ForeignKey(Organization,null=True, blank=True)
|
---|
107 | encryptie = models.BooleanField()
|
---|
108 | class Meta:
|
---|
109 | unique_together = ('mac', 'ssid')
|
---|
110 | def __unicode__(self):
|
---|
111 | return "%s - %s" % (self.mac, self.ssid)
|
---|
112 |
|
---|
113 |
|
---|
114 | def save(self, *args, **kwargs):
|
---|
115 | self.organization = self.get_organization(self.ssid)
|
---|
116 | super(Accespoint, self).save(*args, **kwargs)
|
---|
117 |
|
---|
118 | class Gebruiker(models.Model):
|
---|
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)
|
---|
123 |
|
---|
124 | class Apparatuur(models.Model):
|
---|
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)
|
---|
129 | class Meta:
|
---|
130 | verbose_name_plural = 'Apparatuur'
|
---|
131 |
|
---|
132 | class MeetRondje(models.Model):
|
---|
133 | datum = models.DateTimeField(blank=True,null=True,default=datetime.datetime.now)
|
---|
134 | naam = models.CharField(max_length=64)
|
---|
135 | gebruiker = models.ForeignKey(Gebruiker)
|
---|
136 | apparatuur = models.ForeignKey(Apparatuur)
|
---|
137 | def __unicode__(self):
|
---|
138 | return "%s - %s" % (self.gebruiker.naam, self.naam)
|
---|
139 | class Meta:
|
---|
140 | verbose_name_plural = 'MeetRondjes'
|
---|
141 |
|
---|
142 | class MeetBestand(models.Model):
|
---|
143 | meetrondje = models.ForeignKey(MeetRondje)
|
---|
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'
|
---|
148 | def __unicode__(self):
|
---|
149 | return "%s - %s" % (self.meetrondje, self.bestand)
|
---|
150 |
|
---|
151 |
|
---|
152 | class Meting(models.Model):
|
---|
153 | meetrondje = models.ForeignKey(MeetRondje)
|
---|
154 | accespoint = models.ForeignKey(Accespoint)
|
---|
155 | latitude = models.FloatField()
|
---|
156 | longitude = models.FloatField()
|
---|
157 | signaal = models.IntegerField(max_length=3)
|
---|
158 | objects = managers.MetingManager()
|
---|
159 | def __unicode__(self):
|
---|
160 | return "%s @ %.5f,%.5f : %s" % (self.accespoint.ssid, float(self.latitude), float(self.longitude), self.signaal)
|
---|
161 | class Meta:
|
---|
162 | # This implies that you cannot have multiple messurements on the same
|
---|
163 | # location for a single 'run', thus the data needs cleaned before to make
|
---|
164 | # this properly hold.
|
---|
165 | unique_together = ('meetrondje', 'accespoint', 'latitude', 'longitude'),
|
---|
166 | verbose_name_plural = 'Metingen'
|
---|
167 |
|
---|