__init__.py 7.3 KB

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