pandora/pandora/item/views.py

484 lines
16 KiB
Python
Raw Normal View History

2009-08-01 14:14:54 +00:00
# -*- coding: utf-8 -*-
# vi:si:et:sw=4:sts=4:ts=4
from __future__ import division
2009-08-01 14:14:54 +00:00
import os.path
2009-08-16 12:23:29 +00:00
import re
from datetime import datetime
from urllib2 import unquote
import mimetypes
2009-08-16 12:23:29 +00:00
2010-02-03 12:05:38 +00:00
from django import forms
from django.core.paginator import Paginator
from django.contrib.auth.decorators import login_required
2009-08-01 14:14:54 +00:00
from django.contrib.auth.models import User
2010-07-01 08:49:08 +00:00
from django.db.models import Q, Avg, Count, Sum
from django.http import HttpResponse, Http404
2010-09-07 14:05:38 +00:00
from django.shortcuts import render_to_response, get_object_or_404, get_list_or_404, redirect
2009-08-01 14:14:54 +00:00
from django.template import RequestContext
2010-02-08 10:26:25 +00:00
from django.conf import settings
2010-02-03 12:05:38 +00:00
2009-12-31 15:04:32 +00:00
try:
import simplejson as json
except ImportError:
from django.utils import simplejson as json
2010-02-03 12:05:38 +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
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
import utils
2010-06-25 11:53:57 +00:00
import tasks
2010-01-25 09:20:21 +00:00
2010-10-16 11:58:57 +00:00
from user.models import getUserJSON
from archive.models import File
2010-09-07 14:05:38 +00:00
from archive import extract
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 = []
if len(sort) == 1:
if sort[0]['key'] in ('title', 'director'):
sort.append({'operator': '-', 'key': 'year'})
if sort[0]['key'] in ('year', ):
sort.append({'operator': '+', 'key': 'director'})
2010-02-22 09:25:29 +00:00
for e in sort:
operator = e['operator']
if operator != '-': operator = ''
2010-09-23 16:01:48 +00:00
key = {'id': 'itemId'}.get(e['key'], e['key'])
#FIXME: this should be a property of models.ItemSort!!!
if operator=='-' and key in ('title', 'director', 'writer', 'producer', 'editor', 'cinematographer', 'language', 'country', 'year'):
key = '%s_desc' % key
order = '%s%s%s' % (operator, prefix, key)
2010-02-22 09:25:29 +00:00
order_by.append(order)
if order_by:
qs = qs.order_by(*order_by)
return qs
def _parse_query(data, user):
query = {}
query['range'] = [0, 100]
2010-07-01 08:49:08 +00:00
query['sort'] = [{'key':'title', 'operator':'+'}]
for key in ('sort', 'keys', 'group', 'list', 'range', 'ids'):
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)
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-07-01 08:49:08 +00:00
def _get_positions(ids, get_ids):
positions = {}
for i in get_ids:
try:
positions[i] = ids.index(i)
except:
pass
return positions
2010-01-25 09:20:21 +00:00
def api_find(request):
'''
param data
2010-06-27 12:41:38 +00:00
{'query': query, 'sort': array, 'range': array}
2010-01-25 09:20:21 +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-02-22 09:25:29 +00:00
with keys, items is list of dicts with requested properties:
return {'status': {'code': int, 'text': string},
'data': {items: array}}
Groups
param data
{'query': query, 'key': string, 'group': string, 'range': array}
query: query object, more on query syntax at
2010-06-30 14:21:04 +00:00
https://wiki.0x2620.org/wiki/pandora/QuerySyntax
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}}
without keys: return number of items in given query
return {'status': {'code': int, 'text': string},
'data': {items: int}}
Positions
param data
{'query': query, 'ids': []}
query: query object, more on query syntax at
2010-06-30 14:21:04 +00:00
https://wiki.0x2620.org/wiki/pandora/QuerySyntax
ids: ids of items for which positions are required
2010-01-25 09:20:21 +00:00
'''
2010-02-22 09:25:29 +00:00
data = json.loads(request.POST['data'])
if settings.JSON_DEBUG:
print json.dumps(data, indent=2)
2010-02-22 09:25:29 +00:00
query = _parse_query(data, request.user)
2010-02-10 13:10:28 +00:00
response = json_response({})
2010-07-01 08:49:08 +00:00
if 'group' in query:
if 'sort' in query:
if len(query['sort']) == 1 and query['sort'][0]['key'] == 'items':
if query['group'] == "year":
query['sort'].append({'key': 'name', 'operator':'-'})
else:
query['sort'].append({'key': 'name', 'operator':'+'})
else:
query['sort'] = [{'key': 'name', 'operator':'+'}]
response['data']['items'] = []
2010-09-23 16:01:48 +00:00
items = 'items'
item_qs = query['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()
2010-07-13 22:20:14 +00:00
name = 'value'
name_sort = 'value_sort'
2010-07-01 08:49:08 +00:00
#replace normalized items/name sort with actual db value
for i in range(0, len(query['sort'])):
if query['sort'][i]['key'] == 'name':
query['sort'][i]['key'] = name_sort
elif query['sort'][i]['key'] == 'items':
2010-07-01 08:49:08 +00:00
query['sort'][i]['key'] = items
qs = _order_query(qs, query['sort'], prefix='')
if 'ids' in query:
#FIXME: this does not scale for larger results
response['data']['positions'] = {}
ids = [j[name] for j in qs]
response['data']['positions'] = _get_positions(ids, query['ids'])
2010-07-01 08:49:08 +00:00
elif 'range' in data:
qs = qs[query['range'][0]:query['range'][1]]
2010-07-13 22:22:36 +00:00
response['data']['items'] = [{'name': i[name], 'items': i[items]} for i in qs]
else:
response['data']['items'] = qs.count()
elif 'ids' in query:
2010-07-01 08:49:08 +00:00
#FIXME: this does not scale for larger results
qs = _order_query(query['qs'], query['sort'])
response['data']['positions'] = {}
2010-09-23 16:01:48 +00:00
ids = [j['itemId'] for j in qs.values('itemId')]
2010-07-01 08:49:08 +00:00
response['data']['positions'] = _get_positions(ids, query['ids'])
elif 'keys' in query:
response['data']['items'] = []
qs = _order_query(query['qs'], query['sort'])
_p = query['keys']
def only_p(m):
r = {}
if m:
m = json.loads(m)
for p in _p:
r[p] = m.get(p, '')
return r
qs = qs[query['range'][0]:query['range'][1]]
response['data']['items'] = [only_p(m['json']) for m in qs.values('json')]
else: # otherwise stats
2010-09-23 16:01:48 +00:00
items = query['qs']
files = File.objects.all().filter(item__in=items)
2010-07-01 08:49:08 +00:00
r = files.aggregate(
Sum('duration'),
Sum('pixels'),
Sum('size')
2010-07-01 08:49:08 +00:00
)
response['data']['duration'] = r['duration__sum']
response['data']['files'] = files.count()
2010-09-23 16:01:48 +00:00
response['data']['items'] = items.count()
response['data']['pixels'] = r['pixels__sum']
2010-09-23 16:01:48 +00:00
response['data']['runtime'] = items.aggregate(Sum('sort__runtime'))['sort__runtime__sum']
2010-07-06 18:28:42 +00:00
if response['data']['runtime'] == None:
response['data']['runtime'] = 1337
response['data']['size'] = r['size__sum']
2009-08-01 14:14:54 +00:00
return render_to_json_response(response)
2009-06-08 16:08:59 +00:00
2010-01-25 09:20:21 +00:00
def api_getItem(request):
'''
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)
2010-09-18 13:03:27 +00:00
#FIXME: check permissions
2010-09-18 14:44:35 +00:00
info = item.get_json()
info['stream'] = item.get_stream()
info['layers'] = item.get_layers()
response['data'] = {'item': info}
2010-01-25 09:20:21 +00:00
return render_to_json_response(response)
@login_required_json
2010-01-25 09:20:21 +00:00
def api_editItem(request):
2010-01-26 02:24:05 +00:00
'''
param data
{id: string, key: value,..}
return {'status': {'code': int, 'text': string},
'data': {}}
2010-01-26 02:24:05 +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'])
if item.editable(request.user):
2010-02-10 13:10:28 +00:00
response = json_response(status=501, text='not implemented')
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)
2010-01-26 02:24:05 +00:00
@login_required_json
2010-01-25 09:20:21 +00:00
def api_removeItem(request):
2010-01-26 02:24:05 +00:00
'''
param data
string id
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({})
itemId = json.loads(request.POST['data'])
2010-09-23 16:01:48 +00:00
item = get_object_or_404_json(models.Item, itemId=itemId)
2010-09-18 13:03:27 +00:00
if item.editable(request.user):
2010-02-10 13:10:28 +00:00
response = json_response(status=501, text='not implemented')
2010-09-18 13:03:27 +00:00
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)
2010-01-26 02:24:05 +00:00
@login_required_json
2010-01-25 09:20:21 +00:00
def api_addLayer(request):
2010-01-26 02:24:05 +00:00
'''
param data
{key: value}
return {'status': {'code': int, 'text': string},
'data': {}}
2010-01-26 02:24:05 +00:00
'''
response = {'status': {'code': 501, 'text': 'not implemented'}}
2010-01-25 09:20:21 +00:00
return render_to_json_response(response)
2010-01-26 02:24:05 +00:00
@login_required_json
2010-01-25 09:20:21 +00:00
def api_removeLayer(request):
2010-01-26 02:24:05 +00:00
'''
param data
{key: value}
return {'status': {'code': int, 'text': string},
'data': {}}
2010-01-26 02:24:05 +00:00
'''
response = {'status': {'code': 501, 'text': 'not implemented'}}
2010-01-25 09:20:21 +00:00
return render_to_json_response(response)
2010-01-26 02:24:05 +00:00
@login_required_json
2010-01-25 09:20:21 +00:00
def api_editLayer(request):
2010-01-26 02:24:05 +00:00
'''
param data
{key: value}
return {'status': {'code': int, 'text': string},
'data': {}}
2010-01-26 02:24:05 +00:00
'''
2010-02-10 13:10:28 +00:00
response = json_response({})
data = json.loads(request.POST['data'])
layer = get_object_or_404_json(models.Layer, pk=data['id'])
2010-09-18 13:03:27 +00:00
if layer.editable(request.user):
response = json_response(status=501, text='not implemented')
else:
response = json_response(status=403, text='permission denied')
return render_to_json_response(response)
2010-02-10 13:10:28 +00:00
response = json_response(status=501, text='not implemented')
2010-01-25 09:20:21 +00:00
return render_to_json_response(response)
2010-01-26 02:24:05 +00:00
2010-09-13 13:46:49 +00:00
'''
List API
'''
@login_required_json
2010-01-25 09:20:21 +00:00
def api_addListItem(request):
2010-01-26 02:24:05 +00:00
'''
param data
{key: value}
return {'status': {'code': int, 'text': string},
'data': {}}
2010-01-26 02:24:05 +00:00
'''
2010-02-10 13:10:28 +00:00
response = json_response(status=501, text='not implemented')
2010-01-25 09:20:21 +00:00
return render_to_json_response(response)
2010-01-26 02:24:05 +00:00
@login_required_json
2010-01-25 09:20:21 +00:00
def api_removeListItem(request):
2010-01-26 02:24:05 +00:00
'''
param data
{key: value}
return {'status': {'code': int, 'text': string},
'data': {}}
2010-01-26 02:24:05 +00:00
'''
2010-02-10 13:10:28 +00:00
response = json_response(status=501, text='not implemented')
2010-01-25 09:20:21 +00:00
return render_to_json_response(response)
2010-01-26 02:24:05 +00:00
@login_required_json
2010-01-25 09:20:21 +00:00
def api_addList(request):
2010-01-26 02:24:05 +00:00
'''
param data
{key: value}
return {'status': {'code': int, 'text': string},
'data': {}}
2010-01-26 02:24:05 +00:00
'''
2010-02-10 13:10:28 +00:00
response = json_response(status=501, text='not implemented')
2010-01-25 09:20:21 +00:00
return render_to_json_response(response)
2010-01-26 02:24:05 +00:00
@login_required_json
2010-01-25 09:20:21 +00:00
def api_editList(request):
2010-01-26 02:24:05 +00:00
'''
param data
{key: value}
return {'status': {'code': int, 'text': string},
'data': {}}
2010-01-26 02:24:05 +00:00
'''
2010-02-10 13:10:28 +00:00
response = json_response(status=501, text='not implemented')
2010-01-25 09:20:21 +00:00
return render_to_json_response(response)
2010-01-26 02:24:05 +00:00
2010-01-25 09:20:21 +00:00
def api_removeList(request):
2010-01-26 02:24:05 +00:00
'''
param data
{key: value}
return {'status': {'code': int, 'text': string},
'data': {}}
2010-01-26 02:24:05 +00:00
'''
2010-02-10 13:10:28 +00:00
response = json_response(status=501, text='not implemented')
2010-01-25 09:20:21 +00:00
return render_to_json_response(response)
2010-09-13 13:46:49 +00:00
'''
Poster API
'''
2010-01-25 09:20:21 +00:00
def api_parse(request): #parse path and return info
'''
param data
{path: string}
return {'status': {'code': int, 'text': string},
data: {imdb: string}}
'''
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)
def api_setPosterFrame(request): #parse path and return info
'''
param data
2010-09-23 16:01:48 +00:00
{id: itemId, position: float}
2010-08-07 14:31:20 +00:00
return {'status': {'code': int, 'text': string},
data: {}}
'''
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()
response = json_response(status=200, text='ok')
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)
def api_setPoster(request): #parse path and return info
'''
param data
2010-09-23 16:01:48 +00:00
{id: itemId, url: string}
2010-08-07 14:31:20 +00:00
return {'status': {'code': int, 'text': string},
2010-09-10 15:12:22 +00:00
data: {poster: {url,width,height}}}
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):
2010-09-10 15:12:22 +00:00
valid_urls = [p.url for p in item.poster_urls.all()]
if data['url'] in valid_urls:
item.poster_url = data['url']
if item.poster:
item.poster.delete()
item.save()
2010-09-23 16:01:48 +00:00
tasks.updatePoster.delay(item.itemId)
2010-09-10 15:12:22 +00:00
response = json_response(status=200, text='ok')
response['data']['poster'] = item.get_poster()
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)
def api_getImdbId(request):
'''
param data
{title: string, director: string, year: string}
return {'status': {'code': int, 'text': string},
'data': {imdbId:string }}
'''
2010-07-07 22:46:41 +00:00
imdbId = ox.web.imdb.guess(search_title, r['director'], timeout=-1)
if imdbId:
2010-02-10 13:10:28 +00:00
response = json_response({'imdbId': imdbId})
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-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
'''
def frame(request, id, position, size):
2010-09-23 16:01:48 +00:00
item = get_object_or_404(models.Item, itemId=id)
position = float(position.replace(',', '.'))
2010-09-23 16:01:48 +00:00
frame = item.frame(position, int(size))
if not frame:
raise Http404
return HttpFileResponse(frame, content_type='image/jpeg')
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 size == 'large':
size = None
2010-09-23 16:01:48 +00:00
if item.poster:
if size:
size = int(size)
2010-09-23 16:01:48 +00:00
poster_path = item.poster.path.replace('.jpg', '.%d.jpg'%size)
if not os.path.exists(poster_path):
2010-09-23 16:01:48 +00:00
poster_size = max(item.poster.width, item.poster.height)
if size > poster_size:
2010-09-23 16:01:48 +00:00
return redirect('/%s/poster.jpg' % item.itemId)
extract.resize_image(item.poster.path, poster_path, size=size)
else:
2010-09-23 16:01:48 +00:00
poster_path = item.poster.path
else:
2010-09-10 10:09:21 +00:00
if not size: size='large'
2010-09-23 16:01:48 +00:00
return redirect('http://0xdb.org/%s/poster.%s.jpg' % (item.itemId, size))
poster_path = os.path.join(settings.STATIC_ROOT, 'png/posterDark.48.png')
return HttpFileResponse(poster_path, content_type='image/jpeg')
2010-09-08 13:38:44 +00:00
def timeline(request, id, timeline, size, position):
2010-09-23 16:01:48 +00:00
item = get_object_or_404(models.Item, itemId=id)
2010-09-08 13:38:44 +00:00
if timeline == 'strip':
2010-09-23 16:01:48 +00:00
timeline = '%s.%s.%04d.png' %(item.timeline_prefix[:-8] + 'strip', size, int(position))
2010-09-08 13:38:44 +00:00
else:
2010-09-23 16:01:48 +00:00
timeline = '%s.%s.%04d.png' %(item.timeline_prefix, size, int(position))
return HttpFileResponse(timeline, content_type='image/png')
def video(request, id, profile):
2010-09-23 16:01:48 +00:00
item = get_object_or_404(models.Item, itemId=id)
stream = get_object_or_404(item.streams, profile=profile)
path = stream.video.path
content_type = path.endswith('.mp4') and 'video/mp4' or 'video/webm'
return HttpFileResponse(path, content_type=content_type)