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