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

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

Organization needs to be a special Model to make fudgings around with it more easy.

File size: 4.6 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
59
60class 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 name = None
70 if ssid.startswith('ap') and ssid.endswith('wleiden.net'):
71 name = 'WirelessLeiden'
72 elif ssid.startswith('ap') and 'WirelessLeiden' in ssid:
73 name = 'WirelessLeiden'
74 return name
75
76class Node(models.Model):
77 name = models.CharField(max_length=50)
78 latitude = models.FloatField(null=True,blank=True)
79 longitude = models.FloatField(null=True,blank=True)
80 organization = models.ForeignKey(Organization,null=True, blank=True)
81 class Meta:
82 unique_together = ('name', 'organization')
83
84 def __unicode__(self):
85 return "%s - %s" % (self.name, self.organization)
86
87class Accespoint(models.Model):
88 mac = models.CharField(max_length=17)
89 ssid = models.CharField(max_length=64)
90 organization = models.ForeignKey(Organization,null=True, blank=True)
91 encryptie = models.BooleanField()
92 class Meta:
93 unique_together = ('mac', 'ssid')
94 def __unicode__(self):
95 return "%s - %s" % (self.mac, self.ssid)
96
97
98 def save(self, *args, **kwargs):
99 self.organization = self.get_organization(self.ssid)
100 super(Accespoint, self).save(*args, **kwargs)
101
102class Gebruiker(models.Model):
103 naam = models.CharField(max_length=64)
104 email = models.CharField(max_length=64)
105 def __unicode__(self):
106 return "%s - %s" % (self.naam, self.email)
107
108class Apparatuur(models.Model):
109 antenne = models.CharField(max_length=64)
110 kaart = models.CharField(max_length=64)
111 def __unicode__(self):
112 return "%s - %s" % (self.antenne, self.kaart)
113
114class MeetRondje(models.Model):
115 datum = models.DateTimeField(blank=True,null=True)
116 naam = models.CharField(max_length=64)
117 gebruiker = models.ForeignKey(Gebruiker)
118 apparatuur = models.ForeignKey(Apparatuur)
119 def __unicode__(self):
120 return "%s - %s" % (self.gebruiker.naam, self.naam)
121
122class MeetBestand(models.Model):
123 meetrondje = models.ForeignKey(MeetRondje)
124 bestand = models.FileField(upload_to='meet-bestand/%Y/%m/%d')
125
126
127class Meting(models.Model):
128 meetrondje = models.ForeignKey(MeetRondje)
129 accespoint = models.ForeignKey(Accespoint)
130 latitude = models.FloatField(name='Latitude', db_column='lat')
131 longitude = models.FloatField(name='Longitude', db_column='lng')
132 signaal = models.IntegerField(max_length=3)
133 objects = managers.MetingManager()
134 def __unicode__(self):
135 return "%s @ %.5f,%.5f : %s" % (self.accespoint.ssid, float(self.latitude), float(self.longitude), self.signaal)
136 class Meta:
137 # This implies that you cannot have multiple messurements on the same
138 # location for a single 'run', thus the data needs cleaned before to make
139 # this properly hold.
140 unique_together = ('meetrondje', 'accespoint', 'latitude', 'longitude'),
Note: See TracBrowser for help on using the repository browser.