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 oxuser.views import api_login, api_logout, api_register, api_recover, api_preferences
from oxuser.models import getUserJSON
def api(request):
@ -40,116 +41,23 @@ def api(request):
if f:
response = f(request)
else:
response = {'status': 404, 'statusText': 'Unknown function %s' % function}
response = render_to_json_response(response)
response = render_to_json_response(
{'status': {'code': 404, 'text': 'Unknown function %s' % function}})
return response
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.user = request.user.json()
response = {'status': {'code': 200, 'text': 'ok'}, 'data': {}}
if request.user.is_authenticated():
response['data']['user'] = getUserJSON(request.user)
else:
response['data']['user'] = {'name': 'Guest'}
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__'):
order_by = []
for e in s.split(','):
@ -196,17 +104,36 @@ def _parse_query(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
sort: comma seperated list of field:order, default: director:asc,year:desc
range: result ragne, from:to or from
s: comma seperated list of field:order, default: director:asc,year:desc
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)
response = {}
response = {'status': {'code': 200, 'text':'ok'}, 'data':{}}
if 'p' in query:
response['items'] = []
response['data']['items'] = []
qs = _order_query(query['q'], query['s'])
if 'n' in query:
response = {'items': qs.count()}
@ -221,13 +148,13 @@ def api_find(request):
return r
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:
if query['s'].split(':')[0] not in ('name', 'items'):
query['s'] = 'name'
#FIXME: also filter lists here
response['items'] = []
response['data']['items'] = []
name = 'name'
items = 'movies'
movie_qs = query['q']
@ -243,7 +170,7 @@ def api_find(request):
qs = movie_qs.values('imdb__year').annotate(movies=Count('id'))
name='imdb__year'
if 'n' in query:
response['items'] = qs.count()
response['data']['items'] = qs.count()
else:
#replace normalized items/name sort with actual db value
order_by = query['s'].split(":")
@ -256,18 +183,18 @@ def api_find(request):
qs = _order_query(qs, order_by, '')
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:
#FIXME: also filter lists here
movies = models.Movie.objects.filter(available=True)
files = models.File.objects.all()
response['items'] = movies.count()
response['files'] = files.count()
response['data']['items'] = movies.count()
response['data']['files'] = files.count()
r = files.aggregate(Count('size'), Count('pixels'), Count('duration'))
response['pixels'] = r['pixels__count']
response['size'] = r['size__count']
response['duration'] = r['duration__count']
response['data']['pixels'] = r['pixels__count']
response['data']['size'] = r['size__count']
response['data']['duration'] = r['duration__count']
return render_to_json_response(response)
def api_getItem(request):
@ -275,98 +202,128 @@ def api_getItem(request):
api('getItem', id)
return item with id
'''
response = {'status': 200, 'statusText': 'ok'}
response = {'status': {'code': 200, 'text': 'ok'}}
itemId = json.loads(request.POST['data'])
item = models.Movie.objects.get(movieId=itemId)
response['item'] = item.json()
return render_to_json_response(response)
@login_required_json
def api_editItem(request):
'''
api('editItem', {key: value})
{'status': 500, 'statusText': 'not implemented'}
param data
{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)
@login_required_json
def api_removeItem(request):
'''
api('removeItem', itemId)
{'status': 500, 'statusText': 'not implemented'}
param data
{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)
@login_required_json
def api_addLayer(request):
'''
api('addItem', {key: value})
{'status': 500, 'statusText': 'not implemented'}
param data
{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)
@login_required_json
def api_removeLayer(request):
'''
api('removeItem', layerId)
{'status': 500, 'statusText': 'not implemented'}
param data
{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)
@login_required_json
def api_editLayer(request):
'''
api('editLayer', {key: value})
{'status': 500, 'statusText': 'not implemented'}
param data
{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)
@login_required_json
def api_addListItem(request):
'''
api('addListItem', {key: value})
{'status': 500, 'statusText': 'not implemented'}
param data
{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)
@login_required_json
def api_removeListItem(request):
'''
api('removeListItem', {key: value})
{'status': 500, 'statusText': 'not implemented'}
param data
{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)
@login_required_json
def api_addList(request):
'''
api('addList', {key: value})
{'status': 500, 'statusText': 'not implemented'}
param data
{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)
@login_required_json
def api_editList(request):
'''
api('editList', {key: value})
{'status': 500, 'statusText': 'not implemented'}
param data
{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)
def api_removeList(request):
'''
api('removeList', {key: value})
{'status': 500, 'statusText': 'not implemented'}
param data
{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)
#@login_required_json
def api_update(request):
'''
params:
archive = string
files = json
param data
{archive: string, files: json}
return {'status': {'code': int, 'text': string},
'data': {info: dict, rename: dict}}
'''
print "update request"
data = json.loads(request.POST['data'])
@ -398,41 +355,55 @@ def api_update(request):
rename[oshash] = f.file.path
print "processed files for", archive.name
#remove all files not in files.keys() from ArchiveFile
response = {'status': 200, 'statusText': 'ok'}
response['info'] = needs_data
response['rename'] = rename
response = {'status': {'code': 200, 'text': 'ok'}, 'data': {}}
response['data']['info'] = needs_data
response['data']['rename'] = rename
return render_to_json_response(response)
def api_upload(request): #video, timeline, 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)
@login_required_json
def api_editFile(request): #FIXME: should this be file.files. or part of update
'''
change file / imdb link
'''
response = {'status': 500, 'statusText': 'not implemented'}
response = {'status': {'code': 500, 'text': 'not implemented'}}
return render_to_json_response(response)
def api_parse(request): #parse path and return info
'''
api('guess', {path:'Foobar/The Matrix.avi'})
{imdb: ...}
param data
{path: string}
return {'status': {'code': int, 'text': string},
data: {imdb: string}}
'''
path = json.loads(request.POST['data'])['path']
response = utils.parsePath(path)
response = {'status': 500, 'statusText': 'ok', data: 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'})
{imdb: ...}
param data
{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)

View file

@ -5,19 +5,26 @@ from django.db import models
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):
user = models.ForeignKey(User, related_name='preferences')
created = models.DateTimeField(auto_now_add=True)
modified = models.DateTimeField(auto_now=True)
user = models.ForeignKey(User, related_name='preferences')
key = models.CharField(blank=True, max_length=255)
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):
q = Preference.objects.filter(user=user, key=key)
if q.count() > 0:

View file

@ -2,64 +2,106 @@
# vi:si:et:sw=4:sts=4:ts=4
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.template import RequestContext
from django.utils import simplejson as json
from django import forms
from oxdjango.shortcuts import render_to_json_response
from oxdjango.decorators import login_required_json
import models
class LoginForm(forms.Form):
username = forms.TextInput()
password = forms.TextInput()
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'])
from django.contrib.auth import authenticate, login
form = LoginForm(data, request.FILES)
if form.is_valid():
user = authenticate(username=data['username'], password=data['password'])
if user is not None:
if user.is_active:
user_json = {} #FIXME: preferences etc should be in here
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:
response = {'status': 403, 'statusText': 'Your account is disabled.'}
response = {'status': {'code': 403, 'text': 'Your account is disabled.'}}
print "Your account has been disabled!"
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)
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():
request.user.logout()
logout(request)
return render_to_json_response(response)
class RegisterForm(forms.Form):
username = forms.TextInput()
password = forms.TextInput()
email = forms.TextInput()
def api_register(request):
'''
data: {'username': username, 'password': password, 'email': email}
param data
{'username': username, 'password': password, 'email': email}
username
password
email
return {'status': {'code': int, 'text': string}}
'''
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)
def api_recover(request):
data = json.loads(request.POST['data'])
class RecoverForm(forms.Form):
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
q = models.User.objects.filter(username=username)
if q.count() > 0:
@ -70,24 +112,27 @@ def api_recover(request):
user = q[0]
if user:
#user.sendmail(...) #FIXME: send recovery mail
response = {'status': 200, 'statusText': 'recover email sent.'}
response = {'status': {'code': 200, 'text': 'recover email sent.'}}
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)
@login_required_json
def api_preferences(request):
'''
function: preferences
api('preferences')
return all preferences
api('preferences', 'key1')
return preference key1
api('preferences', [key1, key2])
return preference key1, key2
api('preferences', {key: value})
set preference key to value
param data
string
list
dict
return
if data is string:
return preference with name
if data is list:
return preferences with names
if data is dict:
set key values in dict as preferences
'''
response = {'status': 200, 'statusText': 'ok'}
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
# system time zone.
TIME_ZONE = 'Europe/Berlin'
TIME_ZONE = 'Asia/Kolkata'
# Language code for this installation. All choices can be found here:
# http://www.i18nguy.com/unicode/language-identifiers.html
@ -85,9 +86,12 @@ INSTALLED_APPS = (
'django.contrib.sites',
'django.contrib.admin',
'django.contrib.humanize',
'django_extensions',
# 'south',
'backend',
'oxuser',
)
#rabbitmq connection settings