diff --git a/djangoldp/__init__.py b/djangoldp/__init__.py index 7f7595fa3486f400aad249cd1e95a12a4a9ce5bc..9d420347f01415d00dcb6df4a56e8f7f8b84ba0a 100644 --- a/djangoldp/__init__.py +++ b/djangoldp/__init__.py @@ -5,4 +5,4 @@ __version__ = '0.0.0' options.DEFAULT_NAMES += ( 'lookup_field', 'rdf_type', 'rdf_context', 'auto_author', 'owner_field', 'owner_urlid_field', 'view_set', 'container_path', 'permission_classes', 'serializer_fields', 'serializer_fields_exclude', 'empty_containers', - 'nested_fields', 'depth', 'permission_roles', 'inherit_permissions', 'public_field', 'static_version', 'static_params') + 'nested_fields', 'depth', 'permission_roles', 'inherit_permissions', 'public_field', 'static_version', 'static_params', 'active_field') diff --git a/djangoldp/filters.py b/djangoldp/filters.py index c1da410640cb324e57b9ff95e973b9f3bfe05c70..3cf502d134700942b69e3d192e9fcf53a658cd1c 100644 --- a/djangoldp/filters.py +++ b/djangoldp/filters.py @@ -21,13 +21,23 @@ class OwnerFilterBackend(BaseFilterBackend): class PublicFilterBackend(BaseFilterBackend): """ - No filter applied. - This class is useful for permission classes that don't filter objects, so that they can be chained with other + Public filter applied. + This class is on models which bears a is_public boolean field, to filter objects that are public """ def filter_queryset(self, request, queryset, view): public_field = queryset.model._meta.public_field return queryset.filter(**{public_field: True}) +class ActiveFilterBackend(BaseFilterBackend): + """ + Filter which removes inactive objects from the queryset, useful for user for instance and configurable using the active_field meta + """ + def filter_queryset(self, request, queryset, view): + if (hasattr(queryset.model._meta, 'active_field')): + is_active_field = queryset.model._meta.active_field + return queryset.filter(**{is_active_field :True}) + return queryset + class NoFilterBackend(BaseFilterBackend): """ No filter applied. diff --git a/djangoldp/permissions.py b/djangoldp/permissions.py index 95c430990a461f2565f4621d2638df120f68459c..0f03840ebd8d69394c95cd63c222769952ed3732 100644 --- a/djangoldp/permissions.py +++ b/djangoldp/permissions.py @@ -4,7 +4,7 @@ from django.http import Http404 from rest_framework.permissions import BasePermission, DjangoObjectPermissions, OR, AND from rest_framework.filters import BaseFilterBackend from rest_framework_guardian.filters import ObjectPermissionsFilter -from djangoldp.filters import OwnerFilterBackend, NoFilterBackend, PublicFilterBackend, IPFilterBackend +from djangoldp.filters import OwnerFilterBackend, NoFilterBackend, PublicFilterBackend, IPFilterBackend, ActiveFilterBackend from djangoldp.utils import is_anonymous_user, is_authenticated_user, check_client_ip @@ -79,7 +79,7 @@ class LDPBasePermission(BasePermission): """ # filter backends associated with the permissions class. This will be used to filter queryset in the (auto-generated) # view for a model, and in the serializing nested fields - filter_backend = NoFilterBackend + filter_backend = ActiveFilterBackend # by default, all permissions permissions = getattr(settings, 'DJANGOLDP_PERMISSIONS', DEFAULT_DJANGOLDP_PERMISSIONS) # perms_map defines the permissions required for different methods