Author: jure
Date: Wed Mar  6 14:49:35 2013
New Revision: 1453351

URL: http://svn.apache.org/r1453351
Log:
#357 follow up, added request factory for handling product scoped Hrefs ...


Modified:
    
incubator/bloodhound/branches/bep_0003_multiproduct/bloodhound_multiproduct/multiproduct/hooks.py
    incubator/bloodhound/branches/bep_0003_multiproduct/trac/trac/hooks.py
    incubator/bloodhound/branches/bep_0003_multiproduct/trac/trac/web/main.py

Modified: 
incubator/bloodhound/branches/bep_0003_multiproduct/bloodhound_multiproduct/multiproduct/hooks.py
URL: 
http://svn.apache.org/viewvc/incubator/bloodhound/branches/bep_0003_multiproduct/bloodhound_multiproduct/multiproduct/hooks.py?rev=1453351&r1=1453350&r2=1453351&view=diff
==============================================================================
--- 
incubator/bloodhound/branches/bep_0003_multiproduct/bloodhound_multiproduct/multiproduct/hooks.py
 (original)
+++ 
incubator/bloodhound/branches/bep_0003_multiproduct/bloodhound_multiproduct/multiproduct/hooks.py
 Wed Mar  6 14:49:35 2013
@@ -21,8 +21,9 @@ import multiproduct.dbcursor
 
 import re
 
-from trac.hooks import EnvironmentFactoryBase
+from trac.hooks import EnvironmentFactoryBase, RequestFactoryBase
 from trac.web.main import RequestWithSession
+from trac.web.href import Href
 
 PRODUCT_RE = re.compile(r'^/products/(?P<pid>[^/]*)(?P<pathinfo>.*)')
 
@@ -44,3 +45,33 @@ class MultiProductEnvironmentFactory(Env
                 global_env._abs_href = req.abs_href
             env = multiproduct.env.ProductEnvironmentFactory(global_env, pid)
         return env
+
+class ProductizedHref(Href):
+    def __init__(self, global_href, base):
+        super(ProductizedHref, self).__init__(base)
+        self._global_href = global_href
+
+    def __call__(self, *args, **kwargs):
+        if args:
+            # TODO: this should be done using regex or similar
+            if args[0] == 'chrome' or \
+               args[0].startswith('js/') or \
+               args[0].startswith('css/') or\
+               args[0].startswith('img/') or\
+               args[0].startswith('products'):
+                return self._global_href(*args, **kwargs)
+        return super(ProductizedHref, self).__call__(*args, **kwargs)
+
+class ProductRequestWithSession(RequestWithSession):
+    def __init__(self, env, environ, start_response):
+        super(ProductRequestWithSession, self).__init__(environ, 
start_response)
+        self.base_url = env.base_url
+        self.href = ProductizedHref(self.href, env.href.base)
+        self.abs_href = ProductizedHref(self.abs_href, env.abs_href.base)
+
+class ProductRequestFactory(RequestFactoryBase):
+    def create_request(self, env, environ, start_response):
+        if isinstance(env, multiproduct.env.ProductEnvironment):
+            return ProductRequestWithSession(env, environ, start_response)
+        else:
+            return RequestWithSession(environ, start_response)
\ No newline at end of file

Modified: incubator/bloodhound/branches/bep_0003_multiproduct/trac/trac/hooks.py
URL: 
http://svn.apache.org/viewvc/incubator/bloodhound/branches/bep_0003_multiproduct/trac/trac/hooks.py?rev=1453351&r1=1453350&r2=1453351&view=diff
==============================================================================
--- incubator/bloodhound/branches/bep_0003_multiproduct/trac/trac/hooks.py 
(original)
+++ incubator/bloodhound/branches/bep_0003_multiproduct/trac/trac/hooks.py Wed 
Mar  6 14:49:35 2013
@@ -28,6 +28,10 @@ class EnvironmentFactoryBase(object):
     def open_environment(self, environ, env_path, global_env, use_cache=False):
         return None
 
+class RequestFactoryBase(object):
+    def create_request(self, env, environ, start_response):
+        return None
+
 def _get_plugins_dir(env_path):
     return os.path.normcase(os.path.realpath(os.path.join(env_path, 
'plugins')))
 
@@ -73,3 +77,7 @@ def install_global_hooks():
 def environment_factory(env):
     hook_path = env.config.get('trac', 'environment_factory', default=None)
     return _get_hook_class(env.path, hook_path, EnvironmentFactoryBase) if 
hook_path else None
+
+def request_factory(env):
+    hook_path = env.config.get('trac', 'request_factory', default=None)
+    return _get_hook_class(env.path, hook_path, RequestFactoryBase) if 
hook_path else None

Modified: 
incubator/bloodhound/branches/bep_0003_multiproduct/trac/trac/web/main.py
URL: 
http://svn.apache.org/viewvc/incubator/bloodhound/branches/bep_0003_multiproduct/trac/trac/web/main.py?rev=1453351&r1=1453350&r2=1453351&view=diff
==============================================================================
--- incubator/bloodhound/branches/bep_0003_multiproduct/trac/trac/web/main.py 
(original)
+++ incubator/bloodhound/branches/bep_0003_multiproduct/trac/trac/web/main.py 
Wed Mar  6 14:49:35 2013
@@ -434,6 +434,7 @@ def dispatch_request(environ, start_resp
     run_once = environ['wsgi.run_once']
 
     env = env_error = None
+    global_env = None
     try:
         from trac.hooks import environment_factory
         global_env = open_environment(env_path, use_cache=not run_once)
@@ -464,7 +465,10 @@ def dispatch_request(environ, start_resp
     except Exception, e:
         env_error = e
 
-    req = RequestWithSession(environ, start_response)
+    from trac.hooks import request_factory
+    factory = request_factory(global_env)
+    req = factory().create_request(env, environ, start_response) if factory \
+            else RequestWithSession(environ, start_response)
     translation.make_activable(lambda: req.locale, env.path if env else None)
     try:
         return _dispatch_request(req, env, env_error)


Reply via email to