cablegates/pandora/place/models.py

111 lines
3.8 KiB
Python
Raw Permalink 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
from django.db.models import Q
import managers
from annotation.models import Annotation
2011-06-05 16:30:30 +00:00
from item.models import Item
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-05-30 11:23:49 +00:00
type = models.CharField(max_length=1000, default='')
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)
2011-06-05 16:30:30 +00:00
items = models.ManyToManyField(Item, blank=True, related_name='places')
2011-06-20 15:02:53 +00:00
annotations = models.ManyToManyField(Annotation, blank=True, related_name='places')
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):
2011-05-30 09:24:30 +00:00
if user and user.is_staff or self.user == user:
2011-02-24 18:39:58 +00:00
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,
2011-05-30 09:24:30 +00:00
'editable': self.editable(user)
2011-02-24 18:39:58 +00:00
}
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
2011-06-02 08:19:15 +00:00
def get_matches(self):
2011-06-20 15:02:53 +00:00
q = Q(value__contains=" " + self.name)|Q(value__startswith=self.name)
for name in self.alternativeNames:
2011-06-20 15:02:53 +00:00
q = q|Q(value__contains=" " + name)|Q(value__startswith=name)
2011-06-02 08:19:15 +00:00
return Annotation.objects.filter(q)
2011-06-01 22:47:15 +00:00
2011-06-02 08:19:15 +00:00
def update_matches(self):
2011-06-05 16:30:30 +00:00
matches = self.get_matches()
self.matches = matches.count()
2011-06-20 15:02:53 +00:00
for i in self.annotations.exclude(id__in=matches):
self.annotations.remove(i)
for i in matches.exclude(id__in=self.annotations.all()):
self.annotations.add(i)
2011-06-05 16:30:30 +00:00
ids = list(set([a.item.id for a in matches]))
for i in self.items.exclude(id__in=ids):
self.items.remove(i)
for i in Item.objects.filter(id__in=ids).exclude(id__in=self.items.all()):
self.items.add(i)
2011-05-30 13:40:16 +00:00
self.save()
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)