Commits

mitar committed ddcbf76

We have to use both Django and Trac URL roots.

Comments (0)

Files changed (2)

cmsplugin_markup_tracwiki/lazy.py

-class LazyObject(object):
-    """
-    A wrapper for another class that can be used to delay instantiation of the
-    wrapped class.
-    """
-    def __init__(self, *args, **kwargs):
-        self._wrapped = None
-        self._wrapped_args = args
-        self._wrapped_kwargs = kwargs
-
-    def __getattr__(self, name):
-        if self._wrapped is None:
-            self._setup()
-        return getattr(self._wrapped, name)
-
-    def __setattr__(self, name, value):
-        if name in ["_wrapped", "_wrapped_args", "_wrapped_kwargs"]:
-            # Assign to __dict__ to avoid infinite __setattr__ loops.
-            self.__dict__[name] = value
-        else:
-            if self._wrapped is None:
-                self._setup()
-            setattr(self._wrapped, name, value)
-
-    def __delattr__(self, name):
-        if name == ["_wrapped", "_wrapped_args", "_wrapped_kwargs"]:
-            raise TypeError("can't delete %s." % (name,))
-        if self._wrapped is None:
-            self._setup()
-        delattr(self._wrapped, name)
-
-    def _setup(self):
-        """
-        Must be implemented by subclasses to initialise the wrapped object.
-        """
-        raise NotImplementedError
-
-    # introspection support:
-    __members__ = property(lambda self: self.__dir__())
-
-    def __dir__(self):
-        if self._wrapped is None:
-            self._setup()
-        return dir(self._wrapped)
-
-    def __call__(self, *args, **kwargs):
-        if self._wrapped is None:
-            self._setup()
-        return self._wrapped(*args, **kwargs)
-
-    def __add__(self, other):
-        if self._wrapped is None:
-            self._setup()
-        return self._wrapped + other

cmsplugin_markup_tracwiki/tracwiki.py

+import functools
 import inspect
 import os
 import re
 from django.core import urlresolvers
 from django.core.servers import basehttp
 from django.db.models import Q
-from django.utils import functional
 from django.utils import translation as django_translation
 from django.utils import safestring
 
 
 from cmsplugin_markup import plugins as markup_plugins
 
-from cmsplugin_markup_tracwiki import lazy
-
 OBJ_ADMIN_RE_PATTERN = ur'\[\[CMSPlugin\(\s*(\d+)\s*\)\]\]'
 OBJ_ADMIN_RE = re.compile(OBJ_ADMIN_RE_PATTERN)
 
 
 TRACWIKI_HEADER_OFFSET = 1
 
-if not hasattr(urlresolvers, 'reverse_lazy'):
-    urlresolvers.reverse_lazy = functional.lazy(urlresolvers.reverse, str)
+def tracwiki_base_path():
+    return urlresolvers.reverse('cmsplugin_markup_tracwiki', kwargs={'path': ''})
 
-class LazyHref(lazy.LazyObject):
-    def _setup(self):
-        self._wrapped = web.href.Href(*self._wrapped_args, **self._wrapped_kwargs)
+def temporary_switch_to_trac_root(f):
+    @functools.wraps(f)
+    def wrapper(req, *args, **kwargs):
+        orig_href = req.href
+        req.href = web.href.Href(tracwiki_base_path())
+        f(req, *args, **kwargs)
+        req.href = orig_href
+    return wrapper
 
-def tracwiki_base_path():
-    return urlresolvers.reverse_lazy('cmsplugin_markup_tracwiki', kwargs={'path': ''})
+# Those two methods should always use trac root
+trac_chrome.add_stylesheet = temporary_switch_to_trac_root(trac_chrome.add_stylesheet)
+trac_chrome.add_script = temporary_switch_to_trac_root(trac_chrome.add_script)
 
 class DjangoEnvironment(test.EnvironmentStub):
     """A Django environment for Trac."""
             else:
                 __import__(name=module_and_class[0], fromlist=[module_and_class[1]])
 
-        self.href = LazyHref(tracwiki_base_path())
-
         self.config.set('trac', 'default_charset', 'utf-8')
         self.config.set('trac', 'never_obfuscate_mailto', True)
 
 
         # TODO: Sync activated locales with Django?
 
-    def set_abs_href(self, request):
+    def _set_abs_href(self, request):
         site = sites_models.Site.objects.get_current() if sites_models.Site._meta.installed else sites_models.RequestSite(request)
 
         server_port = str(request.META.get('SERVER_PORT', '80'))
         if request.is_secure():
-            self.abs_href = LazyHref('https://' + site.domain + (':' + server_port if server_port != '443' else '') + self.href())
+            self.abs_href = web.href.Href('https://' + site.domain + (':' + server_port if server_port != '443' else '') + self.href())
         else:
-            self.abs_href = LazyHref('http://' + site.domain + (':' + server_port if server_port != '80' else '') + self.href())
+            self.abs_href = web.href.Href('http://' + site.domain + (':' + server_port if server_port != '80' else '') + self.href())
+
+    def switch_to_django_root(self, request=None):
+        self.href = web.href.Href(urlresolvers.reverse('pages-root'))
+
+        if request:
+            self._set_abs_href(request)
+
+    def switch_to_trac_root(self, request=None):
+        self.href = web.href.Href(tracwiki_base_path())
+
+        if request:
+            self._set_abs_href(request)
     
     def get_templates_dir(self):
         return getattr(settings, 'CMS_MARKUP_TRAC_TEMPLATES_DIR', super(DjangoEnvironment, self).get_templates_dir())
 
 class DjangoChrome(trac_chrome.Chrome):
-    pass
+    @property
+    def htdocs_location(self):
+        return web.href.Href(tracwiki_base_path()).chrome('common')
 
 class DjangoRequestDispatcher(main.RequestDispatcher):
     pass
 
     def parse(self, value, context=None, placeholder=None):
         request = _get_django_request(context=context)
-        self.env.set_abs_href(request)
+        self.env.switch_to_django_root(request)
         if not context:
             context = template.RequestContext(request, {})
         req = DjangoRequest(self.env, request, context, placeholder)
         return trac_urls + urls
 
     def serve_trac_path(self, request, path):
-        self.env.set_abs_href(request)
+        self.env.switch_to_trac_root(request)
         context = template.RequestContext(request, {})
         req = DjangoRequest(self.env, request, context, None)
 
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.