__init__.py 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. import collections
  2. import inspect
  3. from django.apps import AppConfig
  4. from django.template.loader import get_template
  5. from django.utils.module_loading import import_string
  6. from extras.registry import registry
  7. # Initialize plugin registry stores
  8. registry['plugin_template_content_classes'] = collections.defaultdict(list)
  9. registry['plugin_nav_menu_links'] = {}
  10. #
  11. # Plugin AppConfig class
  12. #
  13. class PluginConfig(AppConfig):
  14. """
  15. Subclass of Django's built-in AppConfig class, to be used for NetBox plugins.
  16. """
  17. # Plugin metadata
  18. author = ''
  19. author_email = ''
  20. description = ''
  21. version = ''
  22. # Root URL path under /plugins. If not set, the plugin's label will be used.
  23. base_url = None
  24. # Minimum/maximum compatible versions of NetBox
  25. min_version = None
  26. max_version = None
  27. # Default configuration parameters
  28. default_settings = {}
  29. # Mandatory configuration parameters
  30. required_settings = []
  31. # Middleware classes provided by the plugin
  32. middleware = []
  33. # Caching configuration
  34. caching_config = {}
  35. # Default integration paths. Plugin authors can override these to customize the paths to
  36. # integrated components.
  37. template_content_classes = 'template_content.template_content_classes'
  38. menu_items = 'navigation.menu_items'
  39. def ready(self):
  40. # Register template content
  41. try:
  42. class_list = import_string(f"{self.__module__}.{self.template_content_classes}")
  43. register_template_content_classes(class_list)
  44. except ImportError:
  45. pass
  46. # Register navigation menu items (if defined)
  47. try:
  48. menu_items = import_string(f"{self.__module__}.{self.menu_items}")
  49. register_menu_items(self.verbose_name, menu_items)
  50. except ImportError:
  51. pass
  52. #
  53. # Template content injection
  54. #
  55. class PluginTemplateContent:
  56. """
  57. This class is used to register plugin content to be injected into core NetBox templates.
  58. It contains methods that are overridden by plugin authors to return template content.
  59. The `model` attribute on the class defines the which model detail page this class renders
  60. content for. It should be set as a string in the form '<app_label>.<model_name>'.
  61. """
  62. model = None
  63. def __init__(self, obj, context):
  64. self.obj = obj
  65. self.context = context
  66. def render(self, template, extra_context=None):
  67. """
  68. Convenience menthod for rendering the provided template name. The detail page object is automatically
  69. passed into the template context as `obj` and the origional detail page's context is available as
  70. `obj_context`. An additional context dictionary may be passed as `extra_context`.
  71. """
  72. context = {
  73. 'obj': self.obj,
  74. 'obj_context': self.context
  75. }
  76. if isinstance(extra_context, dict):
  77. context.update(extra_context)
  78. return get_template(template).render(context)
  79. def left_page(self):
  80. """
  81. Content that will be rendered on the left of the detail page view. Content should be returned as an
  82. HTML string. Note that content does not need to be marked as safe because this is automatically handled.
  83. """
  84. raise NotImplementedError
  85. def right_page(self):
  86. """
  87. Content that will be rendered on the right of the detail page view. Content should be returned as an
  88. HTML string. Note that content does not need to be marked as safe because this is automatically handled.
  89. """
  90. raise NotImplementedError
  91. def full_width_page(self):
  92. """
  93. Content that will be rendered within the full width of the detail page view. Content should be returned as an
  94. HTML string. Note that content does not need to be marked as safe because this is automatically handled.
  95. """
  96. raise NotImplementedError
  97. def buttons(self):
  98. """
  99. Buttons that will be rendered and added to the existing list of buttons on the detail page view. Content
  100. should be returned as an HTML string. Note that content does not need to be marked as safe because this is
  101. automatically handled.
  102. """
  103. raise NotImplementedError
  104. def register_template_content_classes(class_list):
  105. """
  106. Register a list of PluginTemplateContent classes
  107. """
  108. # Validation
  109. for template_content_class in class_list:
  110. if not inspect.isclass(template_content_class):
  111. raise TypeError('Plugin content class {} was passes as an instance!'.format(template_content_class))
  112. if not issubclass(template_content_class, PluginTemplateContent):
  113. raise TypeError('{} is not a subclass of extras.plugins.PluginTemplateContent!'.format(template_content_class))
  114. if template_content_class.model is None:
  115. raise TypeError('Plugin content class {} does not define a valid model!'.format(template_content_class))
  116. registry['plugin_template_content_classes'][template_content_class.model].append(template_content_class)
  117. #
  118. # Navigation menu links
  119. #
  120. class PluginNavMenuLink:
  121. """
  122. This class represents a nav menu item. This constitutes primary link and its text, but also allows for
  123. specifying additional link buttons that appear to the right of the item in the van menu.
  124. Links are specified as Django reverse URL strings.
  125. Buttons are each specified as a list of PluginNavMenuButton instances.
  126. """
  127. def __init__(self, link, link_text, permission=None, buttons=None):
  128. self.link = link
  129. self.link_text = link_text
  130. self.permission = permission
  131. if buttons is None:
  132. self.buttons = []
  133. else:
  134. self.buttons = buttons
  135. class PluginNavMenuButton:
  136. """
  137. This class represents a button which is a part of the nav menu link item.
  138. Note that button colors should come from ButtonColorChoices
  139. """
  140. def __init__(self, link, title, icon_class, color, permission=None):
  141. self.link = link
  142. self.title = title
  143. self.icon_class = icon_class
  144. self.color = color
  145. self.permission = permission
  146. def register_menu_items(section_name, class_list):
  147. """
  148. Register a list of PluginNavMenuLink instances for a given menu section (e.g. plugin name)
  149. """
  150. # Validation
  151. for menu_link in class_list:
  152. if not isinstance(menu_link, PluginNavMenuLink):
  153. raise TypeError(f"{menu_link} must be an instance of extras.plugins.PluginNavMenuLink")
  154. for button in menu_link.buttons:
  155. if not isinstance(button, PluginNavMenuButton):
  156. raise TypeError(f"{button} must be an instance of extras.plugins.PluginNavMenuButton")
  157. registry['plugin_nav_menu_links'][section_name] = class_list