forked from 0x2620/pandora
api, api, api, today its all about the api
This commit is contained in:
parent
c3b589b90c
commit
eb05595e2c
4 changed files with 289 additions and 262 deletions
|
@ -26,11 +26,12 @@ 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):
|
||||||
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']
|
||||||
#FIXME: possible to do this in f
|
#FIXME: possible to do this in f
|
||||||
#data = json.loads(request.POST['data'])
|
#data = json.loads(request.POST['data'])
|
||||||
|
@ -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,99 +202,129 @@ 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'])
|
||||||
archive = data['archive']
|
archive = data['archive']
|
||||||
|
@ -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
|
||||||
response = {'status': 500, 'statusText': 'not implemented'}
|
param file
|
||||||
|
return {'status': {'code': int, 'text': string},
|
||||||
|
'data': {}}
|
||||||
|
'''
|
||||||
|
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},
|
||||||
path = json.loads(request.POST['data'])['path']
|
data: {imdb: string}}
|
||||||
|
'''
|
||||||
|
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},
|
||||||
response = {'status': 500, 'statusText': 'not implemented'}
|
'data': {imdbId:string }}
|
||||||
|
'''
|
||||||
|
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)
|
||||||
|
|
||||||
|
|
||||||
|
@ -615,41 +586,41 @@ def add_video(request, oshash):
|
||||||
|
|
||||||
|
|
||||||
def apidoc(request):
|
def apidoc(request):
|
||||||
'''
|
'''
|
||||||
this is used for online documentation at http://127.0.0.1:8000/api/
|
this is used for online documentation at http://127.0.0.1:8000/api/
|
||||||
'''
|
'''
|
||||||
import sys
|
import sys
|
||||||
def trim(docstring):
|
def trim(docstring):
|
||||||
if not docstring:
|
if not docstring:
|
||||||
return ''
|
return ''
|
||||||
# Convert tabs to spaces (following the normal Python rules)
|
# Convert tabs to spaces (following the normal Python rules)
|
||||||
# and split into a list of lines:
|
# and split into a list of lines:
|
||||||
lines = docstring.expandtabs().splitlines()
|
lines = docstring.expandtabs().splitlines()
|
||||||
# Determine minimum indentation (first line doesn't count):
|
# Determine minimum indentation (first line doesn't count):
|
||||||
indent = sys.maxint
|
indent = sys.maxint
|
||||||
for line in lines[1:]:
|
for line in lines[1:]:
|
||||||
stripped = line.lstrip()
|
stripped = line.lstrip()
|
||||||
if stripped:
|
if stripped:
|
||||||
indent = min(indent, len(line) - len(stripped))
|
indent = min(indent, len(line) - len(stripped))
|
||||||
# Remove indentation (first line is special):
|
# Remove indentation (first line is special):
|
||||||
trimmed = [lines[0].strip()]
|
trimmed = [lines[0].strip()]
|
||||||
if indent < sys.maxint:
|
if indent < sys.maxint:
|
||||||
for line in lines[1:]:
|
for line in lines[1:]:
|
||||||
trimmed.append(line[indent:].rstrip())
|
trimmed.append(line[indent:].rstrip())
|
||||||
# Strip off trailing and leading blank lines:
|
# Strip off trailing and leading blank lines:
|
||||||
while trimmed and not trimmed[-1]:
|
while trimmed and not trimmed[-1]:
|
||||||
trimmed.pop()
|
trimmed.pop()
|
||||||
while trimmed and not trimmed[0]:
|
while trimmed and not trimmed[0]:
|
||||||
trimmed.pop(0)
|
trimmed.pop(0)
|
||||||
# Return a single string:
|
# Return a single string:
|
||||||
return '\n'.join(trimmed)
|
return '\n'.join(trimmed)
|
||||||
|
|
||||||
functions = filter(lambda x: x.startswith('api_'), globals().keys())
|
functions = filter(lambda x: x.startswith('api_'), globals().keys())
|
||||||
api = []
|
api = []
|
||||||
for f in sorted(functions):
|
for f in sorted(functions):
|
||||||
api.append({
|
api.append({
|
||||||
'name': f[4:],
|
'name': f[4:],
|
||||||
'doc': trim(globals()[f].__doc__).replace('\n', '<br>\n')
|
'doc': trim(globals()[f].__doc__).replace('\n', '<br>\n')
|
||||||
})
|
})
|
||||||
context = RequestContext(request, {'api': api})
|
context = RequestContext(request, {'api': api})
|
||||||
return render_to_response('api.html', context)
|
return render_to_response('api.html', context)
|
||||||
|
|
|
@ -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:
|
||||||
|
|
|
@ -2,92 +2,137 @@
|
||||||
# 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)
|
user_json = models.getUserJSON(user)
|
||||||
response = {'status': 200, 'message': 'You are logged in.', 'user': user_json}
|
response = {'status': {'code': 200, 'message': 'You are logged in.', 'user': user_json}}
|
||||||
|
else:
|
||||||
|
response = {'status': {'code': 403, 'text': 'Your account is disabled.'}}
|
||||||
|
print "Your account has been disabled!"
|
||||||
else:
|
else:
|
||||||
response = {'status': 403, 'statusText': 'Your account is disabled.'}
|
response = {'status': {'code': 403, 'text': 'Your username and password were incorrect.'}}
|
||||||
print "Your account has been disabled!"
|
|
||||||
else:
|
else:
|
||||||
response = {'status': 403, 'statusText': 'Your username and password were incorrect.'}
|
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):
|
||||||
user = None
|
'''
|
||||||
q = models.User.objects.filter(username=username)
|
param data
|
||||||
if q.count() > 0:
|
{'username_or_email': username}
|
||||||
user = q[0]
|
|
||||||
else:
|
return {'status': {'code': int, 'text': string}}
|
||||||
q = models.User.objects.filter(email=username)
|
'''
|
||||||
|
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:
|
if q.count() > 0:
|
||||||
user = q[0]
|
user = q[0]
|
||||||
if user:
|
else:
|
||||||
#user.sendmail(...) #FIXME: send recovery mail
|
q = models.User.objects.filter(email=username)
|
||||||
response = {'status': 200, 'statusText': 'recover email sent.'}
|
if q.count() > 0:
|
||||||
|
user = q[0]
|
||||||
|
if user:
|
||||||
|
#user.sendmail(...) #FIXME: send recovery mail
|
||||||
|
response = {'status': {'code': 200, 'text': 'recover email sent.'}}
|
||||||
|
else:
|
||||||
|
response = {'status': {'code': 404, 'text': 'user or email not found.'}}
|
||||||
else:
|
else:
|
||||||
response = {'status': 404, 'statusText': 'user or email not found.'}
|
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:
|
||||||
|
|
|
@ -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
|
||||||
|
|
Loading…
Reference in a new issue