towards entity layer

This commit is contained in:
j 2014-11-20 13:20:48 +00:00
parent 0e69c987a9
commit b7ab390425
3 changed files with 19 additions and 7 deletions

View file

@ -13,6 +13,7 @@ from django.db.models.signals import pre_delete
import ox import ox
from clip.models import Clip from clip.models import Clip
from entity.models import Entity
from changelog.models import Changelog from changelog.models import Changelog
from item.utils import sort_string, get_by_key from item.utils import sort_string, get_by_key
@ -224,18 +225,24 @@ class Annotation(models.Model):
if self.languages: if self.languages:
j['languages'] = self.languages.split(',') j['languages'] = self.languages.split(',')
l = self.get_layer() l = self.get_layer()
if l['type'] == 'place': if l['type'] == 'entity':
qs = self.places.all() qs = Entity.objects.filter(id=ox.fromAZ(self.value))
if qs.count() > 0: if qs.count() > 0:
j['place'] = qs[0].json(user=user) j['entity'] = qs[0].json(user=user)
else: else:
j['place'] = {} j['entity'] = {}
elif l['type'] == 'event': elif l['type'] == 'event':
qs = self.events.all() qs = self.events.all()
if qs.count() > 0: if qs.count() > 0:
j['event'] = qs[0].json(user=user) j['event'] = qs[0].json(user=user)
else: else:
j['event'] = {} j['event'] = {}
elif l['type'] == 'place':
qs = self.places.all()
if qs.count() > 0:
j['place'] = qs[0].json(user=user)
else:
j['place'] = {}
if layer or (keys and 'layer' in keys): if layer or (keys and 'layer' in keys):
j['layer'] = self.layer j['layer'] = self.layer

View file

@ -10,6 +10,7 @@ from django.db import models
from django.db.models import Max from django.db.models import Max
from django.contrib.auth.models import User from django.contrib.auth.models import User
from django.db.models.signals import pre_delete from django.db.models.signals import pre_delete
from django.conf import settings
import ox import ox
from ox.django import fields from ox.django import fields
@ -44,7 +45,7 @@ class Entity(models.Model):
self.name_sort = ox.sort_string(self.name or u'')[:255].lower() self.name_sort = ox.sort_string(self.name or u'')[:255].lower()
self.name_find = '||' + self.name + '||'.join(self.alternativeNames) + '||' self.name_find = '||' + self.name + '||'.join(self.alternativeNames) + '||'
super(Entity, self).save(*args, **kwargs) super(Entity, self).save(*args, **kwargs)
#self.update_matches() self.update_matches()
def __unicode__(self): def __unicode__(self):
return self.get_id() return self.get_id()
@ -134,7 +135,11 @@ class Entity(models.Model):
url = unquote(urls[0]) url = unquote(urls[0])
if url != urls[0]: if url != urls[0]:
urls.append(url) urls.append(url)
matches = self.items.count() entity_layers = [l['id'] for l in settings.CONFIG['layers'] if l['type'] == 'entity']
if entity_layers:
matches = annotation.models.Annotation.objects.filter(layer__in=entity_layers, value=self.get_id()).count()
else:
matches = 0
for url in urls: for url in urls:
matches += annotation.models.Annotation.objects.filter(value__contains=url).count() matches += annotation.models.Annotation.objects.filter(value__contains=url).count()
matches += item.models.Item.objects.filter(data__contains=url).count() matches += item.models.Item.objects.filter(data__contains=url).count()

View file

@ -49,7 +49,7 @@ def addEntity(request, data):
existing_names.append(name) existing_names.append(name)
if not exists: if not exists:
data['name'] = ox.escape_html(data['name']) data['name'] = ox.escape_html(data['name'])
entity = models.Entity(name=data['name']) entity = models.Entity(name=data['name'], type=data['type'])
entity.user = request.user entity.user = request.user
for key in ('type', 'alternativeNames'): for key in ('type', 'alternativeNames'):
if key in data and data[key]: if key in data and data[key]: