10/04/2008

Automatic list of actions from a controller

A little recipe to list all the actions off a controller automatically:

class SomethingController(BaseController):

    def index(self):
        # magic to return a list of actions this controller supports 
        html = [h.link_to(f.replace('_', ' '), h.url_for(controller='something', action=f)) + "<br>"
                for f in dir(self) 
                if (not f.startswith('_') and 
                    callable(getattr(self, f)) and 
                    f not in ('index', 'start_response')
                )]
        return "\n".join(html)

No comments: