pandora/pandora/archive/views.py

642 lines
22 KiB
Python
Raw Normal View History

# -*- coding: utf-8 -*-
# vi:si:et:sw=4:sts=4:ts=4
from __future__ import division
import os.path
from datetime import datetime
from django import forms
2011-01-01 11:44:42 +00:00
from django.shortcuts import get_object_or_404, redirect
from django.conf import settings
from django.db.models import Count
import ox
2010-12-25 10:14:52 +00:00
from ox.utils import 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.views import task_status
2011-01-29 11:19:11 +00:00
from item import utils
from item.models import get_item, Item
2011-02-22 15:26:06 +00:00
from item.views import parse_query
2010-11-08 16:34:25 +00:00
import item.tasks
from ox.django.api import actions
import models
import tasks
2010-08-10 22:01:41 +00:00
@login_required_json
def removeVolume(request):
2010-08-10 22:01:41 +00:00
data = json.loads(request.POST['data'])
user = request.user
try:
volume = models.Volume.objects.get(user=user, name=data['volume'])
volume.files.delete()
volume.delete()
response = json_response()
2010-08-10 22:01:41 +00:00
except models.Volume.DoesNotExist:
response = json_response(status=404, text='volume not found')
return render_to_json_response(response)
2011-01-13 08:33:14 +00:00
actions.register(removeVolume, cache=False)
2010-08-10 22:01:41 +00:00
2011-01-01 11:44:42 +00:00
2010-08-10 22:01:41 +00:00
@login_required_json
def update(request):
'''
2013-02-08 17:01:26 +00:00
2 steps:
send files
{volume: 'Videos', files: [{oshash:, path:, mtime:, ,,}]}
send info about changed/new files
{volume: 'Videos', info: {oshash: {...}]}
2010-08-10 22:01:41 +00:00
call volume/files first and fill in requested info after that
2013-03-04 19:35:06 +00:00
takes {
2010-08-10 22:01:41 +00:00
volume: '',
2010-08-07 14:31:20 +00:00
files: [
2010-12-24 10:31:44 +00:00
{oshash:, path:, mtime:, },
...
],
2010-08-07 14:31:20 +00:00
info: {oshash: object}
2010-12-24 10:31:44 +00:00
}
2013-02-08 17:01:26 +00:00
2013-03-04 19:35:06 +00:00
returns {
info: list, // list of files that need info
data: list, // list of flies that should be encoded to highest profile and uploaded
file: list // list of files that should be uploaded as is
2010-12-24 10:31:44 +00:00
}
'''
data = json.loads(request.POST['data'])
2010-08-07 14:31:20 +00:00
user = request.user
upload_only = data.get('upload', False)
2010-08-07 14:31:20 +00:00
2010-08-10 22:01:41 +00:00
response = json_response({'info': [], 'data': [], 'file': []})
2010-09-17 21:06:01 +00:00
volume = None
2010-08-07 14:31:20 +00:00
if 'files' in data:
t = tasks.update_files.delay(user.username, data['volume'], data['files'])
response['data']['taskId'] = t.task_id
2010-08-07 14:31:20 +00:00
user_profile = user.get_profile()
user_profile.files_updated = datetime.now()
user_profile.save()
if 'info' in data:
2013-02-08 23:38:13 +00:00
files = models.File.objects.filter(oshash__in=data['info'].keys())
for f in files:
2013-02-08 17:01:26 +00:00
if not f.info:
f.update_info(data['info'][f.oshash], user)
f.save()
2013-02-08 23:38:13 +00:00
for i in Item.objects.filter(files__in=files).distinct():
i.update_selected()
i.update_wanted()
if not upload_only:
all_files = models.Instance.objects.filter(volume__user=user)
2012-11-09 11:10:25 +00:00
files = all_files.filter(file__available=False)
if volume:
files = files.filter(volume=volume)
response['data']['info'] = [f.file.oshash for f in all_files.filter(file__info='{}')]
response['data']['data'] = [f.file.oshash for f in files.filter(file__is_video=True,
2011-08-23 17:39:34 +00:00
file__available=False,
file__wanted=True)]
response['data']['data'] += [f.file.oshash for f in files.filter(file__is_audio=True,
2011-08-23 17:39:34 +00:00
file__available=False,
file__wanted=True)]
2010-08-07 14:31:20 +00:00
if filter(lambda l: l['id'] == 'subtitles', settings.CONFIG['layers']):
response['data']['file'] = [f.file.oshash
for f in files.filter(file__is_subtitle=True,
file__available=False,
path__endswith='.srt')]
else:
response['data']['file'] = []
return render_to_json_response(response)
2011-01-13 08:33:14 +00:00
actions.register(update, cache=False)
2010-08-07 14:31:20 +00:00
2011-01-01 11:44:42 +00:00
2010-08-10 22:01:41 +00:00
@login_required_json
def upload(request):
'''
2013-03-04 19:35:06 +00:00
takes {
id: string
frame: [] //multipart frames
file: [] //multipart file
}
returns {
info: object,
rename: object
2010-12-24 10:31:44 +00:00
}
'''
2011-02-23 17:32:30 +00:00
response = json_response({})
2011-06-27 13:39:35 +00:00
f = get_object_or_404_json(models.File, oshash=request.POST['id'])
2010-08-24 17:16:33 +00:00
if 'frame' in request.FILES:
if f.frames.count() == 0:
for frame in request.FILES.getlist('frame'):
name = frame.name
#float required?
position = float(os.path.splitext(name)[0])
2011-02-22 16:52:03 +00:00
fr, created = models.Frame.objects.get_or_create(file=f, position=position)
2011-12-29 19:13:03 +00:00
if fr.frame:
fr.frame.delete()
2010-08-24 17:16:33 +00:00
fr.frame.save(name, frame)
2011-11-29 17:50:58 +00:00
os.chmod(fr.frame.path, 0644)
f.item.select_frame()
2011-11-29 14:14:03 +00:00
f.item.save()
item.tasks.update_poster.delay(f.item.itemId)
2010-08-24 17:16:33 +00:00
else:
response = json_response(status=403, text='permissino denied')
if 'file' in request.FILES:
2010-09-17 21:06:01 +00:00
if not f.available:
2011-08-26 14:32:00 +00:00
if f.data:
f.data.delete()
2010-09-03 13:28:44 +00:00
f.data.save('data.raw', request.FILES['file'])
2010-09-17 21:06:01 +00:00
f.save()
2011-11-29 17:50:58 +00:00
os.chmod(f.data.path, 0644)
2011-08-26 15:40:37 +00:00
item.tasks.load_subtitles.delay(f.item.itemId)
response = json_response(text='file saved')
2010-08-24 17:16:33 +00:00
else:
response = json_response(status=403, text='permissino denied')
2010-08-07 14:31:20 +00:00
return render_to_json_response(response)
2011-01-13 08:33:14 +00:00
actions.register(upload, cache=False)
2010-08-07 14:31:20 +00:00
2011-01-01 11:44:42 +00:00
2010-08-07 14:31:20 +00:00
class VideoChunkForm(forms.Form):
chunk = forms.FileField()
chunkId = forms.IntegerField(required=False)
done = forms.IntegerField(required=False)
2012-03-06 22:05:21 +00:00
@login_required_json
def addMedia(request):
2012-03-06 22:05:21 +00:00
'''
2013-03-04 19:35:06 +00:00
takes {
id: oshash
filename: string,
item: string
2013-03-04 19:35:06 +00:00
info: {}
}
returns {
item: id,
2012-03-06 22:05:21 +00:00
}
'''
response = json_response({})
data = json.loads(request.POST['data'])
oshash = data.pop('id')
2013-02-25 10:30:29 +00:00
if not request.user.get_profile().capability('canAddItems'):
2012-03-06 22:05:21 +00:00
response = json_response(status=403, text='permissino denied')
elif models.File.objects.filter(oshash=oshash).count() > 0:
f = models.File.objects.get(oshash=oshash)
2012-03-16 16:18:26 +00:00
if f.available:
response['status']['text'] = 'file exists'
2012-03-06 22:05:21 +00:00
response['data']['item'] = f.item.itemId
2012-03-16 16:18:26 +00:00
response['data']['itemUrl'] = request.build_absolute_uri('/%s' % f.item.itemId)
2012-03-06 22:05:21 +00:00
else:
2013-03-01 09:14:57 +00:00
if 'item' in data:
i = Item.objects.get(itemId=data['item'])
else:
title = ox.parse_movie_path(os.path.splitext(data['filename'])[0])['title']
i = Item()
i.data = {
'title': title,
'director': data.get('director', []),
}
i.user = request.user
i.save()
2012-03-06 22:05:21 +00:00
f = models.File(oshash=oshash, item=i)
2012-03-16 16:18:26 +00:00
f.path = data.get('filename', 'Untitled')
2012-11-16 01:50:10 +00:00
extension = f.path.split('.')
if len(extension) > 1:
extension = extension[-1]
else:
extension = 'webm'
2012-03-16 16:18:26 +00:00
f.selected = True
2012-03-06 22:05:21 +00:00
f.info = data['info']
2013-02-08 17:01:26 +00:00
f.info['extension'] = extension
2013-02-09 01:49:17 +00:00
f.parse_info()
2012-03-06 22:05:21 +00:00
f.save()
response['data']['item'] = i.itemId
2012-03-16 16:18:26 +00:00
response['data']['itemUrl'] = request.build_absolute_uri('/%s' % i.itemId)
2012-03-06 22:05:21 +00:00
return render_to_json_response(response)
actions.register(addMedia, cache=False)
2011-01-01 11:44:42 +00:00
2010-08-07 14:31:20 +00:00
@login_required_json
def firefogg_upload(request):
2010-08-13 17:17:23 +00:00
profile = request.GET['profile']
2011-06-27 13:39:35 +00:00
oshash = request.GET['id']
2011-09-06 12:06:59 +00:00
config = settings.CONFIG['video']
2012-03-06 22:05:21 +00:00
video_profile = "%sp.%s" % (max(config['resolutions']), config['formats'][0])
2011-08-19 15:37:37 +00:00
2010-08-07 14:31:20 +00:00
#handle video upload
if request.method == 'POST':
#post next chunk
2010-08-13 17:17:23 +00:00
if 'chunk' in request.FILES and oshash:
2010-09-03 13:28:44 +00:00
f = get_object_or_404(models.File, oshash=oshash)
2010-08-07 14:31:20 +00:00
form = VideoChunkForm(request.POST, request.FILES)
2011-08-19 15:37:37 +00:00
if form.is_valid() and profile == video_profile and f.editable(request.user):
2010-08-07 14:31:20 +00:00
c = form.cleaned_data['chunk']
chunk_id = form.cleaned_data['chunkId']
response = {
'result': 1,
2012-01-02 03:50:54 +00:00
'resultUrl': request.build_absolute_uri('/%s'%f.item.itemId)
2010-08-07 14:31:20 +00:00
}
if not f.save_chunk(c, chunk_id, form.cleaned_data['done']):
2010-08-07 14:31:20 +00:00
response['result'] = -1
elif form.cleaned_data['done']:
2011-08-23 17:39:34 +00:00
f.uploading = False
2010-09-03 13:28:44 +00:00
f.save()
#FIXME: this fails badly if rabbitmq goes down
try:
2011-08-19 16:54:42 +00:00
t = tasks.process_stream.delay(f.id)
2011-01-01 11:44:42 +00:00
response['resultUrl'] = t.task_id
except:
pass
2010-08-07 14:31:20 +00:00
response['result'] = 1
response['done'] = 1
return render_to_json_response(response)
2010-09-03 13:28:44 +00:00
#init upload
2011-08-19 15:37:37 +00:00
elif oshash and profile == video_profile:
2010-08-13 17:17:23 +00:00
#404 if oshash is not know, files must be registered via update api first
f = get_object_or_404(models.File, oshash=oshash)
2010-09-03 13:28:44 +00:00
if f.editable(request.user):
2011-08-18 18:19:09 +00:00
f.streams.all().delete()
2012-01-07 10:48:05 +00:00
f.delete_frames()
2011-08-23 17:39:34 +00:00
f.uploading = True
2010-09-03 13:28:44 +00:00
f.save()
if f.item.rendered and f.selected:
Item.objects.filter(id=f.item.id).update(rendered=False)
2010-09-03 13:28:44 +00:00
response = {
2011-06-27 13:39:35 +00:00
'uploadUrl': request.build_absolute_uri('/api/upload/?id=%s&profile=%s' % (f.oshash, profile)),
2012-01-02 03:50:54 +00:00
'url': request.build_absolute_uri('/%s' % f.item.itemId),
2010-09-03 13:28:44 +00:00
'result': 1
}
return render_to_json_response(response)
else:
response = json_response(status=404, text='permission denied')
2010-08-07 14:31:20 +00:00
response = json_response(status=400, text='this request requires POST')
return render_to_json_response(response)
2011-01-01 11:44:42 +00:00
@login_required_json
def taskStatus(request):
#FIXME: should check if user has permissions to get status
data = json.loads(request.POST['data'])
if 'taskId' in data:
task_id = data['taskId']
else:
task_id = data['task_id']
response = task_status(request, task_id)
return render_to_json_response(response)
2011-01-13 08:33:14 +00:00
actions.register(taskStatus, cache=False)
2011-06-27 13:39:35 +00:00
2011-06-15 13:05:06 +00:00
@login_required_json
def moveMedia(request):
2011-06-15 13:05:06 +00:00
'''
change file / item link
2013-03-04 19:35:06 +00:00
takes {
2011-07-03 18:11:35 +00:00
ids: ids of files
2011-06-15 13:05:06 +00:00
itemId: new itemId
}
2013-03-04 19:35:06 +00:00
returns {
2011-06-15 13:05:06 +00:00
}
'''
data = json.loads(request.POST['data'])
if Item.objects.filter(itemId=data['itemId']).count() == 1:
i = Item.objects.get(itemId=data['itemId'])
2011-07-03 18:30:47 +00:00
else:
data['itemId'] = data['itemId'].strip()
2011-07-03 18:30:47 +00:00
if len(data['itemId']) != 7:
del data['itemId']
2011-08-17 18:37:24 +00:00
if 'director' in data and isinstance(data['director'], basestring):
if data['director'] == '':
data['director'] = []
else:
data['director'] = data['director'].split(', ')
2011-10-26 14:04:50 +00:00
i = get_item(data, user=request.user)
2011-07-03 18:30:47 +00:00
else:
2011-10-26 14:04:50 +00:00
i = get_item({'imdbId': data['itemId']}, user=request.user)
2011-08-16 17:03:14 +00:00
changed = [i.itemId]
2011-06-15 13:05:06 +00:00
for f in models.File.objects.filter(oshash__in=data['ids']):
2011-07-05 14:28:22 +00:00
if f.item.id != i.itemId and f.editable(request.user):
2011-07-03 18:30:47 +00:00
if f.item.itemId not in changed:
changed.append(f.item.itemId)
2011-07-05 14:28:22 +00:00
f.item = i
2011-06-15 13:05:06 +00:00
f.save()
2011-07-03 18:30:47 +00:00
for itemId in changed:
c = Item.objects.get(itemId=itemId)
2013-03-01 09:14:57 +00:00
if c.files.count() == 0 and settings.CONFIG['itemRequiresVideo']:
2011-08-16 17:03:14 +00:00
c.delete()
else:
c.rendered = False
c.save()
2011-08-19 16:54:42 +00:00
item.tasks.update_timeline.delay(itemId)
2011-06-15 13:05:06 +00:00
response = json_response(text='updated')
2011-07-05 14:28:22 +00:00
response['data']['itemId'] = i.itemId
2011-06-15 13:05:06 +00:00
return render_to_json_response(response)
actions.register(moveMedia, cache=False)
2011-01-01 11:44:42 +00:00
2011-10-23 16:19:47 +00:00
@login_required_json
def editMedia(request):
2011-10-23 16:19:47 +00:00
'''
change file / item link
2013-03-04 19:35:06 +00:00
takes {
2013-02-08 17:05:18 +00:00
files: [
{id:, key1: value1, key2: value2}
...
]
2011-10-23 16:19:47 +00:00
}
2013-02-08 17:05:18 +00:00
possible keys: part, partTitle, language, ignore, extension, version, episodes
2011-10-23 16:19:47 +00:00
2013-03-04 19:35:06 +00:00
returns {
2011-10-23 16:19:47 +00:00
}
'''
data = json.loads(request.POST['data'])
2013-02-08 17:05:18 +00:00
ignore = []
dont_ignore = []
response = json_response(status=200, text='updated')
response['data']['files'] = []
for info in data['files']:
f = get_object_or_404_json(models.File, oshash=info['id'])
if f.editable(request.user):
if 'ignore' in info:
if info['ignore']:
ignore.append(info['id'])
else:
dont_ignore.append(info['id'])
update = False
2013-02-08 17:51:12 +00:00
for key in f.PATH_INFO:
2013-02-08 17:05:18 +00:00
if key in info:
2013-02-08 17:51:12 +00:00
f.info[key] = info[key]
2013-02-08 17:05:18 +00:00
update = True
if update:
f.save()
response['data']['files'].append(f.json())
else:
response['data']['files'].append({'id': info['id'], 'error': 'permission denied'})
if ignore:
models.Instance.objects.filter(file__oshash__in=ignore).update(ignore=True)
if dont_ignore:
models.Instance.objects.filter(file__oshash__in=dont_ignore).update(ignore=False)
if ignore or dont_ignore:
files = models.File.objects.filter(oshash__in=ignore+dont_ignore)
#FIXME: is this to slow to run sync?
for i in Item.objects.filter(files__in=files).distinct():
i.update_selected()
i.update_wanted()
2011-10-23 16:19:47 +00:00
return render_to_json_response(response)
actions.register(editMedia, cache=False)
2011-06-27 13:39:35 +00:00
2010-08-07 14:31:20 +00:00
2012-01-15 15:05:37 +00:00
@login_required_json
def removeMedia(request):
2012-01-15 15:05:37 +00:00
data = json.loads(request.POST['data'])
response = json_response()
if request.user.get_profile().get_level() == 'admin':
2012-02-11 12:43:02 +00:00
qs = models.File.objects.filter(oshash__in=data['ids'], instances__id=None)
for f in qs:
if f.item.sort.numberoffiles:
f.item.sort.numberoffiles -= 1
f.item.sort.save()
2012-02-11 12:43:02 +00:00
qs.delete()
2012-01-15 15:05:37 +00:00
else:
response = json_response(status=403, text='permissino denied')
return render_to_json_response(response)
actions.register(removeMedia, cache=False)
2012-01-15 15:05:37 +00:00
def getPath(request):
'''
change file / item link
2013-03-04 19:35:06 +00:00
takes {
2012-09-11 12:42:33 +00:00
id: [hash of file]
}
2013-03-04 19:35:06 +00:00
returns {
id: path
}
'''
data = json.loads(request.POST['data'])
2012-09-11 13:39:49 +00:00
response = json_response()
2012-09-11 12:42:33 +00:00
ids = data['id']
if isinstance(ids, basestring):
ids = [ids]
2012-09-12 16:16:54 +00:00
for f in models.File.objects.filter(oshash__in=ids).values('path', 'oshash').order_by('sort_path'):
2012-09-11 12:42:33 +00:00
response['data'][f['oshash']] = f['path']
return render_to_json_response(response)
actions.register(getPath, cache=True)
2011-01-01 11:44:42 +00:00
2010-09-14 14:10:37 +00:00
def lookup_file(request, oshash):
oshash = oshash.replace('/', '')
2010-09-14 14:10:37 +00:00
f = get_object_or_404(models.File, oshash=oshash)
2012-06-28 13:55:38 +00:00
return redirect('%s/files' % f.item.get_absolute_url())
2010-09-14 14:10:37 +00:00
2011-01-19 12:06:03 +00:00
def _order_query(qs, sort, prefix=''):
order_by = []
if len(sort) == 1:
sort.append({'operator': '+', 'key': 'path'})
2011-01-19 12:06:03 +00:00
sort.append({'operator': '-', 'key': 'created'})
2011-01-19 12:06:03 +00:00
for e in sort:
operator = e['operator']
if operator != '-':
operator = ''
2011-02-01 13:19:34 +00:00
key = {
'id': 'item__itemId',
'users': 'instances__volume__user__username',
'resolution': 'width',
'path': 'sort_path'
2011-02-01 13:19:34 +00:00
}.get(e['key'], e['key'])
2011-01-19 12:06:03 +00:00
#if operator=='-' and '%s_desc'%key in models.ItemSort.descending_fields:
# key = '%s_desc' % key
order = '%s%s%s' % (operator, prefix, key)
order_by.append(order)
if key == 'part':
order = '%s%s%s' % (operator, prefix, 'sort_path')
order_by.append(order)
2011-01-19 12:06:03 +00:00
if order_by:
qs = qs.order_by(*order_by)
return qs
def findMedia(request):
2011-01-01 11:44:42 +00:00
'''
2013-03-04 19:35:06 +00:00
takes {
2011-01-19 12:06:03 +00:00
'query': query,
'sort': array,
'range': array
}
query: query object, more on query syntax at
https://wiki.0x2620.org/wiki/pandora/QuerySyntax
sort: array of key, operator dics
[
{
key: "year",
operator: "-"
},
{
key: "director",
operator: ""
}
]
range: result range, array [from, to]
keys: array of keys to return
group: group elements by, country, genre, director...
with keys, items is list of dicts with requested properties:
2013-03-04 19:35:06 +00:00
returns {
items: [object]
}
2011-01-19 12:06:03 +00:00
Groups
2013-03-04 19:35:06 +00:00
takes {
2011-01-19 12:06:03 +00:00
'query': query,
'key': string,
'group': string,
'range': array
}
query: query object, more on query syntax at
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: path, items
2011-01-19 12:06:03 +00:00
with keys
items contains list of {'path': string, 'items': int}:
2013-03-04 19:35:06 +00:00
returns {
items: [object]
}
2011-01-19 12:06:03 +00:00
without keys: return number of items in given query
2013-03-04 19:35:06 +00:00
returns {
items: int
}
2011-01-19 12:06:03 +00:00
Positions
2013-03-04 19:35:06 +00:00
takes {
2011-01-19 12:06:03 +00:00
'query': query,
2013-03-04 19:35:06 +00:00
'positions': [string]
2011-01-19 12:06:03 +00:00
}
query: query object, more on query syntax at
https://wiki.0x2620.org/wiki/pandora/QuerySyntax
2011-06-01 11:03:43 +00:00
positions: ids of items for which positions are required
2013-03-04 19:35:06 +00:00
returns {
positions: {
id: position
2011-01-19 12:06:03 +00:00
}
}
2011-01-01 11:44:42 +00:00
'''
2011-01-19 12:06:03 +00:00
data = json.loads(request.POST['data'])
if not data.get('sort'):
data['sort'] = [{'key': 'path', 'operator': '+'}]
2011-02-22 15:26:06 +00:00
query = parse_query(data, request.user)
2011-01-19 12:06:03 +00:00
response = json_response({})
if 'group' in 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":
2011-10-29 23:32:11 +00:00
order_by = (order_by, 'sortvalue')
2011-01-19 12:06:03 +00:00
else:
order_by = (order_by,)
else:
2011-10-29 23:32:11 +00:00
order_by = query['sort'][0]['operator'] == '-' and '-sortvalue' or 'sortvalue'
2011-01-19 12:06:03 +00:00
order_by = (order_by, 'items')
else:
2011-10-29 23:32:11 +00:00
order_by = ('-sortvalue', 'items')
2011-01-19 12:06:03 +00:00
response['data']['items'] = []
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(*order_by)
2011-06-01 11:03:43 +00:00
if 'positions' in query:
2011-01-19 12:06:03 +00:00
#FIXME: this does not scale for larger results
response['data']['positions'] = {}
ids = [j['value'] for j in qs]
2011-06-01 11:03:43 +00:00
response['data']['positions'] = utils.get_positions(ids, query['positions'])
2011-01-19 12:06:03 +00:00
elif 'range' in data:
qs = qs[query['range'][0]:query['range'][1]]
response['data']['items'] = [{'path': i['value'], 'items': i[items]} for i in qs]
2011-01-19 12:06:03 +00:00
else:
response['data']['items'] = qs.count()
2011-06-01 11:03:43 +00:00
elif 'positions' in query:
2011-01-19 12:06:03 +00:00
#FIXME: this does not scale for larger results
2011-06-20 17:09:53 +00:00
qs = models.File.objects.filter(item__in=query['qs'])
qs = _order_query(qs, query['sort'])
2011-01-19 12:06:03 +00:00
response['data']['positions'] = {}
2011-06-20 17:21:21 +00:00
ids = [j['oshash'] for j in qs.values('oshash')]
2011-06-01 11:03:43 +00:00
response['data']['positions'] = utils.get_positions(ids, query['positions'])
2011-01-19 12:06:03 +00:00
elif 'keys' in query:
response['data']['items'] = []
qs = models.File.objects.filter(item__in=query['qs'])
2011-02-01 13:19:34 +00:00
qs = _order_query(qs, query['sort'])
qs = qs.select_related()
2011-01-19 12:06:03 +00:00
keys = query['keys']
qs = qs[query['range'][0]:query['range'][1]]
response['data']['items'] = [f.json(keys) for f in qs]
else: # otherwise stats
items = query['qs']
files = models.File.objects.filter(item__in=query['qs'])
2011-01-29 11:19:11 +00:00
response['data']['items'] = files.count()
2011-01-19 12:06:03 +00:00
return render_to_json_response(response)
actions.register(findMedia)
2011-01-19 12:06:03 +00:00
def parsePath(request): #parse path and return info
'''
2013-03-04 19:35:06 +00:00
takes {
path: string
}
2013-03-04 19:35:06 +00:00
returns {
imdb: string
}
'''
path = json.loads(request.POST['data'])['path']
response = json_response(ox.parse_movie_path(path))
return render_to_json_response(response)
actions.register(parsePath)
2012-01-20 18:15:54 +00:00
def getMediaInfo(request):
2012-01-20 18:15:54 +00:00
'''
2013-03-04 19:35:06 +00:00
takes {
2012-01-20 18:15:54 +00:00
id: oshash of stream file
}
2013-03-04 19:35:06 +00:00
returns {
item: itemId,
file: oshash of source file
2012-01-20 18:15:54 +00:00
}
'''
data = json.loads(request.POST['data'])
f = None
qs = models.Stream.objects.filter(oshash=data['id'])
if qs.count() > 0:
s = qs[0]
f = s.file
else:
qs = models.File.objects.filter(oshash=data['id'])
if qs.count() > 0:
f = qs[0]
response = json_response()
if f:
response['data'] = {
'file': f.oshash,
'item': f.item.itemId
}
return render_to_json_response(response)
actions.register(getMediaInfo)
2012-01-20 18:15:54 +00:00