Coverage for netbox/views/generic/base.py: 53%

32 statements  

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

1from django.core.exceptions import ImproperlyConfigured 

2from django.shortcuts import get_object_or_404 

3from django.views.generic import View 

4 

5from utilities.views import ObjectPermissionRequiredMixin 

6 

7__all__ = ( 

8 'BaseMultiObjectView', 

9 'BaseObjectView', 

10) 

11 

12 

13class BaseObjectView(ObjectPermissionRequiredMixin, View): 

14 """ 

15 Base class for generic views which display or manipulate a single object. 

16 

17 Attributes: 

18 queryset: Django QuerySet from which the object(s) will be fetched 

19 template_name: The name of the HTML template file to render 

20 """ 

21 queryset = None 

22 template_name = None 

23 

24 def dispatch(self, request, *args, **kwargs): 

25 self.queryset = self.get_queryset(request) 

26 return super().dispatch(request, *args, **kwargs) 

27 

28 def get_queryset(self, request): 

29 """ 

30 Return the base queryset for the view. By default, this returns `self.queryset.all()`. 

31 

32 Args: 

33 request: The current request 

34 """ 

35 if self.queryset is None: 

36 raise ImproperlyConfigured( 

37 f"{self.__class__.__name__} does not define a queryset. Set queryset on the class or " 

38 f"override its get_queryset() method." 

39 ) 

40 return self.queryset.all() 

41 

42 def get_object(self, **kwargs): 

43 """ 

44 Return the object being viewed or modified. The object is identified by an arbitrary set of keyword arguments 

45 gleaned from the URL, which are passed to `get_object_or_404()`. (Typically, only a primary key is needed.) 

46 

47 If no matching object is found, return a 404 response. 

48 """ 

49 return get_object_or_404(self.queryset, **kwargs) 

50 

51 def get_extra_context(self, request, instance): 

52 """ 

53 Return any additional context data to include when rendering the template. 

54 

55 Args: 

56 request: The current request 

57 instance: The object being viewed 

58 """ 

59 return {} 

60 

61 

62class BaseMultiObjectView(ObjectPermissionRequiredMixin, View): 

63 """ 

64 Base class for generic views which display or manipulate multiple objects. 

65 

66 Attributes: 

67 queryset: Django QuerySet from which the object(s) will be fetched 

68 table: The django-tables2 Table class used to render the objects list 

69 template_name: The name of the HTML template file to render 

70 """ 

71 queryset = None 

72 table = None 

73 template_name = None 

74 

75 def dispatch(self, request, *args, **kwargs): 

76 self.queryset = self.get_queryset(request) 

77 return super().dispatch(request, *args, **kwargs) 

78 

79 def get_queryset(self, request): 

80 """ 

81 Return the base queryset for the view. By default, this returns `self.queryset.all()`. 

82 

83 Args: 

84 request: The current request 

85 """ 

86 if self.queryset is None: 

87 raise ImproperlyConfigured( 

88 f"{self.__class__.__name__} does not define a queryset. Set queryset on the class or " 

89 f"override its get_queryset() method." 

90 ) 

91 return self.queryset.all() 

92 

93 def get_extra_context(self, request): 

94 """ 

95 Return any additional context data to include when rendering the template. 

96 

97 Args: 

98 request: The current request 

99 """ 

100 return {}