Make view a property

Encapsulates check for a view instance.
This commit is contained in:
Carlton Gibson 2017-08-23 11:28:20 +02:00
parent 183f13ccf4
commit dc92b55c84

View File

@ -263,10 +263,23 @@ class APIViewSchemaDescriptor(object):
Responsible for per-view instrospection and schema generation. Responsible for per-view instrospection and schema generation.
""" """
def __get__(self, instance, owner): def __get__(self, instance, owner):
# ???: Is this TOO simple? (Option is to return a new instance each time.)
self.view = instance self.view = instance
return self 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): def get_link(self, path, method, base_url):
""" """
Generate `coreapi.Link` for self.view, path and method. Generate `coreapi.Link` for self.view, path and method.
@ -279,10 +292,6 @@ class APIViewSchemaDescriptor(object):
* method: The HTTP request method. * method: The HTTP request method.
* base_url: The project "mount point" as given to SchemaGenerator * 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_path_fields(path, method)
fields += self.get_serializer_fields(path, method) fields += self.get_serializer_fields(path, method)
fields += self.get_pagination_fields(path, method) fields += self.get_pagination_fields(path, method)