Changeset 251:d3a695dd1aa5

Show
Ignore:
Timestamp:
01/30/10 14:49:36 (7 months ago)
Author:
rgrp <http://rufuspollock.org>
Branch:
default
Message:

[controllers/site.py][m]: introduce deliverance proxy+theming a la  http://rufuspollock.org/code/deliverance for about and news page.

  • shakespeare/config/deployment.ini_tmpl: add config options to allow deliverance stuff to be turned on and off and specify destination
Location:
shakespeare
Files:
2 modified

Legend:

Unmodified
Added
Removed
  • shakespeare/config/deployment.ini_tmpl

    r245 r251  
    3737#beaker.session.data_dir = %(here)s/data/sessions 
    3838 
     39# repoze.who authentication 
     40# you should not have to change this though you will need to symlink provided 
     41# who.ini into same directory as this config file 
     42who.config_file = %(here)s/who.ini 
     43who.log_level = error 
     44who.log_file = stdout 
     45 
    3946# =========================== 
    4047# User configured variables 
     
    4451# execute malicious code after an exception is raised. 
    4552set debug = false 
     53 
     54# site_title for use around the site 
     55# for more serious customization suggest making your own templates (e.g. 
     56# layout.html) and using extra_template_paths option 
     57# site_title = Your Site Title 
    4658 
    4759# We use sqlalchemy to connect and work with databases. 
     
    6375# extra_template_paths = %(here)s/shksprdata/templates 
    6476 
    65 # repoze.who authentication 
    66 # you should not have to change this 
    67 who.config_file = %(here)s/who.ini 
    68 who.log_level = error 
    69 who.log_file = stdout 
     77## deliverance/proxying stuff 
     78# set to any value to enable 
     79deliverance.enable =  
     80# destination to proxy from 
     81deliverance.dest = http://blog.openshakespeare.org/ 
     82 
    7083 
    7184# Logging configuration 
  • shakespeare/controllers/site.py

    r164 r251  
    11import logging 
     2import os 
    23 
    34import genshi 
     5from pylons import config 
    46 
    57from shakespeare.lib.base import * 
     
    1517log = logging.getLogger(__name__) 
    1618 
     19DELIVERANCE_ENABLE = bool(config.get('deliverance.enable', '')) 
     20print DELIVERANCE_ENABLE 
     21 
     22 
     23# based on http://rufuspollock.org/code/deliverance 
     24import paste.urlmap 
     25import deliverance.middleware 
     26import paste.proxy 
     27from webob import Request, Response 
     28from deliverance.middleware import DeliveranceMiddleware, SubrequestRuleGetter 
     29from deliverance.log import PrintingLogger 
     30def create_deliverance_proxy(): 
     31    # where we are proxying from 
     32    dest = config['deliverance.dest'] 
     33 
     34    # use a urlmap so we can mount theme and urlset 
     35    app = paste.urlmap.URLMap() 
     36 
     37    # set up theme consistent with our rules file 
     38    app['/theme.html'] = Response(render('index.html')) 
     39 
     40    # rules_path = os.path.join('demo-rules.xml') 
     41    # rules = open(rules_path).read() 
     42    rules = '''<ruleset> 
     43 
     44  <theme href="/theme.html" /> 
     45 
     46  <!-- These are the default rules for anything with class="default" or no class: --> 
     47  <rule> 
     48    <replace content="children:#content" theme="children:#content" /> 
     49    <append content="children:#sidebar" theme="children:#primary" /> 
     50  </rule> 
     51</ruleset> 
     52''' 
     53    app['/rules.xml'] = Response(rules, content_type="application/xml") 
     54 
     55    class MyProxy(object): 
     56        def __init__(self, dest): 
     57           self.proxy = paste.proxy.Proxy(dest)  
     58         
     59        def __call__(self, environ, start_response): 
     60            req = Request(environ) 
     61            res = req.get_response(self.proxy) 
     62            # result = '' 
     63            # for out in self.proxy(environ, start_response): 
     64            #    result += out 
     65            # res = Response(result) 
     66            res.decode_content() 
     67            return res(environ, start_response) 
     68 
     69    app['/'] = MyProxy(dest) 
     70 
     71    deliv = DeliveranceMiddleware(app, SubrequestRuleGetter('/rules.xml'), 
     72        PrintingLogger, 
     73        log_factory_kw=dict(print_level=logging.WARNING)) 
     74    return deliv 
     75 
    1776 
    1877class SiteController(BaseController): 
    19  
    2078    def index(self): 
    2179        return render('index.html') 
    2280 
    2381    def about(self): 
    24         return render('about.html') 
     82        if DELIVERANCE_ENABLE: 
     83            return self.deliverance(request.environ, self.start_response) 
     84        else: 
     85            return render('about.html') 
    2586 
    2687    def guide(self): 
    2788        return render('guide.html') 
    2889 
     90    def news(self): 
     91        if DELIVERANCE_ENABLE: 
     92            # modify path for proxy to strip out /news/ 
     93            currentpath = request.environ['PATH_INFO'] 
     94            request.environ['PATH_INFO'] = currentpath[5:] 
     95            return self.deliverance(request.environ, self.start_response) 
     96        else: 
     97            return '' 
     98 
     99    @property 
     100    def deliverance(self): 
     101        if not hasattr(self, '_deliverance'): 
     102            self._deliverance = create_deliverance_proxy() 
     103        return self._deliverance 
     104