Warning: Can't synchronize with repository "(default)" (Unsupported version control system "svn": No module named svn). Look in the Trac log for more information.

Changes between Version 1 and Version 2 of RoutesIntegration


Ignore:
Timestamp:
12/12/05 09:48:39 (14 years ago)
Author:
spleeman@…
Comment:

--

Legend:

Unmodified
Added
Removed
Modified
  • RoutesIntegration

    v1 v2  
    55This is a preliminary integration of routes which is implimented on an app by app basis.  No magical discovery of controllers is available yet, but I plan on implimenting this in the near future. 
    66 
    7 Firstly, you don't need to connect your controllers to the root controller as you would usually do.  The connection of the required controller is done on an ad hoc basis. 
     7== Installing Routes == 
    88 
    9 ''More to come this evening, when I actually have time :) - Lee'' 
     9Installing routes is extremely simple: 
     10 
     11{{{ 
     12easy_install routes 
     13}}} 
     14 
     15'''The usual caveats regarding sudo and --script-dir apply''' 
     16 
     17== The root controller == 
     18 
     19Your root controller will need to look like this: 
     20 
     21{{{ 
     22#!python 
     23class Root(controllers.RootController): 
     24     
     25    def __init__(self): 
     26        self.base_re = re.compile( r'^(?P<protocol>[a-zA-Z]+)://(?P<host>.*)' ) 
     27         
     28        # Change the controllers here to the controllers in your app: 
     29        self.controllers = {'pickup': Pickup(), 'main': Main()} 
     30         
     31        # Here you need to add your own routes (http://routes.groovie.org/manual.html) 
     32        m.connect(':controller/:action', controller='main', action='index') 
     33        m.connect('pickup/:id/:action', controller='pickup', action='prepare', requirements=dict(id='\d{1,11}')) 
     34         
     35        m.create_regs(self.controllers.keys()) 
     36         
     37    def redirect(self, url): 
     38        # Recreated this function as the cherrypy one is depreciated 
     39        raise cherrypy.HTTPRedirect(turbogears.url(url), 302) 
     40     
     41     
     42    @turbogears.expose() 
     43    def default(self, *args, **kwargs): 
     44 
     45        base = cherrypy.request.base 
     46        d = self.base_re.match( base ).groupdict() 
     47        host = d[ 'host' ] 
     48        proto = d[ 'protocol' ] 
     49        path = cherrypy.request.path 
     50 
     51        con = request_config() 
     52        con.mapper = m # the Routes Mapper object 
     53        con.host = host 
     54        con.protocol = proto 
     55        con.redirect = self.redirect 
     56        con.mapper_dict = m.match( path ) 
     57 
     58        if con.mapper_dict: 
     59            c = con.mapper_dict.pop( 'controller' ) 
     60            controller = self.controllers[c] 
     61            action = con.mapper_dict.pop( 'action', 'index' ) 
     62            try: 
     63                meth = getattr(controller, action, getattr(controller, 'default')) 
     64            except AttributeError: 
     65                raise cherrypy.NotFound( path ) 
     66            kwargs.update( con.mapper_dict ) 
     67             
     68            return meth( **kwargs ) 
     69 
     70        raise cherrypy.NotFound( path ) 
     71}}} 
     72 
     73You will also need to add the follwing lines to the top of your controllers.py: 
     74 
     75{{{ 
     76#!python 
     77from routes import * 
     78import re 
     79# Create the mapper object 
     80m = Mapper() 
     81}}} 
     82 
     83Note that you don't need to connect your controllers to the root controller as you would usually do.  The connection of the required controller is done on an ad hoc basis. 
     84 
     85''I hope to update this using a subclassable RoutesController (which itself will be a subclass of RootController) and a few other bits in the TG internals that will allow you to enable and define routes in the .cfg files.''