2010-07-12 14:56:14 +00:00
|
|
|
# -*- 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
|
2010-07-12 14:56:14 +00:00
|
|
|
from django.conf import settings
|
2013-02-21 13:28:36 +00:00
|
|
|
from django.db.models import Count
|
2010-07-12 14:56:14 +00:00
|
|
|
|
2011-10-15 15:21:41 +00:00
|
|
|
import ox
|
2010-12-25 10:14:52 +00:00
|
|
|
from ox.utils import json
|
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
|
2010-12-22 07:45:37 +00:00
|
|
|
from ox.django.views import task_status
|
2010-07-12 14:56:14 +00:00
|
|
|
|
2011-01-29 11:19:11 +00:00
|
|
|
from item import utils
|
2011-09-05 16:34:05 +00:00
|
|
|
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
|
2012-01-03 20:18:47 +00:00
|
|
|
from ox.django.api import actions
|
2010-07-12 14:56:14 +00:00
|
|
|
|
2010-12-23 15:01:53 +00:00
|
|
|
import models
|
|
|
|
import tasks
|
2014-04-11 16:56:22 +00:00
|
|
|
from chunk import process_chunk
|
2010-12-23 15:01:53 +00:00
|
|
|
|
2010-12-24 09:23:34 +00:00
|
|
|
|
2010-08-10 22:01:41 +00:00
|
|
|
@login_required_json
|
2014-10-06 08:26:43 +00:00
|
|
|
def removeVolume(request, data):
|
2010-08-10 22:01:41 +00:00
|
|
|
user = request.user
|
|
|
|
try:
|
|
|
|
volume = models.Volume.objects.get(user=user, name=data['volume'])
|
|
|
|
volume.files.delete()
|
|
|
|
volume.delete()
|
2010-12-24 09:23:34 +00:00
|
|
|
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
|
2014-10-06 08:26:43 +00:00
|
|
|
def update(request, data):
|
2010-07-12 14:56:14 +00:00
|
|
|
'''
|
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
|
|
|
}
|
2010-07-12 14:56:14 +00:00
|
|
|
'''
|
2010-08-07 14:31:20 +00:00
|
|
|
user = request.user
|
2011-04-06 18:57:25 +00:00
|
|
|
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:
|
2010-12-25 13:45:19 +00:00
|
|
|
t = tasks.update_files.delay(user.username, data['volume'], data['files'])
|
2010-12-23 15:01:53 +00:00
|
|
|
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()
|
|
|
|
|
2014-07-09 18:27:21 +00:00
|
|
|
if 'info' in data and data['info']:
|
|
|
|
t = tasks.update_info.delay(user.username, data['info'])
|
|
|
|
response['data']['taskId'] = t.task_id
|
2011-04-06 18:57:25 +00:00
|
|
|
if not upload_only:
|
2012-11-09 11:07:38 +00:00
|
|
|
all_files = models.Instance.objects.filter(volume__user=user)
|
2012-11-09 11:10:25 +00:00
|
|
|
files = all_files.filter(file__available=False)
|
2011-04-06 18:57:25 +00:00
|
|
|
if volume:
|
|
|
|
files = files.filter(volume=volume)
|
2012-11-09 11:07:38 +00:00
|
|
|
response['data']['info'] = [f.file.oshash for f in all_files.filter(file__info='{}')]
|
2011-04-06 18:57:25 +00:00
|
|
|
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)]
|
2011-04-06 18:57:25 +00:00
|
|
|
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
|
|
|
|
2012-02-15 15:49:40 +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'] = []
|
2010-07-12 14:56:14 +00:00
|
|
|
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
|
2014-10-06 08:26:43 +00:00
|
|
|
def upload(request, data=None):
|
2010-07-12 14:56:14 +00:00
|
|
|
'''
|
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
|
|
|
}
|
2010-07-12 14:56:14 +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:
|
2013-07-22 07:30:27 +00:00
|
|
|
if f.editable(request.user):
|
2013-07-22 18:58:03 +00:00
|
|
|
f.frames.all().delete()
|
2010-08-24 17:16:33 +00:00
|
|
|
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)
|
2014-09-28 14:02:53 +00:00
|
|
|
fr.save()
|
2011-11-06 13:12:15 +00:00
|
|
|
f.item.select_frame()
|
2011-11-29 14:14:03 +00:00
|
|
|
f.item.save()
|
2014-09-19 12:26:46 +00:00
|
|
|
item.tasks.update_poster.delay(f.item.public_id)
|
2010-08-24 17:16:33 +00:00
|
|
|
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)
|
2014-09-19 12:26:46 +00:00
|
|
|
item.tasks.load_subtitles.delay(f.item.public_id)
|
2010-12-24 09:23:34 +00:00
|
|
|
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
|
|
|
|
2012-03-06 22:05:21 +00:00
|
|
|
@login_required_json
|
2014-10-06 08:26:43 +00:00
|
|
|
def addMedia(request, data):
|
2012-03-06 22:05:21 +00:00
|
|
|
'''
|
2013-03-04 19:35:06 +00:00
|
|
|
takes {
|
|
|
|
id: oshash
|
2013-03-12 09:36:00 +00:00
|
|
|
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({})
|
|
|
|
oshash = data.pop('id')
|
2013-02-25 10:30:29 +00:00
|
|
|
if not request.user.get_profile().capability('canAddItems'):
|
2014-03-05 20:29:01 +00:00
|
|
|
response = json_response(status=403, text='permission denied')
|
2012-03-06 22:05:21 +00:00
|
|
|
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'
|
2014-09-19 12:26:46 +00:00
|
|
|
response['data']['item'] = f.item.public_id
|
|
|
|
response['data']['itemUrl'] = request.build_absolute_uri('/%s' % f.item.public_id)
|
2012-03-06 22:05:21 +00:00
|
|
|
else:
|
2013-03-01 09:14:57 +00:00
|
|
|
if 'item' in data:
|
2014-09-19 12:26:46 +00:00
|
|
|
i = Item.objects.get(public_id=data['item'])
|
2013-03-01 09:14:57 +00:00
|
|
|
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()
|
2013-07-03 17:18:02 +00:00
|
|
|
i.make_poster(True)
|
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
|
2013-03-26 11:54:03 +00:00
|
|
|
if 'info' in data and data['info']:
|
|
|
|
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()
|
2014-09-19 12:26:46 +00:00
|
|
|
response['data']['item'] = i.public_id
|
|
|
|
response['data']['itemUrl'] = request.build_absolute_uri('/%s' % i.public_id)
|
2012-03-06 22:05:21 +00:00
|
|
|
return render_to_json_response(response)
|
2013-03-25 12:57:20 +00:00
|
|
|
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']
|
2014-02-15 16:13:43 +00:00
|
|
|
|
|
|
|
resolution, format = profile.split('p.')
|
|
|
|
resolution = int(resolution)
|
|
|
|
if resolution not in config['resolutions'] \
|
|
|
|
or format not in config['formats']:
|
|
|
|
response = json_response(status=500, text='invalid profile')
|
|
|
|
return render_to_json_response(response)
|
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)
|
2014-04-11 16:56:22 +00:00
|
|
|
if f.editable(request.user):
|
|
|
|
def save_chunk(chunk, offset, done):
|
|
|
|
return f.save_chunk_stream(chunk, offset, resolution, format, done)
|
|
|
|
response = process_chunk(request, save_chunk)
|
2014-09-19 12:26:46 +00:00
|
|
|
response['resultUrl'] = request.build_absolute_uri('/%s'%f.item.public_id)
|
2014-04-11 16:56:22 +00:00
|
|
|
if response.get('done'):
|
2011-08-23 17:39:34 +00:00
|
|
|
f.uploading = False
|
2014-04-11 12:08:14 +00:00
|
|
|
if response['result'] == 1:
|
|
|
|
f.queued = True
|
|
|
|
f.wanted = False
|
|
|
|
else:
|
|
|
|
f.queued = False
|
|
|
|
f.wanted = True
|
2010-09-03 13:28:44 +00:00
|
|
|
f.save()
|
2010-11-15 19:15:52 +00:00
|
|
|
#FIXME: this fails badly if rabbitmq goes down
|
2010-11-30 15:36:09 +00:00
|
|
|
try:
|
2013-11-10 18:30:34 +00:00
|
|
|
t = f.process_stream()
|
2011-01-01 11:44:42 +00:00
|
|
|
response['resultUrl'] = t.task_id
|
2010-11-30 15:36:09 +00:00
|
|
|
except:
|
|
|
|
pass
|
2010-08-07 14:31:20 +00:00
|
|
|
return render_to_json_response(response)
|
2010-09-03 13:28:44 +00:00
|
|
|
#init upload
|
2014-02-15 16:13:43 +00:00
|
|
|
elif oshash:
|
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
|
2014-03-02 09:32:35 +00:00
|
|
|
f.failed = False
|
2010-09-03 13:28:44 +00:00
|
|
|
f.save()
|
2012-04-18 09:11:12 +00:00
|
|
|
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 = {
|
2013-12-10 15:38:52 +00:00
|
|
|
'uploadUrl': '/api/upload/?id=%s&profile=%s' % (f.oshash, profile),
|
2014-09-19 12:26:46 +00:00
|
|
|
'url': request.build_absolute_uri('/%s' % f.item.public_id),
|
2010-09-03 13:28:44 +00:00
|
|
|
'result': 1
|
|
|
|
}
|
|
|
|
return render_to_json_response(response)
|
2012-09-08 15:03:59 +00:00
|
|
|
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')
|
2010-07-12 14:56:14 +00:00
|
|
|
return render_to_json_response(response)
|
|
|
|
|
2013-03-26 11:54:03 +00:00
|
|
|
@login_required_json
|
|
|
|
def direct_upload(request):
|
|
|
|
if 'id' in request.GET:
|
2014-10-05 12:58:53 +00:00
|
|
|
oshash = request.GET['id']
|
2013-03-26 11:54:03 +00:00
|
|
|
else:
|
|
|
|
oshash = request.POST['id']
|
|
|
|
response = json_response(status=400, text='this request requires POST')
|
|
|
|
if 'chunk' in request.FILES:
|
2014-10-05 12:58:53 +00:00
|
|
|
file = models.File.objects.get(oshash=oshash)
|
2014-04-11 16:56:22 +00:00
|
|
|
if file.editable(request.user):
|
|
|
|
response = process_chunk(request, file.save_chunk)
|
|
|
|
response['resultUrl'] = request.build_absolute_uri(file.item.get_absolute_url())
|
|
|
|
if response.get('done'):
|
2013-03-26 11:54:03 +00:00
|
|
|
file.uploading = False
|
2014-04-11 12:08:14 +00:00
|
|
|
if response['result'] == 1:
|
|
|
|
file.queued = True
|
|
|
|
file.wanted = False
|
|
|
|
else:
|
|
|
|
file.queued = False
|
|
|
|
file.wanted = True
|
2013-03-26 11:54:03 +00:00
|
|
|
file.save()
|
|
|
|
#try/execpt so it does not fail if rabitmq is down
|
|
|
|
try:
|
2013-11-10 18:30:34 +00:00
|
|
|
t = file.extract_stream()
|
2013-03-26 11:54:03 +00:00
|
|
|
response['resultUrl'] = t.task_id
|
|
|
|
except:
|
|
|
|
pass
|
|
|
|
return render_to_json_response(response)
|
|
|
|
#init upload
|
|
|
|
else:
|
|
|
|
file, created = models.File.objects.get_or_create(oshash=oshash)
|
|
|
|
if file.editable(request.user):
|
2013-09-26 14:55:37 +00:00
|
|
|
#remove previous uploads
|
|
|
|
if not created:
|
|
|
|
file.streams.all().delete()
|
|
|
|
file.delete_frames()
|
|
|
|
if file.item.rendered and file.selected:
|
|
|
|
Item.objects.filter(id=file.item.id).update(rendered=False)
|
2013-03-26 11:54:03 +00:00
|
|
|
file.uploading = True
|
|
|
|
file.save()
|
|
|
|
upload_url = request.build_absolute_uri('/api/upload/direct/?id=%s' % file.oshash)
|
|
|
|
return render_to_json_response({
|
|
|
|
'uploadUrl': upload_url,
|
|
|
|
'url': request.build_absolute_uri(file.item.get_absolute_url()),
|
|
|
|
'result': 1
|
|
|
|
})
|
|
|
|
else:
|
|
|
|
response = json_response(status=403, text='permission denied')
|
|
|
|
return render_to_json_response(response)
|
|
|
|
|
2011-01-01 11:44:42 +00:00
|
|
|
|
2010-07-12 14:56:14 +00:00
|
|
|
@login_required_json
|
2014-10-06 08:26:43 +00:00
|
|
|
def taskStatus(request, data):
|
2010-12-22 07:45:37 +00:00
|
|
|
#FIXME: should check if user has permissions to get status
|
2013-03-04 15:41:25 +00:00
|
|
|
if 'taskId' in data:
|
|
|
|
task_id = data['taskId']
|
|
|
|
else:
|
|
|
|
task_id = data['task_id']
|
2010-12-22 07:45:37 +00:00
|
|
|
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)
|
2010-12-22 07:45:37 +00:00
|
|
|
|
2011-06-27 13:39:35 +00:00
|
|
|
|
2011-06-15 13:05:06 +00:00
|
|
|
@login_required_json
|
2014-10-06 08:26:43 +00:00
|
|
|
def moveMedia(request, data):
|
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
|
2014-09-19 12:26:46 +00:00
|
|
|
item: new public_id
|
2011-06-15 13:05:06 +00:00
|
|
|
}
|
|
|
|
|
2013-03-04 19:35:06 +00:00
|
|
|
returns {
|
2011-06-15 13:05:06 +00:00
|
|
|
}
|
|
|
|
'''
|
2014-09-19 12:26:46 +00:00
|
|
|
if Item.objects.filter(public_id=data['item']).count() == 1:
|
|
|
|
i = Item.objects.get(public_id=data['item'])
|
2011-07-03 18:30:47 +00:00
|
|
|
else:
|
2014-09-19 12:26:46 +00:00
|
|
|
data['public_id'] = data.pop('item').strip()
|
|
|
|
if len(data['public_id']) != 7:
|
|
|
|
del data['public_id']
|
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:
|
2014-09-19 12:26:46 +00:00
|
|
|
i = get_item({'imdbId': data['public_id']}, user=request.user)
|
|
|
|
changed = [i.public_id]
|
2011-06-15 13:05:06 +00:00
|
|
|
for f in models.File.objects.filter(oshash__in=data['ids']):
|
2014-09-19 12:26:46 +00:00
|
|
|
if f.item.id != i.public_id and f.editable(request.user):
|
|
|
|
if f.item.public_id not in changed:
|
|
|
|
changed.append(f.item.public_id)
|
2011-07-05 14:28:22 +00:00
|
|
|
f.item = i
|
2011-06-15 13:05:06 +00:00
|
|
|
f.save()
|
2014-09-19 12:26:46 +00:00
|
|
|
for public_id in changed:
|
|
|
|
c = Item.objects.get(public_id=public_id)
|
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()
|
2014-09-19 12:26:46 +00:00
|
|
|
item.tasks.update_timeline.delay(public_id)
|
2011-06-15 13:05:06 +00:00
|
|
|
response = json_response(text='updated')
|
2014-09-19 12:26:46 +00:00
|
|
|
response['data']['item'] = i.public_id
|
2011-06-15 13:05:06 +00:00
|
|
|
return render_to_json_response(response)
|
2013-03-25 12:57:20 +00:00
|
|
|
actions.register(moveMedia, cache=False)
|
2011-01-01 11:44:42 +00:00
|
|
|
|
2011-10-23 16:19:47 +00:00
|
|
|
@login_required_json
|
2014-10-06 08:26:43 +00:00
|
|
|
def editMedia(request, data):
|
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
|
|
|
}
|
|
|
|
'''
|
2013-02-08 17:05:18 +00:00
|
|
|
ignore = []
|
2014-07-23 15:27:27 +00:00
|
|
|
save_items = []
|
2013-02-08 17:05:18 +00:00
|
|
|
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]
|
2014-07-23 15:27:27 +00:00
|
|
|
if key == 'language' and (f.is_video or f.is_audio):
|
2014-09-03 11:20:52 +00:00
|
|
|
save_items.append(f.item.id)
|
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()
|
2014-07-23 15:27:27 +00:00
|
|
|
if save_items:
|
|
|
|
for i in Item.objects.filter(id__in=list(set(save_items))):
|
|
|
|
i.save()
|
2011-10-23 16:19:47 +00:00
|
|
|
return render_to_json_response(response)
|
2013-03-25 12:57:20 +00:00
|
|
|
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
|
2014-10-06 08:26:43 +00:00
|
|
|
def removeMedia(request, data):
|
2012-01-15 15:05:37 +00:00
|
|
|
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)
|
2014-03-09 16:45:13 +00:00
|
|
|
selected = set([f.item.id for f in qs if f.selected])
|
|
|
|
items = list(set([f.item.id for f in qs]))
|
2012-02-11 12:43:02 +00:00
|
|
|
qs.delete()
|
2014-03-09 16:45:13 +00:00
|
|
|
for i in Item.objects.filter(id__in=items):
|
|
|
|
if i.id in selected:
|
|
|
|
i.update_timeline()
|
|
|
|
else:
|
|
|
|
i.save()
|
2012-01-15 15:05:37 +00:00
|
|
|
else:
|
|
|
|
response = json_response(status=403, text='permissino denied')
|
|
|
|
return render_to_json_response(response)
|
2013-03-25 12:57:20 +00:00
|
|
|
actions.register(removeMedia, cache=False)
|
2012-01-15 15:05:37 +00:00
|
|
|
|
2014-10-06 08:26:43 +00:00
|
|
|
def getPath(request, data):
|
2012-08-21 22:18:00 +00:00
|
|
|
'''
|
|
|
|
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]
|
2012-08-21 22:18:00 +00:00
|
|
|
}
|
|
|
|
|
2013-03-04 19:35:06 +00:00
|
|
|
returns {
|
|
|
|
id: path
|
2012-08-21 22:18:00 +00:00
|
|
|
}
|
|
|
|
'''
|
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']
|
2012-08-21 22:18:00 +00:00
|
|
|
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):
|
2012-06-13 07:45:39 +00:00
|
|
|
oshash = oshash.replace('/', '')
|
2010-09-14 14:10:37 +00:00
|
|
|
f = get_object_or_404(models.File, oshash=oshash)
|
2013-07-25 12:36:48 +00:00
|
|
|
return redirect('%s/media' % 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:
|
2011-10-15 15:21:41 +00:00
|
|
|
sort.append({'operator': '+', 'key': 'path'})
|
2011-01-19 12:06:03 +00:00
|
|
|
sort.append({'operator': '-', 'key': 'created'})
|
2010-07-12 14:56:14 +00:00
|
|
|
|
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 = {
|
2014-09-19 12:26:46 +00:00
|
|
|
'id': 'item__public_id',
|
2011-02-01 13:19:34 +00:00
|
|
|
'users': 'instances__volume__user__username',
|
|
|
|
'resolution': 'width',
|
2014-10-17 15:49:01 +00:00
|
|
|
'path': 'sort_path',
|
|
|
|
'partTitle': 'part_title',
|
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)
|
2013-02-08 12:26:56 +00:00
|
|
|
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
|
|
|
|
|
|
|
|
|
2014-10-06 08:26:43 +00:00
|
|
|
def findMedia(request, data):
|
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
|
|
|
|
}
|
|
|
|
|
2013-04-25 14:37:16 +00:00
|
|
|
query: item query object, more on query syntax at
|
2011-01-19 12:06:03 +00:00
|
|
|
https://wiki.0x2620.org/wiki/pandora/QuerySyntax
|
2013-04-25 14:37:16 +00:00
|
|
|
get media for all items matching the given query
|
|
|
|
|
2011-01-19 12:06:03 +00:00
|
|
|
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...
|
|
|
|
|
2011-10-15 15:21:41 +00:00
|
|
|
possible values for keys: path, items
|
2011-01-19 12:06:03 +00:00
|
|
|
|
|
|
|
with keys
|
2011-10-15 15:21:41 +00:00
|
|
|
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-11-06 13:12:15 +00:00
|
|
|
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]]
|
2011-10-15 15:21:41 +00:00
|
|
|
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'])
|
2011-10-15 15:21:41 +00:00
|
|
|
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)
|
|
|
|
|
2013-03-25 12:57:20 +00:00
|
|
|
actions.register(findMedia)
|
2011-01-19 12:06:03 +00:00
|
|
|
|
2014-10-06 08:26:43 +00:00
|
|
|
def parsePath(request, data): #parse path and return info
|
2011-10-15 15:21:41 +00:00
|
|
|
'''
|
2013-03-04 19:35:06 +00:00
|
|
|
takes {
|
2011-10-15 15:21:41 +00:00
|
|
|
path: string
|
|
|
|
}
|
2013-03-04 19:35:06 +00:00
|
|
|
returns {
|
|
|
|
imdb: string
|
2011-10-15 15:21:41 +00:00
|
|
|
}
|
|
|
|
'''
|
2014-10-06 08:26:43 +00:00
|
|
|
path = data['path']
|
2011-10-15 15:21:41 +00:00
|
|
|
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
|
|
|
|
2014-10-06 08:26:43 +00:00
|
|
|
def getMediaInfo(request, data):
|
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 {
|
2014-09-19 12:26:46 +00:00
|
|
|
item: public_id,
|
2013-03-04 19:35:06 +00:00
|
|
|
file: oshash of source file
|
2012-01-20 18:15:54 +00:00
|
|
|
}
|
|
|
|
'''
|
|
|
|
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,
|
2014-09-19 12:26:46 +00:00
|
|
|
'item': f.item.public_id
|
2012-01-20 18:15:54 +00:00
|
|
|
}
|
|
|
|
return render_to_json_response(response)
|
2013-03-25 12:57:20 +00:00
|
|
|
actions.register(getMediaInfo)
|
2012-01-20 18:15:54 +00:00
|
|
|
|