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]
edited = []
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':
item.edit(data)
response = item.json()
edited.append(id)
else:
logger.info('can only edit available items %s', id)
state.db.session.commit()
if len(ids) > 1:
response = data
response['id'] = edited

View file

@ -75,10 +75,13 @@ class Item(db.Model):
self.meta = {}
@classmethod
def get(cls, id):
def get(cls, id, for_update=False):
if isinstance(id, list):
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
def get_or_create(cls, id, info=None):

View file

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