2009-08-01 14:14:54 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
# vi:si:et:sw=4:sts=4:ts=4
|
2010-02-16 12:41:57 +00:00
|
|
|
from __future__ import division
|
2009-08-01 14:14:54 +00:00
|
|
|
import os.path
|
2011-01-25 14:39:03 +00:00
|
|
|
from datetime import datetime
|
2011-06-27 19:40:44 +00:00
|
|
|
import mimetypes
|
2011-01-01 11:44:42 +00:00
|
|
|
|
2011-01-25 14:39:03 +00:00
|
|
|
from django.db.models import Count, Sum, Max
|
2010-02-16 12:41:57 +00:00
|
|
|
from django.http import HttpResponse, Http404
|
2011-01-01 11:44:42 +00:00
|
|
|
from django.shortcuts import get_object_or_404, redirect
|
2010-02-08 10:26:25 +00:00
|
|
|
from django.conf import settings
|
2010-02-03 12:05:38 +00:00
|
|
|
|
2010-11-26 15:07:24 +00:00
|
|
|
from ox.utils import json
|
2010-02-03 12:05:38 +00:00
|
|
|
|
2010-11-23 09:53:12 +00:00
|
|
|
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.http import HttpFileResponse
|
2011-06-01 11:03:43 +00:00
|
|
|
from django.db.models import Q
|
2010-07-07 22:46:41 +00:00
|
|
|
import ox
|
2009-12-31 15:04:32 +00:00
|
|
|
|
2009-08-01 14:14:54 +00:00
|
|
|
import models
|
2009-10-04 22:00:08 +00:00
|
|
|
import utils
|
2010-06-25 11:53:57 +00:00
|
|
|
import tasks
|
2010-01-25 09:20:21 +00:00
|
|
|
|
2010-07-12 14:56:14 +00:00
|
|
|
from archive.models import File
|
2010-09-07 14:05:38 +00:00
|
|
|
from archive import extract
|
2011-06-01 11:03:43 +00:00
|
|
|
from annotation.models import Annotation
|
2010-09-07 14:05:38 +00:00
|
|
|
|
2010-12-22 07:45:37 +00:00
|
|
|
from api.actions import actions
|
2010-11-25 15:21:23 +00:00
|
|
|
|
2011-01-01 11:44:42 +00:00
|
|
|
|
2010-02-22 09:25:29 +00:00
|
|
|
def _order_query(qs, sort, prefix='sort__'):
|
2009-08-16 12:23:29 +00:00
|
|
|
order_by = []
|
2010-07-12 14:56:14 +00:00
|
|
|
if len(sort) == 1:
|
2011-01-02 09:51:39 +00:00
|
|
|
if sort[0]['key'] == 'title':
|
2010-07-12 14:56:14 +00:00
|
|
|
sort.append({'operator': '-', 'key': 'year'})
|
|
|
|
sort.append({'operator': '+', 'key': 'director'})
|
2011-01-02 09:51:39 +00:00
|
|
|
elif sort[0]['key'] == 'director':
|
|
|
|
sort.append({'operator': '-', 'key': 'year'})
|
|
|
|
sort.append({'operator': '+', 'key': 'title'})
|
|
|
|
elif sort[0]['key'] == 'year':
|
|
|
|
sort.append({'operator': '+', 'key': 'director'})
|
|
|
|
sort.append({'operator': '+', 'key': 'title'})
|
2011-01-02 10:08:17 +00:00
|
|
|
elif not sort[0]['key'] in ('value', 'value_sort'):
|
2011-01-02 09:51:39 +00:00
|
|
|
sort.append({'operator': '+', 'key': 'director'})
|
|
|
|
sort.append({'operator': '-', 'key': 'year'})
|
|
|
|
sort.append({'operator': '+', 'key': 'title'})
|
2011-01-03 14:14:54 +00:00
|
|
|
|
2010-02-22 09:25:29 +00:00
|
|
|
for e in sort:
|
2010-06-30 08:40:48 +00:00
|
|
|
operator = e['operator']
|
2011-01-01 11:44:42 +00:00
|
|
|
if operator != '-':
|
|
|
|
operator = ''
|
2011-01-25 14:39:03 +00:00
|
|
|
key = {
|
|
|
|
'id': 'itemId',
|
|
|
|
'accessed': 'accessed__access',
|
2011-01-24 02:35:52 +00:00
|
|
|
'viewed': 'accessed__access',
|
2011-01-25 14:39:03 +00:00
|
|
|
}.get(e['key'], e['key'])
|
2011-01-24 02:35:52 +00:00
|
|
|
if key not in ('accessed__access', 'accessed__accessed'):
|
2011-01-25 14:39:03 +00:00
|
|
|
key = "%s%s" % (prefix, key)
|
|
|
|
order = '%s%s' % (operator, key)
|
2010-02-22 09:25:29 +00:00
|
|
|
order_by.append(order)
|
|
|
|
if order_by:
|
2011-01-24 22:46:16 +00:00
|
|
|
qs = qs.order_by(*order_by, nulls_last=True)
|
2010-02-22 09:25:29 +00:00
|
|
|
return qs
|
|
|
|
|
2011-01-24 13:44:38 +00:00
|
|
|
def _order_by_group(query):
|
|
|
|
if 'sort' in query:
|
|
|
|
if len(query['sort']) == 1 and query['sort'][0]['key'] == 'items':
|
|
|
|
if query['group'] == "year":
|
|
|
|
order_by = query['sort'][0]['operator'] == '-' and 'items' or '-items'
|
|
|
|
else:
|
|
|
|
order_by = query['sort'][0]['operator'] == '-' and '-items' or 'items'
|
|
|
|
if query['group'] != "keyword":
|
|
|
|
order_by = (order_by, 'value_sort')
|
|
|
|
else:
|
|
|
|
order_by = (order_by,)
|
|
|
|
else:
|
|
|
|
order_by = query['sort'][0]['operator'] == '-' and '-value_sort' or 'value_sort'
|
|
|
|
order_by = (order_by, 'items')
|
|
|
|
else:
|
|
|
|
order_by = ('-value_sort', 'items')
|
|
|
|
return order_by
|
2011-01-04 07:32:32 +00:00
|
|
|
|
2011-02-22 15:26:06 +00:00
|
|
|
def parse_query(data, user):
|
2010-02-22 09:25:29 +00:00
|
|
|
query = {}
|
|
|
|
query['range'] = [0, 100]
|
2010-07-01 08:49:08 +00:00
|
|
|
query['sort'] = [{'key':'title', 'operator':'+'}]
|
2011-06-01 11:03:43 +00:00
|
|
|
for key in ('sort', 'keys', 'group', 'range', 'position', 'positions'):
|
2010-02-22 09:25:29 +00:00
|
|
|
if key in data:
|
|
|
|
query[key] = data[key]
|
2010-09-23 16:01:48 +00:00
|
|
|
query['qs'] = models.Item.objects.find(data, user)
|
2011-06-01 11:03:43 +00:00
|
|
|
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))
|
|
|
|
|
2010-02-22 09:25:29 +00:00
|
|
|
#group by only allows sorting by name or number of itmes
|
|
|
|
return query
|
2009-08-01 14:14:54 +00:00
|
|
|
|
2010-12-22 07:45:37 +00:00
|
|
|
def find(request):
|
2010-01-25 09:20:21 +00:00
|
|
|
'''
|
2010-12-24 09:23:34 +00:00
|
|
|
param data {
|
|
|
|
'query': query,
|
|
|
|
'sort': array,
|
|
|
|
'range': array
|
|
|
|
}
|
2011-01-01 11:44:42 +00:00
|
|
|
|
2010-02-22 09:25:29 +00:00
|
|
|
query: query object, more on query syntax at
|
2010-06-30 14:21:04 +00:00
|
|
|
https://wiki.0x2620.org/wiki/pandora/QuerySyntax
|
2010-06-27 12:41:38 +00:00
|
|
|
sort: array of key, operator dics
|
|
|
|
[
|
|
|
|
{
|
|
|
|
key: "year",
|
|
|
|
operator: "-"
|
|
|
|
},
|
|
|
|
{
|
|
|
|
key: "director",
|
|
|
|
operator: ""
|
|
|
|
}
|
|
|
|
]
|
2010-02-22 09:25:29 +00:00
|
|
|
range: result range, array [from, to]
|
|
|
|
keys: array of keys to return
|
|
|
|
group: group elements by, country, genre, director...
|
2010-01-26 13:11:57 +00:00
|
|
|
|
2010-02-22 09:25:29 +00:00
|
|
|
with keys, items is list of dicts with requested properties:
|
2010-01-26 13:11:57 +00:00
|
|
|
return {'status': {'code': int, 'text': string},
|
2010-01-29 11:03:45 +00:00
|
|
|
'data': {items: array}}
|
2010-01-26 13:11:57 +00:00
|
|
|
|
2010-06-30 11:18:04 +00:00
|
|
|
Groups
|
2010-12-24 09:23:34 +00:00
|
|
|
param data {
|
|
|
|
'query': query,
|
|
|
|
'key': string,
|
|
|
|
'group': string,
|
|
|
|
'range': array
|
|
|
|
}
|
2011-01-01 11:44:42 +00:00
|
|
|
|
2010-06-30 11:18:04 +00:00
|
|
|
query: query object, more on query syntax at
|
2010-06-30 14:21:04 +00:00
|
|
|
https://wiki.0x2620.org/wiki/pandora/QuerySyntax
|
2010-06-30 11:18:04 +00:00
|
|
|
range: result range, array [from, to]
|
|
|
|
keys: array of keys to return
|
|
|
|
group: group elements by, country, genre, director...
|
|
|
|
|
|
|
|
possible values for keys: name, items
|
|
|
|
|
|
|
|
with keys
|
|
|
|
items contains list of {'name': string, 'items': int}:
|
|
|
|
return {'status': {'code': int, 'text': string},
|
|
|
|
'data': {items: array}}
|
2010-01-26 13:11:57 +00:00
|
|
|
|
2010-06-30 11:18:04 +00:00
|
|
|
without keys: return number of items in given query
|
2010-01-26 13:11:57 +00:00
|
|
|
return {'status': {'code': int, 'text': string},
|
|
|
|
'data': {items: int}}
|
|
|
|
|
2010-06-30 11:18:04 +00:00
|
|
|
Positions
|
2010-12-24 09:23:34 +00:00
|
|
|
param data {
|
|
|
|
'query': query,
|
2011-06-01 11:03:43 +00:00
|
|
|
'positions': [],
|
|
|
|
'sort': array
|
2010-12-24 09:23:34 +00:00
|
|
|
}
|
2011-01-01 11:44:42 +00:00
|
|
|
|
2010-06-30 11:18:04 +00:00
|
|
|
query: query object, more on query syntax at
|
2010-06-30 14:21:04 +00:00
|
|
|
https://wiki.0x2620.org/wiki/pandora/QuerySyntax
|
2011-06-01 11:03:43 +00:00
|
|
|
positions: ids of items for which positions are required
|
2011-01-13 12:06:05 +00:00
|
|
|
return {
|
|
|
|
status: {...},
|
|
|
|
data: {
|
|
|
|
positions: {
|
|
|
|
id: position
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2010-01-25 09:20:21 +00:00
|
|
|
'''
|
2010-02-22 09:25:29 +00:00
|
|
|
data = json.loads(request.POST['data'])
|
2010-06-26 14:32:08 +00:00
|
|
|
if settings.JSON_DEBUG:
|
|
|
|
print json.dumps(data, indent=2)
|
2011-02-22 15:26:06 +00:00
|
|
|
query = parse_query(data, request.user)
|
2011-01-01 11:44:42 +00:00
|
|
|
|
2010-02-10 13:10:28 +00:00
|
|
|
response = json_response({})
|
2010-07-01 08:49:08 +00:00
|
|
|
if 'group' in query:
|
2010-06-30 11:18:04 +00:00
|
|
|
response['data']['items'] = []
|
2010-09-23 16:01:48 +00:00
|
|
|
items = 'items'
|
|
|
|
item_qs = query['qs']
|
2011-01-24 13:44:38 +00:00
|
|
|
order_by = _order_by_group(query)
|
2010-09-23 16:01:48 +00:00
|
|
|
qs = models.Facet.objects.filter(key=query['group']).filter(item__id__in=item_qs)
|
2011-01-03 06:44:38 +00:00
|
|
|
qs = qs.values('value').annotate(items=Count('id')).order_by(*order_by)
|
2011-01-03 14:14:54 +00:00
|
|
|
|
2011-06-01 11:03:43 +00:00
|
|
|
if 'positions' in query:
|
2010-07-01 08:49:08 +00:00
|
|
|
response['data']['positions'] = {}
|
2011-01-03 14:14:54 +00:00
|
|
|
ids = [j['value'] for j in qs]
|
2011-06-01 11:03:43 +00:00
|
|
|
response['data']['positions'] = utils.get_positions(ids, query['positions'])
|
2010-07-01 08:49:08 +00:00
|
|
|
elif 'range' in data:
|
2010-06-30 11:18:04 +00:00
|
|
|
qs = qs[query['range'][0]:query['range'][1]]
|
2011-01-03 14:14:54 +00:00
|
|
|
response['data']['items'] = [{'name': i['value'], 'items': i[items]} for i in qs]
|
2010-06-30 11:18:04 +00:00
|
|
|
else:
|
|
|
|
response['data']['items'] = qs.count()
|
2011-06-01 11:03:43 +00:00
|
|
|
elif 'position' in query:
|
2010-06-30 11:18:04 +00:00
|
|
|
qs = _order_query(query['qs'], query['sort'])
|
2010-09-23 16:01:48 +00:00
|
|
|
ids = [j['itemId'] for j in qs.values('itemId')]
|
2011-06-01 11:03:43 +00:00
|
|
|
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'])
|
2010-07-01 08:49:08 +00:00
|
|
|
elif 'keys' in query:
|
|
|
|
response['data']['items'] = []
|
|
|
|
qs = _order_query(query['qs'], query['sort'])
|
|
|
|
_p = query['keys']
|
2011-01-25 14:39:03 +00:00
|
|
|
def only_p_sums(m):
|
|
|
|
r = {}
|
|
|
|
for p in _p:
|
2011-01-24 02:35:52 +00:00
|
|
|
if p == 'viewed' and request.user.is_authenticated():
|
|
|
|
value = m.accessed.filter(user=request.user).annotate(v=Max('access'))
|
|
|
|
r[p] = value.exists() and value[0].v or None
|
|
|
|
elif p == 'accessed':
|
|
|
|
r[p] = m.a
|
2011-01-25 14:39:03 +00:00
|
|
|
elif p == 'popularity':
|
2011-01-24 02:35:52 +00:00
|
|
|
r[p] = m.sort.popularity
|
2011-01-25 14:39:03 +00:00
|
|
|
else:
|
|
|
|
r[p] = m.json.get(p, '')
|
2011-06-01 11:03:43 +00:00
|
|
|
if 'annotations' in query:
|
|
|
|
n = query['annotations']
|
|
|
|
r['annotations'] = [a.json(layer=True)
|
|
|
|
for a in query['aqs'].filter(itemID=m.id)[:n]]
|
2011-01-25 14:39:03 +00:00
|
|
|
return r
|
2010-07-01 08:49:08 +00:00
|
|
|
def only_p(m):
|
|
|
|
r = {}
|
|
|
|
if m:
|
2011-08-16 15:36:35 +00:00
|
|
|
m = json.loads(m, object_hook=ox.django.fields.from_json)
|
2010-07-01 08:49:08 +00:00
|
|
|
for p in _p:
|
|
|
|
r[p] = m.get(p, '')
|
2011-06-01 11:03:43 +00:00
|
|
|
if 'annotations' in query:
|
|
|
|
n = query['annotations']
|
|
|
|
r['annotations'] = [a.json(layer=True)
|
|
|
|
for a in query['aqs'].filter(item__itemId=m['id'])[:n]]
|
2010-07-01 08:49:08 +00:00
|
|
|
return r
|
|
|
|
qs = qs[query['range'][0]:query['range'][1]]
|
2011-01-24 13:44:38 +00:00
|
|
|
#response['data']['items'] = [m.get_json(_p) for m in qs]
|
2011-01-25 14:39:03 +00:00
|
|
|
if 'popularity' in _p:
|
|
|
|
qs = qs.annotate(popularity=Sum('accessed__accessed'))
|
2011-01-24 02:35:52 +00:00
|
|
|
if 'accessed' in _p:
|
|
|
|
qs = qs.annotate(a=Max('accessed__access'))
|
|
|
|
if 'viewed' in _p or 'popularity' in _p or 'accessed' in _p:
|
2011-01-25 14:39:03 +00:00
|
|
|
response['data']['items'] = [only_p_sums(m) for m in qs]
|
|
|
|
else:
|
|
|
|
response['data']['items'] = [only_p(m['json']) for m in qs.values('json')]
|
2011-06-01 11:03:43 +00:00
|
|
|
|
2010-07-01 08:49:08 +00:00
|
|
|
else: # otherwise stats
|
2010-09-23 16:01:48 +00:00
|
|
|
items = query['qs']
|
2011-04-08 08:20:59 +00:00
|
|
|
files = File.objects.filter(item__in=items).filter(size__gt=0)
|
2010-07-01 08:49:08 +00:00
|
|
|
r = files.aggregate(
|
2010-07-12 14:56:14 +00:00
|
|
|
Sum('duration'),
|
|
|
|
Sum('pixels'),
|
|
|
|
Sum('size')
|
2010-07-01 08:49:08 +00:00
|
|
|
)
|
2010-07-12 14:56:14 +00:00
|
|
|
response['data']['duration'] = r['duration__sum']
|
2010-01-26 13:11:57 +00:00
|
|
|
response['data']['files'] = files.count()
|
2010-09-23 16:01:48 +00:00
|
|
|
response['data']['items'] = items.count()
|
2010-07-12 14:56:14 +00:00
|
|
|
response['data']['pixels'] = r['pixels__sum']
|
2011-01-24 22:46:16 +00:00
|
|
|
response['data']['runtime'] = items.aggregate(Sum('sort__runtime'))['sort__runtime__sum']
|
2010-07-12 14:56:14 +00:00
|
|
|
response['data']['size'] = r['size__sum']
|
2011-01-24 13:44:38 +00:00
|
|
|
for key in ('runtime', 'duration', 'pixels', 'size'):
|
|
|
|
if response['data'][key] == None:
|
|
|
|
response['data'][key] = 0
|
2009-08-01 14:14:54 +00:00
|
|
|
return render_to_json_response(response)
|
2010-12-22 07:45:37 +00:00
|
|
|
actions.register(find)
|
|
|
|
|
2011-01-04 07:32:32 +00:00
|
|
|
|
2011-01-03 14:14:54 +00:00
|
|
|
def autocomplete(request):
|
|
|
|
'''
|
|
|
|
param data
|
|
|
|
key
|
|
|
|
value
|
|
|
|
operator '', '^', '$'
|
|
|
|
range
|
|
|
|
return
|
|
|
|
'''
|
|
|
|
data = json.loads(request.POST['data'])
|
|
|
|
if not 'range' in data:
|
|
|
|
data['range'] = [0, 10]
|
|
|
|
op = data.get('operator', '')
|
|
|
|
|
2011-01-14 14:32:48 +00:00
|
|
|
site_config = models.site_config()
|
2011-01-24 09:38:46 +00:00
|
|
|
key = site_config['keys'][data['key']]
|
2011-01-25 15:14:36 +00:00
|
|
|
order_by = key.get('autocompleteSortKey', False)
|
2011-01-15 14:22:29 +00:00
|
|
|
if order_by:
|
2011-01-24 22:46:16 +00:00
|
|
|
order_by = '-sort__%s' % order_by
|
2011-01-15 14:22:29 +00:00
|
|
|
else:
|
|
|
|
order_by = '-items'
|
2011-01-25 15:14:36 +00:00
|
|
|
sort_type = key.get('sort', key.get('type', 'string'))
|
2011-01-24 09:38:46 +00:00
|
|
|
if sort_type == 'title':
|
2011-02-22 15:26:06 +00:00
|
|
|
qs = parse_query({'query': data.get('query', {})}, request.user)['qs']
|
2011-01-03 14:14:54 +00:00
|
|
|
if data['value']:
|
|
|
|
if op == '':
|
|
|
|
qs = qs.filter(find__key=data['key'], find__value__icontains=data['value'])
|
|
|
|
elif op == '^':
|
|
|
|
qs = qs.filter(find__key=data['key'], find__value__istartswith=data['value'])
|
|
|
|
elif op == '$':
|
|
|
|
qs = qs.filter(find__key=data['key'], find__value__iendswith=data['value'])
|
2011-01-25 15:23:21 +00:00
|
|
|
qs = qs.order_by(order_by, nulls_last=True)
|
2011-01-03 14:14:54 +00:00
|
|
|
qs = qs[data['range'][0]:data['range'][1]]
|
|
|
|
response = json_response({})
|
|
|
|
response['data']['items'] = [i.get(data['key']) for i in qs]
|
|
|
|
else:
|
|
|
|
qs = models.Facet.objects.filter(key=data['key'])
|
|
|
|
if data['value']:
|
|
|
|
if op == '':
|
|
|
|
qs = qs.filter(value__icontains=data['value'])
|
|
|
|
elif op == '^':
|
|
|
|
qs = qs.filter(value__istartswith=data['value'])
|
|
|
|
elif op == '$':
|
|
|
|
qs = qs.filter(value__iendswith=data['value'])
|
2011-01-15 14:22:29 +00:00
|
|
|
qs = qs.values('value').annotate(items=Count('id'))
|
|
|
|
qs = qs.order_by(order_by)
|
2011-01-03 14:14:54 +00:00
|
|
|
qs = qs[data['range'][0]:data['range'][1]]
|
|
|
|
response = json_response({})
|
|
|
|
response['data']['items'] = [i['value'] for i in qs]
|
|
|
|
return render_to_json_response(response)
|
|
|
|
actions.register(autocomplete)
|
2010-12-22 07:45:37 +00:00
|
|
|
|
2011-01-04 07:32:32 +00:00
|
|
|
|
2011-07-30 10:51:23 +00:00
|
|
|
def get(request):
|
|
|
|
'''
|
|
|
|
param data {
|
|
|
|
id: string
|
|
|
|
keys: array
|
|
|
|
}
|
|
|
|
return item array
|
|
|
|
'''
|
|
|
|
response = json_response({})
|
|
|
|
data = json.loads(request.POST['data'])
|
|
|
|
item = get_object_or_404_json(models.Item, itemId=data['id'])
|
|
|
|
if item.access(request.user):
|
|
|
|
info = item.get_json(data['keys'])
|
2011-08-06 13:34:56 +00:00
|
|
|
if not data['keys'] or 'stream' in data['keys']:
|
2011-07-30 10:51:23 +00:00
|
|
|
info['stream'] = item.get_stream()
|
2011-08-06 13:34:56 +00:00
|
|
|
if not data['keys'] or 'layers' in data['keys']:
|
2011-07-30 10:51:23 +00:00
|
|
|
info['layers'] = item.get_layers(request.user)
|
2011-07-30 12:23:06 +00:00
|
|
|
response['data'] = info
|
2011-07-30 10:51:23 +00:00
|
|
|
else:
|
|
|
|
response = json_response(status=403, text='permission denied')
|
|
|
|
return render_to_json_response(response)
|
|
|
|
actions.register(get)
|
|
|
|
|
2010-12-22 07:45:37 +00:00
|
|
|
def getItem(request):
|
2010-01-25 09:20:21 +00:00
|
|
|
'''
|
2010-01-27 06:43:17 +00:00
|
|
|
param data
|
|
|
|
string id
|
|
|
|
|
2010-09-18 13:03:27 +00:00
|
|
|
return item array
|
2010-01-25 09:20:21 +00:00
|
|
|
'''
|
2010-02-10 13:10:28 +00:00
|
|
|
response = json_response({})
|
2010-01-25 09:20:21 +00:00
|
|
|
itemId = json.loads(request.POST['data'])
|
2010-09-23 16:01:48 +00:00
|
|
|
item = get_object_or_404_json(models.Item, itemId=itemId)
|
2011-01-24 13:44:38 +00:00
|
|
|
if item.access(request.user):
|
|
|
|
info = item.get_json()
|
|
|
|
info['stream'] = item.get_stream()
|
2011-02-11 10:21:25 +00:00
|
|
|
info['layers'] = item.get_layers(request.user)
|
2011-05-26 07:34:52 +00:00
|
|
|
response['data'] = info
|
2011-01-24 13:44:38 +00:00
|
|
|
else:
|
|
|
|
response = json_response(status=403, text='permission denied')
|
2010-01-25 09:20:21 +00:00
|
|
|
return render_to_json_response(response)
|
2010-12-22 07:45:37 +00:00
|
|
|
actions.register(getItem)
|
2010-01-25 09:20:21 +00:00
|
|
|
|
2011-01-04 07:32:32 +00:00
|
|
|
|
2010-01-26 13:11:57 +00:00
|
|
|
@login_required_json
|
2010-12-22 07:45:37 +00:00
|
|
|
def editItem(request):
|
2010-01-26 02:24:05 +00:00
|
|
|
'''
|
2010-12-24 09:23:34 +00:00
|
|
|
param data {
|
|
|
|
id: string,
|
|
|
|
key: value,..
|
|
|
|
}
|
|
|
|
return {
|
|
|
|
status: {'code': int, 'text': string},
|
|
|
|
data: {}
|
|
|
|
}
|
2010-01-26 02:24:05 +00:00
|
|
|
'''
|
2010-01-27 06:43:17 +00:00
|
|
|
data = json.loads(request.POST['data'])
|
2010-09-23 16:01:48 +00:00
|
|
|
item = get_object_or_404_json(models.Item, itemId=data['id'])
|
2010-01-27 06:43:17 +00:00
|
|
|
if item.editable(request.user):
|
2011-02-25 12:12:56 +00:00
|
|
|
response = json_response(status=200, text='ok')
|
2010-09-18 13:03:27 +00:00
|
|
|
item.edit(data)
|
|
|
|
else:
|
2010-02-10 13:10:28 +00:00
|
|
|
response = json_response(status=403, text='permissino denied')
|
2010-01-25 09:20:21 +00:00
|
|
|
return render_to_json_response(response)
|
2011-01-13 08:33:14 +00:00
|
|
|
actions.register(editItem, cache=False)
|
2010-01-26 02:24:05 +00:00
|
|
|
|
2010-01-26 13:11:57 +00:00
|
|
|
@login_required_json
|
2011-08-01 17:57:26 +00:00
|
|
|
def remove(request):
|
2010-01-26 02:24:05 +00:00
|
|
|
'''
|
2011-08-01 17:57:26 +00:00
|
|
|
param data {
|
|
|
|
id: string
|
|
|
|
}
|
2010-01-27 06:43:17 +00:00
|
|
|
|
|
|
|
return {'status': {'code': int, 'text': string}}
|
2010-01-26 02:24:05 +00:00
|
|
|
'''
|
2010-02-10 13:10:28 +00:00
|
|
|
response = json_response({})
|
2011-08-01 17:57:26 +00:00
|
|
|
data = json.loads(request.POST['data'])
|
|
|
|
item = get_object_or_404_json(models.Item, itemId=data['id'])
|
2010-09-18 13:03:27 +00:00
|
|
|
if item.editable(request.user):
|
2011-08-01 17:57:26 +00:00
|
|
|
#FIXME: is this cascading enough or do we end up with orphan files etc.
|
|
|
|
item.delete()
|
|
|
|
response = json_response(status=200, text='removed')
|
2010-09-18 13:03:27 +00:00
|
|
|
else:
|
2010-11-25 15:21:23 +00:00
|
|
|
response = json_response(status=403, text='permission denied')
|
2010-01-25 09:20:21 +00:00
|
|
|
return render_to_json_response(response)
|
2011-08-01 17:57:26 +00:00
|
|
|
actions.register(remove, cache=False)
|
2010-01-26 02:24:05 +00:00
|
|
|
|
2010-09-13 13:46:49 +00:00
|
|
|
'''
|
|
|
|
Poster API
|
|
|
|
'''
|
2010-12-22 07:45:37 +00:00
|
|
|
def parse(request): #parse path and return info
|
2010-01-26 13:11:57 +00:00
|
|
|
'''
|
2010-12-24 09:23:34 +00:00
|
|
|
param data {
|
|
|
|
path: string
|
|
|
|
}
|
|
|
|
return {
|
|
|
|
status: {'code': int, 'text': string},
|
2010-12-24 10:31:44 +00:00
|
|
|
data: {
|
|
|
|
imdb: string
|
|
|
|
}
|
2010-12-24 09:23:34 +00:00
|
|
|
}
|
2010-01-26 13:11:57 +00:00
|
|
|
'''
|
|
|
|
path = json.loads(request.POST['data'])['path']
|
2010-08-07 14:31:20 +00:00
|
|
|
response = json_response(utils.parse_path(path))
|
|
|
|
return render_to_json_response(response)
|
2010-12-22 07:45:37 +00:00
|
|
|
actions.register(parse)
|
2010-08-07 14:31:20 +00:00
|
|
|
|
|
|
|
|
2010-12-22 07:45:37 +00:00
|
|
|
def setPosterFrame(request): #parse path and return info
|
2010-08-07 14:31:20 +00:00
|
|
|
'''
|
2010-12-24 09:23:34 +00:00
|
|
|
param data {
|
|
|
|
id: itemId,
|
|
|
|
position: float
|
|
|
|
}
|
|
|
|
return {
|
|
|
|
status: {'code': int, 'text': string},
|
2010-12-24 10:31:44 +00:00
|
|
|
data: {
|
|
|
|
}
|
2010-12-24 09:23:34 +00:00
|
|
|
}
|
2010-08-07 14:31:20 +00:00
|
|
|
'''
|
|
|
|
data = json.loads(request.POST['data'])
|
2010-09-23 16:01:48 +00:00
|
|
|
item = get_object_or_404_json(models.Item, itemId=data['id'])
|
2010-08-07 14:31:20 +00:00
|
|
|
if item.editable(request.user):
|
|
|
|
#FIXME: some things need to be updated after changing this
|
|
|
|
item.poster_frame = data['position']
|
|
|
|
item.save()
|
2011-06-06 18:38:16 +00:00
|
|
|
tasks.update_poster(item.itemId)
|
2010-12-24 09:23:34 +00:00
|
|
|
response = json_response()
|
2010-09-18 13:03:27 +00:00
|
|
|
else:
|
2010-08-07 14:31:20 +00:00
|
|
|
response = json_response(status=403, text='permissino denied')
|
|
|
|
return render_to_json_response(response)
|
2011-01-13 08:33:14 +00:00
|
|
|
actions.register(setPosterFrame, cache=False)
|
2010-08-07 14:31:20 +00:00
|
|
|
|
2010-12-22 07:45:37 +00:00
|
|
|
def setPoster(request): #parse path and return info
|
2010-08-07 14:31:20 +00:00
|
|
|
'''
|
2010-12-24 09:23:34 +00:00
|
|
|
param data {
|
|
|
|
id: itemId,
|
2011-07-30 12:52:49 +00:00
|
|
|
source: string
|
2010-12-24 09:23:34 +00:00
|
|
|
}
|
|
|
|
return {
|
|
|
|
status: {'code': int, 'text': string},
|
2010-12-24 10:31:44 +00:00
|
|
|
data: {
|
|
|
|
poster: {url,width,height}
|
|
|
|
}
|
2010-12-24 09:23:34 +00:00
|
|
|
}
|
2010-08-07 14:31:20 +00:00
|
|
|
'''
|
|
|
|
data = json.loads(request.POST['data'])
|
2010-09-23 16:01:48 +00:00
|
|
|
item = get_object_or_404_json(models.Item, itemId=data['id'])
|
2011-07-26 17:22:23 +00:00
|
|
|
response = json_response()
|
2010-08-07 14:31:20 +00:00
|
|
|
if item.editable(request.user):
|
2011-07-30 12:52:49 +00:00
|
|
|
valid_sources = [p['source'] for p in item.get_posters()]
|
|
|
|
if data['source'] in valid_sources:
|
|
|
|
item.poster_source = data['source']
|
2010-09-10 15:12:22 +00:00
|
|
|
if item.poster:
|
|
|
|
item.poster.delete()
|
|
|
|
item.save()
|
2011-08-01 16:55:18 +00:00
|
|
|
tasks.update_poster(item.itemId)
|
2010-12-24 09:23:34 +00:00
|
|
|
response = json_response()
|
2011-08-19 12:20:30 +00:00
|
|
|
response['data']['posterAspect'] = item.poster_width/item.poster_height
|
2010-09-10 15:12:22 +00:00
|
|
|
else:
|
|
|
|
response = json_response(status=403, text='invalid poster url')
|
2010-09-18 13:03:27 +00:00
|
|
|
else:
|
2010-09-10 15:12:22 +00:00
|
|
|
response = json_response(status=403, text='permission denied')
|
2010-01-25 09:20:21 +00:00
|
|
|
return render_to_json_response(response)
|
2011-01-13 08:33:14 +00:00
|
|
|
actions.register(setPoster, cache=False)
|
2010-01-25 09:20:21 +00:00
|
|
|
|
2011-06-04 16:15:38 +00:00
|
|
|
def lookup(request):
|
|
|
|
'''
|
|
|
|
param data {
|
|
|
|
title: string,
|
|
|
|
director: [string],
|
|
|
|
year: string,
|
|
|
|
id: string
|
|
|
|
}
|
|
|
|
return {
|
|
|
|
status: {'code': int, 'text': string},
|
|
|
|
data: {
|
|
|
|
title: string,
|
|
|
|
director: [string],
|
|
|
|
year: string,
|
|
|
|
id: string
|
|
|
|
}
|
|
|
|
}
|
|
|
|
'''
|
|
|
|
data = json.loads(request.POST['data'])
|
|
|
|
if 'id' in data:
|
|
|
|
i = models.Item.objects.get(itemId=data['id'])
|
|
|
|
r = {'id': i.itemId}
|
|
|
|
for key in ('title', 'director', 'year'):
|
|
|
|
r[key] = i.get(key)
|
|
|
|
response = json_response(r)
|
|
|
|
else:
|
|
|
|
response = json_response(status=404, text='not found')
|
|
|
|
return render_to_json_response(response)
|
|
|
|
actions.register(lookup)
|
|
|
|
|
2010-12-22 07:45:37 +00:00
|
|
|
def getImdbId(request):
|
2010-01-26 13:11:57 +00:00
|
|
|
'''
|
2010-12-24 09:23:34 +00:00
|
|
|
param data {
|
|
|
|
title: string,
|
|
|
|
director: string,
|
|
|
|
year: string
|
|
|
|
}
|
|
|
|
return {
|
|
|
|
status: {'code': int, 'text': string},
|
2010-12-24 10:31:44 +00:00
|
|
|
data: {
|
|
|
|
imdbId:string
|
|
|
|
}
|
2010-12-24 09:23:34 +00:00
|
|
|
}
|
2010-01-26 13:11:57 +00:00
|
|
|
'''
|
2011-01-01 11:44:42 +00:00
|
|
|
data = json.loads(request.POST['data'])
|
|
|
|
imdbId = ox.web.imdb.getImdbId(data['title'], data['director'], timeout=-1)
|
2010-01-26 13:11:57 +00:00
|
|
|
if imdbId:
|
2010-02-10 13:10:28 +00:00
|
|
|
response = json_response({'imdbId': imdbId})
|
2010-01-26 13:11:57 +00:00
|
|
|
else:
|
2010-09-18 13:03:27 +00:00
|
|
|
response = json_response(status=404, text='not found')
|
2010-01-25 09:20:21 +00:00
|
|
|
return render_to_json_response(response)
|
2010-12-22 07:45:37 +00:00
|
|
|
actions.register(getImdbId)
|
2010-01-25 09:20:21 +00:00
|
|
|
|
2010-09-13 13:46:49 +00:00
|
|
|
'''
|
2010-09-18 15:16:45 +00:00
|
|
|
media delivery
|
2010-09-13 13:46:49 +00:00
|
|
|
'''
|
2011-08-16 10:11:07 +00:00
|
|
|
def frame(request, id, size, position=None):
|
2010-09-23 16:01:48 +00:00
|
|
|
item = get_object_or_404(models.Item, itemId=id)
|
2011-08-16 13:46:11 +00:00
|
|
|
frame = None
|
2011-08-16 10:11:07 +00:00
|
|
|
if not position:
|
2011-08-16 13:46:11 +00:00
|
|
|
frames = item.poster_frames()
|
|
|
|
if frames:
|
|
|
|
position = item.poster_frame
|
|
|
|
if position == -1:
|
|
|
|
position = int(len(frames)/2)
|
|
|
|
position = frames[int(position)]['position']
|
|
|
|
elif item.poster_frame == -1 and item.sort.duration:
|
2011-08-16 10:11:07 +00:00
|
|
|
position = item.sort.duration/2
|
|
|
|
else:
|
|
|
|
position = item.poster_frame
|
|
|
|
else:
|
|
|
|
position = float(position.replace(',', '.'))
|
2011-08-16 13:46:11 +00:00
|
|
|
|
|
|
|
if not frame:
|
|
|
|
frame = item.frame(position, int(size))
|
|
|
|
|
2010-09-08 11:56:58 +00:00
|
|
|
if not frame:
|
|
|
|
raise Http404
|
|
|
|
return HttpFileResponse(frame, content_type='image/jpeg')
|
|
|
|
|
2011-07-26 17:22:23 +00:00
|
|
|
def poster_frame(request, id, position):
|
|
|
|
item = get_object_or_404(models.Item, itemId=id)
|
|
|
|
position = int(position)
|
|
|
|
frames = item.poster_frames()
|
|
|
|
if frames and len(frames) > position:
|
2011-08-04 13:28:06 +00:00
|
|
|
frame = frames[position]['path']
|
2011-07-26 17:22:23 +00:00
|
|
|
return HttpFileResponse(frame, content_type='image/jpeg')
|
|
|
|
raise Http404
|
|
|
|
|
2011-01-04 07:32:32 +00:00
|
|
|
|
|
|
|
def image_to_response(item, image, size=None):
|
|
|
|
if size:
|
|
|
|
size = int(size)
|
|
|
|
path = image.path.replace('.jpg', '.%d.jpg'%size)
|
|
|
|
if not os.path.exists(path):
|
|
|
|
image_size = max(image.width, image.height)
|
|
|
|
if size > image_size:
|
2011-08-06 18:00:15 +00:00
|
|
|
path = image.path
|
|
|
|
else:
|
|
|
|
extract.resize_image(image.path, path, size=size)
|
2011-01-04 07:32:32 +00:00
|
|
|
else:
|
|
|
|
path = image.path
|
|
|
|
return HttpFileResponse(path, content_type='image/jpeg')
|
|
|
|
|
2011-07-26 17:22:23 +00:00
|
|
|
def poster_local(request, id):
|
|
|
|
item = get_object_or_404(models.Item, itemId=id)
|
|
|
|
poster = item.path('poster.local.jpg')
|
|
|
|
poster = os.path.abspath(os.path.join(settings.MEDIA_ROOT, poster))
|
|
|
|
return HttpFileResponse(poster, content_type='image/jpeg')
|
2011-01-04 07:32:32 +00:00
|
|
|
|
2010-09-13 15:19:38 +00:00
|
|
|
def poster(request, id, size=None):
|
2010-09-23 16:01:48 +00:00
|
|
|
item = get_object_or_404(models.Item, itemId=id)
|
|
|
|
if item.poster:
|
2011-01-04 07:32:32 +00:00
|
|
|
return image_to_response(item, item.poster, size)
|
2010-09-08 11:56:58 +00:00
|
|
|
else:
|
|
|
|
poster_path = os.path.join(settings.STATIC_ROOT, 'png/posterDark.48.png')
|
2011-08-06 18:00:15 +00:00
|
|
|
response = HttpFileResponse(poster_path, content_type='image/jpeg')
|
|
|
|
response['Cache-Control'] = 'no-cache'
|
|
|
|
return response
|
2010-09-08 11:56:58 +00:00
|
|
|
|
2011-01-04 07:32:32 +00:00
|
|
|
|
|
|
|
def icon(request, id, size=None):
|
|
|
|
item = get_object_or_404(models.Item, itemId=id)
|
|
|
|
if item.icon:
|
|
|
|
return image_to_response(item, item.icon, size)
|
|
|
|
else:
|
|
|
|
raise Http404
|
|
|
|
|
|
|
|
|
2011-08-06 18:00:15 +00:00
|
|
|
def timeline(request, id, size, position):
|
2010-09-23 16:01:48 +00:00
|
|
|
item = get_object_or_404(models.Item, itemId=id)
|
2011-08-06 18:00:15 +00:00
|
|
|
timeline = '%s.%s.%04d.png' %(item.timeline_prefix, size, int(position))
|
2010-09-08 11:56:58 +00:00
|
|
|
return HttpFileResponse(timeline, content_type='image/png')
|
|
|
|
|
2011-01-04 07:32:32 +00:00
|
|
|
|
2011-01-16 13:28:57 +00:00
|
|
|
def timeline_overview(request, id, size):
|
|
|
|
item = get_object_or_404(models.Item, itemId=id)
|
|
|
|
timeline = '%s.%s.png' %(item.timeline_prefix, size)
|
|
|
|
return HttpFileResponse(timeline, content_type='image/png')
|
|
|
|
|
2011-07-03 16:21:27 +00:00
|
|
|
def torrent(request, id, filename=None):
|
|
|
|
item = get_object_or_404(models.Item, itemId=id)
|
|
|
|
if not item.torrent:
|
|
|
|
raise Http404
|
|
|
|
if not filename or filename.endswith('.torrent'):
|
|
|
|
response = HttpFileResponse(item.torrent.path,
|
|
|
|
content_type='application/x-bittorrent')
|
|
|
|
filename = "%s.torrent" % item.get('title')
|
2011-08-11 20:10:06 +00:00
|
|
|
response['Content-Disposition'] = 'attachment; filename="%s"' % filename.encode('utf-8')
|
2011-07-03 16:21:27 +00:00
|
|
|
return response
|
|
|
|
while filename.startswith('/'):
|
|
|
|
filename = filename[1:]
|
|
|
|
filename = filename.replace('/../', '/')
|
|
|
|
filename = item.path('torrent/%s' % filename)
|
|
|
|
filename = os.path.abspath(os.path.join(settings.MEDIA_ROOT, filename))
|
2011-08-11 20:10:06 +00:00
|
|
|
response = HttpFileResponse(filename)
|
|
|
|
response['Content-Disposition'] = 'attachment; filename="%s"' % \
|
|
|
|
os.path.basename(filename.encode('utf-8'))
|
|
|
|
return response
|
2011-07-03 16:21:27 +00:00
|
|
|
|
2011-08-18 12:01:37 +00:00
|
|
|
def video(request, id, resolution, format, index=None):
|
2010-09-23 16:01:48 +00:00
|
|
|
item = get_object_or_404(models.Item, itemId=id)
|
2011-08-06 18:00:15 +00:00
|
|
|
if index:
|
2011-08-17 17:26:10 +00:00
|
|
|
index = int(index) - 1
|
2011-06-27 19:40:44 +00:00
|
|
|
else:
|
2011-08-18 12:01:37 +00:00
|
|
|
index = 0
|
|
|
|
videos = item.main_videos()
|
|
|
|
if index > len(videos):
|
|
|
|
raise Http404
|
|
|
|
|
2011-08-18 18:01:03 +00:00
|
|
|
stream = videos[index].streams.filter(resolution=resolution, format=format)
|
|
|
|
if stream.count() == 0:
|
|
|
|
raise Http404
|
|
|
|
stream = stream[0]
|
2011-08-18 12:01:37 +00:00
|
|
|
path = stream.video.path
|
|
|
|
|
2010-12-31 12:04:33 +00:00
|
|
|
#server side cutting
|
2011-08-18 12:01:37 +00:00
|
|
|
#FIXME: this needs to join segments if needed
|
2011-04-22 23:34:01 +00:00
|
|
|
t = request.GET.get('t')
|
2011-01-02 09:51:39 +00:00
|
|
|
if t:
|
2010-12-31 12:04:33 +00:00
|
|
|
t = map(float, t.split(','))
|
2011-08-18 12:01:37 +00:00
|
|
|
ext = '.%s' % format
|
2011-06-27 19:40:44 +00:00
|
|
|
content_type = mimetypes.guess_type(path)[0]
|
2011-01-02 09:51:39 +00:00
|
|
|
if len(t) == 2 and t[1] > t[0] and stream.info['duration']>=t[1]:
|
2011-01-01 10:25:13 +00:00
|
|
|
response = HttpResponse(extract.chop(path, t[0], t[1]), content_type=content_type)
|
2011-01-02 09:51:39 +00:00
|
|
|
filename = "Clip of %s - %s-%s - %s %s%s" % (
|
2010-12-31 12:04:33 +00:00
|
|
|
item.get('title'),
|
|
|
|
ox.formatDuration(t[0] * 1000),
|
|
|
|
ox.formatDuration(t[1] * 1000),
|
|
|
|
settings.SITENAME,
|
2011-01-02 09:51:39 +00:00
|
|
|
item.itemId,
|
|
|
|
ext
|
2010-12-31 12:04:33 +00:00
|
|
|
)
|
|
|
|
response['Content-Disposition'] = 'attachment; filename="%s"' % filename
|
2011-01-01 10:25:13 +00:00
|
|
|
return response
|
|
|
|
else:
|
2011-01-02 09:51:39 +00:00
|
|
|
filename = "%s - %s %s%s" % (
|
2011-01-01 10:25:13 +00:00
|
|
|
item.get('title'),
|
|
|
|
settings.SITENAME,
|
2011-01-02 09:51:39 +00:00
|
|
|
item.itemId,
|
|
|
|
ext
|
2011-01-01 10:25:13 +00:00
|
|
|
)
|
|
|
|
response = HttpFileResponse(path, content_type=content_type)
|
|
|
|
response['Content-Disposition'] = 'attachment; filename="%s"' % filename
|
2010-12-31 12:04:33 +00:00
|
|
|
return response
|
2011-06-27 19:40:44 +00:00
|
|
|
return HttpFileResponse(path)
|