faster update_find
This commit is contained in:
parent
ea9de0f311
commit
813e3b591b
2 changed files with 30 additions and 20 deletions
|
@ -168,7 +168,7 @@ class Item(db.Model):
|
|||
elif sort_type == 'person':
|
||||
if not isinstance(value, list):
|
||||
value = [value]
|
||||
value = list(map(get_sort_name, value))
|
||||
value = [get_sort_name(v, commit=commit) for v in value]
|
||||
value = ox.sort_string('\n'.join(value)).lower()
|
||||
elif sort_type == 'title':
|
||||
value = self.get_sorttitle().lower()
|
||||
|
@ -192,10 +192,16 @@ class Item(db.Model):
|
|||
state.db.session.add(s)
|
||||
|
||||
def update_find(self, commit=True):
|
||||
current_values = {}
|
||||
for f in Find.query.filter_by(item_id=self.id):
|
||||
if f.key not in current_values:
|
||||
current_values[f.key] = set()
|
||||
current_values[f.key].add(f.value)
|
||||
|
||||
def add(k, v):
|
||||
if k in current_values and v in current_values[k]:
|
||||
f = Find.query.filter_by(item_id=self.id, key=k, value=v).first()
|
||||
if not f:
|
||||
else:
|
||||
f = Find(item_id=self.id, key=k)
|
||||
if f.value != v:
|
||||
f.findvalue = unicodedata.normalize('NFKD', v).lower()
|
||||
|
@ -203,7 +209,7 @@ class Item(db.Model):
|
|||
if k in self.filter_keys:
|
||||
sort_type = utils.get_by_id(settings.config['itemKeys'], k).get('sortType')
|
||||
if sort_type == 'person':
|
||||
f.sortvalue = get_sort_name(f.value)
|
||||
f.sortvalue = get_sort_name(f.value, commit=commit)
|
||||
else:
|
||||
f.sortvalue = f.value
|
||||
if f.sortvalue:
|
||||
|
@ -236,24 +242,27 @@ class Item(db.Model):
|
|||
]
|
||||
for v in value:
|
||||
add(key['id'], v)
|
||||
if key['id'] in current_values:
|
||||
removed_values = current_values[key['id']] - set(value)
|
||||
if removed_values:
|
||||
for f in Find.query.filter_by(item_id=self.id,
|
||||
key=key['id']).filter(Find.value.notin_(value)):
|
||||
key=key['id']).filter(Find.value.in_(removed_values)):
|
||||
state.db.session.delete(f)
|
||||
for f in Find.query.filter_by(item_id=self.id).filter(Find.key.notin_(keys)):
|
||||
removed_keys = set(current_values) - set(keys)
|
||||
if removed_keys:
|
||||
for f in Find.query.filter_by(item_id=self.id).filter(Find.key.in_(removed_keys)):
|
||||
state.db.session.delete(f)
|
||||
|
||||
def update(self, modified=None, commit=True):
|
||||
for key in ('mediastate', 'coverRatio', 'previewRatio'):
|
||||
if key in self.meta:
|
||||
if key not in self.info:
|
||||
self.info[key] = self.meta[key]
|
||||
del self.meta[key]
|
||||
self.info['mediastate'] = 'available' # available, unavailable, transferring
|
||||
def update_mediastate(self):
|
||||
# available, unavailable, transferring
|
||||
t = Transfer.get(self.id)
|
||||
if t and t.added and t.progress < 1:
|
||||
self.info['mediastate'] = 'transferring'
|
||||
else:
|
||||
self.info['mediastate'] = 'available' if self.files.count() else 'unavailable'
|
||||
|
||||
def update(self, modified=None, commit=True):
|
||||
self.update_mediastate()
|
||||
if modified:
|
||||
self.modified = modified
|
||||
else:
|
||||
|
|
|
@ -11,13 +11,13 @@ import db
|
|||
import state
|
||||
|
||||
|
||||
def get_sort_name(name, sortname=None):
|
||||
def get_sort_name(name, sortname=None, commit=True):
|
||||
name = unicodedata.normalize('NFKD', name).strip()
|
||||
if name:
|
||||
person = Person.get(name)
|
||||
if not person:
|
||||
person = Person(name=name, sortname=sortname)
|
||||
person.save(False)
|
||||
person.save(update=False, commit=commit)
|
||||
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, update=True):
|
||||
def save(self, update=True, commit=True):
|
||||
from .models import Item, Find
|
||||
if not self.sortname:
|
||||
self.sortname = ox.get_sort_name(self.name)
|
||||
|
@ -55,6 +55,7 @@ class Person(db.Model):
|
|||
Find.query.filter_by(key='author', value=self.name).update({
|
||||
'sortvalue': self.sortname.lower()
|
||||
})
|
||||
if commit:
|
||||
state.db.session.commit()
|
||||
|
||||
def json(self, keys=None):
|
||||
|
|
Loading…
Reference in a new issue