avoid loop in get_sort_name
This commit is contained in:
parent
62c0c9337f
commit
27943bf1a5
4 changed files with 25 additions and 21 deletions
|
@ -14,7 +14,6 @@ from websocket import trigger_event
|
|||
import meta
|
||||
from . import models
|
||||
from . import query
|
||||
from .person import get_sort_name
|
||||
import settings
|
||||
import state
|
||||
import utils
|
||||
|
|
|
@ -207,7 +207,7 @@ class Item(db.Model):
|
|||
else:
|
||||
f.sortvalue = f.value
|
||||
if f.sortvalue:
|
||||
f.sortvalue = unicodedata.normalize('NFKD', f.sortvalue).lower()
|
||||
f.sortvalue = ox.sort_string(unicodedata.normalize('NFKD', f.sortvalue)).lower()
|
||||
else:
|
||||
f.sortvalue = None
|
||||
state.db.session.add(f)
|
||||
|
|
|
@ -17,7 +17,7 @@ def get_sort_name(name, sortname=None):
|
|||
person = Person.get(name)
|
||||
if not person:
|
||||
person = Person(name=name, sortname=sortname)
|
||||
person.save()
|
||||
person.save(False)
|
||||
sortname = unicodedata.normalize('NFKD', person.sortname)
|
||||
else:
|
||||
sortname = ''
|
||||
|
@ -37,7 +37,7 @@ class Person(db.Model):
|
|||
def get(cls, name):
|
||||
return cls.query.filter_by(name=name).first()
|
||||
|
||||
def save(self):
|
||||
def save(self, update=True):
|
||||
from .models import Item, Find
|
||||
if not self.sortname:
|
||||
self.sortname = ox.get_sort_name(self.name)
|
||||
|
@ -45,15 +45,16 @@ class Person(db.Model):
|
|||
self.sortsortname = ox.sort_string(self.sortname)
|
||||
self.numberofnames = len(self.name.split(' '))
|
||||
state.db.session.add(self)
|
||||
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()
|
||||
})
|
||||
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()
|
||||
})
|
||||
state.db.session.commit()
|
||||
|
||||
def json(self, keys=None):
|
||||
|
|
|
@ -460,19 +460,23 @@ def migrate_10():
|
|||
from item.person import get_sort_name
|
||||
import unicodedata
|
||||
sort_names = {}
|
||||
updates = {}
|
||||
for f in Find.query.filter(Find.key.in_(Item.filter_keys)):
|
||||
sort_type = get_by_id(settings.config['itemKeys'], f.key).get('sortType')
|
||||
if sort_type == 'person':
|
||||
if f.value in sort_names:
|
||||
f.sortvalue = sort_names[f.value]
|
||||
sortvalue = sort_names[f.value]
|
||||
else:
|
||||
f.sortvalue = sort_names[f.value] = get_sort_name(f.value)
|
||||
sortvalue = sort_names[f.value] = get_sort_name(f.value)
|
||||
else:
|
||||
f.sortvalue = f.value
|
||||
if f.sortvalue:
|
||||
f.sortvalue = unicodedata.normalize('NFKD', f.sortvalue).lower()
|
||||
else:
|
||||
f.sortvalue = None
|
||||
session.add(f)
|
||||
sortvalue = f.value
|
||||
if sortvalue:
|
||||
sortvalue = ox.sort_string(unicodedata.normalize('NFKD', sortvalue)).lower()
|
||||
if not f.key in updates:
|
||||
updates[f.key] = {}
|
||||
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()
|
||||
return 10
|
||||
|
|
Loading…
Reference in a new issue