openmedialibrary/oml/item/person.py

66 lines
1.8 KiB
Python
Raw Normal View History

2014-05-04 17:26:43 +00:00
# -*- coding: utf-8 -*-
# vi:si:et:sw=4:sts=4:ts=4
2014-09-02 22:32:44 +00:00
2014-05-04 17:26:43 +00:00
import unicodedata
import ox
2014-08-12 08:16:57 +00:00
import sqlalchemy as sa
2014-05-04 17:26:43 +00:00
2014-08-09 15:03:16 +00:00
import db
2015-03-08 12:48:22 +00:00
import state
2014-08-12 08:16:57 +00:00
2014-05-04 17:26:43 +00:00
def get_sort_name(name, sortname=None):
name = unicodedata.normalize('NFKD', name).strip()
if name:
person = Person.get(name)
if not person:
person = Person(name=name, sortname=sortname)
2016-01-27 09:39:10 +00:00
person.save(False)
2014-05-04 17:26:43 +00:00
sortname = unicodedata.normalize('NFKD', person.sortname)
else:
2014-09-02 22:32:44 +00:00
sortname = ''
2014-05-04 17:26:43 +00:00
return sortname
class Person(db.Model):
2014-08-09 15:03:16 +00:00
__tablename__ = 'person'
name = sa.Column(sa.String(1024), primary_key=True)
sortname = sa.Column(sa.String())
numberofnames = sa.Column(sa.Integer())
2014-05-04 17:26:43 +00:00
def __repr__(self):
return self.name
@classmethod
def get(cls, name):
return cls.query.filter_by(name=name).first()
2016-01-27 09:39:10 +00:00
def save(self, update=True):
2016-01-25 17:33:37 +00:00
from .models import Item, Find
2014-05-04 17:26:43 +00:00
if not self.sortname:
self.sortname = ox.get_sort_name(self.name)
self.sortname = unicodedata.normalize('NFKD', self.sortname)
self.sortsortname = ox.sort_string(self.sortname)
self.numberofnames = len(self.name.split(' '))
2014-08-09 16:14:14 +00:00
state.db.session.add(self)
2016-01-27 09:39:10 +00:00
if update:
for i in Item.find({
'query': {
'conditions': [{'key': 'author', 'value': self.name, 'operator': '=='}]
}
}):
i.update_sort()
Find.query.filter_by(key='author', value=self.name).update({
'sortvalue': self.sortname.lower()
})
2014-08-09 16:14:14 +00:00
state.db.session.commit()
2014-05-04 17:26:43 +00:00
2015-05-14 11:03:49 +00:00
def json(self, keys=None):
r = {}
r['name'] = self.name
r['sortname'] = self.sortname
r['numberofnames'] = self.numberofnames
return r