From e1989eed577d84a1070749d926b780dd31de9939 Mon Sep 17 00:00:00 2001 From: j <0x006A@0x2620.org> Date: Wed, 4 Jan 2012 01:48:47 +0530 Subject: [PATCH] use ox.django.api instead of local copy --- pandora/annotation/views.py | 2 +- pandora/api/__init__.py | 0 pandora/api/actions.py | 117 -------------------- pandora/api/management/__init__.py | 0 pandora/api/management/commands/__init__.py | 0 pandora/api/models.py | 3 - pandora/api/urls.py | 10 -- pandora/api/views.py | 71 ------------ pandora/app/views.py | 18 ++- pandora/archive/views.py | 2 +- pandora/clip/views.py | 2 +- pandora/edit/views.py | 2 +- pandora/event/views.py | 2 +- pandora/item/views.py | 2 +- pandora/itemlist/views.py | 2 +- pandora/log/views.py | 2 +- pandora/news/views.py | 2 +- pandora/person/views.py | 2 +- pandora/place/views.py | 2 +- pandora/settings.py | 1 - pandora/text/views.py | 2 +- pandora/title/views.py | 2 +- pandora/tv/views.py | 2 +- pandora/urls.py | 6 +- pandora/user/views.py | 2 +- 25 files changed, 34 insertions(+), 222 deletions(-) delete mode 100644 pandora/api/__init__.py delete mode 100644 pandora/api/actions.py delete mode 100644 pandora/api/management/__init__.py delete mode 100644 pandora/api/management/commands/__init__.py delete mode 100644 pandora/api/models.py delete mode 100644 pandora/api/urls.py delete mode 100644 pandora/api/views.py diff --git a/pandora/annotation/views.py b/pandora/annotation/views.py index 9074aefe..e45141f6 100644 --- a/pandora/annotation/views.py +++ b/pandora/annotation/views.py @@ -11,7 +11,7 @@ from ox.django.shortcuts import render_to_json_response, get_object_or_404_json, from item.models import Item -from api.actions import actions +from ox.django.api import actions from item import utils from item.models import Item diff --git a/pandora/api/__init__.py b/pandora/api/__init__.py deleted file mode 100644 index e69de29b..00000000 diff --git a/pandora/api/actions.py b/pandora/api/actions.py deleted file mode 100644 index f80a29f6..00000000 --- a/pandora/api/actions.py +++ /dev/null @@ -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() - diff --git a/pandora/api/management/__init__.py b/pandora/api/management/__init__.py deleted file mode 100644 index e69de29b..00000000 diff --git a/pandora/api/management/commands/__init__.py b/pandora/api/management/commands/__init__.py deleted file mode 100644 index e69de29b..00000000 diff --git a/pandora/api/models.py b/pandora/api/models.py deleted file mode 100644 index 04d0a3ce..00000000 --- a/pandora/api/models.py +++ /dev/null @@ -1,3 +0,0 @@ -# -*- coding: utf-8 -*- -# vi:si:et:sw=4:sts=4:ts=4 - diff --git a/pandora/api/urls.py b/pandora/api/urls.py deleted file mode 100644 index fb0b40cf..00000000 --- a/pandora/api/urls.py +++ /dev/null @@ -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'), -) - diff --git a/pandora/api/views.py b/pandora/api/views.py deleted file mode 100644 index b2e6a08e..00000000 --- a/pandora/api/views.py +++ /dev/null @@ -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', '
\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) diff --git a/pandora/app/views.py b/pandora/app/views.py index 6ce58459..37b55a39 100644 --- a/pandora/app/views.py +++ b/pandora/app/views.py @@ -5,6 +5,8 @@ try: except: import elementtree.ElementTree as ET +import copy + from django.shortcuts import render_to_response, redirect from django.template import RequestContext from django.conf import settings @@ -17,8 +19,9 @@ from ox.utils import json import models -from api.actions import actions +from user.models import init_user +from ox.django.api import actions def intro(request): context = RequestContext(request, {'settings': settings}) @@ -129,3 +132,16 @@ def redirect_url(request, url): else: return HttpResponse(''%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) diff --git a/pandora/archive/views.py b/pandora/archive/views.py index 7aa4e6af..aefead40 100644 --- a/pandora/archive/views.py +++ b/pandora/archive/views.py @@ -19,7 +19,7 @@ from item import utils from item.models import get_item, Item from item.views import parse_query import item.tasks -from api.actions import actions +from ox.django.api import actions import models import tasks diff --git a/pandora/clip/views.py b/pandora/clip/views.py index a0453d1e..8a65ed97 100644 --- a/pandora/clip/views.py +++ b/pandora/clip/views.py @@ -6,7 +6,7 @@ from django.conf import settings from ox.utils import json 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 item.models import Item diff --git a/pandora/edit/views.py b/pandora/edit/views.py index fe05a9fd..2a442aa3 100644 --- a/pandora/edit/views.py +++ b/pandora/edit/views.py @@ -6,7 +6,7 @@ from ox.utils import 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 api.actions import actions +from ox.django.api import actions import models diff --git a/pandora/event/views.py b/pandora/event/views.py index a3486e41..173c0331 100644 --- a/pandora/event/views.py +++ b/pandora/event/views.py @@ -7,7 +7,7 @@ from ox.utils import 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 api.actions import actions +from ox.django.api import actions from item import utils import models diff --git a/pandora/item/views.py b/pandora/item/views.py index 5098a356..3987cb7d 100644 --- a/pandora/item/views.py +++ b/pandora/item/views.py @@ -28,7 +28,7 @@ from archive.models import File, Stream from archive import extract from clip.models import Clip -from api.actions import actions +from ox.django.api import actions def _order_query(qs, sort, prefix='sort__'): diff --git a/pandora/itemlist/views.py b/pandora/itemlist/views.py index 4b065a91..f9f8b46f 100644 --- a/pandora/itemlist/views.py +++ b/pandora/itemlist/views.py @@ -14,7 +14,7 @@ from ox.django.http import HttpFileResponse import models -from api.actions import actions +from ox.django.api import actions from item import utils from item.models import Item diff --git a/pandora/log/views.py b/pandora/log/views.py index 6abf979c..4b36103c 100644 --- a/pandora/log/views.py +++ b/pandora/log/views.py @@ -8,7 +8,7 @@ from ox.utils import 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 api.actions import actions +from ox.django.api import actions from item import utils diff --git a/pandora/news/views.py b/pandora/news/views.py index b475b233..7a2e2f79 100644 --- a/pandora/news/views.py +++ b/pandora/news/views.py @@ -11,7 +11,7 @@ from ox.django.shortcuts import render_to_json_response, get_object_or_404_json, from item.models import Item -from api.actions import actions +from ox.django.api import actions from item import utils from item.models import Item diff --git a/pandora/person/views.py b/pandora/person/views.py index 5c932ae4..d28a88bf 100644 --- a/pandora/person/views.py +++ b/pandora/person/views.py @@ -10,7 +10,7 @@ from ox.utils import 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 api.actions import actions +from ox.django.api import actions from item import utils import models diff --git a/pandora/place/views.py b/pandora/place/views.py index 60e024a8..711c7cf8 100644 --- a/pandora/place/views.py +++ b/pandora/place/views.py @@ -10,7 +10,7 @@ from ox.utils import 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 api.actions import actions +from ox.django.api import actions from item import utils import models diff --git a/pandora/settings.py b/pandora/settings.py index 3554312f..780ddd45 100644 --- a/pandora/settings.py +++ b/pandora/settings.py @@ -108,7 +108,6 @@ INSTALLED_APPS = ( 'edit', 'news', 'user', - 'api', 'urlalias', 'tv', ) diff --git a/pandora/text/views.py b/pandora/text/views.py index 5523c013..9001c1b0 100644 --- a/pandora/text/views.py +++ b/pandora/text/views.py @@ -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 import models -from api.actions import actions +from ox.django.api import actions def getText(request): diff --git a/pandora/title/views.py b/pandora/title/views.py index 6a0b7aac..9e0872f3 100644 --- a/pandora/title/views.py +++ b/pandora/title/views.py @@ -11,7 +11,7 @@ from ox.utils import 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 api.actions import actions +from ox.django.api import actions from item import utils import models diff --git a/pandora/tv/views.py b/pandora/tv/views.py index 27d3ead4..f991c0ce 100644 --- a/pandora/tv/views.py +++ b/pandora/tv/views.py @@ -7,7 +7,7 @@ from ox.utils import json from ox.django.shortcuts import render_to_json_response, json_response from itemlist.views import get_list_or_404_json -from api.actions import actions +from ox.django.api import actions def tv(request): data = json.loads(request.POST['data']) diff --git a/pandora/urls.py b/pandora/urls.py index 17a98696..6f1a06c1 100644 --- a/pandora/urls.py +++ b/pandora/urls.py @@ -13,8 +13,7 @@ admin.autodiscover() import monkey_patch.models -from api import actions -actions.autodiscover() +import ox.django.api.urls def serve_static_file(path, location, content_type): return HttpFileResponse(location, content_type=content_type) @@ -24,8 +23,7 @@ urlpatterns = patterns('', (r'^api/upload/$', 'archive.views.firefogg_upload'), (r'^url=(?P.*)$', 'app.views.redirect_url'), (r'^file/(?P.*)$', 'archive.views.lookup_file'), - (r'^api$', include('api.urls')), - (r'^api/$', include('api.urls')), + (r'^api/?$', include(ox.django.api.urls)), (r'^resetUI$', 'user.views.reset_ui'), (r'^list/(?P.*?)/icon(?P\d*).jpg$', 'itemlist.views.icon'), (r'^robots.txt$', serve_static_file, {'location': os.path.join(settings.STATIC_ROOT, 'robots.txt'), 'content_type': 'text/plain'}), diff --git a/pandora/user/views.py b/pandora/user/views.py index abfc9c49..38f5d662 100644 --- a/pandora/user/views.py +++ b/pandora/user/views.py @@ -17,7 +17,7 @@ from ox.django.decorators import admin_required_json, login_required_json import ox -from api.actions import actions +from ox.django.api import actions from item.models import Access, Item from item import utils