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

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

Start importing clients as well, to generate an ignore list...

File size: 3.5 KB
Line 
1from gheat import managers
2import datetime
3import binascii
4import hashlib
5
6from django.core import validators
7from django.db import models
8from django.dispatch import receiver
9from django.utils.encoding import smart_unicode
10from django.utils.translation import ugettext_lazy as _
11
12class 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
42class 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
54class WirelessClient(models.Model):
55 mac = models.CharField(max_length=17, unique=True)
56 def __unicode__(self):
57 return self.mac
58
59class Accespoint(models.Model):
60 mac = models.CharField(max_length=17)
61 ssid = models.CharField(max_length=64)
62 encryptie = models.BooleanField()
63 class Meta:
64 unique_together = ('mac', 'ssid')
65 def __unicode__(self):
66 return "%s - %s" % (self.mac, self.ssid)
67
68class Gebruiker(models.Model):
69 naam = models.CharField(max_length=64)
70 email = models.CharField(max_length=64)
71 def __unicode__(self):
72 return "%s - %s" % (self.naam, self.email)
73
74class Apparatuur(models.Model):
75 antenne = models.CharField(max_length=64)
76 kaart = models.CharField(max_length=64)
77 def __unicode__(self):
78 return "%s - %s" % (self.antenne, self.kaart)
79
80class MeetRondje(models.Model):
81 datum = models.DateTimeField(blank=True,null=True)
82 naam = models.CharField(max_length=64)
83 gebruiker = models.ForeignKey(Gebruiker)
84 apparatuur = models.ForeignKey(Apparatuur)
85 def __unicode__(self):
86 return "%s - %s" % (self.gebruiker.naam, self.naam)
87
88class MeetBestand(models.Model):
89 meetrondje = models.ForeignKey(MeetRondje)
90 bestand = models.FileField(upload_to='meet-bestand/%Y/%m/%d')
91
92
93class Meting(models.Model):
94 meetrondje = models.ForeignKey(MeetRondje)
95 accespoint = models.ForeignKey(Accespoint)
96 latitude = models.FloatField(name='Latitude', db_column='lat')
97 longitude = models.FloatField(name='Longitude', db_column='lng')
98 signaal = models.IntegerField(max_length=3)
99 objects = managers.MetingManager()
100 def __unicode__(self):
101 return "%s @ %.5f,%.5f : %s" % (self.accespoint.ssid, float(self.latitude), float(self.longitude), self.signaal)
102 class Meta:
103 # This implies that you cannot have multiple messurements on the same
104 # location for a single 'run', thus the data needs cleaned before to make
105 # this properly hold.
106 unique_together = ('meetrondje', 'accespoint', 'latitude', 'longitude'),
Note: See TracBrowser for help on using the repository browser.