web/application.py
changeset 2058 7ef12c03447c
parent 1977 606923dff11b
child 2235 d5987f75c97c
--- a/web/application.py	Fri Jun 05 15:06:55 2009 +0200
+++ b/web/application.py	Fri Jun 05 15:09:20 2009 +0200
@@ -10,21 +10,25 @@
 import sys
 from time import clock, time
 
+from logilab.common.deprecation import obsolete
+
 from rql import BadRQLQuery
 
-from cubicweb import set_log_methods
-from cubicweb import (ValidationError, Unauthorized, AuthenticationError,
-                   NoSelectableObject, RepositoryError)
-from cubicweb.cwvreg import CubicWebRegistry
-from cubicweb.web import (LOGGER, StatusResponse, DirectResponse, Redirect, NotFound,
-                       RemoteCallFailed, ExplicitLogin, InvalidSession)
+from cubicweb import set_log_methods, cwvreg
+from cubicweb import (
+    ValidationError, Unauthorized, AuthenticationError, NoSelectableObject,
+    RepositoryError)
+from cubicweb.web import LOGGER, component
+from cubicweb.web import (
+    StatusResponse, DirectResponse, Redirect, NotFound,
+    RemoteCallFailed, ExplicitLogin, InvalidSession)
 from cubicweb.web.component import Component
 
 # make session manager available through a global variable so the debug view can
 # print information about web session
 SESSION_MANAGER = None
 
-class AbstractSessionManager(Component):
+class AbstractSessionManager(component.Component):
     """manage session data associated to a session identifier"""
     id = 'sessionmanager'
 
@@ -38,8 +42,7 @@
         if self.session_time:
             assert self.cleanup_session_time < self.session_time
             assert self.cleanup_anon_session_time < self.session_time
-        self.authmanager = self.vreg.select_component('authmanager')
-        assert self.authmanager, 'no authentication manager found'
+        self.authmanager = self.vreg.select('components', 'authmanager')
 
     def clean_sessions(self):
         """cleanup sessions which has not been unused since a given amount of
@@ -87,7 +90,7 @@
         raise NotImplementedError()
 
 
-class AbstractAuthenticationManager(Component):
+class AbstractAuthenticationManager(component.Component):
     """authenticate user associated to a request and check session validity"""
     id = 'authmanager'
 
@@ -110,8 +113,7 @@
     SESSION_VAR = '__session'
 
     def __init__(self, appli):
-        self.session_manager = appli.vreg.select_component('sessionmanager')
-        assert self.session_manager, 'no session manager found'
+        self.session_manager = appli.vreg.select('components', 'sessionmanager')
         global SESSION_MANAGER
         SESSION_MANAGER = self.session_manager
         if not 'last_login_time' in appli.vreg.schema:
@@ -209,20 +211,8 @@
 
 
 class CubicWebPublisher(object):
-    """Central registry for the web application. This is one of the central
-    object in the web application, coupling dynamically loaded objects with
-    the application's schema and the application's configuration objects.
-
-    It specializes the VRegistry by adding some convenience methods to
-    access to stored objects. Currently we have the following registries
-    of objects known by the web application (library may use some others
-    additional registries):
-    * controllers, which are directly plugged into the application
-      object to handle request publishing
-    * views
-    * templates
-    * components
-    * actions
+    """the publisher is a singleton hold by the web frontend, and is responsible
+    to publish HTTP request.
     """
 
     def __init__(self, config, debug=None,
@@ -231,7 +221,7 @@
         super(CubicWebPublisher, self).__init__()
         # connect to the repository and get application's schema
         if vreg is None:
-            vreg = CubicWebRegistry(config, debug=debug)
+            vreg = cwvreg.CubicWebRegistry(config, debug=debug)
         self.vreg = vreg
         self.info('starting web application from %s', config.apphome)
         self.repo = config.repository(vreg)
@@ -250,7 +240,7 @@
             self.publish = self.main_publish
         # instantiate session and url resolving helpers
         self.session_handler = session_handler_fact(self)
-        self.url_resolver = vreg.select_component('urlpublisher')
+        self.url_resolver = vreg.select('components', 'urlpublisher')
 
     def connect(self, req):
         """return a connection for a logged user object according to existing
@@ -259,15 +249,6 @@
         """
         self.session_handler.set_session(req)
 
-    def select_controller(self, oid, req):
-        """return the most specific view according to the resultset"""
-        vreg = self.vreg
-        try:
-            return vreg.select(vreg.registry_objects('controllers', oid),
-                               req=req, appli=self)
-        except NoSelectableObject:
-            raise Unauthorized(req._('not authorized'))
-
     # publish methods #########################################################
 
     def log_publish(self, path, req):
@@ -292,6 +273,14 @@
             finally:
                 self._logfile_lock.release()
 
+    @obsolete("use vreg.select('controllers', ...)")
+    def select_controller(self, oid, req):
+        try:
+            controller = self.vreg.select('controllers', oid, req=req,
+                                          appli=self)
+        except NoSelectableObject:
+            raise Unauthorized(req._('not authorized'))
+
     def main_publish(self, path, req):
         """method called by the main publisher to process <path>
 
@@ -316,7 +305,11 @@
         try:
             try:
                 ctrlid, rset = self.url_resolver.process(req, path)
-                controller = self.select_controller(ctrlid, req)
+                try:
+                    controller = self.vreg.select('controllers', ctrlid, req,
+                                                  appli=self)
+                except NoSelectableObject:
+                    raise Unauthorized(req._('not authorized'))
                 req.update_search_state()
                 result = controller.publish(rset=rset)
                 if req.cnx is not None:
@@ -384,7 +377,7 @@
             if tb:
                 req.data['excinfo'] = excinfo
             req.form['vid'] = 'error'
-            errview = self.vreg.select_view('error', req, None)
+            errview = self.vreg.select('views', 'error', req)
             template = self.main_template_id(req)
             content = self.vreg.main_template(req, template, view=errview)
         except:
@@ -399,7 +392,7 @@
 
     def notfound_content(self, req):
         req.form['vid'] = '404'
-        view = self.vreg.select_view('404', req, None)
+        view = self.vreg.select('views', '404', req)
         template = self.main_template_id(req)
         return self.vreg.main_template(req, template, view=view)