openmedialibrary/oml/item/person.py

48 lines
1.2 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
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)
person.save()
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()
def save(self):
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)
state.db.session.commit()
2014-05-04 17:26:43 +00:00