Skip to content

Commit

Permalink
fix(generic): respect APIS_DETAIL_VIEWS_ALLOWED setting
Browse files Browse the repository at this point in the history
Skip adding permissions to DetailView if APIS_DETAIL_VIEWS_ALLOWED is
set by using a DetailViewObjectMixin that overrides GenericModelMixin's
get_permission_required method.

fixes #1400
  • Loading branch information
gythaogg committed Nov 17, 2024
1 parent 4798c98 commit 1ac5a4a
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 2 deletions.
12 changes: 12 additions & 0 deletions apis_core/core/mixins.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,3 +51,15 @@ def get_permission_required(self):
if getattr(settings, "APIS_LIST_VIEWS_ALLOWED", False):
return []
return super().get_permission_required()


class DetailViewObjectMixin:
"""
This mixin ensures that no permissions are required for the detail view
of the object IF APIS_DETAIL_VIEWS_ALLOWED is set.
"""

def get_permission_required(self):
if getattr(settings, "APIS_DETAIL_VIEWS_ALLOWED", False):
return []
return super().get_permission_required()
6 changes: 4 additions & 2 deletions apis_core/generic/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
from django_tables2.tables import table_factory

from apis_core.apis_metainfo.models import Uri
from apis_core.core.mixins import ListViewObjectFilterMixin
from apis_core.core.mixins import DetailViewObjectMixin, ListViewObjectFilterMixin
from apis_core.utils.helpers import create_object_from_uri, get_importer_for_model

from .filtersets import GenericFilterSet
Expand Down Expand Up @@ -211,7 +211,9 @@ def get_paginate_by(self, table_data) -> Optional[int]:
return getattr(self.get_table_class(), "paginate_by", None)


class Detail(GenericModelMixin, PermissionRequiredMixin, DetailView):
class Detail(
DetailViewObjectMixin, GenericModelMixin, PermissionRequiredMixin, DetailView
):
"""
Detail view for a generic model.
Access requires the `<model>_view` permission.
Expand Down

0 comments on commit 1ac5a4a

Please sign in to comment.