Christian Scholz avatar Christian Scholz committed 11df110

removed unused code such as overriding of variants in every possible call. This might get readded in case it's actually needed.
Also changed that implementation of PageVariants in that they are now instantiated not on import time but on every call to the page.

TODO: This is now on every call to a slot which is slow, should really be done on setting the variant name.

Comments (0)

Files changed (1)

quantumcore/dynamicpage/page.py

     """base class for variants which return an empty unicode string in case
     a slot is not defined"""
     
+    def __init__(self, page):
+        """initialize the page variant with the page it belongs to. With this one can e.g.
+        access the keyword arguments passed into the page"""
+        self.page = page
+    
     def __call__(self, slot, **kw):
         """render the given slot"""
         m = getattr(self, slot, None)
         if m is None:
             return u''
-        return m(**kw)
+        instance_kw = copy.copy(self.page.kw)
+        instance_kw.update(kw)
+        return m(**instance_kw)
         
-class StrictPageVariant(object):
+class StrictPageVariant(PageVariant):
     """base class for variants which checks for the existance of slots
     and raises an AttributeError in case it's missing."""
     
     def __call__(self, slot, **kw):
         """render the given slot"""
         m = getattr(self, slot)
+        instance_kw = copy.copy(self.page.kw)
+        instance_kw.update(kw)
         return m(**kw)
   
         
     default_variant = None
     default_template = 'main'
     active_variant = None
+    _variant_object = (None,None) # name, instance
     
-    def __init__(self, tmplstore, variant = None, **kw):
+    def __init__(self, tmplstore, variant_name = None, **kw):
         """initialize the Page with the template store to use"""
         self.tmplstore = tmplstore
         self.kw = kw
-        self.variant = variant # the variant to use
+        if variant_name is None:
+            self.variant_name = self.default_variant
+        else:
+            self.variant_name = variant_name
         
         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)
                         
     def serve_resources_wsgi(self,environ,start_response):
         """serve resources like CSS and JS. You need to have quantumcore.resources installed
             return self.css_resources.render_wsgi(environ, start_response)
         # TODO: raise a 404 or some other exception so that the mainloop can raise a 404
         return None
+        
+    def set_variant(self, variant_name):
+        """change the variant to use"""
+        self.variant_name = variant_name
     
-    def render(self, variant = None, template=None):
+    def render(self, template=None):
         """render the selected variant. The variant is chosen in the following priorities:
         
         1. If a ``variant`` argument is present, use this
         3. Use the default variant inside the ``Page`` class
         
         The selected variant is stored inside ``active_variant`` inside the instance.
-        """
-        if variant is not None:
-            self.active_variant= variant
-        elif self.variant is not None:
-            self.active_variant = self.variant
-        else:
-            self.active_variant = self.default_variant
-            
+        """            
         if template is None:
             template = self.default_template
             
         # get the necessary objects
-        tmpl = self.tmplstore[template]
-        
-        # TODO: This is not used?!?
-        # Why do we compute things many times like active_variant?
-        vobj = self.variants[self.active_variant]
-        
-        # we might add a hook method here, like vobj.preprocess(self)
-        
+        tmpl = self.tmplstore[template]        
         return tmpl(page = self, **self.kw)
         
     __call__ = render
     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."""
-        if variant is not None:
-            variant_to_use= variant
-        elif self.variant is not None:
-            variant_to_use = self.variant
-        else:
-            variant_to_use = self.default_variant
-        
-        vobj = self.variants[variant]
-        all_kws = copy.copy(self.kw)
-        all_kws.update(kw)
-        return vobj(slot, page = self, **all_kws)
+        it's just overriding temporarily the decision from the ``render()`` method."""        
+        return self.variant(slot, page = self, **kw)
         
     def __getitem__(self, slotname):
         """return a rendered slot. This will directly return HTML. It will use the variant
     def render_wsgi(self, 
                     environ, 
                     start_response, 
-                    variant = None, 
                     template = None, 
                     encoding="utf-8",
                     **kw):
         """render the template in WSGI form with setting the appropriate headers"""
         return render_data_to_wsgi(
-            self(variant=variant, template=template).encode(encoding), 
+            self(template=template).encode(encoding), 
             start_response
             )
         
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.