SHOW:
|
|
- or go back to the newest paste.
| 1 | class Coordinates(object): | |
| 2 | ||
| 3 | def __init__(self, lng, lat): | |
| 4 | super(Coordinates, self).__init__() | |
| 5 | self.lng = float(lng) | |
| 6 | self.lat = float(lat) | |
| 7 | ||
| 8 | def to_sql(self): | |
| 9 | return "GeomFromText('POINT(%s %s)')" % (self.lng, self.lat)
| |
| 10 | ||
| 11 | def __unicode__(self): | |
| 12 | return 'Coordinates(lat=%s, lng=%s)' % (self.lat, self.lng) | |
| 13 | ||
| 14 | @staticmethod | |
| 15 | def to_python(value): | |
| 16 | - | return Coordinates(0, 0)# value |
| 16 | + | return value |
| 17 | - | lat = None |
| 17 | + | # write the rest when i know what to do |
| 18 | - | lng = None |
| 18 | + | return Coordinates(0, 0) |
| 19 | ||
| 20 | - | if isinstance(value, (list, tuple)): |
| 20 | + | |
| 21 | - | lat, lng = value |
| 21 | + | |
| 22 | - | return Coordinates(lat, lng) |
| 22 | + | |
| 23 | __metaclass__ = models.SubfieldBase | |
| 24 | - | if isinstance(value, (str, unicode)): |
| 24 | + | |
| 25 | - | lat, lng = value.split(' ')
|
| 25 | + | |
| 26 | - | return Coordinates(lat, lng) |
| 26 | + | |
| 27 | super(GeometryField, self).__init__(*args, **kwargs) | |
| 28 | self.spatial = True | |
| 29 | ||
| 30 | def db_type(self, connection): | |
| 31 | return 'GEOMETRY' | |
| 32 | ||
| 33 | def to_python(self, value): | |
| 34 | if isinstance(value, Coordinates): | |
| 35 | return value | |
| 36 | ||
| 37 | return Coordinates.to_python(value) | |
| 38 | ||
| 39 | def get_prep_value(self, value): | |
| 40 | return self.to_python(value) | |
| 41 | ||
| 42 | def get_db_prep_save(self, value, connection): | |
| 43 | return self.to_python(value).to_sql() | |
| 44 | ||
| 45 | def value_to_string(self, obj): | |
| 46 | value = self._get_val_from_obj(obj) | |
| 47 | return self.get_db_prep_value(value) |