make commit optional

This commit is contained in:
j 2016-02-10 19:31:33 +05:30
parent c82efdaff1
commit 3cde05b9ba

View file

@ -148,9 +148,9 @@ class Item(db.Model):
f = self.files.first() f = self.files.first()
return f.fullpath() if f else None return f.fullpath() if f else None
def update_sort(self): def update_sort(self, commit=True):
update = False update = False
s = Sort.get_or_create(self.id) s = Sort.get_or_create(self.id, commit=commit)
for key in config['itemKeys']: for key in config['itemKeys']:
if key.get('sort'): if key.get('sort'):
value = self.json().get(key['id'], None) value = self.json().get(key['id'], None)
@ -191,7 +191,7 @@ class Item(db.Model):
if update: if update:
state.db.session.add(s) state.db.session.add(s)
def update_find(self): def update_find(self, commit=True):
def add(k, v): def add(k, v):
f = Find.query.filter_by(item_id=self.id, key=k, value=v).first() f = Find.query.filter_by(item_id=self.id, key=k, value=v).first()
@ -242,26 +242,28 @@ class Item(db.Model):
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).filter(Find.key.notin_(keys)):
state.db.session.delete(f) state.db.session.delete(f)
def update(self, modified=None): def update(self, modified=None, commit=True):
for key in ('mediastate', 'coverRatio', 'previewRatio'): for key in ('mediastate', 'coverRatio', 'previewRatio'):
if key in self.meta: if key in self.meta:
if key not in self.info: if key not in self.info:
self.info[key] = self.meta[key] self.info[key] = self.meta[key]
del self.meta[key] del self.meta[key]
users = list(map(str, list(self.users)))
self.info['mediastate'] = 'available' # available, unavailable, transferring 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 settings.USER_ID in users else 'unavailable' self.info['mediastate'] = 'available' if self.files.count() else 'unavailable'
if modified: if modified:
self.modified = modified self.modified = modified
else: else:
self.modified = datetime.utcnow() self.modified = datetime.utcnow()
self.update_sort() self.update_sort(commit=commit)
self.update_find() self.update_find(commit=commit)
self.save() if commit:
self.save()
else:
state.db.session.add(self)
def save(self): def save(self):
state.db.session.add(self) state.db.session.add(self)
@ -555,12 +557,13 @@ class Sort(db.Model):
return cls.query.filter_by(item_id=item_id).first() return cls.query.filter_by(item_id=item_id).first()
@classmethod @classmethod
def get_or_create(cls, item_id): def get_or_create(cls, item_id, commit=True):
f = cls.get(item_id) f = cls.get(item_id)
if not f: if not f:
f = cls(item_id=item_id) f = cls(item_id=item_id)
state.db.session.add(f) state.db.session.add(f)
state.db.session.commit() if commit:
state.db.session.commit()
return f return f
Item.sort_keys = [] Item.sort_keys = []
@ -604,12 +607,13 @@ class Find(db.Model):
return cls.query.filter_by(item_id=item, key=key).first() return cls.query.filter_by(item_id=item, key=key).first()
@classmethod @classmethod
def get_or_create(cls, item, key): def get_or_create(cls, item, key, commit=True):
f = cls.get(item, key) f = cls.get(item, key)
if not f: if not f:
f = cls(item_id=item, key=key) f = cls(item_id=item, key=key)
state.db.session.add(f) state.db.session.add(f)
state.db.session.commit() if commit:
state.db.session.commit()
return f return f
class File(db.Model): class File(db.Model):