def get_object_name(self, path: str, method: str, action_name: str) -> str: action = get_action(path, method, self.view) # Try to deduce the ID from the view's model model = getattr(getattr(self.view, 'queryset', None), 'model', None) if model is not None: return model.__name__ # Try with the serializer class name if isinstance(self.view, SerializerClassMapApiView): serializer_class = self.view.get_serializer_class('response', action) if serializer_class: object_name = serializer_class.__name__ if object_name.endswith('Serializer'): object_name = object_name[:-10] return object_name object_name = self.get_object_name_by_view_class_name( clean_suffixes=['APIView', 'View', 'ViewSet']) # Due to camel-casing of classes and `action` being lowercase, apply title in order to find if action truly # comes at the end of the name if object_name.endswith(action_name.title()): # ListView, UpdateAPIView, ThingDelete ... object_name = object_name[:-len(action_name)] return object_name
def get_action_name(self, path: str, method: str) -> str: action = get_action(path, method, self.view) if is_list_view(path, method, self.view): return 'list' elif action not in self.method_mapping: return action.lower() else: return self.method_mapping[method.lower()].lower()
def get_content_schema_by_type(self, path: str, method: str, schema_type: str) -> OpenAPISchema: content_schema = {} if schema_type == 'responses': resources_schema_method_name = 'get_resources_response_schema' schema_method_name = 'get_response_schema' else: resources_schema_method_name = 'get_resources_request_body_schema' schema_method_name = 'get_request_body_schema' vendor = getattr(settings, 'API_VENDOR_STRING', 'vendor').lower() default_content_type = f'application/vnd.{vendor}' resources_schema_method = getattr(self, resources_schema_method_name) content_schema[default_content_type] = { 'schema': resources_schema_method(path, method) } if not self.generator: return content_schema version_content_type = f'{default_content_type}.{self.generator.api_version}' for resource, handler in self.view.resource_handlers_map.items(): view = self.generator.create_view(handler, method, request=self.view.request) if not hasattr(view, get_action(path, method, view)): continue schema_method = getattr(view.schema, schema_method_name) default_resource_schema = schema_method(path, method) resource_content_type = f'{version_content_type}-{resource}' content_schema[resource_content_type] = { 'schema': default_resource_schema } for api_format in self.generator.api_formats: if api_format != self.generator.api_default_format: resource_schema = schema_method(path, method, api_format=api_format) if resource_schema != default_resource_schema: content_type = f'{resource_content_type}.{api_format}' content_schema[content_type] = { 'schema': resource_schema } return content_schema
def get_meta_schema( self, path: str, method: str, api_format: str = None) -> typing.Optional[OpenAPISchema]: if issubclass(self.view.__class__, SerializerClassMapApiView): action = get_action(path, method, self.view) serializer_class = self.view.get_serializer_class( 'meta', action, api_format, use_default=False) if serializer_class is None: return None serializer = serializer_class() return self.get_serializer_schema(serializer)
def get_resources_request_body_schema(self, path: str, method: str) -> OpenAPISchema: schemas = {} if self.generator: for resource, handler in self.get_resources(method).items(): view = self.generator.create_view(handler, method, request=self.view.request) if hasattr(view, get_action(path, method, view)): schemas[resource] = view.schema.get_request_body_schema(path, method) list_schemas = [] for schema in schemas.values(): if schema not in list_schemas: list_schemas.append(schema) if len(list_schemas) == 1: return list_schemas[0] return {'oneOf': list_schemas}
def get_action_code_schemas(self, path: str, method: str) -> typing.Iterator[CodeActionSchemaTuple]: success_codes_seen: typing.Set[str] = set() error_codes_seen: typing.Set[str] = set() action = get_action(path, method, self.view) kwargs_variants = get_action_map_kwargs(action, getattr(self.view, 'schema_action_codes_map', None)) for kwargs_variant in kwargs_variants: for code, action_schema in get_action_code_schemas_from_map(*kwargs_variant): if code > '400': codes_seen_set = error_codes_seen else: codes_seen_set = success_codes_seen if code not in codes_seen_set: codes_seen_set.add(code) if action_schema is not None: yield normalize_action_schema(code, action_schema)
def get_serializer( self, path: str, method: str, stage: str, api_format: str = None, ) -> typing.Optional[BaseSerializer]: view = self.view action = get_action(path, method, view) serializer_class = None if issubclass(view.__class__, SerializerClassMapApiView): serializer_class = view.get_serializer_class(stage, action, api_format) if serializer_class is None: serializer = view.get_serializer() else: serializer = serializer_class() return serializer