update findMetadata

This commit is contained in:
j 2016-01-11 20:26:11 +05:30
parent d88cb45727
commit 1405ccb6ca
2 changed files with 30 additions and 8 deletions

View file

@ -217,7 +217,8 @@ actions.register(autocomplete)
def findMetadata(data): def findMetadata(data):
''' '''
takes { takes {
query: string, key: string,
value: string
} }
returns { returns {
items: [{ items: [{
@ -226,9 +227,34 @@ def findMetadata(data):
} }
key is one of the supported identifiers: isbn10, isbn13... key is one of the supported identifiers: isbn10, isbn13...
''' '''
response = {} response = {
'items': []
}
logger.debug('findMetadata %s', data) logger.debug('findMetadata %s', data)
response['items'] = meta.find(data['query']) if data['key'] == 'isbn':
r = meta.lookup(data['key'], data['value'])
if r:
response['items'].append(r)
elif data['key'] == 'title':
r = meta.find(data['value'])
for isbn in meta.find(data['value']):
r = meta.lookup('isbn', isbn)
if r:
response['items'].append(r)
elif data['key'] == 'id':
import user.models
items = {}
for m in user.models.Metadata.query.filter_by(item_id=data['value']):
if m.data_hash not in items:
items[m.data_hash] = m.data
items[m.data_hash]['users'] = [m.user_id]
j = items[m.data_hash]
for key in [k['id'] for k in settings.config['itemKeys'] if isinstance(k['type'], list)]:
if key in j and not isinstance(j[key], list):
j[key] = [j[key]]
else:
items[m.data_hash]['users'].append(m.user_id)
response['items'] = list(items.values())
return response return response
actions.register(findMetadata) actions.register(findMetadata)

View file

@ -22,11 +22,7 @@ def find(query):
for isbn in isbns: for isbn in isbns:
if isbn not in done: if isbn not in done:
isbn = stdnum.isbn.to_isbn13(isbn) isbn = stdnum.isbn.to_isbn13(isbn)
r = { results.append(isbn)
'isbn': [isbn],
'primaryid': ['isbn', isbn]
}
results.append(r)
done.add(isbn) done.add(isbn)
if len(isbn) == 13 and isbn.startswith('978'): if len(isbn) == 13 and isbn.startswith('978'):
done.add(stdnum.isbn.to_isbn10(isbn)) done.add(stdnum.isbn.to_isbn10(isbn))