Skip to content

Add support for decorating MethodViews with @Blueprint.route()#3405

Closed
dougthor42 wants to merge 6 commits intopallets:masterfrom
dougthor42:blueprints-and-methodviews
Closed

Add support for decorating MethodViews with @Blueprint.route()#3405
dougthor42 wants to merge 6 commits intopallets:masterfrom
dougthor42:blueprints-and-methodviews

Conversation

@dougthor42
Copy link
Copy Markdown
Contributor

This updates the Blueprint.route decorator to support decorating MethodView classes.

Fixes Issue #3404.

Comments

  • I assume this will be part of the v2.0.0 release, so that's what I used for versionchanged and the changelog entry. If this is incorrect please let me know.
  • I wasn't sure where to put the tests - the code changes are part of blueprints.py but really only effect views, so I put it in view.py.
  • Let me know what other tests should be added.

@dougthor42 dougthor42 changed the title Blueprints and methodviews Add support for decorating MethodViews with @Blueprint.route() Oct 25, 2019
@davidism
Copy link
Copy Markdown
Member

davidism commented Feb 12, 2020

You can already use MethodView with Blueprint with bp.add_url_rule(), which corresponds to app.add_url_rule(). This also obscures the behavior of as_view(), which is important for View classes. If we add this I want the docs to be clear that route is a shortcut that doesn't pass class arguments and sets the endpoint name to the class name.

Also, this should probably apply to add_url_rule and route on both Flask and Blueprint, for any View subclass, not just MethodView.

@davidism davidism closed this Feb 12, 2020
@davidism davidism reopened this Feb 12, 2020
@davidism
Copy link
Copy Markdown
Member

Going to close this due to lack of response. I'm open to merging this in the future, but it needs improvement.

@davidism davidism closed this Mar 11, 2020
@dougthor42
Copy link
Copy Markdown
Contributor Author

Sorry for the delay.

I want the docs to be clear that route is a shortcut that doesn't pass class arguments and sets the endpoint name to the class name.

I've added a blurb for this to the docs. Let me know if you're happy with it. I also added a second example for Blueprints+MethodViews.

apply to add_url_rule and route on both Flask and Blueprint, for any View subclass, not just MethodView

Can you elaborate on this? My naive interpretation is just:

diff --git a/src/flask/blueprints.py b/src/flask/blueprints.py
index 0e974ab2..13392174 100644
--- a/src/flask/blueprints.py
+++ b/src/flask/blueprints.py
@@ -14,6 +14,7 @@ from functools import update_wrapper
 from .helpers import _endpoint_from_view_func
 from .helpers import _PackageBoundObject
 from .views import MethodViewType
+from .views import View

 # a singleton sentinel value for parameter defaults
 _sentinel = object()
@@ -283,7 +284,7 @@ class Blueprint(_PackageBoundObject):
             endpoint = options.pop("endpoint", f.__name__)

             # Support decorating MethodView classes. See Issue #3404
-            if isinstance(f, MethodViewType):
+            if isinstance(f, (View, MethodViewType)):
                 f = f.as_view(endpoint)

             self.add_url_rule(rule, endpoint, f, **options)

Is that what you're talking about or am I completely misunderstanding?

@dougthor42
Copy link
Copy Markdown
Contributor Author

dougthor42 commented Mar 29, 2020

Oh, and you can see the changes in https://github.com/dougthor42/flask/tree/blueprints-and-methodviews. I thought that GH would continue to update the MR even after it's closed, but I thought wrong.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Nov 14, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants