basic sort title api

This commit is contained in:
j 2016-01-25 23:03:37 +05:30
parent 7e40c2667b
commit 47942e204c
2 changed files with 70 additions and 31 deletions

View file

@ -38,12 +38,22 @@ class Person(db.Model):
return cls.query.filter_by(name=name).first()
def save(self):
from .models import Item, Find
if not self.sortname:
self.sortname = ox.get_sort_name(self.name)
self.sortname = unicodedata.normalize('NFKD', self.sortname)
self.sortsortname = ox.sort_string(self.sortname)
self.numberofnames = len(self.name.split(' '))
state.db.session.add(self)
for i in Item.find({
'query': {
'conditions': [{'key': 'author', 'value': self.name, 'operator': '=='}]
}
}):
i.update_sort()
Find.query.filter_by(key='author', value=self.name).update({
'sortvalue': self.sortname.lower()
})
state.db.session.commit()
def json(self, keys=None):

View file

@ -1,10 +1,16 @@
# -*- coding: utf-8 -*-
# vi:si:et:sw=4:sts=4:ts=4
import hashlib
import json
import unicodedata
import ox
from sqlalchemy.orm import load_only
from oxtornado import actions
from . import models
from . import query
import utils
import state
import logging
logger = logging.getLogger(__name__)
@ -14,17 +20,19 @@ def editTitle(data):
'''
takes {
id string
sortname string
sorttitle string
}
'''
response = {}
'''
item = models.Item.get(data['id'])
item.sortname = unicodedata.normalize('NFKD', data['sortname'])
item.save()
response['name'] = item.name
response['sortname'] = item.sortname
'''
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']
return response
actions.register(editTitle)
@ -41,34 +49,55 @@ def findTitles(data):
}
'''
response = {}
#q = query.parse(data)
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
})
q = {
'qs': models.Item.query,
'qs': models.Item.find({
'query': {
'conditions': conditions, 'operator': '&'
}
if 'range' in data:
q['range'] = data['range']
}),
}
_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:
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)
if 'position' in data:
pass
#ids = [i.id for i in q['qs'].options(load_only('id'))]
#response['position'] = utils.get_positions(ids, [data['qs'][0].id])[0]
print('fixme', data)
elif 'positions' in data:
#ids = [i.id for i in q['qs'].options(load_only('id'))]
#response['positions'] = utils.get_positions(ids, data['positions'])
response['positions'] = []
print('fixme', data)
ids = [t['id'] for t in titles]
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 = {}
for key in (data['keys'] or ['title', 'sorttitle']):
if key == 'title':
j[key] = i.info.get(key)
elif key == 'sorttitle':
j[key] = i.sort[0].title
response['items'].append(j)
response['items'] = titles[data['range'][0]:data['range'][1]]
else:
response['items'] = q['qs'].count()
response['items'] = len(titles)
return response
actions.register(findTitles)