I discussed in a previous post how I used a basic implementation of GAE’s WSGIApplication for Pretty URL mapping. While this approach served my initial needs, it required a lot of extra logic to read params from incoming URLs and did not elegantly handle URLs invoked in an AJAX call (i.e. logic was need in the handler to interpret the URL and delegate to the appropriate action). My goal was to implement a Pretty URL mapping scheme that allowed me to:
-configure URL mappings in one place only
-gracefully pass params on URLs
-invoke actions contained in my handlers
Happily, I stumbled upon Moraes’ recipe; it was a snap to follow the very clear directions to import Routes and override the WSGIApplication. Further support from the Routes documentation allowed me to quickly and easily meet all my Pretty URL goals.
All URL mappings are configured in routing.py. Params passed on URLs are discretely mapped and retrieved. For instance this mapping accepts a param named ‘id’ and hits a handler in views.py called ‘MyHandler’:
map.connect('handler/:id', controller = 'views:MyHandler')
In views.py, ‘id’ is picked up as an argument of the get() method:
<br />class MyHandler(ParentHandler):<br />def get(self, id): ...<br />
How cool is that?! Crisp and clean and no caffeine. It gets better.
I can also map to a handler and an action. This is particularly handy for my api.py, which accepts AJAX requests. With this mapping the correct handler’s action is invoked without any messy delegation at the top of my api.py:
map.connect('api/handler/action', controller = 'api:MyHandler', action = 'action')
How great is this? Pretty URLs and cleaner code. Yes, I’ve added the Routes resources and Moraes’ overridden WSGIApplication, but the separation of mapping logic from other aspects of the application pleases me and I believe makes for a more robust code base. Not to mention, I learned a new thing! Thanks Mr Moraes, my URLs got pulchriTUDE!