def get_links(self, request=None): links = LinkNode() paths = [] view_endpoints = [] for path, method, callback in self.endpoints: view = self.create_view(callback, method, request) path = self.coerce_path(path, method, view) paths.append(path) view_endpoints.append((path, method, view)) # Only generate the path prefix for paths that will be included if not paths: return None prefix = self.determine_path_prefix(paths) child_path = self.url.split('com')[1][:-1] for path, method, view in view_endpoints: if not self.has_view_permissions(path, method, view): link = self.get_link(path, method, view, base_url='') else: link = view.schema.get_link(path, method, base_url='') link._url = child_path + link._url link._encoding = "multipart/form-data" # 添加下面这一行方便在views编写过程中自定义参数. link._fields += self.get_core_fields(view, method) subpath = path[len(prefix):] keys = self.get_keys(subpath, method, view) # from rest_framework.schemas.generators import LinkNode, insert_into insert_into(links, keys, link) return links
def get_links(self, request=None): """ Return a dictionary containing all the links that should be included in the API schema. """ links = LinkNode() # Generate (path, method, view) given (path, method, callback). paths = [] view_endpoints = [] for path, method, callback in self.endpoints: view = self.create_view(callback, method, request) if getattr(view, 'exclude_from_schema', False): continue path = self.coerce_path(path, method, view) paths.append(path) view_endpoints.append((path, method, view)) # Only generate the path prefix for paths that will be included if not paths: return None prefix = self.determine_path_prefix(paths) for path, method, view in view_endpoints: if not self.has_view_permissions(path, method, view): continue link = self.get_link(path, method, view, version=getattr(request, 'version', None)) subpath = path[len(prefix):] keys = self.get_keys(subpath, method, view) try: insert_into(links, keys, link) except Exception: continue return links
def get_links(self, request=None): # from rest_framework.schemas.generators import LinkNode, links = LinkNode() paths = [] view_endpoints = [] for path, method, callback in self.endpoints: view = self.create_view(callback, method, request) path = self.coerce_path(path, method, view) paths.append(path) view_endpoints.append((path, method, view)) # Only generate the path prefix for paths that will be included if not paths: return None prefix = self.determine_path_prefix(paths) for path, method, view in view_endpoints: if not self.has_view_permissions(path, method, view): continue link = view.schema.get_link(path, method, base_url=self.url) # 添加下面这一行方便在views编写过程中自定义参数. link._fields += self.get_core_fields(view) subpath = path[len(prefix):] keys = self.get_keys(subpath, method, view) # from rest_framework.schemas.generators import LinkNode, insert_into insert_into(links, keys, link) return links
def get_links(self, request=None): """ Return a dictionary containing all the links that should be included in the API schema. """ links = LinkNode() # Generate (path, method, view) given (path, method, callback). paths = [] view_endpoints = [] for path_with_kwargs, method, callback in self.endpoints: path, kwargs = path_with_kwargs view = self.create_view(callback, method, request) view.kwargs = kwargs path = self.coerce_path(path, method, view) paths.append(path) view_endpoints.append((path, method, view)) # Only generate the path prefix for paths that will be included if not paths: return None prefix = self.determine_path_prefix(paths) for path, method, view in view_endpoints: if not self.has_view_permissions(path, method, view): continue link = view.schema.get_link(path, method, base_url=self.url) subpath = path[len(prefix):] keys = self.get_keys(subpath, method, view) insert_into(links, keys, link) return links
def get_links(self, request=None): links = LinkNode() # Generate (path, method, view) given (path, method, callback). paths = [] view_endpoints = [] for path, method, callback in self.endpoints: view = self.create_view(callback, method, request) path = self.coerce_path(path, method, view) paths.append(path) view_endpoints.append((path, method, view)) # Only generate the path prefix for paths that will be included if not paths: return None prefix = self.determine_path_prefix(paths) for path, method, view in view_endpoints: if not self.has_view_permissions(path, method, view): continue fields = view.schema.get_path_fields(path, method) fields += view.schema.get_serializer_fields(path, method) fields += view.schema.get_pagination_fields(path, method) fields += view.schema.get_filter_fields(path, method) manual_fields = view.schema.get_manual_fields(path, method) fields = view.schema.update_fields(fields, manual_fields) if fields and any( [field.location in ('form', 'body') for field in fields]): encoding = view.schema.get_encoding(path, method) else: encoding = None description = view.schema.get_description(path, method) if description and len(description) > 0: query_fields, description = self.get_docstring_fields( description) fields += query_fields if self.url and path.startswith('/'): path = path[1:] link = coreapi.Link(url=urlparse.urljoin(self.url, path), action=method.lower(), encoding=encoding, fields=fields, description=description) subpath = path[len(prefix):] keys = self.get_keys(subpath, method, view) insert_into(links, keys, link) return links
def insert_into(target, keys, value): """ Nested dictionary insertion. 这个方法是在django-money的文件找到的 """ for key in keys[:-1]: if key not in target: target[key] = LinkNode() target = target[key] try: target.links.append((keys[-1], value)) except TypeError: msg = INSERT_INTO_COLLISION_FMT.format(value_url=value.url, target_url=target.url, keys=keys) raise ValueError(msg)
def get_links(self, request=None): """Almost copy of parent, here I use subpath to create the link and save the base path Also I call the new get definitions, which generate object definitions from serializers ued in views""" links = LinkNode() # Generate (path, method, view) given (path, method, callback). paths = [] view_endpoints = [] for path, method, callback in self.endpoints: view = self.create_view(callback, method, request) if getattr(view, 'exclude_from_schema', False): continue path = self.coerce_path(path, method, view) paths.append(path) view_endpoints.append((path, method, view)) # Only generate the path prefix for paths that will be included if not paths: return None self.prefix = self.determine_path_prefix(paths) for path, method, view in view_endpoints: if self.check_view_permissions and not self.has_view_permissions( path, method, view): continue prefix_len = len(self.prefix) if prefix_len == 1: # meaning prefix == '/', in other words there is not a common prefix subpath = path else: subpath = path[prefix_len:] link = view.schema.get_link(subpath, method, base_url=self.url) keys = self.get_keys(subpath, method, view) insert_into(links, keys, link) obj_def = self.add_object_definitions(method, view) if obj_def: if obj_def.title not in self.definitions: self.definitions[obj_def.title] = obj_def return links
def get_links(self, request=None): links = LinkNode() # Generate (path, method, view) given (path, method, callback). paths = [] view_endpoints = [] for path, method, callback in self.endpoints: view = self.create_view(callback, method, request) path = self.coerce_path(path, method, view) paths.append(path) view_endpoints.append((path, method, view)) # Only generate the path prefix for paths that will be included if not paths: return None prefix = self.determine_path_prefix(paths) for path, method, view in view_endpoints: if not self.has_view_permissions(path, method, view): continue link = view.schema.get_link(path, method, base_url=self.url) fields = list(link.fields) yaml_doc = None method_desc = link.description # 如果存在视图注释 取视图注释 否则 取方法注释 doc_desc = view.__doc__ if view and view.__doc__ else method_desc # 尝试用yaml解析注释 try: yaml_doc = yaml.load(doc_desc) except: yaml_doc = None if yaml_doc and type(yaml_doc) != str: params = yaml_doc.get('parameters', []) method_desc = yaml_doc.get('description', '') _schema = coreschema.String() for i in params: _name = i.get('name') _desc = i.get('description') _required = i.get('required', False) _type = i.get('type', None) _type = 'integer' if _type == 'int' else _type _location = i.get('location', 'formData') field = coreapi.Field( name=_name, location=_location, required=_required, description=_desc, type=_type, schema=_schema, example='', ) fields.append(field) else: pass # 因为Link类被设置为无法修改属性 只能新建一个 nl = coreapi.Link( url=link.url, action=link.action, encoding=link.encoding, fields=tuple(fields), description=method_desc, ) subpath = path[len(prefix):] keys = self.get_keys(subpath, method, view) insert_into(links, keys, nl) return links