cablegates/pandora/place/models.py

85 lines
2.7 KiB
Python
Raw Normal View History

# -*- coding: utf-8 -*-
# vi:si:et:sw=4:sts=4:ts=4
from __future__ import division, with_statement
from django.db import models
2011-01-01 11:44:42 +00:00
import ox
from ox.django import fields
2011-02-24 18:39:58 +00:00
from django.contrib.auth.models import User, Group
import managers
2011-01-01 11:44:42 +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')
2011-03-05 14:31:56 +00:00
name = models.CharField(max_length=1024)
2011-05-28 13:57:47 +00:00
alternativeNames = fields.TupleField(default=[])
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)
2011-02-25 10:23:46 +00:00
countryCode = models.CharField(max_length=16, default='')
wikipediaId = models.CharField(max_length=1000, blank=True)
2011-03-03 22:37:37 +00:00
type= 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-05-28 13:57:47 +00:00
area = models.FloatField(default=0)
2011-03-03 22:37:37 +00:00
matches = models.IntegerField(default=0)
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',
2011-05-28 13:57:47 +00:00
'name', 'alternativeNames', 'geoname', 'countryCode',
2011-02-24 18:39:58 +00:00
'south', 'west', 'north', 'east',
2011-05-28 13:57:47 +00:00
'lat', 'lng', 'area', 'matches', 'type'):
j[key] = getattr(self, key)
2011-02-24 18:39:58 +00:00
return j
def save(self, *args, **kwargs):
if not self.name_sort:
2011-03-05 14:31:56 +00:00
self.name_sort = self.name #', '.join(self.name)
2011-02-24 18:39:58 +00:00
self.geoname_sort = ', '.join(reversed(self.geoname.split(', ')))
2011-05-28 13:57:47 +00:00
self.name_find = '|%s|'%'|'.join([self.name]+list(self.alternativeNames))
#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)
#update area
2011-05-28 13:57:47 +00:00
#self.area= ox.location.area(self.south, self.west, self.north, self.east)
super(Place, self).save(*args, **kwargs)