pythononwheels / pow_devel

development repo for PyhtonOnWheels framework
www.pythononwheels.org
MIT License
75 stars 10 forks source link

Include Werkzeug routing #28

Closed pythononwheels closed 6 years ago

pythononwheels commented 6 years ago

make the app.add_route decorator accept werkzeug style routes as well. Scope: This will only affect the regex. Usage stays the same.

app.add_route( route_regex, dispatch={} ) Current usage:

@app.add_route("/thanks/*", dispatch={"get": "_get"} )
@app.add_route("/thanks/([0-9]+)*", dispatch={"get": "testme"})
class ThanksHandler(BaseHandler):
    def _get(self):
        print("  .. in _get" )
        self.render("thanks.tmpl")

    def testme(self, index=0 ):
        print("  .. in testme: index = " + str(index))
        self.render("thanks.tmpl", index=index)

-->regex groups will be handed over to the handler mathod as parameters

New Option: will include Werkzeug routres as well @app.add_route("/thanks/<int:year>", dispatch={"get": "testme"}) => Rule("/thanks/<int:year>", endpoint="testme") @app.add_route("/thanks/<int:year>/<int:month>/<int:day>/<slug>", dispatch={"get": "testme"}) => Rule('/thanks<int:year>/<int:month>/<int:day>/<slug>', endpoint="testme")

The compiled Rule will be added to the tornado routes with the accordiung dispathc dict. See application.add_route()

fin_route = Rule.compile()
# cls is the handler class
# dispatch is the dispatch dict from app.add_route() decorator.

route_tuple = (fin_route,cls, dispatch)
handlers.append((route_tuple,pos))

see: http://werkzeug.pocoo.org/docs/0.14/routing/

pythononwheels commented 6 years ago

added to v.8.2. werkzeug routes work directly. also added an example to handlers/shorties.py

@app.add_route('/werkzeug/<int:year>', dispatch={"get" : "test"})
@app.add_route('/werkzeug/<uuid:identifier>', dispatch={"get" : "testuuid"})
@app.add_route('/werkzeug/<uuid:identifier>.<format>', dispatch={"get" : "testuuid"})
class WerkzeugTestHandler(BaseHandler):
    # on HTTP GET this method will be called. See dispatch parameter.
    def test(self, year=None):
        self.write("I got year: " + str(year))

    def testuuid(self, anotherone=None, identifier=None, format="html"):
        self.write("I got uuid: " + str(identifier)
                    + "<hr> I got anotherone ? == " + str(anotherone)
)