Changeset 9663 for src


Ignore:
Timestamp:
Sep 5, 2011, 3:50:22 PM (13 years ago)
Author:
rick
Message:

Cleanup of old cuft.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/django_gheat/gheat/models.py

    r9660 r9663  
    1 from gheat import managers
    21import datetime
    3 import binascii
    4 import hashlib
    52
    63from django.core import validators
    7 from django.core.files.storage import FileSystemStorage
    84from django.db import models
    9 from django.dispatch import receiver
    10 from django.utils.encoding import smart_unicode
    11 from django.utils.translation import ugettext_lazy as _
    125
    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, 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 
    26 class 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 
    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.key
    63 
    64   @staticmethod
    65   def make_key(source):
    66     return hashlib.md5(source).hexdigest()
    676
    687class WirelessClient(models.Model):
     
    172111  longitude = models.FloatField()
    173112  signaal = models.IntegerField(max_length=3)
    174   objects = managers.MetingManager()
    175113  def __unicode__(self):
    176114    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.