From 3cde05b9ba8386d52a5fef973ff147bc2457641d Mon Sep 17 00:00:00 2001 From: j Date: Wed, 10 Feb 2016 19:31:33 +0530 Subject: [PATCH] make commit optional --- oml/item/models.py | 30 +++++++++++++++++------------- 1 file changed, 17 insertions(+), 13 deletions(-) diff --git a/oml/item/models.py b/oml/item/models.py index a28c64c..3adb8ff 100644 --- a/oml/item/models.py +++ b/oml/item/models.py @@ -148,9 +148,9 @@ class Item(db.Model): f = self.files.first() return f.fullpath() if f else None - def update_sort(self): + def update_sort(self, commit=True): update = False - s = Sort.get_or_create(self.id) + s = Sort.get_or_create(self.id, commit=commit) for key in config['itemKeys']: if key.get('sort'): value = self.json().get(key['id'], None) @@ -191,7 +191,7 @@ class Item(db.Model): if update: state.db.session.add(s) - def update_find(self): + def update_find(self, commit=True): def add(k, v): 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)): state.db.session.delete(f) - def update(self, modified=None): + 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] - users = list(map(str, list(self.users))) self.info['mediastate'] = 'available' # 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 settings.USER_ID in users else 'unavailable' + self.info['mediastate'] = 'available' if self.files.count() else 'unavailable' if modified: self.modified = modified else: self.modified = datetime.utcnow() - self.update_sort() - self.update_find() - self.save() + self.update_sort(commit=commit) + self.update_find(commit=commit) + if commit: + self.save() + else: + state.db.session.add(self) def save(self): state.db.session.add(self) @@ -555,12 +557,13 @@ class Sort(db.Model): return cls.query.filter_by(item_id=item_id).first() @classmethod - def get_or_create(cls, item_id): + def get_or_create(cls, item_id, commit=True): f = cls.get(item_id) if not f: f = cls(item_id=item_id) state.db.session.add(f) - state.db.session.commit() + if commit: + state.db.session.commit() return f Item.sort_keys = [] @@ -604,12 +607,13 @@ class Find(db.Model): return cls.query.filter_by(item_id=item, key=key).first() @classmethod - def get_or_create(cls, item, key): + def get_or_create(cls, item, key, commit=True): f = cls.get(item, key) if not f: f = cls(item_id=item, key=key) state.db.session.add(f) - state.db.session.commit() + if commit: + state.db.session.commit() return f class File(db.Model):