openmedialibrary/oml/item/title_api.py

105 lines
2.9 KiB
Python
Raw Normal View History

2015-05-14 11:03:49 +00:00
# -*- coding: utf-8 -*-
# vi:si:et:sw=4:sts=4:ts=4
2016-01-25 17:33:37 +00:00
import hashlib
import json
import unicodedata
import ox
from sqlalchemy.orm import load_only
2015-05-14 11:03:49 +00:00
from oxtornado import actions
from . import models
2016-01-25 17:33:37 +00:00
import utils
import state
2015-05-14 11:03:49 +00:00
import logging
2015-11-29 14:56:38 +00:00
logger = logging.getLogger(__name__)
2015-05-14 11:03:49 +00:00
def editTitle(data):
'''
takes {
id string
2016-01-25 17:33:37 +00:00
sorttitle string
2015-05-14 11:03:49 +00:00
}
'''
response = {}
2016-01-25 17:33:37 +00:00
ids = data['id'].split(':')
data = {
'sorttitle': unicodedata.normalize('NFKD', data['sorttitle'])
}
for item in models.Item.query.filter(models.Item.id.in_(ids)):
if item.meta.get('sorttitle') != data['sorttitle']:
item.edit(data)
response['title'] = item.meta.get('title', 'Untitled')
response['sorttitle'] = item.meta['sorttitle']
2015-05-14 11:03:49 +00:00
return response
actions.register(editTitle)
def findTitles(data):
'''
takes {
query {
conditions [{}]
operator string
}
keys [string]
sort [{}]
range [int, int]
}
'''
response = {}
2016-01-25 17:33:37 +00:00
conditions = [
{'key': 'list', 'value': ':', 'operator': '=='}
]
if data.get('query', {}).get('conditions'):
value = data.get('query', {}).get('conditions')[0].get('value')
if value:
conditions.append({
'key': 'title', 'operator': '=', 'value': value
})
2015-05-14 11:03:49 +00:00
q = {
2016-01-25 17:33:37 +00:00
'qs': models.Item.find({
'query': {
'conditions': conditions, 'operator': '&'
}
}),
2015-05-14 11:03:49 +00:00
}
2016-01-25 17:33:37 +00:00
_keydata = data.copy()
for key in ('range', 'position', 'positions'):
if key in _keydata:
del _keydata[key]
key = 'title:' + hashlib.sha1(json.dumps(_keydata, sort_keys=True).encode('utf-8')).hexdigest()
titles = state.cache.get(key)
if titles is None:
2016-02-26 11:39:01 +00:00
state.cache.lock(key)
2016-01-25 17:33:37 +00:00
response['items'] = []
_titles = {}
for i in q['qs']:
title = i.meta.get('title')
if not title in _titles:
_titles[title] = {
'id': i.id,
'title': title,
'sorttitle': i.get_sorttitle()
}
else:
_titles[title]['id'] += ':' + i.id
titles = list(_titles.values())
if 'sort' in data:
titles.sort(key=lambda t: ox.sort_string(t[data['sort'][0]['key']]).lower(),
reverse=data['sort'][0]['operator']=='-')
state.cache.set(key, titles)
2015-05-14 11:03:49 +00:00
if 'position' in data:
pass
elif 'positions' in data:
2016-01-25 17:33:37 +00:00
ids = [t['id'] for t in titles]
response['positions'] = utils.get_positions(ids, data['positions'])
2015-05-14 11:03:49 +00:00
elif 'keys' in data:
2016-01-25 17:33:37 +00:00
response['items'] = titles[data['range'][0]:data['range'][1]]
2015-05-14 11:03:49 +00:00
else:
2016-01-25 17:33:37 +00:00
response['items'] = len(titles)
2015-05-14 11:03:49 +00:00
return response
actions.register(findTitles)