views.py 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  1. from typing import Iterable
  2. from django.conf import settings
  3. from django.contrib.auth.mixins import AccessMixin
  4. from django.core.exceptions import ImproperlyConfigured
  5. from django.urls import reverse
  6. from django.urls.exceptions import NoReverseMatch
  7. from django.utils.http import url_has_allowed_host_and_scheme
  8. from django.utils.translation import gettext_lazy as _
  9. from netbox.plugins import PluginConfig
  10. from netbox.registry import registry
  11. from utilities.relations import get_related_models
  12. from .permissions import resolve_permission
  13. __all__ = (
  14. 'ConditionalLoginRequiredMixin',
  15. 'ContentTypePermissionRequiredMixin',
  16. 'GetRelatedModelsMixin',
  17. 'GetReturnURLMixin',
  18. 'ObjectPermissionRequiredMixin',
  19. 'ViewTab',
  20. 'get_viewname',
  21. 'register_model_view',
  22. )
  23. #
  24. # View Mixins
  25. #
  26. class ConditionalLoginRequiredMixin(AccessMixin):
  27. """
  28. Similar to Django's LoginRequiredMixin, but enforces authentication only if LOGIN_REQUIRED is True.
  29. """
  30. def dispatch(self, request, *args, **kwargs):
  31. if settings.LOGIN_REQUIRED and not request.user.is_authenticated:
  32. return self.handle_no_permission()
  33. return super().dispatch(request, *args, **kwargs)
  34. class ContentTypePermissionRequiredMixin(ConditionalLoginRequiredMixin):
  35. """
  36. Similar to Django's built-in PermissionRequiredMixin, but extended to check model-level permission assignments.
  37. This is related to ObjectPermissionRequiredMixin, except that it does not enforce object-level permissions,
  38. and fits within NetBox's custom permission enforcement system.
  39. additional_permissions: An optional iterable of statically declared permissions to evaluate in addition to those
  40. derived from the object type
  41. """
  42. additional_permissions = list()
  43. def get_required_permission(self):
  44. """
  45. Return the specific permission necessary to perform the requested action on an object.
  46. """
  47. raise NotImplementedError(_("{self.__class__.__name__} must implement get_required_permission()").format(
  48. class_name=self.__class__.__name__
  49. ))
  50. def has_permission(self):
  51. user = self.request.user
  52. permission_required = self.get_required_permission()
  53. # Check that the user has been granted the required permission(s).
  54. if user.has_perms((permission_required, *self.additional_permissions)):
  55. return True
  56. return False
  57. def dispatch(self, request, *args, **kwargs):
  58. if not self.has_permission():
  59. return self.handle_no_permission()
  60. return super().dispatch(request, *args, **kwargs)
  61. class ObjectPermissionRequiredMixin(ConditionalLoginRequiredMixin):
  62. """
  63. Similar to Django's built-in PermissionRequiredMixin, but extended to check for both model-level and object-level
  64. permission assignments. If the user has only object-level permissions assigned, the view's queryset is filtered
  65. to return only those objects on which the user is permitted to perform the specified action.
  66. additional_permissions: An optional iterable of statically declared permissions to evaluate in addition to those
  67. derived from the object type
  68. """
  69. additional_permissions = list()
  70. def get_required_permission(self):
  71. """
  72. Return the specific permission necessary to perform the requested action on an object.
  73. """
  74. raise NotImplementedError(_("{class_name} must implement get_required_permission()").format(
  75. class_name=self.__class__.__name__
  76. ))
  77. def has_permission(self):
  78. user = self.request.user
  79. permission_required = self.get_required_permission()
  80. # Check that the user has been granted the required permission(s).
  81. if user.has_perms((permission_required, *self.additional_permissions)):
  82. # Update the view's QuerySet to filter only the permitted objects
  83. action = resolve_permission(permission_required)[1]
  84. self.queryset = self.queryset.restrict(user, action)
  85. return True
  86. return False
  87. def dispatch(self, request, *args, **kwargs):
  88. if not hasattr(self, 'queryset'):
  89. raise ImproperlyConfigured(
  90. _(
  91. '{class_name} has no queryset defined. ObjectPermissionRequiredMixin may only be used on views '
  92. 'which define a base queryset'
  93. ).format(class_name=self.__class__.__name__)
  94. )
  95. if not self.has_permission():
  96. return self.handle_no_permission()
  97. return super().dispatch(request, *args, **kwargs)
  98. class GetReturnURLMixin:
  99. """
  100. Provides logic for determining where a user should be redirected after processing a form.
  101. """
  102. default_return_url = None
  103. def get_return_url(self, request, obj=None):
  104. # First, see if `return_url` was specified as a query parameter or form data. Use this URL only if it's
  105. # considered safe.
  106. return_url = request.GET.get('return_url') or request.POST.get('return_url')
  107. if return_url and url_has_allowed_host_and_scheme(return_url, allowed_hosts=None):
  108. return return_url
  109. # Next, check if the object being modified (if any) has an absolute URL.
  110. if obj is not None and obj.pk and hasattr(obj, 'get_absolute_url'):
  111. return obj.get_absolute_url()
  112. # Fall back to the default URL (if specified) for the view.
  113. if self.default_return_url is not None:
  114. return reverse(self.default_return_url)
  115. # Attempt to dynamically resolve the list view for the object
  116. if hasattr(self, 'queryset'):
  117. model_opts = self.queryset.model._meta
  118. try:
  119. return reverse(f'{model_opts.app_label}:{model_opts.model_name}_list')
  120. except NoReverseMatch:
  121. pass
  122. # If all else fails, return home. Ideally this should never happen.
  123. return reverse('home')
  124. class GetRelatedModelsMixin:
  125. """
  126. Provides logic for collecting all related models for the currently viewed model.
  127. """
  128. def get_related_models(self, request, instance, omit=[], extra=[]):
  129. """
  130. Get related models of the view's `queryset` model without those listed in `omit`. Will be sorted alphabetical.
  131. Args:
  132. request: Current request being processed.
  133. instance: The instance related models should be looked up for. A list of instances can be passed to match
  134. related objects in this list (e.g. to find sites of a region including child regions).
  135. omit: Remove relationships to these models from the result. Needs to be passed, if related models don't
  136. provide a `_list` view.
  137. extra: Add extra models to the list of automatically determined related models. Can be used to add indirect
  138. relationships.
  139. """
  140. model = self.queryset.model
  141. related = filter(
  142. lambda m: m[0] is not model and m[0] not in omit,
  143. get_related_models(model, False)
  144. )
  145. related_models = [
  146. (
  147. model.objects.restrict(request.user, 'view').filter(**(
  148. {f'{field}__in': instance}
  149. if isinstance(instance, Iterable)
  150. else {field: instance}
  151. )),
  152. f'{field}_id'
  153. )
  154. for model, field in related
  155. ]
  156. related_models.extend(extra)
  157. return sorted(related_models, key=lambda x: x[0].model._meta.verbose_name.lower())
  158. class ViewTab:
  159. """
  160. ViewTabs are used for navigation among multiple object-specific views, such as the changelog or journal for
  161. a particular object.
  162. Args:
  163. label: Human-friendly text
  164. badge: A static value or callable to display alongside the label (optional). If a callable is used, it must
  165. accept a single argument representing the object being viewed.
  166. weight: Numeric weight to influence ordering among other tabs (default: 1000)
  167. permission: The permission required to display the tab (optional).
  168. hide_if_empty: If true, the tab will be displayed only if its badge has a meaningful value. (Tabs without a
  169. badge are always displayed.)
  170. """
  171. def __init__(self, label, badge=None, weight=1000, permission=None, hide_if_empty=False):
  172. self.label = label
  173. self.badge = badge
  174. self.weight = weight
  175. self.permission = permission
  176. self.hide_if_empty = hide_if_empty
  177. def render(self, instance):
  178. """Return the attributes needed to render a tab in HTML."""
  179. badge_value = self._get_badge_value(instance)
  180. if self.badge and self.hide_if_empty and not badge_value:
  181. return None
  182. return {
  183. 'label': self.label,
  184. 'badge': badge_value,
  185. 'weight': self.weight,
  186. }
  187. def _get_badge_value(self, instance):
  188. if not self.badge:
  189. return None
  190. if callable(self.badge):
  191. return self.badge(instance)
  192. return self.badge
  193. #
  194. # Utility functions
  195. #
  196. def get_viewname(model, action=None, rest_api=False):
  197. """
  198. Return the view name for the given model and action, if valid.
  199. :param model: The model or instance to which the view applies
  200. :param action: A string indicating the desired action (if any); e.g. "add" or "list"
  201. :param rest_api: A boolean indicating whether this is a REST API view
  202. """
  203. is_plugin = isinstance(model._meta.app_config, PluginConfig)
  204. app_label = model._meta.app_label
  205. model_name = model._meta.model_name
  206. if rest_api:
  207. viewname = f'{app_label}-api:{model_name}'
  208. if is_plugin:
  209. viewname = f'plugins-api:{viewname}'
  210. if action:
  211. viewname = f'{viewname}-{action}'
  212. else:
  213. viewname = f'{app_label}:{model_name}'
  214. if is_plugin:
  215. viewname = f'plugins:{viewname}'
  216. if action:
  217. viewname = f'{viewname}_{action}'
  218. return viewname
  219. def register_model_view(model, name='', path=None, detail=True, kwargs=None):
  220. """
  221. This decorator can be used to "attach" a view to any model in NetBox. This is typically used to inject
  222. additional tabs within a model's detail view. For example, to add a custom tab to NetBox's dcim.Site model:
  223. @register_model_view(Site, 'myview', path='my-custom-view')
  224. class MyView(ObjectView):
  225. ...
  226. This will automatically create a URL path for MyView at `/dcim/sites/<id>/my-custom-view/` which can be
  227. resolved using the view name `dcim:site_myview'.
  228. Args:
  229. model: The Django model class with which this view will be associated.
  230. name: The string used to form the view's name for URL resolution (e.g. via `reverse()`). This will be appended
  231. to the name of the base view for the model using an underscore. If blank, the model name will be used.
  232. path: The URL path by which the view can be reached (optional). If not provided, `name` will be used.
  233. detail: True if the path applied to an individual object; False if it attaches to the base (list) path.
  234. kwargs: A dictionary of keyword arguments for the view to include when registering its URL path (optional).
  235. """
  236. def _wrapper(cls):
  237. app_label = model._meta.app_label
  238. model_name = model._meta.model_name
  239. if model_name not in registry['views'][app_label]:
  240. registry['views'][app_label][model_name] = []
  241. registry['views'][app_label][model_name].append({
  242. 'name': name,
  243. 'view': cls,
  244. 'path': path if path is not None else name,
  245. 'detail': detail,
  246. 'kwargs': kwargs or {},
  247. })
  248. return cls
  249. return _wrapper