more api implementation, some templates

This commit is contained in:
j 2010-01-29 16:33:45 +05:30
parent 2bff8f8045
commit 21f8710b38
6 changed files with 107 additions and 112 deletions

View file

@ -13,6 +13,7 @@
Alias /favicon.ico __PREFIX__/oxdb/static/favicon.ico Alias /favicon.ico __PREFIX__/oxdb/static/favicon.ico
Alias /media __PREFIX__/oxdb/media Alias /media __PREFIX__/oxdb/media
Alias /admin/media __PREFIX__/src/django/django/contrib/admin/media Alias /admin/media __PREFIX__/src/django/django/contrib/admin/media
Alias /static/js/jquery.js __PREFIX__/src/django/django/contrib/admin/media/js/jquery.min.js
WSGIScriptAlias / __PREFIX__/wsgi/django.wsgi WSGIScriptAlias / __PREFIX__/wsgi/django.wsgi
WSGIDaemonProcess oxdb user=oxdb group=oxdb threads=25 python-path=__PREFIX__/lib/python2.6/site-packages/ WSGIDaemonProcess oxdb user=oxdb group=oxdb threads=25 python-path=__PREFIX__/lib/python2.6/site-packages/

View file

@ -84,6 +84,7 @@ class MovieExtra(models.Model):
title = models.CharField(max_length=1000) title = models.CharField(max_length=1000)
description = models.TextField(blank=True) description = models.TextField(blank=True)
contributor = models.CharField(max_length=1000) contributor = models.CharField(max_length=1000)
rights_level = models.IntegerField(default=-1)
def getMovie(info): def getMovie(info):
''' '''
@ -238,8 +239,6 @@ class Movie(models.Model):
q = q | Q(url__contains=w.url) q = q | Q(url__contains=w.url)
return self.reviews_all.filter(q).filter(manual=False) return self.reviews_all.filter(q).filter(manual=False)
rights_level = models.IntegerField(default=-1)
''' '''
#these values get populated with imdb/oxdb values on save() #these values get populated with imdb/oxdb values on save()
#edits will be overwritten #edits will be overwritten

View file

@ -30,6 +30,11 @@ from oxuser.models import getUserJSON
def api(request): def api(request):
if request.META['REQUEST_METHOD'] == "OPTIONS":
response = HttpResponse('')
#response = render_to_json_response({'status': {'code': 200, 'text': 'please use POST'}})
#response['Access-Control-Allow-Origin'] = '*'
return response
if not 'function' in request.POST: if not 'function' in request.POST:
return apidoc(request) return apidoc(request)
function = request.POST['function'] function = request.POST['function']
@ -43,12 +48,13 @@ def api(request):
else: else:
response = render_to_json_response( response = render_to_json_response(
{'status': {'code': 404, 'text': 'Unknown function %s' % function}}) {'status': {'code': 404, 'text': 'Unknown function %s' % function}})
#response['Access-Control-Allow-Origin'] = '*'
return response return response
def api_hello(request): def api_hello(request):
''' '''
return {'status': {'code': int, 'text': string}, return {'status': {'code': int, 'text': string},
'data': {user: dict}} 'data': {user: object}}
''' '''
response = {'status': {'code': 200, 'text': 'ok'}, 'data': {}} response = {'status': {'code': 200, 'text': 'ok'}, 'data': {}}
if request.user.is_authenticated(): if request.user.is_authenticated():
@ -116,11 +122,11 @@ def api_find(request):
with p, items is list of dicts with requested properties: with p, items is list of dicts with requested properties:
return {'status': {'code': int, 'text': string}, return {'status': {'code': int, 'text': string},
'data': {items: list}} 'data': {items: array}}
with g, items contains list of {'title': string, 'items': int}: with g, items contains list of {'title': string, 'items': int}:
return {'status': {'code': int, 'text': string}, return {'status': {'code': int, 'text': string},
'data': {items: list}} 'data': {items: array}}
with g + n=1, return number of items in given query with g + n=1, return number of items in given query
return {'status': {'code': int, 'text': string}, return {'status': {'code': int, 'text': string},
@ -202,7 +208,7 @@ def api_getItem(request):
param data param data
string id string id
return item dict return item array
''' '''
response = {'status': {'code': 200, 'text': 'ok'}} response = {'status': {'code': 200, 'text': 'ok'}}
itemId = json.loads(request.POST['data']) itemId = json.loads(request.POST['data'])
@ -222,7 +228,7 @@ def api_editItem(request):
data = json.loads(request.POST['data']) data = json.loads(request.POST['data'])
item = get_object_or_404_json(models.Movie, movieId=data['id']) item = get_object_or_404_json(models.Movie, movieId=data['id'])
if item.editable(request.user): if item.editable(request.user):
response = {'status': {'code': 500, 'text': 'not implemented'}} response = {'status': {'code': 501, 'text': 'not implemented'}}
item.edit(data) item.edit(data)
else: else:
response = {'status': {'code': 403, 'text': 'permission denied'}} response = {'status': {'code': 403, 'text': 'permission denied'}}
@ -240,7 +246,7 @@ def api_removeItem(request):
itemId = json.loads(request.POST['data']) itemId = json.loads(request.POST['data'])
item = get_object_or_404_json(models.Movie, movieId=itemId) item = get_object_or_404_json(models.Movie, movieId=itemId)
if item.editable(request.user): if item.editable(request.user):
response = {'status': {'code': 500, 'text': 'not implemented'}} response = {'status': {'code': 501, 'text': 'not implemented'}}
else: else:
response = {'status': {'code': 403, 'text': 'permission denied'}} response = {'status': {'code': 403, 'text': 'permission denied'}}
return render_to_json_response(response) return render_to_json_response(response)
@ -253,7 +259,7 @@ def api_addLayer(request):
return {'status': {'code': int, 'text': string}, return {'status': {'code': int, 'text': string},
'data': {}} 'data': {}}
''' '''
response = {'status': {'code': 500, 'text': 'not implemented'}} response = {'status': {'code': 501, 'text': 'not implemented'}}
return render_to_json_response(response) return render_to_json_response(response)
@login_required_json @login_required_json
@ -264,7 +270,7 @@ def api_removeLayer(request):
return {'status': {'code': int, 'text': string}, return {'status': {'code': int, 'text': string},
'data': {}} 'data': {}}
''' '''
response = {'status': {'code': 500, 'text': 'not implemented'}} response = {'status': {'code': 501, 'text': 'not implemented'}}
return render_to_json_response(response) return render_to_json_response(response)
@login_required_json @login_required_json
@ -279,12 +285,12 @@ def api_editLayer(request):
data = json.loads(request.POST['data']) data = json.loads(request.POST['data'])
layer = get_object_or_404_json(models.Layer, pk=data['id']) layer = get_object_or_404_json(models.Layer, pk=data['id'])
if layer.editable(request.user): if layer.editable(request.user):
response = {'status': {'code': 500, 'text': 'not implemented'}} response = {'status': {'code': 501, 'text': 'not implemented'}}
else: else:
response = {'status': {'code': 403, 'text': 'permission denied'}} response = {'status': {'code': 403, 'text': 'permission denied'}}
return render_to_json_response(response) return render_to_json_response(response)
response = {'status': {'code': 500, 'text': 'not implemented'}} response = {'status': {'code': 501, 'text': 'not implemented'}}
return render_to_json_response(response) return render_to_json_response(response)
@login_required_json @login_required_json
@ -295,7 +301,7 @@ def api_addListItem(request):
return {'status': {'code': int, 'text': string}, return {'status': {'code': int, 'text': string},
'data': {}} 'data': {}}
''' '''
response = {'status': {'code': 500, 'text': 'not implemented'}} response = {'status': {'code': 501, 'text': 'not implemented'}}
return render_to_json_response(response) return render_to_json_response(response)
@login_required_json @login_required_json
@ -306,7 +312,7 @@ def api_removeListItem(request):
return {'status': {'code': int, 'text': string}, return {'status': {'code': int, 'text': string},
'data': {}} 'data': {}}
''' '''
response = {'status': {'code': 500, 'text': 'not implemented'}} response = {'status': {'code': 501, 'text': 'not implemented'}}
return render_to_json_response(response) return render_to_json_response(response)
@login_required_json @login_required_json
@ -317,7 +323,7 @@ def api_addList(request):
return {'status': {'code': int, 'text': string}, return {'status': {'code': int, 'text': string},
'data': {}} 'data': {}}
''' '''
response = {'status': {'code': 500, 'text': 'not implemented'}} response = {'status': {'code': 501, 'text': 'not implemented'}}
return render_to_json_response(response) return render_to_json_response(response)
@login_required_json @login_required_json
@ -328,7 +334,7 @@ def api_editList(request):
return {'status': {'code': int, 'text': string}, return {'status': {'code': int, 'text': string},
'data': {}} 'data': {}}
''' '''
response = {'status': {'code': 500, 'text': 'not implemented'}} response = {'status': {'code': 501, 'text': 'not implemented'}}
return render_to_json_response(response) return render_to_json_response(response)
def api_removeList(request): def api_removeList(request):
@ -338,7 +344,7 @@ def api_removeList(request):
return {'status': {'code': int, 'text': string}, return {'status': {'code': int, 'text': string},
'data': {}} 'data': {}}
''' '''
response = {'status': {'code': 500, 'text': 'not implemented'}} response = {'status': {'code': 501, 'text': 'not implemented'}}
return render_to_json_response(response) return render_to_json_response(response)
#@login_required_json #@login_required_json
@ -347,41 +353,42 @@ def api_update(request):
param data param data
{archive: string, files: json} {archive: string, files: json}
return {'status': {'code': int, 'text': string}, return {'status': {'code': int, 'text': string},
'data': {info: dict, rename: dict}} 'data': {info: object, rename: object}}
''' '''
print "update request"
data = json.loads(request.POST['data']) data = json.loads(request.POST['data'])
archive = data['archive'] archive = data['archive']
files = data['files'] files = data['files']
archive = models.Archive.objects.get(name=archive) archive = get_object_or_404_json(models.Archive, name=archive)
print "update request for", archive.name if archive.editable(request.user):
needs_data = [] needs_data = []
rename = {} rename = {}
for oshash in files: for oshash in files:
print 'checking', oshash print 'checking', oshash
data = files[oshash] data = files[oshash]
q = models.ArchiveFile.objects.filter(archive=archive, file__oshash=oshash) q = models.ArchiveFile.objects.filter(archive=archive, file__oshash=oshash)
if q.count() == 0: if q.count() == 0:
print "adding file", oshash, data['path'] print "adding file", oshash, data['path']
f = models.ArchiveFile.get_or_create(archive, oshash) f = models.ArchiveFile.get_or_create(archive, oshash)
f.update(data) f.update(data)
if not f.file.movie: if not f.file.movie:
send_bg_message({'findMovie': f.file.id}) send_bg_message({'findMovie': f.file.id})
#FIXME: only add if it was not in File #FIXME: only add if it was not in File
else: else:
f = q[0] f = q[0]
if data['path'] != f.path: if data['path'] != f.path:
f.path = data['path'] f.path = data['path']
f.save() f.save()
if f.file.needs_data: if f.file.needs_data:
needs_data.append(oshash) needs_data.append(oshash)
if f.path != f.file.path: if f.path != f.file.path:
rename[oshash] = f.file.path rename[oshash] = f.file.path
print "processed files for", archive.name print "processed files for", archive.name
#remove all files not in files.keys() from ArchiveFile #remove all files not in files.keys() from ArchiveFile
response = {'status': {'code': 200, 'text': 'ok'}, 'data': {}} response = {'status': {'code': 200, 'text': 'ok'}, 'data': {}}
response['data']['info'] = needs_data response['data']['info'] = needs_data
response['data']['rename'] = rename response['data']['rename'] = rename
else:
response = {'status': {'code': 403, 'text': 'permission denied'}}
return render_to_json_response(response) return render_to_json_response(response)
def api_upload(request): #video, timeline, frame def api_upload(request): #video, timeline, frame
@ -396,7 +403,7 @@ def api_upload(request): #video, timeline, frame
if data['item'] == 'timeline': if data['item'] == 'timeline':
print "not implemented" print "not implemented"
response = {'status': {'code': 500, 'text': 'not implemented'}} response = {'status': {'code': 501, 'text': 'not implemented'}}
return render_to_json_response(response) return render_to_json_response(response)
@login_required_json @login_required_json
@ -404,7 +411,7 @@ def api_editFile(request): #FIXME: should this be file.files. or part of update
''' '''
change file / imdb link change file / imdb link
''' '''
response = {'status': {'code': 500, 'text': 'not implemented'}} response = {'status': {'code': 501, 'text': 'not implemented'}}
return render_to_json_response(response) return render_to_json_response(response)
def api_parse(request): #parse path and return info def api_parse(request): #parse path and return info
@ -416,7 +423,7 @@ def api_parse(request): #parse path and return info
''' '''
path = json.loads(request.POST['data'])['path'] path = json.loads(request.POST['data'])['path']
response = utils.parsePath(path) response = utils.parsePath(path)
response = {'status': 500, 'statusText': 'ok', data: response} response = {'status': {'code': 500, 'text': 'ok'}, data: response}
return render_to_json_response(response) return render_to_json_response(response)
def api_getImdbId(request): def api_getImdbId(request):
@ -434,7 +441,7 @@ def api_getImdbId(request):
response = {'status': {'code': 404, 'text': 'not found'}} response = {'status': {'code': 404, 'text': 'not found'}}
return render_to_json_response(response) return render_to_json_response(response)
def api_getFileInfo(request): def api_fileInfo(request):
''' '''
param data param data
oshash string oshash string
@ -449,32 +456,39 @@ def api_getFileInfo(request):
response = {'data': f.json()} response = {'data': f.json()}
return render_to_json_response(response) return render_to_json_response(response)
''' def api_subtitles(request):
GET subtitles?oshash=a41cde31c581e11d '''
> { param data
"languages": ['en', 'fr', 'de'] oshash string
} language string
GET subtitles?oshash=a41cde31c581e11d&language=en subtitle string
> srt file return
POST subtitle?oshash=a41cde31c581e11d&language=en if no language is provided:
srt = {data: {languages: array}}
''' if language is set:
def subtitles(request): {data: {subtitle: string}}
oshash = request.GET['oshash'] if subtitle is set:
language = request.GET.get('language', None) saves subtitle for given language
if request.method == 'POST': '''
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 user = request.user
sub = models.Subtitles.get_or_create(user, oshash, language) sub = models.Subtitles.get_or_create(user, oshash, language)
sub.srt = request.POST['srt'] sub.srt = srt
sub.save() sub.save()
else: else:
response = {'status': {'code': 200, 'text': 'ok'}, 'data':{}}
if language: if language:
q = models.Subtitles.objects.filter(movie_file__oshash=oshash, language=language) q = models.Subtitles.objects.filter(movie_file__oshash=oshash, language=language)
if q.count() > 0: if q.count() > 0:
return HttpResponse(q[0].srt, content_type='text/x-srt') response['data']['subtitle'] = q[0].srt
response = {} return render_to_json_response(response)
l = models.Subtitles.objects.filter(movie_file__oshash=oshash).values('language') l = models.Subtitles.objects.filter(movie_file__oshash=oshash).values('language')
response['languages'] = [f['language'] for f in l] response['data']['languages'] = [f['language'] for f in l]
return render_to_json_response(response) return render_to_json_response(response)
''' '''
@ -509,40 +523,6 @@ def find_files(request):
response['files'][f.movie_file.oshash] = {'path': f.path, 'size': f.movie_file.size} response['files'][f.movie_file.oshash] = {'path': f.path, 'size': f.movie_file.size}
return render_to_json_response(response) return render_to_json_response(response)
'''
POST metadata
> file: {
"duration": 5.266667,
"video_codec": "mpeg1",
"pixel_format": "yuv420p",
"width": 352,
"height": 240,
"pixel_aspect_ratio": "1:1",
"display_aspect_ratio": "22:15",
"framerate": "30:1",
"audio_codec": "mp2",
"samplerate": 44100,
"channels": 1,
"path": "E/Example, The/An Example.avi",
"size": 1646274
"oshash": "a41cde31c581e11d",
"sha1":..,
"md5":..
}
'''
#@login_required_json
def add_metadata(request, archive):
info = json.loads(request.POST['file'])
oshash = info['oshash']
archive = models.Archive.objects.get(name=archive)
if archive.users.filter(user=request.user).count() == 1:
user_file = models.ArchiveFile.get_or_create(archive, oshash)
user_file.update(request.POST)
response = {'status': 200}
else:
response = {'status': 404}
return render_to_json_response(response)
class StillForm(forms.Form): class StillForm(forms.Form):
still = forms.FileField() still = forms.FileField()
position = forms.FloatField() position = forms.FloatField()

View file

@ -131,14 +131,16 @@ def api_preferences(request):
''' '''
param data param data
string string
list array
dict object
return return
if data is empy or {}
if data is string: if data is string:
return preference with name return preference with name
if data is list: if data is array:
return preferences with names return preferences with names
if data is dict: if data is object:
set key values in dict as preferences set key values in dict as preferences
''' '''
response = {'status': {'code': 200, 'text': 'ok'}, 'data':{}} response = {'status': {'code': 200, 'text': 'ok'}, 'data':{}}
@ -154,8 +156,11 @@ def api_preferences(request):
for preference in data: for preference in data:
response['preferences'][preference] = models.getPreference(request.user, preference) response['preferences'][preference] = models.getPreference(request.user, preference)
elif isinstance(data, dict): elif isinstance(data, dict):
del response['data'] if not data:
for key in data: response['data']['preferences'] = models.getPreferences(request.user)
models.setPreference(request.user, key, data[key]) else:
del response['data']
for key in data:
models.setPreference(request.user, key, data[key])
return render_to_json_response(response) return render_to_json_response(response)

View file

@ -0,0 +1,10 @@
{% extends "admin/base.html" %}
{% load i18n %}
{% block title %}{{ title }} | 0xdb.org{% endblock %}
{% block branding %}
<h1 id="site-name">0xdb.org</h1>
{% endblock %}
{% block nav-global %}{% endblock %}

View file

@ -11,7 +11,7 @@
<div> <div>
{% for f in api %} {% for f in api %}
<div> <div>
<h2><a name="{{f.name}}">function: {{f.name}}</a></h2> <h2><a name="{{f.name}}">action: {{f.name}}</a></h2>
<pre style="padding-left: 24px"> <pre style="padding-left: 24px">
{{f.doc|safe}} {{f.doc|safe}}
</pre> </pre>