Skip to content

Commit

Permalink
#10739: Collapse BaseView class
Browse files Browse the repository at this point in the history
  • Loading branch information
jeremystretch committed Dec 12, 2022
1 parent e338f7c commit aacf606
Showing 1 changed file with 35 additions and 14 deletions.
49 changes: 35 additions & 14 deletions netbox/netbox/views/generic/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,30 @@

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
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().
Return the base queryset for the view. By default, this returns `self.queryset.all()`.
Args:
request: The current request
Expand All @@ -26,17 +39,6 @@ def get_queryset(self, request):
)
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):
"""
Return the object being viewed or modified. The object is identified by an arbitrary set of keyword arguments
Expand All @@ -57,7 +59,7 @@ def get_extra_context(self, request, instance):
return {}


class BaseMultiObjectView(BaseView):
class BaseMultiObjectView(ObjectPermissionRequiredMixin, View):
"""
Base class for generic views which display or manipulate multiple objects.
Expand All @@ -66,9 +68,28 @@ class BaseMultiObjectView(BaseView):
table: The django-tables2 Table class used to render the objects list
template_name: The name of the HTML template file to render
"""
queryset = None
table = 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):
"""
Return any additional context data to include when rendering the template.
Expand Down

0 comments on commit aacf606

Please sign in to comment.