Coverage for netbox/plugins/templates.py: 45%

29 statements  

« prev     ^ index     » next       coverage.py v7.15.2, created at 2026-10-10 18:35 +0000

1from django.template.loader import get_template 

2from django.utils.translation import gettext as _ 

3 

4__all__ = ( 

5 'PluginTemplateExtension', 

6) 

7 

8 

9class PluginTemplateExtension: 

10 """ 

11 This class is used to register plugin content to be injected into core NetBox templates. It contains methods 

12 that are overridden by plugin authors to return template content. 

13 

14 The `models` attribute on the class defines the which specific model detail pages this class renders content 

15 for. It should be defined as a list of strings in the following form: 

16 

17 models = ['<app_label>.<model_name>', '<app_label>.<model_name>'] 

18 

19 If `models` is left as None, the extension will render for _all_ models. 

20 

21 The `render()` method provides the following context data: 

22 

23 * object - The object being viewed (object views only) 

24 * model - The type of object being viewed (list views only) 

25 * request - The current request 

26 * settings - Global NetBox settings 

27 * config - Plugin-specific configuration parameters 

28 """ 

29 models = None 

30 

31 def __init__(self, context): 

32 self.context = context 

33 

34 def render(self, template_name, extra_context=None): 

35 """ 

36 Convenience method for rendering the specified Django template using the default context data. An additional 

37 context dictionary may be passed as `extra_context`. 

38 """ 

39 if extra_context is None: 

40 extra_context = {} 

41 elif not isinstance(extra_context, dict): 

42 raise TypeError(_("extra_context must be a dictionary")) 

43 

44 return get_template(template_name).render({**self.context, **extra_context}) 

45 

46 # 

47 # Global methods 

48 # 

49 

50 def head(self): 

51 """ 

52 HTML returned by this method will be inserted in the page's `<head>` block. This may be useful e.g. for 

53 including additional Javascript or CSS resources. 

54 """ 

55 raise NotImplementedError 

56 

57 def navbar(self): 

58 """ 

59 Content that will be rendered inside the top navigation menu. Content should be returned as an HTML 

60 string. Note that content does not need to be marked as safe because this is automatically handled. 

61 """ 

62 raise NotImplementedError 

63 

64 # 

65 # Object list views 

66 # 

67 

68 def list_buttons(self): 

69 """ 

70 Buttons that will be rendered and added to the existing list of buttons on the list view. Content 

71 should be returned as an HTML string. Note that content does not need to be marked as safe because this is 

72 automatically handled. 

73 """ 

74 raise NotImplementedError 

75 

76 # 

77 # Object detail views 

78 # 

79 

80 def buttons(self): 

81 """ 

82 Buttons that will be rendered and added to the existing list of buttons on the detail page view. Content 

83 should be returned as an HTML string. Note that content does not need to be marked as safe because this is 

84 automatically handled. 

85 """ 

86 raise NotImplementedError 

87 

88 def alerts(self): 

89 """ 

90 Arbitrary content to be inserted at the top of an object's detail view. Content should be returned as an 

91 HTML string. Note that content does not need to be marked as safe because this is automatically handled. 

92 """ 

93 raise NotImplementedError 

94 

95 def left_page(self): 

96 """ 

97 Content that will be rendered on the left of the detail page view. Content should be returned as an 

98 HTML string. Note that content does not need to be marked as safe because this is automatically handled. 

99 """ 

100 raise NotImplementedError 

101 

102 def right_page(self): 

103 """ 

104 Content that will be rendered on the right of the detail page view. Content should be returned as an 

105 HTML string. Note that content does not need to be marked as safe because this is automatically handled. 

106 """ 

107 raise NotImplementedError 

108 

109 def full_width_page(self): 

110 """ 

111 Content that will be rendered within the full width of the detail page view. Content should be returned as an 

112 HTML string. Note that content does not need to be marked as safe because this is automatically handled. 

113 """ 

114 raise NotImplementedError