Commits

Christian Scholz  committed 3f4eb37

- fixed ``render_slot()`` to work again
- refactored the resource management in order to be only once initialized as it costs performance otherwise

Now back to 830 r/s from 420 r/s with instantiating the resource manager in each call.

See ``initialize_resources()`` for more information.

  • Participants
  • Parent commits 11df110

Comments (0)

Files changed (1)

File quantumcore/dynamicpage/page.py

     variants = {}
     default_variant = None
     default_template = 'main'
-    active_variant = None
-    _variant_object = (None,None) # name, instance
+    variant = None # the active variant object being used
+    js_resources = None
+    css_resources = None
     
-    def __init__(self, tmplstore, variant_name = None, **kw):
+    def __init__(self, tmplstore, 
+                       variant = None, 
+                       **kw):
         """initialize the Page with the template store to use"""
         self.tmplstore = tmplstore
         self.kw = kw
-        if variant_name is None:
-            self.variant_name = self.default_variant
+        if variant is None:
+            self.set_variant(self.default_variant)
         else:
-            self.variant_name = variant_name
+            self.set_variant(variant)
         
         if USE_RESOURCE_MANAGER:
-            self.js_resources = JSResourceManager(self.javascript, prefix_url=self.prefix+"/js")
-            self.css_resources = CSSResourceManager(self.css, prefix_url=self.prefix+"/css")
-            
-    @property
-    def variant(self):
-        """return the (cached) variant object to use"""        
-        return self.variants[self.variant_name](self)
+            self.initialize_resources()
+    
+    def initialize_resources(self, invalidate = False):
+        """call this to (re-)initialize the resource managers. To delete the cached versions
+        first, set ``invalidate`` to ``True`` (defaults to ``False``)"""
+        if invalidate:
+            Page.js_resources = None
+            Page.css_resources = None
+        
+        # we cache the resource managers inside a class variable, not an instance variable
+        # so that it's only initialized once.
+        if Page.js_resources is None:
+            Page.js_resources = JSResourceManager(self.javascript, prefix_url=self.prefix+"/js")
+        if Page.css_resources is None:
+            Page.css_resources = CSSResourceManager(self.css, prefix_url=self.prefix+"/css")
                         
     def serve_resources_wsgi(self,environ,start_response):
         """serve resources like CSS and JS. You need to have quantumcore.resources installed
     def set_variant(self, variant_name):
         """change the variant to use"""
         self.variant_name = variant_name
+        self.variant = self.variants[self.variant_name](self)
+        
     
     def render(self, template=None):
         """render the selected variant. The variant is chosen in the following priorities:
     def render_slot(self, slot, variant = None, **kw):
         """render just a slot. We choose the variant to use like above but
         we do not store the found variant as the active one. This means that 
-        it's just overriding temporarily the decision from the ``render()`` method."""        
-        return self.variant(slot, page = self, **kw)
+        it's just overriding temporarily the decision from the ``render()`` method."""
+        if variant is not None:
+            # TODO: this should really be cached!
+            vobj = self.variants[variant](self)
+        else:
+            vobj = self.variant
+        return vobj(slot, **kw)
         
     def __getitem__(self, slotname):
         """return a rendered slot. This will directly return HTML. It will use the variant
         store in ``active_variant`` which usually was set by the ``render()``method."""
-        return self.render_slot(slotname, self.active_variant)
+        return self.render_slot(slotname)
         
     def render_wsgi(self, 
                     environ,