Changeset 9184 for src/django_gheat/gheat
- Timestamp:
- May 12, 2011, 12:05:59 PM (14 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
src/django_gheat/gheat/models.py
r9179 r9184 1 from django.db import models2 1 from gheat import managers 3 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 4 54 5 55 class Accespoint(models.Model):
Note:
See TracChangeset
for help on using the changeset viewer.