2010-11-26 15:07:24 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
# vi:si:et:sw=4:sts=4:ts=4
|
|
|
|
from __future__ import division, with_statement
|
2011-02-24 19:22:20 +00:00
|
|
|
from datetime import datetime
|
2010-11-26 15:07:24 +00:00
|
|
|
from django.db import models
|
|
|
|
|
2011-01-01 11:44:42 +00:00
|
|
|
import ox
|
2010-11-26 15:07:24 +00:00
|
|
|
from ox.django import fields
|
2011-02-24 18:39:58 +00:00
|
|
|
from django.contrib.auth.models import User, Group
|
2010-11-26 15:07:24 +00:00
|
|
|
|
|
|
|
import managers
|
|
|
|
|
2011-01-01 11:44:42 +00:00
|
|
|
|
2010-11-26 15:07:24 +00:00
|
|
|
class Place(models.Model):
|
|
|
|
'''
|
|
|
|
Places are named locations, they should have geographical information attached to them.
|
|
|
|
'''
|
2011-02-24 18:39:58 +00:00
|
|
|
created = models.DateTimeField(auto_now_add=True)
|
|
|
|
modified = models.DateTimeField(auto_now=True)
|
2011-02-24 18:44:35 +00:00
|
|
|
user = models.ForeignKey(User, null=True, related_name='places')
|
2010-11-26 15:07:24 +00:00
|
|
|
|
2011-02-24 18:39:58 +00:00
|
|
|
name = fields.TupleField(default=[])
|
2010-11-26 15:07:24 +00:00
|
|
|
name_sort = models.CharField(max_length=200)
|
|
|
|
name_find = models.TextField(default='', editable=False)
|
|
|
|
|
|
|
|
geoname = models.CharField(max_length=1024, unique=True)
|
2011-02-24 18:39:58 +00:00
|
|
|
geoname_sort = models.CharField(max_length=1024, unique=True)
|
2010-11-26 15:07:24 +00:00
|
|
|
|
|
|
|
wikipediaId = models.CharField(max_length=1000, blank=True)
|
|
|
|
|
2011-02-24 18:39:58 +00:00
|
|
|
south = models.FloatField(default=0)
|
|
|
|
west = models.FloatField(default=0)
|
|
|
|
north = models.FloatField(default=0)
|
|
|
|
east = models.FloatField(default=0)
|
|
|
|
lat = models.FloatField(default=0)
|
|
|
|
lng = models.FloatField(default=0)
|
2011-02-24 19:22:20 +00:00
|
|
|
size = models.FloatField(default=0)
|
2010-11-26 15:07:24 +00:00
|
|
|
|
|
|
|
objects = managers.PlaceManager()
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
ordering = ('name_sort', )
|
|
|
|
|
|
|
|
def __unicode__(self):
|
|
|
|
return self.name
|
|
|
|
|
2011-02-24 18:39:58 +00:00
|
|
|
def editable(self, user):
|
|
|
|
if user.is_staff or self.user == user:
|
|
|
|
return True
|
|
|
|
return False
|
|
|
|
|
|
|
|
def get_id(self):
|
|
|
|
return ox.to32(self.id)
|
|
|
|
|
2011-02-24 19:22:20 +00:00
|
|
|
def json(self, user=None):
|
2011-02-24 18:39:58 +00:00
|
|
|
j = {
|
|
|
|
'id': self.get_id(),
|
|
|
|
'user': self.user.username,
|
|
|
|
}
|
|
|
|
for key in ('created', 'modified',
|
|
|
|
'name', 'geoname',
|
|
|
|
'south', 'west', 'north', 'east',
|
|
|
|
'lat', 'lng', 'size'):
|
2010-11-26 15:07:24 +00:00
|
|
|
j[key] = getattr(self, key)
|
2011-02-24 19:22:20 +00:00
|
|
|
|
|
|
|
if isinstance(j[key], datetime):
|
|
|
|
j[key] = j[key].strftime('%Y-%m-%dT%H:%M:%SZ')
|
2011-02-24 18:39:58 +00:00
|
|
|
return j
|
2010-11-26 15:07:24 +00:00
|
|
|
|
|
|
|
def save(self, *args, **kwargs):
|
|
|
|
if not self.name_sort:
|
2011-02-24 18:39:58 +00:00
|
|
|
self.name_sort = ', '.join(self.name)
|
|
|
|
self.geoname_sort = ', '.join(reversed(self.geoname.split(', ')))
|
2010-11-26 15:07:24 +00:00
|
|
|
|
2011-02-24 18:39:58 +00:00
|
|
|
self.name_find = '|%s|'%'|'.join(self.name)
|
2010-11-26 15:07:24 +00:00
|
|
|
|
|
|
|
#update center
|
2011-02-24 18:39:58 +00:00
|
|
|
#self.lat = ox.location.center(self.south, self.north)
|
|
|
|
#self.lng = ox.location.center(self.east, self.west)
|
2010-11-26 15:07:24 +00:00
|
|
|
|
|
|
|
#update area
|
2011-02-24 18:39:58 +00:00
|
|
|
#self.size = ox.location.area(self.south, self.west, self.north, self.east)
|
2010-11-26 15:07:24 +00:00
|
|
|
|
|
|
|
super(Place, self).save(*args, **kwargs)
|