avoid loop in get_sort_name

This commit is contained in:
j 2016-01-27 15:09:10 +05:30
parent 62c0c9337f
commit 27943bf1a5
4 changed files with 25 additions and 21 deletions

View file

@ -14,7 +14,6 @@ from websocket import trigger_event
import meta import meta
from . import models from . import models
from . import query from . import query
from .person import get_sort_name
import settings import settings
import state import state
import utils import utils

View file

@ -207,7 +207,7 @@ class Item(db.Model):
else: else:
f.sortvalue = f.value f.sortvalue = f.value
if f.sortvalue: if f.sortvalue:
f.sortvalue = unicodedata.normalize('NFKD', f.sortvalue).lower() f.sortvalue = ox.sort_string(unicodedata.normalize('NFKD', f.sortvalue)).lower()
else: else:
f.sortvalue = None f.sortvalue = None
state.db.session.add(f) state.db.session.add(f)

View file

@ -17,7 +17,7 @@ def get_sort_name(name, sortname=None):
person = Person.get(name) person = Person.get(name)
if not person: if not person:
person = Person(name=name, sortname=sortname) person = Person(name=name, sortname=sortname)
person.save() person.save(False)
sortname = unicodedata.normalize('NFKD', person.sortname) sortname = unicodedata.normalize('NFKD', person.sortname)
else: else:
sortname = '' sortname = ''
@ -37,7 +37,7 @@ class Person(db.Model):
def get(cls, name): def get(cls, name):
return cls.query.filter_by(name=name).first() return cls.query.filter_by(name=name).first()
def save(self): def save(self, update=True):
from .models import Item, Find from .models import Item, Find
if not self.sortname: if not self.sortname:
self.sortname = ox.get_sort_name(self.name) self.sortname = ox.get_sort_name(self.name)
@ -45,6 +45,7 @@ class Person(db.Model):
self.sortsortname = ox.sort_string(self.sortname) self.sortsortname = ox.sort_string(self.sortname)
self.numberofnames = len(self.name.split(' ')) self.numberofnames = len(self.name.split(' '))
state.db.session.add(self) state.db.session.add(self)
if update:
for i in Item.find({ for i in Item.find({
'query': { 'query': {
'conditions': [{'key': 'author', 'value': self.name, 'operator': '=='}] 'conditions': [{'key': 'author', 'value': self.name, 'operator': '=='}]

View file

@ -460,19 +460,23 @@ def migrate_10():
from item.person import get_sort_name from item.person import get_sort_name
import unicodedata import unicodedata
sort_names = {} sort_names = {}
updates = {}
for f in Find.query.filter(Find.key.in_(Item.filter_keys)): for f in Find.query.filter(Find.key.in_(Item.filter_keys)):
sort_type = get_by_id(settings.config['itemKeys'], f.key).get('sortType') sort_type = get_by_id(settings.config['itemKeys'], f.key).get('sortType')
if sort_type == 'person': if sort_type == 'person':
if f.value in sort_names: if f.value in sort_names:
f.sortvalue = sort_names[f.value] sortvalue = sort_names[f.value]
else: else:
f.sortvalue = sort_names[f.value] = get_sort_name(f.value) sortvalue = sort_names[f.value] = get_sort_name(f.value)
else: else:
f.sortvalue = f.value sortvalue = f.value
if f.sortvalue: if sortvalue:
f.sortvalue = unicodedata.normalize('NFKD', f.sortvalue).lower() sortvalue = ox.sort_string(unicodedata.normalize('NFKD', sortvalue)).lower()
else: if not f.key in updates:
f.sortvalue = None updates[f.key] = {}
session.add(f) updates[f.key][f.value] = sortvalue
for key in updates:
for value in updates[key]:
Find.query.filter_by(key=key, value=value).update({'sortvalue': updates[key][value]})
session.commit() session.commit()
return 10 return 10