positions not ids and other cleanups

This commit is contained in:
j 2011-06-01 13:03:43 +02:00
parent b32ac91211
commit 3b85387481
9 changed files with 72 additions and 49 deletions

View file

@ -360,12 +360,12 @@ Groups
Positions Positions
param data { param data {
'query': query, 'query': query,
'ids': [] 'positions': []
} }
query: query object, more on query syntax at query: query object, more on query syntax at
https://wiki.0x2620.org/wiki/pandora/QuerySyntax https://wiki.0x2620.org/wiki/pandora/QuerySyntax
ids: ids of items for which positions are required positions: ids of items for which positions are required
return { return {
status: {...}, status: {...},
data: { data: {
@ -403,24 +403,24 @@ Positions
qs = models.Facet.objects.filter(key=query['group']).filter(item__id__in=item_qs) qs = models.Facet.objects.filter(key=query['group']).filter(item__id__in=item_qs)
qs = qs.values('value').annotate(items=Count('id')).order_by(*order_by) qs = qs.values('value').annotate(items=Count('id')).order_by(*order_by)
if 'ids' in query: if 'positions' in query:
#FIXME: this does not scale for larger results #FIXME: this does not scale for larger results
response['data']['positions'] = {} response['data']['positions'] = {}
ids = [j['value'] for j in qs] ids = [j['value'] for j in qs]
response['data']['positions'] = utils.get_positions(ids, query['ids']) response['data']['positions'] = utils.get_positions(ids, query['positions'])
elif 'range' in data: elif 'range' in data:
qs = qs[query['range'][0]:query['range'][1]] qs = qs[query['range'][0]:query['range'][1]]
response['data']['items'] = [{'name': i['value'], 'items': i[items]} for i in qs] response['data']['items'] = [{'name': i['value'], 'items': i[items]} for i in qs]
else: else:
response['data']['items'] = qs.count() response['data']['items'] = qs.count()
elif 'ids' in query: elif 'positions' in query:
#FIXME: this does not scale for larger results #FIXME: this does not scale for larger results
qs = _order_query(query['qs'], query['sort']) qs = _order_query(query['qs'], query['sort'])
response['data']['positions'] = {} response['data']['positions'] = {}
ids = [j['itemId'] for j in qs.values('itemId')] ids = [j['itemId'] for j in qs.values('itemId')]
response['data']['positions'] = utils.get_positions(ids, query['ids']) response['data']['positions'] = utils.get_positions(ids, query['positions'])
elif 'keys' in query: elif 'keys' in query:
response['data']['items'] = [] response['data']['items'] = []

View file

@ -238,8 +238,8 @@ class ItemManager(Manager):
qs = self.get_query_set() qs = self.get_query_set()
#only include items that have hard metadata #only include items that have hard metadata
qs = qs.filter(available=True) qs = qs.filter(available=True)
conditions = parseConditions(data['query'].get('conditions', []), conditions = parseConditions(data.get('query', {}).get('conditions', []),
data['query'].get('operator', '&')) data.get('query', {}).get('operator', '&'))
qs = qs.filter(conditions).distinct() qs = qs.filter(conditions).distinct()
#anonymous can only see public items #anonymous can only see public items

View file

@ -320,11 +320,6 @@ class Item(models.Model):
def get_layers(self, user=None): def get_layers(self, user=None):
layers = {} layers = {}
layers['subtitles'] = []
#FIXME: should subtitles be stored in Annotation?
qs = self.files.filter(is_subtitle=True, is_main=True, available=True)
if qs.count()>0:
layers['subtitles'] = qs[0].srt()
for l in Layer.objects.all(): for l in Layer.objects.all():
ll = layers.setdefault(l.name, []) ll = layers.setdefault(l.name, [])
qs = Annotation.objects.filter(layer=l, item=self) qs = Annotation.objects.filter(layer=l, item=self)

View file

@ -14,6 +14,7 @@ from ox.utils import json
from ox.django.decorators import login_required_json from ox.django.decorators import login_required_json
from ox.django.shortcuts import render_to_json_response, get_object_or_404_json, json_response from ox.django.shortcuts import render_to_json_response, get_object_or_404_json, json_response
from ox.django.http import HttpFileResponse from ox.django.http import HttpFileResponse
from django.db.models import Q
import ox import ox
import models import models
@ -22,6 +23,7 @@ import tasks
from archive.models import File from archive.models import File
from archive import extract from archive import extract
from annotation.models import Annotation
from api.actions import actions from api.actions import actions
@ -82,14 +84,18 @@ def parse_query(data, user):
query = {} query = {}
query['range'] = [0, 100] query['range'] = [0, 100]
query['sort'] = [{'key':'title', 'operator':'+'}] query['sort'] = [{'key':'title', 'operator':'+'}]
for key in ('sort', 'keys', 'group', 'range', 'ids'): for key in ('sort', 'keys', 'group', 'range', 'position', 'positions'):
if key in data: if key in data:
query[key] = data[key] query[key] = data[key]
query['qs'] = models.Item.objects.find(data, user) query['qs'] = models.Item.objects.find(data, user)
if 'annotations' in data:
query['annotations'] = data['annotations']
#FIXME: annotations need manager find(data, user)
query['aqs'] = Annotation.objects.filter(Q(layer__private=False)|Q(user=user))
#group by only allows sorting by name or number of itmes #group by only allows sorting by name or number of itmes
return query return query
def find(request): def find(request):
''' '''
param data { param data {
@ -147,12 +153,13 @@ Groups
Positions Positions
param data { param data {
'query': query, 'query': query,
'ids': [] 'positions': [],
'sort': array
} }
query: query object, more on query syntax at query: query object, more on query syntax at
https://wiki.0x2620.org/wiki/pandora/QuerySyntax https://wiki.0x2620.org/wiki/pandora/QuerySyntax
ids: ids of items for which positions are required positions: ids of items for which positions are required
return { return {
status: {...}, status: {...},
data: { data: {
@ -176,20 +183,31 @@ Positions
qs = models.Facet.objects.filter(key=query['group']).filter(item__id__in=item_qs) qs = models.Facet.objects.filter(key=query['group']).filter(item__id__in=item_qs)
qs = qs.values('value').annotate(items=Count('id')).order_by(*order_by) qs = qs.values('value').annotate(items=Count('id')).order_by(*order_by)
if 'ids' in query: if 'positions' in query:
response['data']['positions'] = {} response['data']['positions'] = {}
ids = [j['value'] for j in qs] ids = [j['value'] for j in qs]
response['data']['positions'] = utils.get_positions(ids, query['ids']) response['data']['positions'] = utils.get_positions(ids, query['positions'])
elif 'range' in data: elif 'range' in data:
qs = qs[query['range'][0]:query['range'][1]] qs = qs[query['range'][0]:query['range'][1]]
response['data']['items'] = [{'name': i['value'], 'items': i[items]} for i in qs] response['data']['items'] = [{'name': i['value'], 'items': i[items]} for i in qs]
else: else:
response['data']['items'] = qs.count() response['data']['items'] = qs.count()
elif 'ids' in query: elif 'position' in query:
qs = _order_query(query['qs'], query['sort']) qs = _order_query(query['qs'], query['sort'])
response['data']['positions'] = {}
ids = [j['itemId'] for j in qs.values('itemId')] ids = [j['itemId'] for j in qs.values('itemId')]
response['data']['positions'] = utils.get_positions(ids, query['ids']) data['conditions'] = data['conditions'] + {
'value': query['position'],
'key': query['sort'][0]['key'],
'operator': '^'
}
query = parse_query(data, request.user)
qs = _order_query(query['qs'], query['sort'])
if qs.count() > 0:
response['data']['position'] = utils.get_positions(ids, [qs[0].itemId])[0]
elif 'positions' in query:
qs = _order_query(query['qs'], query['sort'])
ids = [j['itemId'] for j in qs.values('itemId')]
response['data']['positions'] = utils.get_positions(ids, query['positions'])
elif 'keys' in query: elif 'keys' in query:
response['data']['items'] = [] response['data']['items'] = []
qs = _order_query(query['qs'], query['sort']) qs = _order_query(query['qs'], query['sort'])
@ -206,6 +224,10 @@ Positions
r[p] = m.sort.popularity r[p] = m.sort.popularity
else: else:
r[p] = m.json.get(p, '') r[p] = m.json.get(p, '')
if 'annotations' in query:
n = query['annotations']
r['annotations'] = [a.json(layer=True)
for a in query['aqs'].filter(itemID=m.id)[:n]]
return r return r
def only_p(m): def only_p(m):
r = {} r = {}
@ -213,6 +235,10 @@ Positions
m = json.loads(m) m = json.loads(m)
for p in _p: for p in _p:
r[p] = m.get(p, '') r[p] = m.get(p, '')
if 'annotations' in query:
n = query['annotations']
r['annotations'] = [a.json(layer=True)
for a in query['aqs'].filter(item__itemId=m['id'])[:n]]
return r return r
qs = qs[query['range'][0]:query['range'][1]] qs = qs[query['range'][0]:query['range'][1]]
#response['data']['items'] = [m.get_json(_p) for m in qs] #response['data']['items'] = [m.get_json(_p) for m in qs]
@ -224,6 +250,7 @@ Positions
response['data']['items'] = [only_p_sums(m) for m in qs] response['data']['items'] = [only_p_sums(m) for m in qs]
else: else:
response['data']['items'] = [only_p(m['json']) for m in qs.values('json')] response['data']['items'] = [only_p(m['json']) for m in qs.values('json')]
else: # otherwise stats else: # otherwise stats
items = query['qs'] items = query['qs']
files = File.objects.filter(item__in=items).filter(size__gt=0) files = File.objects.filter(item__in=items).filter(size__gt=0)

View file

@ -35,7 +35,7 @@ def parse_query(data, user):
query = {} query = {}
query['range'] = [0, 100] query['range'] = [0, 100]
query['sort'] = [{'key':'user', 'operator':'+'}, {'key':'name', 'operator':'+'}] query['sort'] = [{'key':'user', 'operator':'+'}, {'key':'name', 'operator':'+'}]
for key in ('keys', 'group', 'list', 'range', 'ids', 'sort'): for key in ('keys', 'group', 'list', 'range', 'position', 'positions', 'sort'):
if key in data: if key in data:
query[key] = data[key] query[key] = data[key]
query['qs'] = models.List.objects.find(data, user) query['qs'] = models.List.objects.find(data, user)
@ -100,9 +100,12 @@ def findLists(request):
qs = qs[query['range'][0]:query['range'][1]] qs = qs[query['range'][0]:query['range'][1]]
response['data']['items'] = [l.json(data['keys'], request.user) for l in qs] response['data']['items'] = [l.json(data['keys'], request.user) for l in qs]
elif 'ids' in data: elif 'position' in data:
#FIXME: actually implement position requests
response['data']['position'] = 0
elif 'positions' in data:
ids = [i.get_id() for i in qs] ids = [i.get_id() for i in qs]
response['data']['positions'] = utils.get_positions(ids, query['ids']) response['data']['positions'] = utils.get_positions(ids, query['positions'])
else: else:
response['data']['items'] = qs.count() response['data']['items'] = qs.count()
return render_to_json_response(response) return render_to_json_response(response)

View file

@ -146,19 +146,8 @@ class PlaceManager(Manager):
#join query with operator #join query with operator
qs = self.get_query_set() qs = self.get_query_set()
south=data['area']['south'] conditions = parseConditions(data.get('query', {}).get('conditions', []),
west=data['area']['west'] data.get('query', {}).get('operator', '&'),
north=data['area']['north']
east=data['area']['east']
qs = qs.filter(Q(
Q(Q(south__gt=south)|Q(south__lt=north)|Q(west__gt=west)|Q(west__lt=east)) &
Q(Q(south__gt=south)|Q(south__lt=north)|Q(west__lt=east)|Q(east__gt=east)) &
Q(Q(north__gt=south)|Q(north__lt=north)|Q(west__gt=west)|Q(west__lt=east)) &
Q(Q(north__gt=south)|Q(north__lt=north)|Q(east__gt=west)|Q(east__lt=east))
))
conditions = parseConditions(data['query'].get('conditions', []),
data['query'].get('operator', '&'),
user) user)
if conditions: if conditions:
qs = qs.filter(conditions) qs = qs.filter(conditions)

View file

@ -124,8 +124,7 @@ def parse_query(data, user):
query = {} query = {}
query['range'] = [0, 100] query['range'] = [0, 100]
query['sort'] = [{'key':'name', 'operator':'+'}] query['sort'] = [{'key':'name', 'operator':'+'}]
query['area'] = {'south': -180.0, 'west': -180.0, 'north': 180.0, 'east': 180.0} for key in ('keys', 'group', 'list', 'range', 'position', 'positions', 'sort', 'query'):
for key in ('area', 'keys', 'group', 'list', 'range', 'ids', 'sort', 'query'):
if key in data: if key in data:
query[key] = data[key] query[key] = data[key]
query['qs'] = models.Place.objects.find(query, user) query['qs'] = models.Place.objects.find(query, user)
@ -183,7 +182,7 @@ def findPlaces(request):
} }
} }
param data param data
{'query': query, 'sort': array, 'range': array, 'area': array} {'query': query, 'sort': array, 'range': array}
query: query object, more on query syntax at query: query object, more on query syntax at
https://wiki.0x2620.org/wiki/pandora/QuerySyntax https://wiki.0x2620.org/wiki/pandora/QuerySyntax
@ -199,7 +198,6 @@ def findPlaces(request):
} }
] ]
range: result range, array [from, to] range: result range, array [from, to]
area: {south:, west:, north:, east:} only return places in that square
with keys, items is list of dicts with requested properties: with keys, items is list of dicts with requested properties:
return {'status': {'code': int, 'text': string}, return {'status': {'code': int, 'text': string},
@ -207,11 +205,11 @@ def findPlaces(request):
Positions Positions
param data param data
{'query': query, 'ids': []} {'query': query, 'positions': []}
query: query object, more on query syntax at query: query object, more on query syntax at
https://wiki.0x2620.org/wiki/pandora/QuerySyntax https://wiki.0x2620.org/wiki/pandora/QuerySyntax
ids: ids of places for which positions are required positions: ids of places for which positions are required
''' '''
data = json.loads(request.POST['data']) data = json.loads(request.POST['data'])
print data print data
@ -222,9 +220,20 @@ Positions
if 'keys' in data: if 'keys' in data:
qs = qs[query['range'][0]:query['range'][1]] qs = qs[query['range'][0]:query['range'][1]]
response['data']['items'] = [p.json(request.user) for p in qs] response['data']['items'] = [p.json(request.user) for p in qs]
elif 'ids' in data: elif 'position' in query:
ids = [i.get_id() for i in qs] ids = [i.get_id() for i in qs]
response['data']['positions'] = utils.get_positions(ids, query['ids']) data['conditions'] = data['conditions'] + {
'value': query['position'],
'key': query['sort'][0]['key'],
'operator': '^'
}
query = parse_query(data, request.user)
qs = order_query(query['qs'], query['sort'])
if qs.count() > 0:
response['data']['position'] = utils.get_positions(ids, [qs[0].itemId])[0]
elif 'positions' in data:
ids = [i.get_id() for i in qs]
response['data']['positions'] = utils.get_positions(ids, query['positions'])
else: else:
response['data']['items'] = qs.count() response['data']['items'] = qs.count()
response['data']['area'] = qs.aggregate( response['data']['area'] = qs.aggregate(

View file

@ -6,8 +6,8 @@ pandora.ui.flipbook = function(item) {
pandora.URL.set(item + '/timeline'); pandora.URL.set(item + '/timeline');
}); });
pandora.api.getItem(item, function(result) { pandora.api.getItem(item, function(result) {
var duration = result.data.item.duration, var duration = result.data.duration,
posterFrame = result.data.item.posterFrame || parseInt(duration/2), posterFrame = result.data.posterFrame || parseInt(duration/2),
steps = 24, steps = 24,
framePrefix = '/' + item + '/frame/' + that.width() + '/', framePrefix = '/' + item + '/frame/' + that.width() + '/',
frames = {}; frames = {};

View file

@ -369,7 +369,7 @@ pandora.ui.list = function(view) { // fixme: remove view argument
} }
if (data.ids.length == 1) { if (data.ids.length == 1) {
pandora.api.getItem(data.ids[0], function(result) { pandora.api.getItem(data.ids[0], function(result) {
app.ui.infoRatio = result.data.item.stream.aspectRatio; app.ui.infoRatio = result.data.stream.aspectRatio;
var height = app.$ui.info.width() / app.ui.infoRatio + 16; var height = app.$ui.info.width() / app.ui.infoRatio + 16;
if(app.$ui.infoStill) app.$ui.infoStill.removeElement(); if(app.$ui.infoStill) app.$ui.infoStill.removeElement();
app.$ui.infoStill = pandora.ui.flipbook(data.ids[0]) app.$ui.infoStill = pandora.ui.flipbook(data.ids[0])