api, api, api, today its all about the api

This commit is contained in:
j 2010-01-26 18:41:57 +05:30
parent c3b589b90c
commit eb05595e2c
4 changed files with 289 additions and 262 deletions

View file

@ -26,6 +26,7 @@ import utils
from daemon import send_bg_message from daemon import send_bg_message
from oxuser.views import api_login, api_logout, api_register, api_recover, api_preferences from oxuser.views import api_login, api_logout, api_register, api_recover, api_preferences
from oxuser.models import getUserJSON
def api(request): def api(request):
@ -40,116 +41,23 @@ def api(request):
if f: if f:
response = f(request) response = f(request)
else: else:
response = {'status': 404, 'statusText': 'Unknown function %s' % function} response = render_to_json_response(
response = render_to_json_response(response) {'status': {'code': 404, 'text': 'Unknown function %s' % function}})
return response return response
def api_hello(request): def api_hello(request):
''' '''
api('hello', functio(result) { result.user }) return {'status': {'code': int, 'text': string},
'data': {user: dict}}
''' '''
response = {'status': 200, 'statusText': 'ok'} response = {'status': {'code': 200, 'text': 'ok'}, 'data': {}}
response.user = request.user.json() if request.user.is_authenticated():
response['data']['user'] = getUserJSON(request.user)
else:
response['data']['user'] = {'name': 'Guest'}
return render_to_json_response(response) return render_to_json_response(response)
'''
field.length -> movie.sort.all()[0].field
o=0&n=100
a & b | c & d
query
l=user:name or l=name
q=year:1980,hello,country:usa
q=year:1980,hello,country:!usa
q=title:^the$
q=title:^100%24$
q=year:<1970,year:>1960
q=year:<1960,year:>1950,title:sex
!1960-1970
2009.08.02.22.26.35-2009.08.02.22.26.35
!^the
(dddd.dd.dd)-(dddd.dd.dd)
5-8
10000-20000
<2009-08-02-22-26-35
>2009-08-02-22-26-35
2009-08-02-22-26-35<
^the the*
*foo foo$
*foo* foo
s=director:asc,year:desc default: director:asc,year:desc
r=0:100 or r=100 or r=100: default: 0:100
p=id,title,director,date,cast.length default: title,director,year,country
q
List data backend spec:
url = //url for request
params = [] //additional params passed to url, i.e. query, or group
the url must understand the following requests:
number of items:
url?params&n=1
> {items: N}
items sorted by key range X to Y:
url?params&s=key:asc|desc&r=X:Y
> {items: [{k0:v0, k1:v1...}, {k0:v0, k1:v1...}]}
Examples:
/json/find?l=all&s=title&f=all&q=&a=desc&p=id,title,director,date,cast.length
/json/find?r=0:100&l=all&s=title&f=all&q=&a=desc&p=id,title,director,date,cast.length
{
movies=[
{
"id":
"title": "fsdf",
"director":
},
]
}
#get sort order for all ids
/json/find?r=0:1000&l=all&s=title&f=all&q=&a=desc&p=id
{
movies=[
{
"id": id
},
]
}
/json/find?l=all&s=title&f=all&q=&a=desc
{
movies: 1234,
files: 2345,
pixels: 1242345345,
size: 1235,
duration: 1235,
}
/json/find?r=0:100&l=all&s=[name, items]&f=all&q=&a=desc&g=country
{
groups = [ {name:"USA", movies: 123}, {name:"UK", movies: 1234} ]
}
#find as you type: in table, by sort string
#auto compleat in find box
'''
def _order_query(qs, s, prefix='sort__'): def _order_query(qs, s, prefix='sort__'):
order_by = [] order_by = []
for e in s.split(','): for e in s.split(','):
@ -196,17 +104,36 @@ def _parse_query(request):
def api_find(request): def api_find(request):
''' '''
data: {'q': query, 's': sort, 'r': range} param data
{'q': query, 's': sort, 'r': range}
query: query string, can contain field:search more on query syntax at q: query string, can contain field:search more on query syntax at
http://wiki.0xdb.org/wiki/QuerySyntax http://wiki.0xdb.org/wiki/QuerySyntax
sort: comma seperated list of field:order, default: director:asc,year:desc s: comma seperated list of field:order, default: director:asc,year:desc
range: result ragne, from:to or from r: result ragne, from:to or from
p: properties to return, if obmited stats are returned
g: group elements by, country, genre, director...
with p, items is list of dicts with requested properties:
return {'status': {'code': int, 'text': string},
'data': {items: list}}
with g, items contains list of {'title': string, 'items': int}:
return {'status': {'code': int, 'text': string},
'data': {items: list}}
with g + n=1, return number of items in given query
return {'status': {'code': int, 'text': string},
'data': {items: int}}
without p or g, return stats about query:
return {'status': {'code': int, 'text': string},
'data': {items=int, files=int, pixels=int, size=int, duration=int}}
''' '''
query = _parse_query(request) query = _parse_query(request)
response = {} response = {'status': {'code': 200, 'text':'ok'}, 'data':{}}
if 'p' in query: if 'p' in query:
response['items'] = [] response['data']['items'] = []
qs = _order_query(query['q'], query['s']) qs = _order_query(query['q'], query['s'])
if 'n' in query: if 'n' in query:
response = {'items': qs.count()} response = {'items': qs.count()}
@ -221,13 +148,13 @@ def api_find(request):
return r return r
qs = qs[query['i']:query['o']] qs = qs[query['i']:query['o']]
response['items'] = [only_p(m['json']) for m in qs.values('json')] response['data']['items'] = [only_p(m['json']) for m in qs.values('json')]
elif 'g' in query: elif 'g' in query:
if query['s'].split(':')[0] not in ('name', 'items'): if query['s'].split(':')[0] not in ('name', 'items'):
query['s'] = 'name' query['s'] = 'name'
#FIXME: also filter lists here #FIXME: also filter lists here
response['items'] = [] response['data']['items'] = []
name = 'name' name = 'name'
items = 'movies' items = 'movies'
movie_qs = query['q'] movie_qs = query['q']
@ -243,7 +170,7 @@ def api_find(request):
qs = movie_qs.values('imdb__year').annotate(movies=Count('id')) qs = movie_qs.values('imdb__year').annotate(movies=Count('id'))
name='imdb__year' name='imdb__year'
if 'n' in query: if 'n' in query:
response['items'] = qs.count() response['data']['items'] = qs.count()
else: else:
#replace normalized items/name sort with actual db value #replace normalized items/name sort with actual db value
order_by = query['s'].split(":") order_by = query['s'].split(":")
@ -256,18 +183,18 @@ def api_find(request):
qs = _order_query(qs, order_by, '') qs = _order_query(qs, order_by, '')
qs = qs[query['i']:query['o']] qs = qs[query['i']:query['o']]
response['items'] = [{'title': i[name], 'items': i[items]} for i in qs] response['data']['items'] = [{'title': i[name], 'items': i[items]} for i in qs]
else: else:
#FIXME: also filter lists here #FIXME: also filter lists here
movies = models.Movie.objects.filter(available=True) movies = models.Movie.objects.filter(available=True)
files = models.File.objects.all() files = models.File.objects.all()
response['items'] = movies.count() response['data']['items'] = movies.count()
response['files'] = files.count() response['data']['files'] = files.count()
r = files.aggregate(Count('size'), Count('pixels'), Count('duration')) r = files.aggregate(Count('size'), Count('pixels'), Count('duration'))
response['pixels'] = r['pixels__count'] response['data']['pixels'] = r['pixels__count']
response['size'] = r['size__count'] response['data']['size'] = r['size__count']
response['duration'] = r['duration__count'] response['data']['duration'] = r['duration__count']
return render_to_json_response(response) return render_to_json_response(response)
def api_getItem(request): def api_getItem(request):
@ -275,98 +202,128 @@ def api_getItem(request):
api('getItem', id) api('getItem', id)
return item with id return item with id
''' '''
response = {'status': 200, 'statusText': 'ok'} response = {'status': {'code': 200, 'text': 'ok'}}
itemId = json.loads(request.POST['data']) itemId = json.loads(request.POST['data'])
item = models.Movie.objects.get(movieId=itemId) item = models.Movie.objects.get(movieId=itemId)
response['item'] = item.json() response['item'] = item.json()
return render_to_json_response(response) return render_to_json_response(response)
@login_required_json
def api_editItem(request): def api_editItem(request):
''' '''
api('editItem', {key: value}) param data
{'status': 500, 'statusText': 'not implemented'} {key: value}
return {'status': {'code': int, 'text': string},
'data': {}}
''' '''
response = {'status': 500, 'statusText': 'not implemented'} response = {'status': {'code': 500, 'text': 'not implemented'}}
return render_to_json_response(response) return render_to_json_response(response)
@login_required_json
def api_removeItem(request): def api_removeItem(request):
''' '''
api('removeItem', itemId) param data
{'status': 500, 'statusText': 'not implemented'} {key: value}
return {'status': {'code': int, 'text': string},
'data': {}}
''' '''
response = {'status': 500, 'statusText': 'not implemented'} response = {'status': {'code': 500, 'text': 'not implemented'}}
return render_to_json_response(response) return render_to_json_response(response)
@login_required_json
def api_addLayer(request): def api_addLayer(request):
''' '''
api('addItem', {key: value}) param data
{'status': 500, 'statusText': 'not implemented'} {key: value}
return {'status': {'code': int, 'text': string},
'data': {}}
''' '''
response = {'status': 500, 'statusText': 'not implemented'} response = {'status': {'code': 500, 'text': 'not implemented'}}
return render_to_json_response(response) return render_to_json_response(response)
@login_required_json
def api_removeLayer(request): def api_removeLayer(request):
''' '''
api('removeItem', layerId) param data
{'status': 500, 'statusText': 'not implemented'} {key: value}
return {'status': {'code': int, 'text': string},
'data': {}}
''' '''
response = {'status': 500, 'statusText': 'not implemented'} response = {'status': {'code': 500, 'text': 'not implemented'}}
return render_to_json_response(response) return render_to_json_response(response)
@login_required_json
def api_editLayer(request): def api_editLayer(request):
''' '''
api('editLayer', {key: value}) param data
{'status': 500, 'statusText': 'not implemented'} {key: value}
return {'status': {'code': int, 'text': string},
'data': {}}
''' '''
response = {'status': 500, 'statusText': 'not implemented'} response = {'status': {'code': 500, 'text': 'not implemented'}}
return render_to_json_response(response) return render_to_json_response(response)
@login_required_json
def api_addListItem(request): def api_addListItem(request):
''' '''
api('addListItem', {key: value}) param data
{'status': 500, 'statusText': 'not implemented'} {key: value}
return {'status': {'code': int, 'text': string},
'data': {}}
''' '''
response = {'status': 500, 'statusText': 'not implemented'} response = {'status': {'code': 500, 'text': 'not implemented'}}
return render_to_json_response(response) return render_to_json_response(response)
@login_required_json
def api_removeListItem(request): def api_removeListItem(request):
''' '''
api('removeListItem', {key: value}) param data
{'status': 500, 'statusText': 'not implemented'} {key: value}
return {'status': {'code': int, 'text': string},
'data': {}}
''' '''
response = {'status': 500, 'statusText': 'not implemented'} response = {'status': {'code': 500, 'text': 'not implemented'}}
return render_to_json_response(response) return render_to_json_response(response)
@login_required_json
def api_addList(request): def api_addList(request):
''' '''
api('addList', {key: value}) param data
{'status': 500, 'statusText': 'not implemented'} {key: value}
return {'status': {'code': int, 'text': string},
'data': {}}
''' '''
response = {'status': 500, 'statusText': 'not implemented'} response = {'status': {'code': 500, 'text': 'not implemented'}}
return render_to_json_response(response) return render_to_json_response(response)
@login_required_json
def api_editList(request): def api_editList(request):
''' '''
api('editList', {key: value}) param data
{'status': 500, 'statusText': 'not implemented'} {key: value}
return {'status': {'code': int, 'text': string},
'data': {}}
''' '''
response = {'status': 500, 'statusText': 'not implemented'} response = {'status': {'code': 500, 'text': 'not implemented'}}
return render_to_json_response(response) return render_to_json_response(response)
def api_removeList(request): def api_removeList(request):
''' '''
api('removeList', {key: value}) param data
{'status': 500, 'statusText': 'not implemented'} {key: value}
return {'status': {'code': int, 'text': string},
'data': {}}
''' '''
response = {'status': 500, 'statusText': 'not implemented'} response = {'status': {'code': 500, 'text': 'not implemented'}}
return render_to_json_response(response) return render_to_json_response(response)
#@login_required_json #@login_required_json
def api_update(request): def api_update(request):
''' '''
params: param data
archive = string {archive: string, files: json}
files = json return {'status': {'code': int, 'text': string},
'data': {info: dict, rename: dict}}
''' '''
print "update request" print "update request"
data = json.loads(request.POST['data']) data = json.loads(request.POST['data'])
@ -398,41 +355,55 @@ def api_update(request):
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': 200, 'statusText': 'ok'} response = {'status': {'code': 200, 'text': 'ok'}, 'data': {}}
response['info'] = needs_data response['data']['info'] = needs_data
response['rename'] = rename response['data']['rename'] = rename
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
''' '''
upload video, timeline or frame upload video, timeline or frame
param data
param file
return {'status': {'code': int, 'text': string},
'data': {}}
''' '''
response = {'status': 500, 'statusText': 'not implemented'} response = {'status': {'code': 500, 'text': 'not implemented'}}
return render_to_json_response(response) return render_to_json_response(response)
@login_required_json
def api_editFile(request): #FIXME: should this be file.files. or part of update def api_editFile(request): #FIXME: should this be file.files. or part of update
''' '''
change file / imdb link change file / imdb link
''' '''
response = {'status': 500, 'statusText': 'not implemented'} response = {'status': {'code': 500, '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
''' '''
api('guess', {path:'Foobar/The Matrix.avi'}) param data
{imdb: ...} {path: string}
return {'status': {'code': int, 'text': string},
data: {imdb: string}}
''' '''
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': 500, 'statusText': 'ok', data: response}
return render_to_json_response(response) return render_to_json_response(response)
def api_guess(request): #guess imdb based on title, director, year def api_getImdbId(request):
''' '''
api('guess', {title:'The Matrix'}) param data
{imdb: ...} {title: string, director: string, year: string}
return {'status': {'code': int, 'text': string},
'data': {imdbId:string }}
''' '''
response = {'status': 500, 'statusText': 'not implemented'} imdbId = oxweb.imdb.guess(search_title, r['director'], timeout=-1)
if imdbId:
response = {'status': {'code': 200, 'text': 'ok'},
'data': {'imdbId': imdbId}}
else:
response = {'status': {'code': 404, 'text': 'not found'}}
return render_to_json_response(response) return render_to_json_response(response)

View file

@ -5,19 +5,26 @@ from django.db import models
from django.contrib.auth.models import User from django.contrib.auth.models import User
class Group(models.Model):
created = models.DateTimeField(auto_now_add=True)
modified = models.DateTimeField(auto_now=True)
name = models.CharField(blank=True, max_length=255, unique=True)
users = models.ManyToManyField(User, related_name='groups')
class Preference(models.Model): class Preference(models.Model):
user = models.ForeignKey(User, related_name='preferences')
created = models.DateTimeField(auto_now_add=True) created = models.DateTimeField(auto_now_add=True)
modified = models.DateTimeField(auto_now=True) modified = models.DateTimeField(auto_now=True)
user = models.ForeignKey(User, related_name='preferences')
key = models.CharField(blank=True, max_length=255) key = models.CharField(blank=True, max_length=255)
value = models.TextField(blank=True) value = models.TextField(blank=True)
def getUserJSON(user):
json = {}
for key in ('username', 'email'):
json[key] = getattr(user, key)
json['preferences'] = getPreferences(user)
return json
def getPreferences(user):
prefs = {}
for p in Preference.objects.filter(user=user):
prefs[key] = json.loads(p.value)
return prefs
def getPreference(user, key, value=None): def getPreference(user, key, value=None):
q = Preference.objects.filter(user=user, key=key) q = Preference.objects.filter(user=user, key=key)
if q.count() > 0: if q.count() > 0:

View file

@ -2,64 +2,106 @@
# vi:si:et:sw=4:sts=4:ts=4 # vi:si:et:sw=4:sts=4:ts=4
from django.contrib.auth.models import User from django.contrib.auth.models import User
from django.contrib.auth import authenticate, login, logout
from django.shortcuts import render_to_response, get_object_or_404, get_list_or_404 from django.shortcuts import render_to_response, get_object_or_404, get_list_or_404
from django.template import RequestContext from django.template import RequestContext
from django.utils import simplejson as json from django.utils import simplejson as json
from django import forms
from oxdjango.shortcuts import render_to_json_response from oxdjango.shortcuts import render_to_json_response
from oxdjango.decorators import login_required_json from oxdjango.decorators import login_required_json
import models import models
class LoginForm(forms.Form):
username = forms.TextInput()
password = forms.TextInput()
def api_login(request): def api_login(request):
''' '''
data: {'username': username, 'password': password} param data
{'username': username, 'password': password}
username/password to login return {'status': {'code': int, 'text': string}}
''' '''
response = {'status': 403, 'statusText': 'login failed'} response = {'status': {'code': 403, 'text': 'login failed'}}
data = json.loads(request.POST['data']) data = json.loads(request.POST['data'])
form = LoginForm(data, request.FILES)
from django.contrib.auth import authenticate, login if form.is_valid():
user = authenticate(username=data['username'], password=data['password']) user = authenticate(username=data['username'], password=data['password'])
if user is not None: if user is not None:
if user.is_active: if user.is_active:
user_json = {} #FIXME: preferences etc should be in here
login(request, user) login(request, user)
response = {'status': 200, 'message': 'You are logged in.', 'user': user_json} user_json = models.getUserJSON(user)
response = {'status': {'code': 200, 'message': 'You are logged in.', 'user': user_json}}
else: else:
response = {'status': 403, 'statusText': 'Your account is disabled.'} response = {'status': {'code': 403, 'text': 'Your account is disabled.'}}
print "Your account has been disabled!" print "Your account has been disabled!"
else: else:
response = {'status': 403, 'statusText': 'Your username and password were incorrect.'} response = {'status': {'code': 403, 'text': 'Your username and password were incorrect.'}}
else:
response = {'status': {'code':422, 'text': 'invalid data'}}
return render_to_json_response(response) return render_to_json_response(response)
def api_logout(request): def api_logout(request):
''' '''
loggs out currenly logged in user param data
{}
return {'status': {'code': int, 'text': string}}
''' '''
response = {'status': 200, 'statusText': 'logged out'} response = {'status': {'code': 200, 'text': 'logged out'}}
if request.user.is_authenticated(): if request.user.is_authenticated():
request.user.logout() logout(request)
return render_to_json_response(response) return render_to_json_response(response)
class RegisterForm(forms.Form):
username = forms.TextInput()
password = forms.TextInput()
email = forms.TextInput()
def api_register(request): def api_register(request):
''' '''
data: {'username': username, 'password': password, 'email': email} param data
{'username': username, 'password': password, 'email': email}
username return {'status': {'code': int, 'text': string}}
password
email
''' '''
data = json.loads(request.POST['data']) data = json.loads(request.POST['data'])
response = {'status': 422, 'statusText': 'username exists'} form = RegisterForm(data, request.FILES)
if form.is_valid():
if models.User.objects.filter(username=form.data['username']).count() > 0:
response = {'status': 422, 'statusText': 'username or email exists'}
elif models.User.objects.filter(email=form.data['email']).count() > 0:
response = {'status': {'code':422, 'text': 'username or email exists'}}
else:
user = models.User(username=form.data['username'], email=form.data['email'])
user.set_password(form.data['password'])
user = authenticate(username=form.data['username'],
password=form.data['password'])
login(request, user)
response = {'status': {'code':200, 'text': 'account created'}}
else:
response = {'status': {'code':422, 'text': 'username exists'}}
response = {'status': {'code':420, 'text': 'username or email exists'}}
return render_to_json_response(response) return render_to_json_response(response)
def api_recover(request): class RecoverForm(forms.Form):
data = json.loads(request.POST['data']) username_or_email = forms.TextInput()
username = data['username'] def api_recover(request):
'''
param data
{'username_or_email': username}
return {'status': {'code': int, 'text': string}}
'''
data = json.loads(request.POST['data'])
form = RegisterForm(data, request.FILES)
if form.is_valid():
username = data['username_or_email']
user = None user = None
q = models.User.objects.filter(username=username) q = models.User.objects.filter(username=username)
if q.count() > 0: if q.count() > 0:
@ -70,24 +112,27 @@ def api_recover(request):
user = q[0] user = q[0]
if user: if user:
#user.sendmail(...) #FIXME: send recovery mail #user.sendmail(...) #FIXME: send recovery mail
response = {'status': 200, 'statusText': 'recover email sent.'} response = {'status': {'code': 200, 'text': 'recover email sent.'}}
else: else:
response = {'status': 404, 'statusText': 'user or email not found.'} response = {'status': {'code': 404, 'text': 'user or email not found.'}}
else:
response = {'status': {'code':422, 'text': 'username exists'}}
return render_to_json_response(response) return render_to_json_response(response)
@login_required_json @login_required_json
def api_preferences(request): def api_preferences(request):
''' '''
function: preferences param data
string
api('preferences') list
return all preferences dict
api('preferences', 'key1') return
return preference key1 if data is string:
api('preferences', [key1, key2]) return preference with name
return preference key1, key2 if data is list:
api('preferences', {key: value}) return preferences with names
set preference key to value if data is dict:
set key values in dict as preferences
''' '''
response = {'status': 200, 'statusText': 'ok'} response = {'status': 200, 'statusText': 'ok'}
if 'data' not in request.POST: if 'data' not in request.POST:

View file

@ -30,6 +30,7 @@ DATABASE_PORT = '' # Set to empty string for default. Not used with
# If running in a Windows environment this must be set to the same as your # If running in a Windows environment this must be set to the same as your
# system time zone. # system time zone.
TIME_ZONE = 'Europe/Berlin' TIME_ZONE = 'Europe/Berlin'
TIME_ZONE = 'Asia/Kolkata'
# Language code for this installation. All choices can be found here: # Language code for this installation. All choices can be found here:
# http://www.i18nguy.com/unicode/language-identifiers.html # http://www.i18nguy.com/unicode/language-identifiers.html
@ -85,9 +86,12 @@ INSTALLED_APPS = (
'django.contrib.sites', 'django.contrib.sites',
'django.contrib.admin', 'django.contrib.admin',
'django.contrib.humanize', 'django.contrib.humanize',
'django_extensions',
# 'south', # 'south',
'backend', 'backend',
'oxuser',
) )
#rabbitmq connection settings #rabbitmq connection settings