pandora/pandora/place/models.py

143 lines
5.4 KiB
Python
Raw Normal View History

# -*- coding: utf-8 -*-
# vi:si:et:sw=4:sts=4:ts=4
from __future__ import division, with_statement
2011-10-30 14:46:36 +01:00
import re
from django.db import models, transaction
2011-02-24 19:39:58 +01:00
from django.contrib.auth.models import User, Group
from django.db.models import Q
2012-01-02 20:59:43 +05:30
from django.conf import settings
2011-10-30 14:46:36 +01:00
import ox
from ox.django import fields
import managers
from annotation.models import Annotation
2011-06-05 18:30:30 +02: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 19:39:58 +01:00
created = models.DateTimeField(auto_now_add=True)
modified = models.DateTimeField(auto_now=True)
2011-02-24 19:44:35 +01:00
user = models.ForeignKey(User, null=True, related_name='places')
2011-03-05 15:31:56 +01:00
name = models.CharField(max_length=1024)
2011-05-28 15:57:47 +02: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 19:39:58 +01: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 13:23:49 +02:00
type = models.CharField(max_length=1000, default='')
2011-02-24 19:39:58 +01: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 15:57:47 +02:00
area = models.FloatField(default=0)
2011-03-03 22:37:37 +00:00
matches = models.IntegerField(default=0)
2011-06-05 18:30:30 +02:00
items = models.ManyToManyField(Item, blank=True, related_name='places')
2011-06-20 17:02:53 +02: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 19:39:58 +01:00
def editable(self, user):
2011-12-27 08:49:30 +01:00
if not user or user.is_anonymous():
level = 'guest'
else:
level = user.get_profile().get_level()
if self.user == user or level in ('admin', 'staff'):
2011-02-24 19:39:58 +01:00
return True
return False
def get_id(self):
2011-12-18 15:05:49 +05:30
return ox.toAZ(self.id)
2011-02-24 19:39:58 +01:00
2011-10-01 00:28:35 +02:00
def json(self, keys=None, user=None):
2011-02-24 19:39:58 +01:00
j = {
'id': self.get_id(),
'user': self.user.username,
2011-05-30 11:24:30 +02:00
'editable': self.editable(user)
2011-02-24 19:39:58 +01:00
}
for key in ('created', 'modified',
2011-05-28 15:57:47 +02:00
'name', 'alternativeNames', 'geoname', 'countryCode',
2011-02-24 19:39:58 +01:00
'south', 'west', 'north', 'east',
2011-05-28 15:57:47 +02:00
'lat', 'lng', 'area', 'matches', 'type'):
2011-10-01 00:28:35 +02:00
if not keys or key in keys:
j[key] = getattr(self, key)
2011-02-24 19:39:58 +01:00
return j
2011-06-02 10:19:15 +02:00
def get_matches(self):
2012-01-09 00:08:05 +05:30
layers = [l['id'] for l in filter(lambda l: l['type'] == 'place' or l.get('hasPlaces'),
settings.CONFIG['layers'])]
super_matches = []
q = Q(name_find__contains=" " + self.name)|Q(name_find__contains="|%s"%self.name)
for name in self.alternativeNames:
q = q|Q(name_find__contains=" " + name)|Q(name_find__contains="|%s"%name)
for p in Place.objects.filter(q).exclude(id=self.id):
for othername in [p.name] + list(p.alternativeNames):
for name in [self.name] + list(self.alternativeNames):
if name in othername:
super_matches.append(othername)
2011-10-30 15:27:26 +01:00
q = Q(value__icontains=" " + self.name)|Q(value__istartswith=self.name)
for name in self.alternativeNames:
2011-10-30 15:27:26 +01:00
q = q|Q(value__icontains=" " + name)|Q(value__istartswith=name)
matches = []
2012-01-02 20:59:43 +05:30
for a in Annotation.objects.filter(layer__in=layers).filter(q):
2011-10-30 15:27:26 +01:00
value = a.value.lower()
for name in super_matches:
value = value.replace(name.lower(), '')
for name in [self.name] + list(self.alternativeNames):
2011-10-30 15:27:26 +01:00
name = name.lower()
if name in value and re.compile('((^|\s)%s([\.,;:!?\-\/\s]|$))'%name).findall(value):
matches.append(a.id)
break
if not matches:
matches = [-1]
return Annotation.objects.filter(id__in=matches)
2011-06-01 22:47:15 +00:00
@transaction.commit_on_success
2011-06-02 10:19:15 +02:00
def update_matches(self):
2011-06-05 18:30:30 +02:00
matches = self.get_matches()
numberofmatches = matches.count()
2011-06-20 17:02:53 +02: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 18:30:30 +02: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)
if self.matches != numberofmatches:
Place.objects.filter(id=self.id).update(matches=numberofmatches)
2011-05-30 15:40:16 +02:00
def save(self, *args, **kwargs):
if not self.name_sort:
2011-03-05 15:31:56 +01:00
self.name_sort = self.name #', '.join(self.name)
2011-02-24 19:39:58 +01:00
self.geoname_sort = ', '.join(reversed(self.geoname.split(', ')))
2011-05-28 15:57:47 +02:00
self.name_find = '|%s|'%'|'.join([self.name]+list(self.alternativeNames))
#update center
2011-02-24 19:39:58 +01:00
#self.lat = ox.location.center(self.south, self.north)
#self.lng = ox.location.center(self.east, self.west)
#update area
2011-05-28 15:57:47 +02:00
#self.area= ox.location.area(self.south, self.west, self.north, self.east)
super(Place, self).save(*args, **kwargs)