Created
August 12, 2013 12:05
-
-
Save skazhy/6210290 to your computer and use it in GitHub Desktop.
reverse_angular_url for Tornado
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import tornado.web | |
class BaseHandler(tornado.web.RequestHandler): | |
def reverse_angular_url(self, name, *args): | |
# Works like regular reverse_url but replaces regex groups | |
# with :arg, so it can fit nicely in the AngularJS router | |
# for given route named "employee" "/company/(.*)/employees/(.*)$" -> | |
# reverse_angular_url("employee", "company_id", "employee_id") | |
# returns "/company/:company_id/employees/:employee_id" | |
if name in self.application.named_handlers: | |
raw_url = self.application.named_handlers[name]._find_groups()[0] | |
return raw_url % tuple(":" + a for a in args) | |
raise KeyError("%s not found in named urls" % name) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment