2011-03-02 18:56:06 +00:00
|
|
|
#!/usr/bin/python
|
2011-09-02 08:58:53 +00:00
|
|
|
from __future__ import with_statement
|
|
|
|
|
2011-03-17 13:09:46 +00:00
|
|
|
import functools
|
2011-08-30 23:39:20 +00:00
|
|
|
import logging
|
2011-03-21 08:13:31 +00:00
|
|
|
import os
|
2011-09-07 16:14:08 +00:00
|
|
|
import pprint
|
2011-03-21 08:13:31 +00:00
|
|
|
import sys
|
|
|
|
import traceback
|
|
|
|
import uuid
|
|
|
|
import xmlrpclib
|
2011-03-02 18:56:06 +00:00
|
|
|
|
2011-03-10 15:53:45 +00:00
|
|
|
import simplejson
|
2011-09-02 10:01:24 +00:00
|
|
|
import werkzeug.datastructures
|
2011-09-02 08:58:53 +00:00
|
|
|
import werkzeug.exceptions
|
2011-09-02 10:07:13 +00:00
|
|
|
import werkzeug.urls
|
2011-09-02 08:58:53 +00:00
|
|
|
import werkzeug.utils
|
|
|
|
import werkzeug.wrappers
|
|
|
|
import werkzeug.wsgi
|
2011-03-02 18:56:06 +00:00
|
|
|
|
2011-09-02 08:58:53 +00:00
|
|
|
import ast
|
2011-03-28 14:19:20 +00:00
|
|
|
import nonliterals
|
2011-09-02 08:58:53 +00:00
|
|
|
import http
|
2011-09-02 06:30:06 +00:00
|
|
|
# import backendlocal as backend
|
2011-09-14 08:55:45 +00:00
|
|
|
import session as backend
|
2011-09-14 09:51:39 +00:00
|
|
|
import openerplib
|
2011-03-02 18:56:06 +00:00
|
|
|
|
2011-09-02 12:45:42 +00:00
|
|
|
__all__ = ['Root', 'jsonrequest', 'httprequest', 'Controller',
|
|
|
|
'WebRequest', 'JsonRequest', 'HttpRequest']
|
|
|
|
|
2011-09-07 12:37:03 +00:00
|
|
|
_logger = logging.getLogger(__name__)
|
|
|
|
|
2011-04-07 03:04:29 +00:00
|
|
|
#-----------------------------------------------------------
|
2011-08-31 17:37:29 +00:00
|
|
|
# Globals (wont move into a pool)
|
2011-04-07 03:04:29 +00:00
|
|
|
#-----------------------------------------------------------
|
|
|
|
|
|
|
|
applicationsession = {}
|
|
|
|
addons_module = {}
|
|
|
|
addons_manifest = {}
|
|
|
|
controllers_class = {}
|
|
|
|
controllers_object = {}
|
|
|
|
controllers_path = {}
|
|
|
|
|
2011-03-02 18:56:06 +00:00
|
|
|
#----------------------------------------------------------
|
|
|
|
# OpenERP Web RequestHandler
|
|
|
|
#----------------------------------------------------------
|
2011-09-02 08:58:53 +00:00
|
|
|
class WebRequest(object):
|
2011-09-02 12:45:42 +00:00
|
|
|
""" Parent class for all OpenERP Web request types, mostly deals with
|
|
|
|
initialization and setup of the request object (the dispatching itself has
|
|
|
|
to be handled by the subclasses)
|
|
|
|
|
|
|
|
:param request: a wrapped werkzeug Request object
|
|
|
|
:type request: :class:`werkzeug.wrappers.BaseRequest`
|
|
|
|
:param config: configuration object
|
|
|
|
|
|
|
|
.. attribute:: applicationsession
|
|
|
|
|
|
|
|
an application-wide :class:`~collections.Mapping`
|
|
|
|
|
|
|
|
.. attribute:: httprequest
|
|
|
|
|
|
|
|
the original :class:`werkzeug.wrappers.Request` object provided to the
|
|
|
|
request
|
|
|
|
|
|
|
|
.. attribute:: httpsession
|
|
|
|
|
|
|
|
a :class:`~collections.Mapping` holding the HTTP session data for the
|
|
|
|
current http session
|
|
|
|
|
|
|
|
.. attribute:: config
|
|
|
|
|
|
|
|
config parameter provided to the request object
|
|
|
|
|
|
|
|
.. attribute:: params
|
|
|
|
|
|
|
|
:class:`~collections.Mapping` of request parameters, not generally
|
|
|
|
useful as they're provided directly to the handler method as keyword
|
|
|
|
arguments
|
|
|
|
|
|
|
|
.. attribute:: session_id
|
|
|
|
|
|
|
|
opaque identifier for the :class:`backend.OpenERPSession` instance of
|
|
|
|
the current request
|
|
|
|
|
|
|
|
.. attribute:: session
|
|
|
|
|
|
|
|
:class:`~backend.OpenERPSession` instance for the current request
|
|
|
|
|
|
|
|
.. attribute:: context
|
|
|
|
|
|
|
|
:class:`~collections.Mapping` of context values for the current request
|
|
|
|
|
|
|
|
.. attribute:: debug
|
|
|
|
|
|
|
|
``bool``, indicates whether the debug mode is active on the client
|
|
|
|
"""
|
2011-09-02 08:58:53 +00:00
|
|
|
def __init__(self, request, config):
|
2011-08-18 18:36:56 +00:00
|
|
|
self.applicationsession = applicationsession
|
2011-09-02 08:58:53 +00:00
|
|
|
self.httprequest = request
|
|
|
|
self.httpresponse = None
|
|
|
|
self.httpsession = request.session
|
|
|
|
self.config = config
|
|
|
|
def init(self, params):
|
|
|
|
self.params = dict(params)
|
2011-08-18 18:36:56 +00:00
|
|
|
# OpenERP session setup
|
|
|
|
self.session_id = self.params.pop("session_id", None) or uuid.uuid4().hex
|
2011-09-02 08:58:53 +00:00
|
|
|
self.session = self.httpsession.setdefault(
|
|
|
|
self.session_id, backend.OpenERPSession(
|
|
|
|
self.config.server_host, self.config.server_port))
|
2011-08-18 18:36:56 +00:00
|
|
|
self.context = self.params.pop('context', None)
|
2011-09-02 08:58:53 +00:00
|
|
|
self.debug = self.params.pop('debug', False) != False
|
2011-08-18 18:36:56 +00:00
|
|
|
|
2011-09-02 08:58:53 +00:00
|
|
|
class JsonRequest(WebRequest):
|
2011-07-09 16:12:00 +00:00
|
|
|
""" JSON-RPC2 over HTTP.
|
2011-03-02 18:56:06 +00:00
|
|
|
|
2011-04-14 21:43:32 +00:00
|
|
|
Sucessful request::
|
|
|
|
|
|
|
|
--> {"jsonrpc": "2.0",
|
|
|
|
"method": "call",
|
|
|
|
"params": {"session_id": "SID",
|
|
|
|
"context": {},
|
|
|
|
"arg1": "val1" },
|
|
|
|
"id": null}
|
|
|
|
|
|
|
|
<-- {"jsonrpc": "2.0",
|
|
|
|
"result": { "res1": "val1" },
|
|
|
|
"id": null}
|
|
|
|
|
|
|
|
Request producing a error::
|
|
|
|
|
|
|
|
--> {"jsonrpc": "2.0",
|
|
|
|
"method": "call",
|
|
|
|
"params": {"session_id": "SID",
|
|
|
|
"context": {},
|
|
|
|
"arg1": "val1" },
|
|
|
|
"id": null}
|
|
|
|
|
|
|
|
<-- {"jsonrpc": "2.0",
|
|
|
|
"error": {"code": 1,
|
|
|
|
"message": "End user error message.",
|
|
|
|
"data": {"code": "codestring",
|
|
|
|
"debug": "traceback" } },
|
|
|
|
"id": null}
|
2011-03-02 18:56:06 +00:00
|
|
|
|
|
|
|
"""
|
|
|
|
|
2011-03-17 14:06:27 +00:00
|
|
|
def dispatch(self, controller, method, requestf=None, request=None):
|
2011-07-11 11:48:27 +00:00
|
|
|
""" Calls the method asked for by the JSON-RPC2 request
|
2011-03-17 14:06:27 +00:00
|
|
|
|
|
|
|
:param controller: the instance of the controller which received the request
|
|
|
|
:param method: the method which received the request
|
|
|
|
:param requestf: a file-like object containing an encoded JSON-RPC2 request
|
2011-08-18 18:36:56 +00:00
|
|
|
:param request: a JSON-RPC2 request
|
2011-03-17 14:06:27 +00:00
|
|
|
|
2011-08-18 18:36:56 +00:00
|
|
|
:returns: an utf8 encoded JSON-RPC2 reply
|
2011-07-11 11:48:27 +00:00
|
|
|
"""
|
2011-08-18 18:36:56 +00:00
|
|
|
response = {"jsonrpc": "2.0" }
|
|
|
|
error = None
|
2011-03-02 18:56:06 +00:00
|
|
|
try:
|
2011-08-18 18:36:56 +00:00
|
|
|
# Read POST content or POST Form Data named "request"
|
|
|
|
if requestf:
|
|
|
|
self.jsonrequest = simplejson.load(requestf, object_hook=nonliterals.non_literal_decoder)
|
|
|
|
else:
|
|
|
|
self.jsonrequest = simplejson.loads(request, object_hook=nonliterals.non_literal_decoder)
|
|
|
|
self.init(self.jsonrequest.get("params", {}))
|
2011-09-07 16:14:08 +00:00
|
|
|
if _logger.isEnabledFor(logging.DEBUG):
|
|
|
|
_logger.debug("--> %s.%s\n%s", controller.__class__.__name__, method.__name__, pprint.pformat(self.jsonrequest))
|
2011-08-18 18:36:56 +00:00
|
|
|
response['id'] = self.jsonrequest.get('id')
|
2011-07-11 11:48:27 +00:00
|
|
|
response["result"] = method(controller, self, **self.params)
|
2011-09-14 13:18:11 +00:00
|
|
|
except openerplib.AuthenticationError:
|
2011-03-17 14:06:27 +00:00
|
|
|
error = {
|
2011-03-17 15:47:46 +00:00
|
|
|
'code': 100,
|
2011-03-17 14:06:27 +00:00
|
|
|
'message': "OpenERP Session Invalid",
|
|
|
|
'data': {
|
|
|
|
'type': 'session_invalid',
|
|
|
|
'debug': traceback.format_exc()
|
|
|
|
}
|
|
|
|
}
|
2011-03-02 18:56:06 +00:00
|
|
|
except xmlrpclib.Fault, e:
|
2011-03-17 14:06:27 +00:00
|
|
|
error = {
|
2011-03-17 15:47:46 +00:00
|
|
|
'code': 200,
|
2011-03-17 14:06:27 +00:00
|
|
|
'message': "OpenERP Server Error",
|
|
|
|
'data': {
|
2011-03-17 15:47:46 +00:00
|
|
|
'type': 'server_exception',
|
2011-03-17 14:06:27 +00:00
|
|
|
'fault_code': e.faultCode,
|
2011-03-21 08:13:31 +00:00
|
|
|
'debug': "Client %s\nServer %s" % (
|
|
|
|
"".join(traceback.format_exception("", None, sys.exc_traceback)), e.faultString)
|
2011-03-17 14:06:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
except Exception:
|
2011-09-07 14:22:27 +00:00
|
|
|
logging.getLogger(__name__ + '.JSONRequest.dispatch').exception\
|
2011-09-02 08:58:53 +00:00
|
|
|
("An error occured while handling a json request")
|
2011-03-17 14:06:27 +00:00
|
|
|
error = {
|
2011-03-17 15:47:46 +00:00
|
|
|
'code': 300,
|
2011-03-17 14:06:27 +00:00
|
|
|
'message': "OpenERP WebClient Error",
|
|
|
|
'data': {
|
|
|
|
'type': 'client_exception',
|
|
|
|
'debug': "Client %s" % traceback.format_exc()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if error:
|
|
|
|
response["error"] = error
|
|
|
|
|
2011-09-07 16:14:08 +00:00
|
|
|
if _logger.isEnabledFor(logging.DEBUG):
|
|
|
|
_logger.debug("<--\n%s", pprint.pformat(response))
|
2011-04-06 00:51:36 +00:00
|
|
|
content = simplejson.dumps(response, cls=nonliterals.NonLiteralEncoder)
|
2011-09-02 08:58:53 +00:00
|
|
|
return werkzeug.wrappers.Response(
|
|
|
|
content, headers=[('Content-Type', 'application/json'),
|
|
|
|
('Content-Length', len(content))])
|
2011-03-02 18:56:06 +00:00
|
|
|
|
|
|
|
def jsonrequest(f):
|
2011-09-02 12:45:42 +00:00
|
|
|
""" Decorator marking the decorated method as being a handler for a
|
|
|
|
JSON-RPC request (the exact request path is specified via the
|
|
|
|
``$(Controller._cp_path)/$methodname`` combination.
|
|
|
|
|
|
|
|
If the method is called, it will be provided with a :class:`JsonRequest`
|
|
|
|
instance and all ``params`` sent during the JSON-RPC request, apart from
|
|
|
|
the ``session_id``, ``context`` and ``debug`` keys (which are stripped out
|
|
|
|
beforehand)
|
|
|
|
"""
|
2011-03-17 13:09:46 +00:00
|
|
|
@functools.wraps(f)
|
2011-09-02 08:58:53 +00:00
|
|
|
def json_handler(controller, request, config):
|
|
|
|
return JsonRequest(request, config).dispatch(
|
|
|
|
controller, f, requestf=request.stream)
|
|
|
|
json_handler.exposed = True
|
2011-03-17 13:09:46 +00:00
|
|
|
return json_handler
|
2011-03-02 18:56:06 +00:00
|
|
|
|
2011-09-02 08:58:53 +00:00
|
|
|
class HttpRequest(WebRequest):
|
2011-03-02 18:56:06 +00:00
|
|
|
""" Regular GET/POST request
|
|
|
|
"""
|
2011-09-02 08:58:53 +00:00
|
|
|
def dispatch(self, controller, method):
|
2011-09-06 11:49:45 +00:00
|
|
|
params = dict(self.httprequest.args)
|
|
|
|
params.update(self.httprequest.form)
|
|
|
|
params.update(self.httprequest.files)
|
|
|
|
self.init(params)
|
2011-08-18 19:21:50 +00:00
|
|
|
akw = {}
|
2011-09-02 08:58:53 +00:00
|
|
|
for key, value in self.httprequest.args.iteritems():
|
|
|
|
if isinstance(value, basestring) and len(value) < 1024:
|
|
|
|
akw[key] = value
|
2011-08-18 19:21:50 +00:00
|
|
|
else:
|
2011-09-02 08:58:53 +00:00
|
|
|
akw[key] = type(value)
|
2011-09-07 14:21:20 +00:00
|
|
|
_logger.debug("%s --> %s.%s %r", self.httprequest.method, controller.__class__.__name__, method.__name__, akw)
|
2011-09-02 08:58:53 +00:00
|
|
|
r = method(controller, self, **self.params)
|
2011-08-19 03:02:35 +00:00
|
|
|
if self.debug or 1:
|
2011-09-02 08:58:53 +00:00
|
|
|
if isinstance(r, werkzeug.wrappers.BaseResponse):
|
2011-09-07 14:21:20 +00:00
|
|
|
_logger.debug('<-- %s', r)
|
2011-09-02 08:58:53 +00:00
|
|
|
else:
|
2011-09-07 14:21:20 +00:00
|
|
|
_logger.debug("<-- size: %s", len(r))
|
2011-03-02 18:56:06 +00:00
|
|
|
return r
|
|
|
|
|
2011-09-02 08:58:53 +00:00
|
|
|
def make_response(self, data, headers=None, cookies=None):
|
2011-09-02 12:45:42 +00:00
|
|
|
""" Helper for non-HTML responses, or HTML responses with custom
|
|
|
|
response headers or cookies.
|
|
|
|
|
|
|
|
While handlers can just return the HTML markup of a page they want to
|
|
|
|
send as a string if non-HTML data is returned they need to create a
|
|
|
|
complete response object, or the returned data will not be correctly
|
|
|
|
interpreted by the clients.
|
|
|
|
|
|
|
|
:param basestring data: response body
|
|
|
|
:param headers: HTTP headers to set on the response
|
|
|
|
:type headers: ``[(name, value)]``
|
|
|
|
:param collections.Mapping cookies: cookies to set on the client
|
|
|
|
"""
|
2011-09-02 08:58:53 +00:00
|
|
|
response = werkzeug.wrappers.Response(data, headers=headers)
|
|
|
|
if cookies:
|
|
|
|
for k, v in cookies.iteritems():
|
|
|
|
response.set_cookie(k, v)
|
|
|
|
return response
|
|
|
|
|
|
|
|
def not_found(self, description=None):
|
2011-09-02 12:45:42 +00:00
|
|
|
""" Helper for 404 response, return its result from the method
|
|
|
|
"""
|
2011-09-02 08:58:53 +00:00
|
|
|
return werkzeug.exceptions.NotFound(description)
|
|
|
|
|
2011-03-07 09:26:32 +00:00
|
|
|
def httprequest(f):
|
2011-09-02 12:45:42 +00:00
|
|
|
""" Decorator marking the decorated method as being a handler for a
|
|
|
|
normal HTTP request (the exact request path is specified via the
|
|
|
|
``$(Controller._cp_path)/$methodname`` combination.
|
|
|
|
|
|
|
|
If the method is called, it will be provided with a :class:`HttpRequest`
|
|
|
|
instance and all ``params`` sent during the request (``GET`` and ``POST``
|
|
|
|
merged in the same dictionary), apart from the ``session_id``, ``context``
|
|
|
|
and ``debug`` keys (which are stripped out beforehand)
|
|
|
|
"""
|
2011-09-02 08:58:53 +00:00
|
|
|
@functools.wraps(f)
|
|
|
|
def http_handler(controller, request, config):
|
|
|
|
return HttpRequest(request, config).dispatch(controller, f)
|
|
|
|
http_handler.exposed = True
|
2011-04-07 02:47:48 +00:00
|
|
|
return http_handler
|
2011-03-02 18:56:06 +00:00
|
|
|
|
|
|
|
class ControllerType(type):
|
|
|
|
def __init__(cls, name, bases, attrs):
|
|
|
|
super(ControllerType, cls).__init__(name, bases, attrs)
|
2011-03-21 08:06:32 +00:00
|
|
|
controllers_class["%s.%s" % (cls.__module__, cls.__name__)] = cls
|
2011-03-02 18:56:06 +00:00
|
|
|
|
|
|
|
class Controller(object):
|
|
|
|
__metaclass__ = ControllerType
|
|
|
|
|
|
|
|
class Root(object):
|
2011-09-02 12:45:42 +00:00
|
|
|
"""Root WSGI application for the OpenERP Web Client.
|
|
|
|
|
|
|
|
:param options: mandatory initialization options object, must provide
|
|
|
|
the following attributes:
|
|
|
|
|
|
|
|
``server_host`` (``str``)
|
|
|
|
hostname of the OpenERP server to dispatch RPC to
|
|
|
|
``server_port`` (``int``)
|
|
|
|
RPC port of the OpenERP server
|
|
|
|
``serve_static`` (``bool | None``)
|
|
|
|
whether this application should serve the various
|
|
|
|
addons's static files
|
|
|
|
``storage_path`` (``str``)
|
|
|
|
filesystem path where HTTP session data will be stored
|
|
|
|
``dbfilter`` (``str``)
|
|
|
|
only used in case the list of databases is requested
|
|
|
|
by the server, will be filtered by this pattern
|
2011-09-02 12:02:39 +00:00
|
|
|
"""
|
2011-09-02 08:58:53 +00:00
|
|
|
def __init__(self, options):
|
2011-09-13 07:38:04 +00:00
|
|
|
self.root = werkzeug.urls.Href('/web/webclient/home')
|
2011-09-02 08:58:53 +00:00
|
|
|
self.config = options
|
|
|
|
|
|
|
|
self.session_cookie = 'sessionid'
|
2011-03-02 18:56:06 +00:00
|
|
|
self.addons = {}
|
2011-09-02 08:58:53 +00:00
|
|
|
|
|
|
|
static_dirs = self._load_addons()
|
|
|
|
if options.serve_static:
|
|
|
|
self.dispatch = werkzeug.wsgi.SharedDataMiddleware(
|
|
|
|
self.dispatch, static_dirs)
|
|
|
|
|
|
|
|
if options.session_storage:
|
|
|
|
if not os.path.exists(options.session_storage):
|
|
|
|
os.mkdir(options.session_storage, 0700)
|
|
|
|
self.session_storage = options.session_storage
|
|
|
|
|
|
|
|
def __call__(self, environ, start_response):
|
2011-09-02 12:45:42 +00:00
|
|
|
""" Handle a WSGI request
|
|
|
|
"""
|
2011-09-02 08:58:53 +00:00
|
|
|
return self.dispatch(environ, start_response)
|
|
|
|
|
|
|
|
def dispatch(self, environ, start_response):
|
2011-09-02 12:45:42 +00:00
|
|
|
"""
|
|
|
|
Performs the actual WSGI dispatching for the application, may be
|
|
|
|
wrapped during the initialization of the object.
|
|
|
|
|
|
|
|
Call the object directly.
|
|
|
|
"""
|
2011-09-02 08:58:53 +00:00
|
|
|
request = werkzeug.wrappers.Request(environ)
|
2011-09-02 10:01:24 +00:00
|
|
|
request.parameter_storage_class = werkzeug.datastructures.ImmutableDict
|
2011-09-02 08:58:53 +00:00
|
|
|
|
|
|
|
if request.path == '/':
|
|
|
|
return werkzeug.utils.redirect(
|
2011-09-13 07:38:04 +00:00
|
|
|
self.root(dict(request.args, debug='')), 301)(
|
2011-09-02 10:07:13 +00:00
|
|
|
environ, start_response)
|
2011-09-02 08:58:53 +00:00
|
|
|
elif request.path == '/mobile':
|
|
|
|
return werkzeug.utils.redirect(
|
|
|
|
'/web_mobile/static/src/web_mobile.html', 301)(
|
|
|
|
environ, start_response)
|
|
|
|
|
|
|
|
handler = self.find_handler(*(request.path.split('/')[1:]))
|
|
|
|
|
|
|
|
if not handler:
|
|
|
|
response = werkzeug.exceptions.NotFound()
|
|
|
|
else:
|
|
|
|
with http.session(request, self.session_storage, self.session_cookie) as session:
|
|
|
|
result = handler(
|
|
|
|
request, self.config)
|
|
|
|
|
2011-09-02 10:49:20 +00:00
|
|
|
if isinstance(result, basestring):
|
2011-09-02 08:58:53 +00:00
|
|
|
response = werkzeug.wrappers.Response(
|
|
|
|
result, headers=[('Content-Type', 'text/html; charset=utf-8'),
|
|
|
|
('Content-Length', len(result))])
|
2011-09-02 10:49:20 +00:00
|
|
|
else:
|
|
|
|
response = result
|
2011-09-02 08:58:53 +00:00
|
|
|
|
|
|
|
response.set_cookie(self.session_cookie, session.sid)
|
|
|
|
|
|
|
|
return response(environ, start_response)
|
2011-03-21 08:13:31 +00:00
|
|
|
|
2011-03-02 18:56:06 +00:00
|
|
|
def _load_addons(self):
|
2011-09-02 12:02:39 +00:00
|
|
|
"""
|
|
|
|
Loads all addons at the specified addons path, returns a mapping of
|
|
|
|
static URLs to the corresponding directories
|
|
|
|
"""
|
2011-09-02 08:58:53 +00:00
|
|
|
statics = {}
|
2011-09-02 09:44:49 +00:00
|
|
|
addons_path = self.config.addons_path
|
|
|
|
if addons_path not in sys.path:
|
|
|
|
sys.path.insert(0, addons_path)
|
|
|
|
for module in os.listdir(addons_path):
|
2011-09-02 08:58:53 +00:00
|
|
|
if module not in addons_module:
|
2011-09-02 09:44:49 +00:00
|
|
|
manifest_path = os.path.join(addons_path, module, '__openerp__.py')
|
2011-03-07 09:26:32 +00:00
|
|
|
if os.path.isfile(manifest_path):
|
2011-09-02 08:58:53 +00:00
|
|
|
manifest = ast.literal_eval(open(manifest_path).read())
|
2011-09-07 12:37:03 +00:00
|
|
|
_logger.info("Loading %s", module)
|
2011-09-02 08:58:53 +00:00
|
|
|
m = __import__(module)
|
|
|
|
addons_module[module] = m
|
|
|
|
addons_manifest[module] = manifest
|
|
|
|
|
|
|
|
statics['/%s/static' % module] = \
|
2011-09-02 09:44:49 +00:00
|
|
|
os.path.join(addons_path, module, 'static')
|
2011-03-21 08:13:31 +00:00
|
|
|
for k, v in controllers_class.items():
|
2011-03-07 09:26:32 +00:00
|
|
|
if k not in controllers_object:
|
2011-03-02 18:56:06 +00:00
|
|
|
o = v()
|
2011-03-07 09:26:32 +00:00
|
|
|
controllers_object[k] = o
|
2011-03-21 08:13:31 +00:00
|
|
|
if hasattr(o, '_cp_path'):
|
2011-03-07 09:26:32 +00:00
|
|
|
controllers_path[o._cp_path] = o
|
2011-09-02 08:58:53 +00:00
|
|
|
return statics
|
2011-03-02 18:56:06 +00:00
|
|
|
|
2011-09-02 08:58:53 +00:00
|
|
|
def find_handler(self, *l):
|
2011-09-02 12:45:42 +00:00
|
|
|
"""
|
|
|
|
Tries to discover the controller handling the request for the path
|
|
|
|
specified by the provided parameters
|
|
|
|
|
|
|
|
:param l: path sections to a controller or controller method
|
|
|
|
:returns: a callable matching the path sections, or ``None``
|
|
|
|
:rtype: ``Controller | None``
|
|
|
|
"""
|
2011-09-02 08:58:53 +00:00
|
|
|
if len(l) > 1:
|
2011-03-21 17:03:57 +00:00
|
|
|
for i in range(len(l), 1, -1):
|
2011-03-02 18:56:06 +00:00
|
|
|
ps = "/" + "/".join(l[0:i])
|
2011-03-07 09:26:32 +00:00
|
|
|
if ps in controllers_path:
|
|
|
|
c = controllers_path[ps]
|
2011-03-02 18:56:06 +00:00
|
|
|
rest = l[i:] or ['index']
|
|
|
|
meth = rest[0]
|
2011-03-21 08:13:31 +00:00
|
|
|
m = getattr(c, meth)
|
2011-09-02 08:58:53 +00:00
|
|
|
if getattr(m, 'exposed', False):
|
2011-09-07 16:14:08 +00:00
|
|
|
_logger.debug("Dispatching to %s %s %s", ps, c, meth)
|
2011-09-02 08:58:53 +00:00
|
|
|
return m
|
|
|
|
return None
|