- Timestamp:
- Sep 5, 2011, 3:50:22 PM (13 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
src/django_gheat/gheat/models.py
r9660 r9663 1 from gheat import managers2 1 import datetime 3 import binascii4 import hashlib5 2 6 3 from django.core import validators 7 from django.core.files.storage import FileSystemStorage8 4 from django.db import models 9 from django.dispatch import receiver10 from django.utils.encoding import smart_unicode11 from django.utils.translation import ugettext_lazy as _12 5 13 14 # http://djangosnippets.org/snippets/976/15 class OverwriteStorage(FileSystemStorage):16 def get_available_name(self, name):17 """18 Returns a filename that's free on the target storage system, and19 available for new content to be written to.20 """21 # If the filename already exists, remove it as if it was a true file system22 if self.exists(name):23 self.delete(name)24 return name25 26 class BinaryField(models.Field):27 MAGIC = '###MAGIC###'28 description = _('Binary object using base64 (up to %(max_length)s)')29 30 __metaclass__ = models.SubfieldBase31 32 def __init__(self, *args, **kwargs):33 # base64 roughly max the binary size with with 4 times34 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 value43 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 target49 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 56 class 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.key63 64 @staticmethod65 def make_key(source):66 return hashlib.md5(source).hexdigest()67 6 68 7 class WirelessClient(models.Model): … … 172 111 longitude = models.FloatField() 173 112 signaal = models.IntegerField(max_length=3) 174 objects = managers.MetingManager()175 113 def __unicode__(self): 176 114 return "%s @ %.5f,%.5f : %s" % (self.accespoint.ssid, float(self.latitude), float(self.longitude), self.signaal)
Note:
See TracChangeset
for help on using the changeset viewer.