Coverage for cookbook/schemas.py: 42%

36 statements  

« prev     ^ index     » next       coverage.py v7.4.0, created at 2023-12-29 00:47 +0100

1from rest_framework.schemas.openapi import AutoSchema 

2from rest_framework.schemas.utils import is_list_view 

3 

4 

5class QueryParam(object): 

6 def __init__(self, name, description=None, qtype='string', required=False): 

7 self.name = name 

8 self.description = description 

9 self.qtype = qtype 

10 self.required = required 

11 

12 def __str__(self): 

13 return f'{self.name}, {self.qtype}, {self.description}' 

14 

15 

16class QueryParamAutoSchema(AutoSchema): 

17 def get_path_parameters(self, path, method): 

18 if not is_list_view(path, method, self.view): 

19 return super().get_path_parameters(path, method) 

20 parameters = super().get_path_parameters(path, method) 

21 for q in self.view.query_params: 

22 parameters.append({ 

23 "name": q.name, "in": "query", "required": q.required, 

24 "description": q.description, 

25 'schema': {'type': q.qtype, }, 

26 }) 

27 

28 return parameters 

29 

30 

31class TreeSchema(AutoSchema): 

32 def get_path_parameters(self, path, method): 

33 if not is_list_view(path, method, self.view): 

34 return super(TreeSchema, self).get_path_parameters(path, method) 

35 

36 api_name = path.split('/')[2] 

37 parameters = super().get_path_parameters(path, method) 

38 parameters.append({ 

39 "name": 'query', "in": "query", "required": False, 

40 "description": 'Query string matched against {} name.'.format(api_name), 

41 'schema': {'type': 'string', }, 

42 }) 

43 parameters.append({ 

44 "name": 'root', "in": "query", "required": False, 

45 "description": 'Return first level children of {obj} with ID [int]. Integer 0 will return root {obj}s.'.format( 

46 obj=api_name), 

47 'schema': {'type': 'int', }, 

48 }) 

49 parameters.append({ 

50 "name": 'tree', "in": "query", "required": False, 

51 "description": 'Return all self and children of {} with ID [int].'.format(api_name), 

52 'schema': {'type': 'int', }, 

53 }) 

54 return parameters 

55 

56 

57class FilterSchema(AutoSchema): 

58 def get_path_parameters(self, path, method): 

59 if not is_list_view(path, method, self.view): 

60 return super(FilterSchema, self).get_path_parameters(path, method) 

61 

62 api_name = path.split('/')[2] 

63 parameters = super().get_path_parameters(path, method) 

64 parameters.append({ 

65 "name": 'query', "in": "query", "required": False, 

66 "description": 'Query string matched against {} name.'.format(api_name), 

67 'schema': {'type': 'string', }, 

68 }) 

69 return parameters