Skip to content

Commit

Permalink
add APIProjectContextMixin queryset override (#1273)
Browse files Browse the repository at this point in the history
  • Loading branch information
mikkonie committed Sep 7, 2023
1 parent bed01cb commit a47e9e2
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 16 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,13 @@ Changelog for the **SODAR Core** Django app package. Loosely follows the
Unreleased
==========

Added
-----

- **Projectroles**
- ``queryset_project_field`` override in ``APIProjectContextMixin`` (#1273)


Fixed
-----

Expand Down
1 change: 1 addition & 0 deletions docs/source/major_changes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ v0.13.2 (WIP)
Release Highlights
==================

- Add REST API project context queryset field override
- General bug fixes and minor updates


Expand Down
46 changes: 30 additions & 16 deletions projectroles/views_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,14 +118,15 @@ class SODARAPIProjectPermission(ProjectAccessMixin, BasePermission):
single permission_required attribute. Also works with Knox token based
views.
This must be used in the permission_classes attribute in order for token
This must be used in the ``permission_classes`` attribute in order for token
authentication to work.
Requires implementing either permission_required or
get_permission_required() in the view.
Requires implementing either ``permission_required`` or
``get_permission_required()`` in the view.
Project type can be restricted to PROJECT_TYPE_CATEGORY or
PROJECT_TYPE_PROJECT by setting the project_type attribute in the view.
Project type can be restricted to ``PROJECT_TYPE_CATEGORY`` or
``PROJECT_TYPE_PROJECT``, as defined in SODAR constants, by setting the
``project_type`` attribute in the view.
"""

def has_permission(self, request, view):
Expand Down Expand Up @@ -232,11 +233,12 @@ class SODARAPIBaseMixin:

class SODARAPIBaseProjectMixin(ProjectAccessMixin, SODARAPIBaseMixin):
"""
API view mixin for the base DRF APIView class with project permission
API view mixin for the base DRF ``APIView`` class with project permission
checking, but without serializers and other generic view functionality.
Project type can be restricted to PROJECT_TYPE_CATEGORY or
PROJECT_TYPE_PROJECT by setting the project_type attribute in the view.
Project type can be restricted to ``PROJECT_TYPE_CATEGORY`` or
``PROJECT_TYPE_PROJECT``, as defined in SODAR constants, by setting the
``project_type`` attribute in the view.
"""

permission_classes = [SODARAPIProjectPermission]
Expand All @@ -247,6 +249,10 @@ class APIProjectContextMixin(ProjectAccessMixin):
"""
Mixin to provide project context and queryset for generic API views. Can
be used both in SODAR and SODAR Core API base views.
If your model doesn't have a direct "project" relation, set
``queryset_project_field`` in the implementing class to query based on e.g.
a nested foreignkey relation.
"""

def get_serializer_context(self, *args, **kwargs):
Expand All @@ -255,8 +261,9 @@ def get_serializer_context(self, *args, **kwargs):
return context

def get_queryset(self):
project_field = getattr(self, 'queryset_project_field', 'project')
return self.__class__.serializer_class.Meta.model.objects.filter(
project=self.get_project()
**{project_field: self.get_project()}
)


Expand All @@ -267,18 +274,24 @@ class SODARAPIGenericProjectMixin(
API view mixin for generic DRF API views with serializers, SODAR project
context and permission checkin.
Unless overriding permission_classes with their own implementation, the user
MUST supply a permission_required attribute.
Unless overriding ``permission_classes`` with their own implementation, the
user MUST supply a ``permission_required`` attribute.
Replace ``lookup_url_kwarg`` with your view's url kwarg (SODAR project
compatible model name in lowercase).
Replace lookup_url_kwarg with your view's url kwarg (SODAR project
compatible model name in lowercase)
If the lookup is done via a foreign key, change the ``lookup_field``
attribute of your class into ``foreignkey__sodar_uuid``, e.g.
``project__sodar_uuid`` for lists.
If the lookup is done via the project object, change lookup_field into
"sodar_uuid"
If your object(s) don't have a direct ``project`` relation, update the
``queryset_project_field`` to point to the field, e.g.
``someothermodel__project``.
"""

lookup_field = 'sodar_uuid' # Use project__sodar_uuid for lists
lookup_url_kwarg = 'project' # Replace with relevant model
queryset_project_field = 'project' # Replace if no direct project relation


class ProjectQuerysetMixin:
Expand Down Expand Up @@ -316,7 +329,7 @@ class CoreAPIBaseMixin:

class CoreAPIBaseProjectMixin(ProjectAccessMixin, CoreAPIBaseMixin):
"""
SODAR Core API view mixin for the base DRF APIView class with project
SODAR Core API view mixin for the base DRF ``APIView`` class with project
permission checking, but without serializers and other generic view
functionality.
"""
Expand All @@ -331,6 +344,7 @@ class CoreAPIGenericProjectMixin(

lookup_field = 'sodar_uuid' # Use project__sodar_uuid for lists
lookup_url_kwarg = 'project' # Replace with relevant model
queryset_project_field = 'project' # Replace if no direct project relation


# Projectroles Specific Base Views and Mixins ----------------------------------
Expand Down

0 comments on commit a47e9e2

Please sign in to comment.