cablegates/pandora/archive/views.py

328 lines
12 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
import re
from datetime import datetime
from urllib2 import unquote
import mimetypes
from django import forms
from django.core.paginator import Paginator
from django.contrib.auth.decorators import login_required
from django.contrib.auth.models import User
from django.db.models import Q, Avg, Count, Sum
from django.http import HttpResponse, Http404
from django.shortcuts import render_to_response, get_object_or_404, get_list_or_404
from django.template import RequestContext
from django.conf import settings
try:
import simplejson as json
except ImportError:
from django.utils import simplejson as json
from oxdjango.decorators import login_required_json
from oxdjango.shortcuts import render_to_json_response, get_object_or_404_json, json_response
from oxdjango.http import HttpFileResponse
import ox
import models
2010-08-07 14:31:20 +00:00
from backend.utils import oxid, parse_path
import backend.models
2010-08-10 22:01:41 +00:00
@login_required_json
def api_removeVolume(request):
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(status=200, text='ok')
except models.Volume.DoesNotExist:
response = json_response(status=404, text='volume not found')
return render_to_json_response(response)
@login_required_json
def api_update(request):
'''
2010-08-10 22:01:41 +00:00
2 calls possible:
volume/files
info
call volume/files first and fill in requested info after that
param data
2010-08-10 22:01:41 +00:00
volume: '',
2010-08-07 14:31:20 +00:00
files: [
2010-08-10 22:01:41 +00:00
{oshash:, path:, ctime:, atime:, mtime:, }
2010-08-07 14:31:20 +00:00
]
info: {oshash: object}
return {'status': {'code': int, 'text': string},
2010-08-07 14:31:20 +00:00
'data': {info: list, data: list, file: list}}
'''
2010-08-10 22:01:41 +00:00
print "here we go fucker", request.user
data = json.loads(request.POST['data'])
2010-08-07 14:31:20 +00:00
user = request.user
2010-08-10 22:01:41 +00:00
response = json_response({'info': [], 'data': [], 'file': []})
2010-08-07 14:31:20 +00:00
if 'files' in data:
2010-08-10 22:01:41 +00:00
volume, created = models.Volume.objects.get_or_create(user=user, name=data['volume'])
2010-08-07 14:31:20 +00:00
all_files = []
for f in data['files']:
2010-08-10 22:01:41 +00:00
print f
path = f['path']
folder = path.split('/')
name = folder.pop()
if folder and folder[-1] in ('Extras', 'Versions', 'DVDs'):
name = '/'.join([folder.pop(), name])
folder = '/'.join(folder)
print folder
print name
f['folder'] = folder
f['name'] = name
2010-08-07 14:31:20 +00:00
oshash = f['oshash']
all_files.append(oshash)
2010-08-10 22:01:41 +00:00
same_folder = models.FileInstance.objects.filter(folder=folder, volume=volume)
2010-08-07 14:31:20 +00:00
if same_folder.count() > 0:
movie = same_folder[0].file.movie
else:
movie = None
path = os.path.join(folder, name)
2010-08-10 22:01:41 +00:00
instance = models.FileInstance.objects.filter(file__oshash=oshash, volume=volume)
if instance.count()>0:
instance = instance[0]
2010-08-07 14:31:20 +00:00
updated = False
for key in ('atime', 'mtime', 'ctime', 'name', 'folder'):
if f[key] != getattr(instance, key):
setattr(instance, key, f[key])
updated=True
if updated:
2010-08-10 22:01:41 +00:00
instance.save()
else:
#look if oshash is known
2010-08-10 22:01:41 +00:00
file_objects = models.File.objects.filter(oshash=oshash)
if file_objects.count() > 0:
file_object = file_objects[0]
#new oshash, add to database
else:
if not movie:
2010-08-07 14:31:20 +00:00
movie_info = parse_path(folder)
movie = backend.models.getMovie(movie_info)
2010-08-10 22:01:41 +00:00
file_object = models.File()
file_object.oshash = oshash
file_object.name = name
file_object.movie = movie
file_object.save()
response['data']['info'].append(oshash)
instance = models.FileInstance()
instance.volume = volume
instance.file = file_object
for key in ('atime', 'mtime', 'ctime', 'name', 'folder'):
setattr(instance, key, f[key])
instance.save()
2010-08-07 14:31:20 +00:00
#remove deleted files
#FIXME: can this have any bad consequences? i.e. on the selction of used movie files.
2010-08-10 22:01:41 +00:00
models.FileInstance.objects.filter(volume=volume).exclude(file__oshash__in=all_files).delete()
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:
for oshash in data['info']:
info = data['info'][oshash]
2010-08-10 22:01:41 +00:00
instance = models.FileInstance.objects.filter(file__oshash=oshash, volume__user=user)
2010-08-07 14:31:20 +00:00
if instance.count()>0:
instance = instance[0]
if not instance.file.info:
2010-08-10 22:01:41 +00:00
for key in ('atime', 'mtime', 'ctime'):
if key in info:
del info[key]
2010-08-07 14:31:20 +00:00
instance.file.info = info
instance.file.save()
2010-08-10 22:01:41 +00:00
files = models.FileInstance.objects.filter(volume__user=user, file__available=False)
response['data']['info'] = [f.file.oshash for f in files.filter(file__info='{}')]
#needs some flag to find those that are actually used main is to generic
response['data']['data'] = [f.file.oshash for f in files.filter(file__is_video=True, file__is_main=True)]
response['data']['file'] = [f.file.oshash for f in files.filter(file__is_subtitle=True)]
2010-08-07 14:31:20 +00:00
return render_to_json_response(response)
2010-08-07 14:31:20 +00:00
2010-08-10 22:01:41 +00:00
@login_required_json
2010-08-07 14:31:20 +00:00
def api_upload(request):
'''
2010-08-24 17:16:33 +00:00
oshash: string
frame: [] //multipart frames
file: [] //multipart file
return {'status': {'code': int, 'text': string},
2010-08-07 14:31:20 +00:00
'data': {info: object, rename: object}}
'''
2010-08-07 14:31:20 +00:00
user = request.user
f = get_object_or_404(models.File, oshash=request.POST['oshash'])
2010-09-08 17:14:01 +00:00
print request.FILES
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])
fr = models.Frame(file=f, position=position)
fr.save()
fr.frame.save(name, frame)
response = json_response({})
else:
response = json_response(status=403, text='permissino denied')
if 'file' in request.FILES:
2010-09-03 13:28:44 +00:00
if not f.data:
f.data.save('data.raw', request.FILES['file'])
2010-08-24 17:16:33 +00:00
response = json_response({})
else:
response = json_response(status=403, text='permissino denied')
2010-08-07 14:31:20 +00:00
return render_to_json_response(response)
class VideoChunkForm(forms.Form):
chunk = forms.FileField()
chunkId = forms.IntegerField(required=False)
done = forms.IntegerField(required=False)
@login_required_json
def firefogg_upload(request):
2010-08-13 17:17:23 +00:00
profile = request.GET['profile']
2010-09-05 21:08:05 +00:00
if profile.endswith('.webm'):
profile = os.path.splitext(profile)[0]
2010-08-13 17:17:23 +00:00
oshash = request.GET['oshash']
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)
2010-09-03 13:28:44 +00:00
if form.is_valid() and profile == settings.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,
'resultUrl': request.build_absolute_uri('/')
}
2010-09-03 13:28:44 +00:00
if not f.save_chunk(c, chunk_id):
2010-08-07 14:31:20 +00:00
response['result'] = -1
elif form.cleaned_data['done']:
#FIXME: send message to encode deamon to create derivates instead
2010-09-03 13:28:44 +00:00
f.available = True
f.save()
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
elif oshash and profile == settings.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):
if f.video:
f.video.delete()
2010-09-08 17:14:01 +00:00
f.available = False
2010-09-03 13:28:44 +00:00
f.save()
response = {
#is it possible to no hardcode url here?
'uploadUrl': request.build_absolute_uri('/api/upload/?oshash=%s&profile=%s' % (f.oshash, profile)),
'result': 1
}
return render_to_json_response(response)
2010-08-07 14:31:20 +00:00
response = json_response(status=400, text='this request requires POST')
return render_to_json_response(response)
2010-08-07 14:31:20 +00:00
"""
@login_required_json
2010-08-07 14:31:20 +00:00
def list_files(request):
'''
2010-08-07 14:31:20 +00:00
GET list
> {
"files": {
"a41cde31c581e11d": {"path": "E/Example, The/An Example.avi", "size":1646274},
}
}
'''
2010-08-07 14:31:20 +00:00
response = {}
response['files'] = {}
qs = models.UserFile.filter(user=request.user)
p = Paginator(qs, 1000)
for i in p.page_range:
page = p.page(i)
for f in page.object_list:
response['files'][f.movie_file.oshash] = {'path': f.path, 'size': f.movie_file.size}
return render_to_json_response(response)
2010-08-07 14:31:20 +00:00
def find_files(request):
response = {}
query = _parse_query(request)
response['files'] = {}
qs = models.UserFile.filter(user=request.user).filter(movie_file__movie__id__in=query['q'])
p = Paginator(qs, 1000)
for i in p.page_range:
page = p.page(i)
for f in page.object_list:
response['files'][f.movie_file.oshash] = {'path': f.path, 'size': f.movie_file.size}
return render_to_json_response(response)
def api_fileInfo(request):
'''
param data
2010-08-07 14:31:20 +00:00
oshash string
return {'status': {'code': int, 'text': string},
'data': {imdbId:string }}
'''
2010-08-07 14:31:20 +00:00
if 'data' in request.POST:
oshash = json.loads(request.POST['data'])
elif 'oshash' in request.GET:
oshash = request.GET['oshash']
f = models.MovieFile.objects.get(oshash=oshash)
response = {'data': f.json()}
return render_to_json_response(response)
2010-08-07 14:31:20 +00:00
def api_subtitles(request):
'''
param data
oshash string
language string
subtitle string
return
if no language is provided:
{data: {languages: array}}
if language is set:
{data: {subtitle: string}}
if subtitle is set:
saves subtitle for given language
'''
if 'data' in request.POST:
data = json.loads(request.POST['data'])
oshash = data['oshash']
language = data.get('language', None)
srt = data.get('subtitle', None)
if srt:
user = request.user
sub = models.Subtitles.objects.get_or_create(user, oshash, language)
sub.srt = srt
sub.save()
else:
response = json_response({})
if language:
q = models.Subtitles.objects.filter(movie_file__oshash=oshash, language=language)
if q.count() > 0:
response['data']['subtitle'] = q[0].srt
return render_to_json_response(response)
l = models.Subtitles.objects.filter(movie_file__oshash=oshash).values('language')
response['data']['languages'] = [f['language'] for f in l]
return render_to_json_response(response)
"""