use ox.django.api instead of local copy

This commit is contained in:
j 2012-01-04 01:48:47 +05:30
parent 1c408c78e5
commit e1989eed57
25 changed files with 34 additions and 222 deletions

View file

@ -11,7 +11,7 @@ from ox.django.shortcuts import render_to_json_response, get_object_or_404_json,
from item.models import Item from item.models import Item
from api.actions import actions from ox.django.api import actions
from item import utils from item import utils
from item.models import Item from item.models import Item

View file

@ -1,117 +0,0 @@
# -*- coding: utf-8 -*-
# vi:si:et:sw=4:sts=4:ts=4
import sys
import inspect
from django.conf import settings
from ox.django.shortcuts import render_to_json_response, json_response
from ox.utils import json
def autodiscover():
#register api actions from all installed apps
from django.utils.importlib import import_module
from django.utils.module_loading import module_has_submodule
for app in settings.INSTALLED_APPS:
if app != 'api':
mod = import_module(app)
try:
import_module('%s.views'%app)
except:
if module_has_submodule(mod, 'views'):
raise
def trim(docstring):
if not docstring:
return ''
# Convert tabs to spaces (following the normal Python rules)
# and split into a list of lines:
lines = docstring.expandtabs().splitlines()
# Determine minimum indentation (first line doesn't count):
indent = sys.maxint
for line in lines[1:]:
stripped = line.lstrip()
if stripped:
indent = min(indent, len(line) - len(stripped))
# Remove indentation (first line is special):
trimmed = [lines[0].strip()]
if indent < sys.maxint:
for line in lines[1:]:
trimmed.append(line[indent:].rstrip())
# Strip off trailing and leading blank lines:
while trimmed and not trimmed[-1]:
trimmed.pop()
while trimmed and not trimmed[0]:
trimmed.pop(0)
# Return a single string:
return '\n'.join(trimmed)
class ApiActions(dict):
properties = {}
def __init__(self):
def api(request):
'''
returns list of all known api actions
param data {
docs: bool
}
if docs is true, action properties contain docstrings
return {
status: {'code': int, 'text': string},
data: {
actions: {
'api': {
cache: true,
doc: 'recursion'
},
'hello': {
cache: true,
..
}
...
}
}
}
'''
data = json.loads(request.POST.get('data', '{}'))
docs = data.get('docs', False)
code = data.get('code', False)
_actions = self.keys()
_actions.sort()
actions = {}
for a in _actions:
actions[a] = self.properties[a]
if docs:
actions[a]['doc'] = self.doc(a)
if code:
actions[a]['code'] = self.code(a)
response = json_response({'actions': actions})
return render_to_json_response(response)
self.register(api)
def doc(self, f):
return trim(self[f].__doc__)
def code(self, name):
f = self[name]
if name != 'api' and hasattr(f, 'func_closure') and f.func_closure:
f = f.func_closure[0].cell_contents
info = f.func_code.co_filename[len(settings.PROJECT_ROOT)+1:]
info = u'%s:%s' % (info, f.func_code.co_firstlineno)
return info, trim(inspect.getsource(f))
def register(self, method, action=None, cache=True):
if not action:
action = method.func_name
self[action] = method
self.properties[action] = {'cache': cache}
def unregister(self, action):
if action in self:
del self[action]
actions = ApiActions()

View file

@ -1,3 +0,0 @@
# -*- coding: utf-8 -*-
# vi:si:et:sw=4:sts=4:ts=4

View file

@ -1,10 +0,0 @@
# -*- coding: utf-8 -*-
# vi:si:et:sw=4:sts=4:ts=4
from django.conf.urls.defaults import *
urlpatterns = patterns("api.views",
(r'^$', 'api'),
)

View file

@ -1,71 +0,0 @@
# -*- coding: utf-8 -*-
# vi:si:et:sw=4:sts=4:ts=4
from __future__ import division, with_statement
import os
import copy
from django.shortcuts import render_to_response
from django.template import RequestContext
from django.conf import settings
from django.db.models import Max, Sum
from ox.django.shortcuts import render_to_json_response, json_response
from ox.utils import json
from user.models import init_user
from actions import actions
def api(request):
if request.META['REQUEST_METHOD'] == "OPTIONS":
response = render_to_json_response({'status': {'code': 200,
'text': 'use POST'}})
response['Access-Control-Allow-Origin'] = '*'
return response
if not 'action' in request.POST:
methods = actions.keys()
api = []
for f in sorted(methods):
api.append({'name': f,
'doc': actions.doc(f).replace('\n', '<br>\n')})
context = RequestContext(request, {'api': api,
'sitename': settings.SITENAME})
return render_to_response('api.html', context)
function = request.POST['action']
#FIXME: possible to do this in f
#data = json.loads(request.POST['data'])
f = actions.get(function)
if f:
response = f(request)
else:
response = render_to_json_response(json_response(status=400,
text='Unknown function %s' % function))
response['Access-Control-Allow-Origin'] = '*'
return response
def init(request):
'''
return {'status': {'code': int, 'text': string},
'data': {user: object}}
'''
response = json_response({})
config = copy.deepcopy(settings.CONFIG)
del config['keys'] #is this needed?
response['data']['site'] = config
response['data']['user'] = init_user(request.user, request)
return render_to_json_response(response)
actions.register(init)
def error(request):
'''
this action is used to test api error codes, it should return a 503 error
'''
success = error_is_success
return render_to_json_response({})
actions.register(error)

View file

@ -5,6 +5,8 @@ try:
except: except:
import elementtree.ElementTree as ET import elementtree.ElementTree as ET
import copy
from django.shortcuts import render_to_response, redirect from django.shortcuts import render_to_response, redirect
from django.template import RequestContext from django.template import RequestContext
from django.conf import settings from django.conf import settings
@ -17,8 +19,9 @@ from ox.utils import json
import models import models
from api.actions import actions from user.models import init_user
from ox.django.api import actions
def intro(request): def intro(request):
context = RequestContext(request, {'settings': settings}) context = RequestContext(request, {'settings': settings})
@ -129,3 +132,16 @@ def redirect_url(request, url):
else: else:
return HttpResponse('<script>document.location.href=%s;</script>'%json.dumps(url)) return HttpResponse('<script>document.location.href=%s;</script>'%json.dumps(url))
def init(request):
'''
return {'status': {'code': int, 'text': string},
'data': {user: object}}
'''
response = json_response({})
config = copy.deepcopy(settings.CONFIG)
del config['keys']
response['data']['site'] = config
response['data']['user'] = init_user(request.user, request)
return render_to_json_response(response)
actions.register(init)

View file

@ -19,7 +19,7 @@ from item import utils
from item.models import get_item, Item from item.models import get_item, Item
from item.views import parse_query from item.views import parse_query
import item.tasks import item.tasks
from api.actions import actions from ox.django.api import actions
import models import models
import tasks import tasks

View file

@ -6,7 +6,7 @@ from django.conf import settings
from ox.utils import json from ox.utils import json
from ox.django.shortcuts import render_to_json_response, json_response from ox.django.shortcuts import render_to_json_response, json_response
from api.actions import actions from ox.django.api import actions
from annotation.models import Annotation from annotation.models import Annotation
from item.models import Item from item.models import Item

View file

@ -6,7 +6,7 @@ from ox.utils import json
from ox.django.decorators import login_required_json from ox.django.decorators import login_required_json
from ox.django.shortcuts import render_to_json_response, get_object_or_404_json, json_response from ox.django.shortcuts import render_to_json_response, get_object_or_404_json, json_response
from api.actions import actions from ox.django.api import actions
import models import models

View file

@ -7,7 +7,7 @@ from ox.utils import json
from ox.django.decorators import login_required_json from ox.django.decorators import login_required_json
from ox.django.shortcuts import render_to_json_response, get_object_or_404_json, json_response from ox.django.shortcuts import render_to_json_response, get_object_or_404_json, json_response
from api.actions import actions from ox.django.api import actions
from item import utils from item import utils
import models import models

View file

@ -28,7 +28,7 @@ from archive.models import File, Stream
from archive import extract from archive import extract
from clip.models import Clip from clip.models import Clip
from api.actions import actions from ox.django.api import actions
def _order_query(qs, sort, prefix='sort__'): def _order_query(qs, sort, prefix='sort__'):

View file

@ -14,7 +14,7 @@ from ox.django.http import HttpFileResponse
import models import models
from api.actions import actions from ox.django.api import actions
from item import utils from item import utils
from item.models import Item from item.models import Item

View file

@ -8,7 +8,7 @@ from ox.utils import json
from ox.django.decorators import login_required_json, admin_required_json from ox.django.decorators import login_required_json, admin_required_json
from ox.django.shortcuts import render_to_json_response, get_object_or_404_json, json_response from ox.django.shortcuts import render_to_json_response, get_object_or_404_json, json_response
from api.actions import actions from ox.django.api import actions
from item import utils from item import utils

View file

@ -11,7 +11,7 @@ from ox.django.shortcuts import render_to_json_response, get_object_or_404_json,
from item.models import Item from item.models import Item
from api.actions import actions from ox.django.api import actions
from item import utils from item import utils
from item.models import Item from item.models import Item

View file

@ -10,7 +10,7 @@ from ox.utils import json
from ox.django.decorators import login_required_json, admin_required_json from ox.django.decorators import login_required_json, admin_required_json
from ox.django.shortcuts import render_to_json_response, get_object_or_404_json, json_response from ox.django.shortcuts import render_to_json_response, get_object_or_404_json, json_response
from api.actions import actions from ox.django.api import actions
from item import utils from item import utils
import models import models

View file

@ -10,7 +10,7 @@ from ox.utils import json
from ox.django.decorators import login_required_json from ox.django.decorators import login_required_json
from ox.django.shortcuts import render_to_json_response, get_object_or_404_json, json_response from ox.django.shortcuts import render_to_json_response, get_object_or_404_json, json_response
from api.actions import actions from ox.django.api import actions
from item import utils from item import utils
import models import models

View file

@ -108,7 +108,6 @@ INSTALLED_APPS = (
'edit', 'edit',
'news', 'news',
'user', 'user',
'api',
'urlalias', 'urlalias',
'tv', 'tv',
) )

View file

@ -8,7 +8,7 @@ from ox.django.decorators import login_required_json
from ox.django.shortcuts import render_to_json_response, get_object_or_404_json, json_response from ox.django.shortcuts import render_to_json_response, get_object_or_404_json, json_response
import models import models
from api.actions import actions from ox.django.api import actions
def getText(request): def getText(request):

View file

@ -11,7 +11,7 @@ from ox.utils import json
from ox.django.decorators import admin_required_json from ox.django.decorators import admin_required_json
from ox.django.shortcuts import render_to_json_response, get_object_or_404_json, json_response from ox.django.shortcuts import render_to_json_response, get_object_or_404_json, json_response
from api.actions import actions from ox.django.api import actions
from item import utils from item import utils
import models import models

View file

@ -7,7 +7,7 @@ from ox.utils import json
from ox.django.shortcuts import render_to_json_response, json_response from ox.django.shortcuts import render_to_json_response, json_response
from itemlist.views import get_list_or_404_json from itemlist.views import get_list_or_404_json
from api.actions import actions from ox.django.api import actions
def tv(request): def tv(request):
data = json.loads(request.POST['data']) data = json.loads(request.POST['data'])

View file

@ -13,8 +13,7 @@ admin.autodiscover()
import monkey_patch.models import monkey_patch.models
from api import actions import ox.django.api.urls
actions.autodiscover()
def serve_static_file(path, location, content_type): def serve_static_file(path, location, content_type):
return HttpFileResponse(location, content_type=content_type) return HttpFileResponse(location, content_type=content_type)
@ -24,8 +23,7 @@ urlpatterns = patterns('',
(r'^api/upload/$', 'archive.views.firefogg_upload'), (r'^api/upload/$', 'archive.views.firefogg_upload'),
(r'^url=(?P<url>.*)$', 'app.views.redirect_url'), (r'^url=(?P<url>.*)$', 'app.views.redirect_url'),
(r'^file/(?P<oshash>.*)$', 'archive.views.lookup_file'), (r'^file/(?P<oshash>.*)$', 'archive.views.lookup_file'),
(r'^api$', include('api.urls')), (r'^api/?$', include(ox.django.api.urls)),
(r'^api/$', include('api.urls')),
(r'^resetUI$', 'user.views.reset_ui'), (r'^resetUI$', 'user.views.reset_ui'),
(r'^list/(?P<id>.*?)/icon(?P<size>\d*).jpg$', 'itemlist.views.icon'), (r'^list/(?P<id>.*?)/icon(?P<size>\d*).jpg$', 'itemlist.views.icon'),
(r'^robots.txt$', serve_static_file, {'location': os.path.join(settings.STATIC_ROOT, 'robots.txt'), 'content_type': 'text/plain'}), (r'^robots.txt$', serve_static_file, {'location': os.path.join(settings.STATIC_ROOT, 'robots.txt'), 'content_type': 'text/plain'}),

View file

@ -17,7 +17,7 @@ from ox.django.decorators import admin_required_json, login_required_json
import ox import ox
from api.actions import actions from ox.django.api import actions
from item.models import Access, Item from item.models import Access, Item
from item import utils from item import utils