From dc92b55c8455999d86dec408164e65a1effeb1b7 Mon Sep 17 00:00:00 2001 From: Carlton Gibson Date: Wed, 23 Aug 2017 11:28:20 +0200 Subject: [PATCH] Make `view` a property Encapsulates check for a view instance. --- rest_framework/schemas.py | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/rest_framework/schemas.py b/rest_framework/schemas.py index b15085eda..b49ed7f6a 100644 --- a/rest_framework/schemas.py +++ b/rest_framework/schemas.py @@ -263,10 +263,23 @@ class APIViewSchemaDescriptor(object): Responsible for per-view instrospection and schema generation. """ def __get__(self, instance, owner): - # ???: Is this TOO simple? (Option is to return a new instance each time.) self.view = instance return self + @property + def view(self): + """View property.""" + assert self._view is not None, "Schema generation REQUIRES a view instance. (Hint: you accessed `schema` from the view class rather than an instance.)" + return self._view + + @view.setter + def view(self, value): + self._view = value + + @view.deleter + def view(self): + self._view = None + def get_link(self, path, method, base_url): """ Generate `coreapi.Link` for self.view, path and method. @@ -279,10 +292,6 @@ class APIViewSchemaDescriptor(object): * method: The HTTP request method. * base_url: The project "mount point" as given to SchemaGenerator """ - # TODO: make `view` a property: move this check to getter. - assert self.view is not None, "Schema generation REQUIRES a view instance. (Hint: you accessed `schema` from the view CLASS rather than an instance.)" - view = self.view - fields = self.get_path_fields(path, method) fields += self.get_serializer_fields(path, method) fields += self.get_pagination_fields(path, method)