openmedialibrary/oml/item/api.py

265 lines
7.8 KiB
Python

# -*- coding: utf-8 -*-
# vi:si:et:sw=4:sts=4:ts=4
from __future__ import division
import os
import json
from oxflask.api import actions
from oxflask.shortcuts import returns_json
from sqlalchemy.orm import load_only
import query
import models
import settings
import state
import meta
import utils
import logging
logger = logging.getLogger('oml.item.api')
@returns_json
def find(request):
'''
takes {
query {
conditions [{}]
operator string
}
group string
keys [string]
sort [{}]
range [int, int]
}
'''
response = {}
data = json.loads(request.form['data']) if 'data' in request.form else {}
q = query.parse(data)
if 'group' in q:
names = {}
groups = {}
items = [i.id for i in q['qs'].options(load_only('id'))]
qs = models.Find.query.filter_by(key=q['group'])
if items:
qs = qs.filter(models.Find.item_id.in_(items))
for f in qs.values('value', 'findvalue'):
value = f[0]
findvalue = f[1]
if findvalue not in groups:
groups[findvalue] = 0
groups[findvalue] += 1
names[findvalue] = value
g = [{'name': names[k], 'items': groups[k]} for k in groups]
else:
g = []
if 'sort' in q:
g.sort(key=lambda k: k[q['sort'][0]['key']])
if q['sort'][0]['operator'] == '-':
g.reverse()
if 'positions' in data:
response['positions'] = {}
ids = [k['name'] for k in g]
response['positions'] = utils.get_positions(ids, data['positions'])
elif 'range' in data:
response['items'] = g[q['range'][0]:q['range'][1]]
else:
response['items'] = len(g)
elif 'position' in data:
ids = [i.id for i in q['qs'].options(load_only('id'))]
response['position'] = utils.get_positions(ids, [data['qs'][0].id])[0]
elif 'positions' in data:
ids = [i.id for i in q['qs'].options(load_only('id'))]
response['positions'] = utils.get_positions(ids, data['positions'])
elif 'keys' in data:
response['items'] = []
for i in q['qs'][q['range'][0]:q['range'][1]]:
j = i.json()
response['items'].append({k:j[k] for k in j if not data['keys'] or k in data['keys']})
else:
response['items'] = q['qs'].count()
#from sqlalchemy.sql import func
#models.db.session.query(func.sum(models.Item.sort_size).label("size"))
#response['size'] = x.scalar()
response['size'] = sum([i.sort_size or 0 for i in q['qs'].options(load_only('id', 'sort_size'))])
return response
actions.register(find)
@returns_json
def get(request):
'''
takes {
id
keys
}
'''
response = {}
data = json.loads(request.form['data']) if 'data' in request.form else {}
item = models.Item.get(data['id'])
if item:
response = item.json(data['keys'] if 'keys' in data else None)
return response
actions.register(get)
@returns_json
def edit(request):
'''
takes {
id
...
}
setting identifier or base metadata is possible not both at the same time
'''
response = {}
data = json.loads(request.form['data']) if 'data' in request.form else {}
logger.debug('edit %s', data)
item = models.Item.get(data['id'])
keys = filter(lambda k: k in models.Item.id_keys, data.keys())
logger.debug('edit of %s id keys: %s', item, keys)
if item and item.json()['mediastate'] == 'available':
if keys:
key = keys[0]
logger.debug('update mainid %s %s', key, data[key])
if key in ('isbn10', 'isbn13'):
data[key] = utils.normalize_isbn(data[key])
item.update_mainid(key, data[key])
response = item.json()
elif not item.meta.get('mainid'):
logger.debug('chustom data %s', data)
for key in ('title', 'author', 'date', 'publisher', 'edition'):
if key in data:
item.meta[key] = data[key]
item.update()
logger.debug('FIXME: custom metadata not published to changelog!!!')
else:
logger.info('can only edit available items')
return response
actions.register(edit, cache=False)
@returns_json
def remove(request):
'''
takes {
id
}
'''
data = json.loads(request.form['data']) if 'data' in request.form else {}
logger.debug('remove files %s', data)
if 'ids' in data and data['ids']:
for i in models.Item.query.filter(models.Item.id.in_(data['ids'])):
i.remove_file()
return {}
actions.register(remove, cache=False)
@returns_json
def findMetadata(request):
'''
takes {
title: string,
author: [string],
publisher: string,
date: string
}
returns {
items: [{
key: value
}]
}
key is one of the supported identifiers: isbn10, isbn13...
'''
response = {}
data = json.loads(request.form['data']) if 'data' in request.form else {}
logger.debug('findMetadata %s', data)
response['items'] = meta.find(**data)
return response
actions.register(findMetadata)
@returns_json
def getMetadata(request):
'''
takes {
key: value
}
key can be one of the supported identifiers: isbn10, isbn13, oclc, olid,...
'''
data = json.loads(request.form['data']) if 'data' in request.form else {}
logger.debug('getMetadata %s', data)
key, value = data.iteritems().next()
if key in ('isbn10', 'isbn13'):
value = utils.normalize_isbn(value)
response = meta.lookup(key, value)
if response:
response['mainid'] = key
return response
actions.register(getMetadata)
@returns_json
def download(request):
'''
takes {
id
}
'''
response = {}
data = json.loads(request.form['data']) if 'data' in request.form else {}
item = models.Item.get(data['id'])
if item:
item.queue_download()
item.update()
response = {'status': 'queued'}
return response
actions.register(download, cache=False)
@returns_json
def cancelDownloads(request):
'''
takes {
ids
}
'''
response = {}
data = json.loads(request.form['data']) if 'data' in request.form else {}
ids = data['ids']
if ids:
for item in models.Item.query.filter(models.Item.id.in_(ids)):
item.transferprogress = None
item.transferadded = None
p = state.user()
if p in item.users:
item.users.remove(p)
for l in item.lists.filter_by(user_id=settings.USER_ID):
l.items.remove(item)
item.update()
response = {'status': 'cancelled'}
return response
actions.register(cancelDownloads, cache=False)
@returns_json
def scan(request):
state.main.add_callback(state.websockets[0].put, json.dumps(['scan', {}]))
return {}
actions.register(scan, cache=False)
@returns_json
def _import(request):
'''
takes {
path absolute path to import
list listename (add new items to this list)
mode copy|move
}
'''
data = json.loads(request.form['data']) if 'data' in request.form else {}
logger.debug('api.import %s', data)
state.main.add_callback(state.websockets[0].put, json.dumps(['import', data]))
return {}
actions.register(_import, 'import', cache=False)
@returns_json
def cancelImport(request):
state.activity['cancel'] = True
return {}
actions.register(cancelImport, cache=False)