1
0
mirror of https://github.com/netbox-community/netbox.git synced 2024-05-10 07:54:54 +00:00

#10739: Collapse BaseView class

This commit is contained in:
jeremystretch
2022-12-12 16:32:55 -05:00
parent e338f7cfe3
commit aacf606999

View File

@ -4,9 +4,22 @@ from django.views.generic import View
from utilities.views import ObjectPermissionRequiredMixin from utilities.views import ObjectPermissionRequiredMixin
__all__ = (
'BaseObjectView',
'BaseMultiObjectView',
)
class BaseView(ObjectPermissionRequiredMixin, View):
class BaseObjectView(ObjectPermissionRequiredMixin, View):
"""
Base class for generic views which display or manipulate a single object.
Attributes:
queryset: Django QuerySet from which the object(s) will be fetched
template_name: The name of the HTML template file to render
"""
queryset = None queryset = None
template_name = None
def dispatch(self, request, *args, **kwargs): def dispatch(self, request, *args, **kwargs):
self.queryset = self.get_queryset(request) self.queryset = self.get_queryset(request)
@ -14,7 +27,7 @@ class BaseView(ObjectPermissionRequiredMixin, View):
def get_queryset(self, request): def get_queryset(self, request):
""" """
Return the base queryset for the view. By default, this returns self.queryset.all(). Return the base queryset for the view. By default, this returns `self.queryset.all()`.
Args: Args:
request: The current request request: The current request
@ -26,17 +39,6 @@ class BaseView(ObjectPermissionRequiredMixin, View):
) )
return self.queryset.all() return self.queryset.all()
class BaseObjectView(BaseView):
"""
Base class for generic views which display or manipulate a single object.
Attributes:
queryset: Django QuerySet from which the object(s) will be fetched
template_name: The name of the HTML template file to render
"""
template_name = None
def get_object(self, **kwargs): def get_object(self, **kwargs):
""" """
Return the object being viewed or modified. The object is identified by an arbitrary set of keyword arguments Return the object being viewed or modified. The object is identified by an arbitrary set of keyword arguments
@ -57,7 +59,7 @@ class BaseObjectView(BaseView):
return {} return {}
class BaseMultiObjectView(BaseView): class BaseMultiObjectView(ObjectPermissionRequiredMixin, View):
""" """
Base class for generic views which display or manipulate multiple objects. Base class for generic views which display or manipulate multiple objects.
@ -66,9 +68,28 @@ class BaseMultiObjectView(BaseView):
table: The django-tables2 Table class used to render the objects list table: The django-tables2 Table class used to render the objects list
template_name: The name of the HTML template file to render template_name: The name of the HTML template file to render
""" """
queryset = None
table = None table = None
template_name = None template_name = None
def dispatch(self, request, *args, **kwargs):
self.queryset = self.get_queryset(request)
return super().dispatch(request, *args, **kwargs)
def get_queryset(self, request):
"""
Return the base queryset for the view. By default, this returns `self.queryset.all()`.
Args:
request: The current request
"""
if self.queryset is None:
raise ImproperlyConfigured(
f"{self.__class__.__name__} does not define a queryset. Set queryset on the class or "
f"override its get_queryset() method."
)
return self.queryset.all()
def get_extra_context(self, request): def get_extra_context(self, request):
""" """
Return any additional context data to include when rendering the template. Return any additional context data to include when rendering the template.