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

stacking route annotations #10

Closed
chriamue opened this issue Feb 3, 2019 · 5 comments
Closed

stacking route annotations #10

chriamue opened this issue Feb 3, 2019 · 5 comments

Comments

@chriamue
Copy link
Contributor

chriamue commented Feb 3, 2019

Hi, a single route annotation works, tried both,
but stacking route annotations, only first works.

    @route('/list/<string:project>/', defaults={'req_path': ''})
    @route('/list/<string:project>/<path:req_path>')
    def image_listing(self, project, req_path):
...
@briancappello
Copy link
Owner

This should work, as long as you give each route a unique endpoint name.

Reading more about this, in the upstream docs it suggests what you're doing should be working. Personally, that syntax not only looks awful but is also quite un-intuitive. I would much rather have end-user code be able to write this:

@route('/whatever/<string:param>/', defaults=dict(param='value'))

And then behind the scenes, we do what needs to be done for it to work with Werkzeug (namely, registering two routes with Flask). I think that code should end up living in RegisterRoutesHook.process_objects, if you want to play around with it (See the hooks module of the controller bundle). I'm happy to take a stab at it later too, this is getting a bit deep into the weeds of the implementation.

@briancappello
Copy link
Owner

@chriamue I just pushed a fix to master that hopefully implements support for multiple routes with the same endpoint name on view functions/methods. Can you give it a shot? I didn't add any magic (yet) so for now, you need to continue using the multiple routes (one with default parameter, the other with the parameter as part of the url rule)

@chriamue
Copy link
Contributor Author

chriamue commented Feb 4, 2019

Hi thank you for your quick reply.

It seems I'm getting an assertion error now.

@route('/list/<string:project>', defaults=dict(req_path=''))
    @route('/list/<string:project>/<path:req_path>')
    def listing(self, project, req_path):
...
backend_1_2b698f4c7894 | Traceback (most recent call last):
backend_1_2b698f4c7894 |   File "/flask/src/src/flask-unchained/flask_unchained/cli.py", line 56, in cli_create_app
backend_1_2b698f4c7894 |     return AppFactory.create_app(env)
backend_1_2b698f4c7894 |   File "/flask/src/src/flask-unchained/flask_unchained/app_factory.py", line 75, in create_app
backend_1_2b698f4c7894 |     unchained.init_app(app, env, bundles, _config_overrides=_config_overrides)
backend_1_2b698f4c7894 |   File "/flask/src/src/flask-unchained/flask_unchained/unchained.py", line 107, in init_app
backend_1_2b698f4c7894 |     run_hooks_hook.run_hook(app, bundles, _config_overrides=_config_overrides)
backend_1_2b698f4c7894 |   File "/flask/src/src/flask-unchained/flask_unchained/hooks/run_hooks_hook.py", line 34, in run_hook
backend_1_2b698f4c7894 |     hook.run_hook(app, bundles)
backend_1_2b698f4c7894 |   File "/flask/src/src/flask-unchained/flask_unchained/bundles/controller/hooks/register_routes_hook.py", line 27, in run_hook
backend_1_2b698f4c7894 |     self.process_objects(app, routes)
backend_1_2b698f4c7894 |   File "/flask/src/src/flask-unchained/flask_unchained/bundles/controller/hooks/register_routes_hook.py", line 70, in process_objects
backend_1_2b698f4c7894 |     **route.rule_options)
backend_1_2b698f4c7894 |   File "/flask/src/src/flask-unchained/flask_unchained/flask_unchained.py", line 71, in add_url_rule
backend_1_2b698f4c7894 |     provide_automatic_options, **options)
backend_1_2b698f4c7894 |   File "/usr/local/lib/python3.7/site-packages/flask/app.py", line 66, in wrapper_func
backend_1_2b698f4c7894 |     return f(self, *args, **kwargs)
backend_1_2b698f4c7894 |   File "/usr/local/lib/python3.7/site-packages/flask/app.py", line 1221, in add_url_rule
backend_1_2b698f4c7894 |     'existing endpoint function: %s' % endpoint)
backend_1_2b698f4c7894 | AssertionError: View function mapping is overwriting an existing endpoint function: file_controller.listing

@briancappello
Copy link
Owner

Hrm yep, not sure how I didn't run into that before... anyway, fix pushed to master

@chriamue
Copy link
Contributor Author

chriamue commented Feb 4, 2019

Works now, thank you for that.

@chriamue chriamue closed this as completed Feb 4, 2019
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

No branches or pull requests

2 participants