for update

This commit is contained in:
j 2019-01-15 13:20:11 +05:30
parent 52f45beaec
commit 6c7d6bb6b0
3 changed files with 9 additions and 4 deletions

View file

@ -146,13 +146,15 @@ def edit(data):
ids = [ids] ids = [ids]
edited = [] edited = []
for id in ids: for id in ids:
item = models.Item.get(id) state.db.session.begin(subtransactions=True)
item = models.Item.get(id, for_update=True)
if item and item.json().get('mediastate') == 'available': if item and item.json().get('mediastate') == 'available':
item.edit(data) item.edit(data)
response = item.json() response = item.json()
edited.append(id) edited.append(id)
else: else:
logger.info('can only edit available items %s', id) logger.info('can only edit available items %s', id)
state.db.session.commit()
if len(ids) > 1: if len(ids) > 1:
response = data response = data
response['id'] = edited response['id'] = edited

View file

@ -75,10 +75,13 @@ class Item(db.Model):
self.meta = {} self.meta = {}
@classmethod @classmethod
def get(cls, id): def get(cls, id, for_update=False):
if isinstance(id, list): if isinstance(id, list):
id = base64.b32encode(hashlib.sha1(''.join(id)).digest()) id = base64.b32encode(hashlib.sha1(''.join(id)).digest())
return cls.query.filter_by(id=id).first() qs = cls.query.filter_by(id=id)
if for_update:
qs = qs.with_for_update()
return qs.first()
@classmethod @classmethod
def get_or_create(cls, id, info=None): def get_or_create(cls, id, info=None):

View file

@ -209,7 +209,7 @@ def info(pdf):
if settings.server['extract_text']: if settings.server['extract_text']:
text = extract_text(pdf) text = extract_text(pdf)
data['textsize'] = len(text) data['textsize'] = len(text)
if not 'isbn' in data: if 'isbn' not in data:
isbn = extract_isbn(text) isbn = extract_isbn(text)
if isbn: if isbn:
data['isbn'] = isbn data['isbn'] = isbn