Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support rule arguments in route_base #20

Merged
merged 3 commits into from Apr 1, 2013

Conversation

uniphil
Copy link
Contributor

@uniphil uniphil commented Mar 26, 2013

This pull request maybe should have been two. First, it fixes the behaviour of the arguments passed to the proxied views:

  1. As far as I can tell, Flask always calls views with only a **<dict>, so we can drop *args altogether there unless I'm missing something. Maybe strange use cases of people calling view functions manually? I'll put them back if you think it's an issue. The place where Flask calls view functions seems to be centralized to always go through this line in flask.app.

    The dict that is passed is the one from the global request object. From my own testing with flask's built-in @before_request method, it seems you can change the request.view_args dict in a @before_request, and expect the arguments passed to views to be changed accordingly.

    The flask-classy proxy, however, was getting passed **request.view_args by flask, and then passing that into the class's before_request, before_<method>, and finally the actual view method, which means it could not be modified.

    I'm going on a lot here, and it might seem like a pretty weird use-case, but I wanted to be able to have the modified request.view_args passed to my view methods so that I could pull out arguments from cls.route_base first. Which leads me to....

  2. Support for arguments in cls.route_base! I wanted to do class-level pre-processing of arguments in route_base, which lead to all kinds of problems before. The main issue was that adding the route_base argument to my view's method declaration caused the variable to be added to the rule twice, and making werkzeug throw a -fit- ValueError.

    So I pulled in werkzeug.routing.parse_rule to find any rules in the route_base which should not be re-added because of a method declaration, and had them be ignored along with self in build_rules.

I added some tests which I think cover my changes. Seems to be working fine for me so far. Let me know what you think!

@uniphil
Copy link
Contributor Author

uniphil commented Mar 26, 2013

Here's my use-case, which I'm throwing in because I think it's useful to be able to do and not that weird:

class ThingView(FlaskView):
    route_base = '/<account_name>/things'

    def before_request(self, name, **kwargs):
        account_name = request.view_args.pop('account_name')
        # do stuff with the account...

    def index(self):
        # do stuff...

apiguy added a commit that referenced this pull request Apr 1, 2013
Support rule arguments in route_base
@apiguy apiguy merged commit 6b26e15 into apiguy:master Apr 1, 2013
@apiguy
Copy link
Owner

apiguy commented Apr 1, 2013

This is awesome. I can't thank you enough for this.

@uniphil uniphil deleted the enhance/parameterized_base branch April 5, 2013 01:37
@uniphil
Copy link
Contributor Author

uniphil commented Apr 5, 2013

Yay!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants