pandora/pandora/place/models.py

158 lines
5.5 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
2012-01-17 14:28:33 +05:30
from annotation.models import Annotation, get_matches
2012-02-20 14:09:26 +00:00
from annotation.tasks import update_matching_places
2011-06-05 18:30:30 +02:00
from item.models import Item
2012-01-31 22:36:10 +05:30
from changelog.models import Changelog
2011-06-05 18:30:30 +02:00
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)
2012-02-01 15:25:18 +00:00
defined = models.BooleanField(default=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)
2012-02-03 14:44:11 +00:00
geoname = models.CharField(max_length=1024, null=True)
geoname_sort = models.CharField(max_length=1024, null=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='')
2012-02-01 15:25:18 +00:00
south = models.FloatField(default=None, null=True)
west = models.FloatField(default=None, null=True)
north = models.FloatField(default=None, null=True)
east = models.FloatField(default=None, null=True)
lat = models.FloatField(default=None, null=True)
lng = models.FloatField(default=None, null=True)
area = models.FloatField(default=None, null=True)
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
2012-02-01 15:25:18 +00:00
@classmethod
def get_or_create(model, name):
qs = model.objects.filter(name_find__icontains=u'|%s|'%name)
if qs.count() == 0:
instance = model(name=name)
instance.save()
else:
instance = qs[0]
return instance
2011-02-24 19:39:58 +01:00
def editable(self, user):
2012-01-13 01:02:54 +05:30
if user and not user.is_anonymous() \
2012-02-01 15:25:18 +00:00
and (not self.user or \
self.user == user or \
user.get_profile().capability('canEditPlaces')):
2012-01-13 01:02:54 +05:30
return True
2011-02-24 19:39:58 +01:00
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(),
2011-05-30 11:24:30 +02:00
'editable': self.editable(user)
2011-02-24 19:39:58 +01:00
}
2012-02-01 15:25:18 +00:00
if self.user:
j['user'] = self.user.username
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-17 14:28:33 +05:30
return get_matches(self, Place, 'place')
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()
2012-02-20 14:09:26 +00:00
for a in self.annotations.exclude(id__in=matches):
self.annotations.remove(a)
#annotations of type place always need a place
if a.get_layer().get('type') == 'place' and a.places.count() == 0:
a.places.add(Place.get_or_create(a.value))
for p in a.places.all():
p.update_matches()
2011-06-20 17:02:53 +02:00
for i in matches.exclude(id__in=self.annotations.all()):
2012-01-13 16:47:42 +05:30
#need to check again since editEvent might have been called again
if self.annotations.filter(id=i.id).count() == 0:
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()):
2012-01-13 16:47:42 +05:30
if self.items.filter(id=i.id).count() == 0:
self.items.add(i)
if self.matches != numberofmatches:
2012-02-20 14:09:26 +00:00
self.matches = numberofmatches
2012-01-13 01:02:54 +05:30
if numberofmatches:
Place.objects.filter(id=self.id).update(matches=numberofmatches)
else:
self.save()
2011-05-30 15:40:16 +02:00
2012-02-01 15:25:18 +00:00
def make_undefined(self):
self.defined = False
self.south = None
self.west = None
self.north = None
self.east = None
self.lat = None
self.lng = None
self.area = None
2012-02-04 14:02:48 +00:00
self.type = ''
self.geoname = None
self.geoname_sort = None
2012-02-01 15:25:18 +00: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)
2012-02-01 15:25:18 +00:00
if self.geoname:
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))
2012-02-01 15:25:18 +00:00
self.defined = len(filter(None, [getattr(self, key)
for key in ('south', 'west', 'north', 'east')])) > 0
super(Place, self).save(*args, **kwargs)
2012-01-31 22:36:10 +05:30
def log(self):
c = Changelog(type='place')
c.value = self.json()
c.save()