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

Last change on this file since 9639 was 9638, checked in by rick, 14 years ago

Files does not has to be unique.

File size: 5.8 KB
Line 
1from gheat import managers
2import datetime
3import binascii
4import hashlib
5
6from django.core import validators
7from django.core.files.storage import FileSystemStorage
8from django.db import models
9from django.dispatch import receiver
10from django.utils.encoding import smart_unicode
11from django.utils.translation import ugettext_lazy as _
12
13
14# http://djangosnippets.org/snippets/976/
15class 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
26class 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
56class 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
68class WirelessClient(models.Model):
69 mac = models.CharField(max_length=17, unique=True)
70 def __unicode__(self):
71 return self.mac
72
73
74class 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
79
80 @staticmethod
81 def get_name_by_ssid(ssid):
82 """ Try to determine the organization via the SSID """
83 ssid = ssid.lower()
84 name = None
85 if ssid.startswith('ap') and ssid.endswith('wleiden.net'):
86 name = 'WirelessLeiden'
87 elif ssid.startswith('il-') and ssid.endswith('wleiden.net'):
88 name = 'WirelessLeiden'
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'
97 return name
98
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
108class Node(models.Model):
109 name = models.CharField(max_length=50, unique=True)
110 latitude = models.FloatField(null=True,blank=True)
111 longitude = models.FloatField(null=True,blank=True)
112 organization = models.ForeignKey(Organization,null=True, blank=True)
113
114 def __unicode__(self):
115 return "%s - %s" % (self.name, self.organization)
116
117class Accespoint(models.Model):
118 mac = models.CharField(max_length=17)
119 ssid = models.CharField(max_length=64)
120 organization = models.ForeignKey(Organization,null=True, blank=True)
121 encryptie = models.BooleanField()
122 class Meta:
123 unique_together = ('mac', 'ssid')
124 def __unicode__(self):
125 return "%s - %s" % (self.mac, self.ssid)
126
127
128 def save(self, *args, **kwargs):
129 self.organization = self.get_organization(self.ssid)
130 super(Accespoint, self).save(*args, **kwargs)
131
132class Gebruiker(models.Model):
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)
137
138class Apparatuur(models.Model):
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)
143 class Meta:
144 verbose_name_plural = 'Apparatuur'
145
146class MeetRondje(models.Model):
147 datum = models.DateTimeField(blank=True,null=True,default=datetime.datetime.now)
148 naam = models.CharField(max_length=64)
149 gebruiker = models.ForeignKey(Gebruiker)
150 apparatuur = models.ForeignKey(Apparatuur)
151 def __unicode__(self):
152 return "%s - %s" % (self.gebruiker.naam, self.naam)
153 class Meta:
154 verbose_name_plural = 'MeetRondjes'
155
156class MeetBestand(models.Model):
157 meetrondje = models.ForeignKey(MeetRondje)
158 bestand = models.FileField(upload_to='scan-data/%Y/%m/%d',storage=OverwriteStorage())
159 is_imported = models.BooleanField(default=False)
160 class Meta:
161 verbose_name_plural = 'MeetBestanden'
162 def __unicode__(self):
163 return "%s - %s" % (self.meetrondje, self.bestand)
164
165
166class Meting(models.Model):
167 meetrondje = models.ForeignKey(MeetRondje)
168 accespoint = models.ForeignKey(Accespoint)
169 latitude = models.FloatField()
170 longitude = models.FloatField()
171 signaal = models.IntegerField(max_length=3)
172 objects = managers.MetingManager()
173 def __unicode__(self):
174 return "%s @ %.5f,%.5f : %s" % (self.accespoint.ssid, float(self.latitude), float(self.longitude), self.signaal)
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'),
180 verbose_name_plural = 'Metingen'
181
Note: See TracBrowser for help on using the repository browser.