pandora/pandora/person/models.py

112 lines
3.3 KiB
Python
Raw Normal View History

# -*- coding: utf-8 -*-
# vi:si:et:sw=4:sts=4:ts=4
from __future__ import division, print_function, absolute_import
import unicodedata
from django.db import models
2016-02-20 09:06:41 +00:00
from oxdjango import fields
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
from . import managers
2011-10-11 17:44:45 +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)
if created:
if sortname:
person.sortname = sortname
person.save()
2011-10-11 19:05:11 +00:00
sortname = unicodedata.normalize('NFKD', person.sortname)
else:
sortname = u''
2011-10-11 15:40:13 +00:00
return sortname
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)
#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()
def __unicode__(self):
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)
2011-10-11 17:44:45 +00:00
self.sortsortname = utils.sort_string(self.sortname)
2011-10-11 11:29:05 +00:00
self.numberofnames = len(self.name.split(' '))
super(Person, self).save(*args, **kwargs)
def update_itemsort(self):
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(
sortvalue=self.sortname
).update(
sortvalue=self.sortname
)
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()
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:
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()