changeset 4170:02fcab41459f

Factored out some of the loadTheme code
author Florian Krupicka <florian.krupicka@googlemail.com>
date Tue, 17 Jun 2008 01:24:28 +0200
parents 1d5995a6a0e2
children 5746f1f27d3e
files MoinMoin/theme/__init__.py MoinMoin/userprefs/prefs.py MoinMoin/web/contexts.py
diffstat 3 files changed, 55 insertions(+), 1 deletions(-) [+]
line wrap: on
line diff
--- a/MoinMoin/theme/__init__.py	Mon Jun 16 17:21:25 2008 +0200
+++ b/MoinMoin/theme/__init__.py	Tue Jun 17 01:24:28 2008 +0200
@@ -1795,4 +1795,48 @@
         #request.write('<!-- auth_method == %s -->' % repr(request.user.auth_method))
         request.write('</body>\n</html>\n\n')
 
+class ThemeNotFound(Exception):
+    """ Thrown if the supplied theme could not be found anywhere """
 
+def load_theme(request, theme_name=None):
+    """ Load a theme for this request.
+
+    @param request: moin request
+    @param theme_name: the name of the theme
+    @type theme_name: str
+    @rtype: Theme
+    @return: a theme initialized for the request
+    """
+    if theme_name is None or theme_name == '<default>':
+        theme_name = request.cfg.theme_default
+        
+    try:
+        Theme = wikiutil.importPlugin(request.cfg, 'theme', theme_name, 'Theme')
+    except wikiutil.PluginMissingError:
+        raise ThemeNotFound(theme_name)
+
+    return Theme(request)
+
+def load_theme_fallback(request, theme_name=None):
+    """ Try loading a theme, falling back to defaults on error.
+
+    @param request: moin request
+    @param theme_name: the name of the theme
+    @type theme_name: str
+    @rtype: int
+    @return: A statuscode for how successful the loading was
+             0 - theme was loaded
+             1 - fallback to default theme 
+             2 - serious fallback to builtin theme
+    """
+    fallback = 0
+    try:
+        request.theme = load_theme(request, theme_name)
+    except ThemeNotFound:
+        fallback = 1
+        try:
+            request.theme = load_theme(request.cfg.theme_default)
+        except ThemeNotFound:
+            fallback = 2
+            from MoinMoin.theme.modern import Theme
+            request.theme = Theme(request)
--- a/MoinMoin/userprefs/prefs.py	Mon Jun 16 17:21:25 2008 +0200
+++ b/MoinMoin/userprefs/prefs.py	Tue Jun 17 01:24:28 2008 +0200
@@ -9,6 +9,7 @@
 
 import time
 from MoinMoin import user, util, wikiutil, events
+from MoinMoin.theme import load_theme_fallback
 from MoinMoin.widget import html
 from MoinMoin.userprefs import UserPrefBase
 
@@ -163,7 +164,7 @@
             # already loaded theme is just replaced (works cause
             # nothing has been emitted yet)
             request.user.theme_name = theme_name
-            if request.loadTheme(theme_name) > 0:
+            if load_theme_fallback(request, theme_name) > 0:
                 theme_name = wikiutil.escape(theme_name)
                 return 'error', _("The theme '%(theme_name)s' could not be loaded!") % locals()
 
--- a/MoinMoin/web/contexts.py	Mon Jun 16 17:21:25 2008 +0200
+++ b/MoinMoin/web/contexts.py	Tue Jun 17 01:24:28 2008 +0200
@@ -17,6 +17,7 @@
 from MoinMoin.config import multiconfig
 from MoinMoin.formatter import text_html
 from MoinMoin.request import RequestBase
+from MoinMoin.theme import load_theme_fallback
 from MoinMoin.web.request import Request, Response
 from MoinMoin.web.utils import check_spider, UniqueIDGenerator
 from MoinMoin.web.exceptions import Forbidden, SurgeProtection
@@ -280,3 +281,11 @@
             del self._fmt_hd_counters
         if hasattr(self, 'uid_generator'):
             del self.uid_generator
+
+    def initTheme(self):
+        """ Set theme - forced theme, user theme or wiki default """
+        if self.cfg.theme_force:
+            theme_name = self.cfg.theme_default
+        else:
+            theme_name = self.user.theme_name
+        load_theme_fallback(self, theme_name)