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

Have is_list_view recognise RetrieveModel… views #5480

Conversation

carltongibson
Copy link
Collaborator

@carltongibson carltongibson commented Oct 5, 2017

Fixes #5165

Adds isinstance check for RetrieveModelMixin. Useful when you've routed a view to a URL without a lookup parameter. e.g. /accounts/profile/ which pulls its object (the current user) from the request.

@carltongibson carltongibson added this to the 3.7.0 Release milestone Oct 5, 2017
@carltongibson carltongibson requested a review from tomchristie Oct 5, 2017
@carltongibson carltongibson merged commit 2edeb74 into encode:master Oct 5, 2017
1 check passed
@carltongibson carltongibson deleted the 37/is-list-recognise-retrieve-view branch Oct 5, 2017
@matteius
Copy link
Contributor

matteius commented Oct 5, 2017

Just curious if the schema ever inspects a view that is called .as_view() from a URL? The reason I never got that fully working before I think is my test was on a view that was referenced this way, so the as_view() version couldn't resolve that it was an instance of the RetrieveModelMixin. Just curious, thanks!

@carltongibson
Copy link
Collaborator Author

carltongibson commented Oct 5, 2017

@matteius Hi. Yes, I looked again at your test case today. I noticed the as_view() — which isn't right, so why it didn't work.

as_view() returns a callback that takes a request, instantiates a view instance, calls dispatch on it and returns the response.

Calling isinstance on this callback (obviously, once you've seen it) doesn't give the result we're after. We need the view inside the wrapper.

SchemaGenerator has a whole method for mapping callbacks back to views:

def create_view(self, callback, method, request=None):
"""
Given a callback, return an actual view instance.
"""
view = callback.cls()
for attr, val in getattr(callback, 'initkwargs', {}).items():
setattr(view, attr, val)
view.args = ()
view.kwargs = {}
view.format_kwarg = None
view.request = None
view.action_map = getattr(callback, 'actions', None)
actions = getattr(callback, 'actions', None)
if actions is not None:
if method == 'OPTIONS':
view.action = 'metadata'
else:
view.action = actions.get(method.lower())
if request is not None:
view.request = clone_request(request, method)
return view

It's this that we inspect to generate the schema.

@matteius
Copy link
Contributor

matteius commented Oct 5, 2017

Oh wow, so its a solved problem then 💯 Thanks for helping me understand the existing code base better!

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

Successfully merging this pull request may close these issues.

None yet

2 participants