2010-11-26 15:07:24 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
2016-08-23 10:27:06 +00:00
|
|
|
from __future__ import division, print_function, absolute_import
|
2010-11-26 15:07:24 +00:00
|
|
|
|
|
|
|
import unicodedata
|
|
|
|
|
|
|
|
from django.db import models
|
2017-03-03 07:56:35 +00:00
|
|
|
from django.utils.encoding import python_2_unicode_compatible
|
2010-11-26 15:07:24 +00:00
|
|
|
|
2016-02-20 09:06:41 +00:00
|
|
|
from oxdjango import fields
|
2010-11-26 15:07:24 +00:00
|
|
|
import ox
|
|
|
|
|
2011-10-11 17:44:45 +00:00
|
|
|
from item import utils
|
2011-10-29 23:15:44 +00:00
|
|
|
import item.models
|
2011-10-11 17:44:45 +00:00
|
|
|
|
2016-08-23 10:27:06 +00:00
|
|
|
from . import managers
|
2011-10-11 17:44:45 +00:00
|
|
|
|
2012-09-14 09:50:08 +00:00
|
|
|
def get_name_sort(name, sortname=None):
|
2011-10-11 19:05:11 +00:00
|
|
|
name = unicodedata.normalize('NFKD', name).strip()
|
|
|
|
if name:
|
|
|
|
person, created = Person.objects.get_or_create(name=name)
|
2011-10-15 15:21:41 +00:00
|
|
|
if created:
|
2012-09-14 09:50:08 +00:00
|
|
|
if sortname:
|
|
|
|
person.sortname = sortname
|
2011-10-15 15:21:41 +00:00
|
|
|
person.save()
|
2019-06-28 11:06:00 +00:00
|
|
|
sortname = unicodedata.normalize('NFKD', person.sortname)
|
2011-10-11 19:05:11 +00:00
|
|
|
else:
|
|
|
|
sortname = u''
|
2011-10-11 15:40:13 +00:00
|
|
|
return sortname
|
2010-11-26 15:07:24 +00:00
|
|
|
|
2017-03-03 07:56:35 +00:00
|
|
|
@python_2_unicode_compatible
|
2010-11-26 15:07:24 +00:00
|
|
|
class Person(models.Model):
|
2011-04-05 10:49:20 +00:00
|
|
|
name = models.CharField(max_length=200, unique=True)
|
2011-10-11 15:40:13 +00:00
|
|
|
sortname = models.CharField(max_length=200)
|
2011-10-11 17:44:45 +00:00
|
|
|
sortsortname = models.CharField(max_length=200)
|
2011-10-11 15:01:11 +00:00
|
|
|
edited = models.BooleanField(default=False)
|
2011-10-11 11:29:05 +00:00
|
|
|
numberofnames = models.IntegerField(default=0)
|
2010-11-26 15:07:24 +00:00
|
|
|
|
|
|
|
#FIXME: how to deal with aliases
|
|
|
|
aliases = fields.TupleField(default=[])
|
|
|
|
|
|
|
|
imdbId = models.CharField(max_length=7, blank=True)
|
|
|
|
wikipediaId = models.CharField(max_length=1000, blank=True)
|
|
|
|
|
2011-10-11 11:29:05 +00:00
|
|
|
objects = managers.PersonManager()
|
2010-11-26 15:07:24 +00:00
|
|
|
|
2017-03-03 07:56:35 +00:00
|
|
|
def __str__(self):
|
2010-11-26 15:07:24 +00:00
|
|
|
return self.name
|
|
|
|
|
|
|
|
def save(self, *args, **kwargs):
|
2011-10-11 15:40:13 +00:00
|
|
|
if not self.sortname:
|
|
|
|
self.sortname = ox.get_sort_name(self.name)
|
|
|
|
self.sortname = unicodedata.normalize('NFKD', self.sortname)
|
2019-06-28 07:25:32 +00:00
|
|
|
self.sortsortname = utils.sort_string(self.sortname).lower()
|
2011-10-11 11:29:05 +00:00
|
|
|
self.numberofnames = len(self.name.split(' '))
|
2010-11-26 15:07:24 +00:00
|
|
|
super(Person, self).save(*args, **kwargs)
|
2012-02-15 16:01:24 +00:00
|
|
|
|
|
|
|
def update_itemsort(self):
|
2019-06-28 07:25:32 +00:00
|
|
|
sortname = self.sortname.lower()
|
2011-10-29 23:15:44 +00:00
|
|
|
item.models.Facet.objects.filter(
|
2011-10-30 00:50:38 +00:00
|
|
|
key__in=item.models.Item.person_keys + ['name'],
|
2011-10-29 23:32:11 +00:00
|
|
|
value=self.name
|
|
|
|
).exclude(
|
2019-06-28 07:25:32 +00:00
|
|
|
sortvalue=sortname
|
2011-10-29 23:32:11 +00:00
|
|
|
).update(
|
2019-06-28 07:25:32 +00:00
|
|
|
sortvalue=sortname
|
2011-10-29 23:32:11 +00:00
|
|
|
)
|
2012-02-15 16:01:24 +00:00
|
|
|
for i in item.models.Item.objects.filter(facets__in=item.models.Facet.objects.filter(
|
|
|
|
key__in=item.models.Item.person_keys + ['name'],
|
|
|
|
value=self.name)
|
|
|
|
).distinct():
|
|
|
|
i.update_sort()
|
2010-11-26 15:07:24 +00:00
|
|
|
|
|
|
|
def get_or_create(model, name, imdbId=None):
|
|
|
|
if imdbId:
|
|
|
|
q = model.objects.filter(name=name, imdbId=imdbId)
|
|
|
|
else:
|
|
|
|
q = model.objects.all().filter(name=name)
|
|
|
|
if q.count() > 0:
|
|
|
|
o = q[0]
|
|
|
|
else:
|
|
|
|
o = model.objects.create(name=name)
|
|
|
|
if imdbId:
|
|
|
|
o.imdbId = imdbId
|
|
|
|
o.save()
|
|
|
|
return o
|
|
|
|
get_or_create = classmethod(get_or_create)
|
|
|
|
|
2011-10-11 11:29:05 +00:00
|
|
|
def get_id(self):
|
2011-12-18 09:35:49 +00:00
|
|
|
return ox.toAZ(self.id)
|
2011-10-11 11:29:05 +00:00
|
|
|
|
|
|
|
def json(self, keys=None, user=None):
|
|
|
|
j = {
|
|
|
|
'id': self.get_id(),
|
|
|
|
'name': self.name,
|
2011-10-11 15:40:13 +00:00
|
|
|
'sortname': self.sortname,
|
2011-10-11 11:29:05 +00:00
|
|
|
'numberofnames': self.numberofnames,
|
|
|
|
}
|
|
|
|
if keys:
|
2016-08-23 10:27:06 +00:00
|
|
|
for key in list(j):
|
2011-10-11 11:29:05 +00:00
|
|
|
if key not in keys:
|
|
|
|
del j[key]
|
|
|
|
return j
|
2011-10-11 15:23:31 +00:00
|
|
|
|
|
|
|
|
|
|
|
def update_sort_name():
|
|
|
|
for p in Person.objects.all():
|
2012-03-27 17:42:49 +00:00
|
|
|
_sortname = ox.get_sort_name(p.name).lower()
|
2011-10-11 15:40:13 +00:00
|
|
|
_sortname = unicodedata.normalize('NFKD', _sortname)
|
|
|
|
if (not p.edited and _sortname != p.sortname) or \
|
|
|
|
(p.edited and _sortname == p.sortname):
|
|
|
|
p.sortname = _sortname
|
2011-10-11 15:23:31 +00:00
|
|
|
p.edited = False
|
|
|
|
p.save()
|