core: add group filter by member username and pk
Signed-off-by: Jens Langhammer <jens.langhammer@beryju.org>
This commit is contained in:
parent
f6e1bfdfc8
commit
8ae7403abc
|
@ -1,5 +1,7 @@
|
||||||
"""Groups API Viewset"""
|
"""Groups API Viewset"""
|
||||||
from django.db.models.query import QuerySet
|
from django.db.models.query import QuerySet
|
||||||
|
from django_filters.filters import ModelMultipleChoiceFilter
|
||||||
|
from django_filters.filterset import FilterSet
|
||||||
from rest_framework.fields import BooleanField, CharField, JSONField
|
from rest_framework.fields import BooleanField, CharField, JSONField
|
||||||
from rest_framework.serializers import ListSerializer, ModelSerializer
|
from rest_framework.serializers import ListSerializer, ModelSerializer
|
||||||
from rest_framework.viewsets import ModelViewSet
|
from rest_framework.viewsets import ModelViewSet
|
||||||
|
@ -57,13 +59,32 @@ class GroupSerializer(ModelSerializer):
|
||||||
]
|
]
|
||||||
|
|
||||||
|
|
||||||
|
class GroupFilter(FilterSet):
|
||||||
|
"""Filter for groups"""
|
||||||
|
|
||||||
|
members_by_username = ModelMultipleChoiceFilter(
|
||||||
|
field_name="users__username",
|
||||||
|
to_field_name="username",
|
||||||
|
queryset=User.objects.all(),
|
||||||
|
)
|
||||||
|
members_by_pk = ModelMultipleChoiceFilter(
|
||||||
|
field_name="users",
|
||||||
|
queryset=User.objects.all(),
|
||||||
|
)
|
||||||
|
|
||||||
|
class Meta:
|
||||||
|
|
||||||
|
model = Group
|
||||||
|
fields = ["name", "is_superuser", "members_by_pk", "members_by_username"]
|
||||||
|
|
||||||
|
|
||||||
class GroupViewSet(UsedByMixin, ModelViewSet):
|
class GroupViewSet(UsedByMixin, ModelViewSet):
|
||||||
"""Group Viewset"""
|
"""Group Viewset"""
|
||||||
|
|
||||||
queryset = Group.objects.all()
|
queryset = Group.objects.all()
|
||||||
serializer_class = GroupSerializer
|
serializer_class = GroupSerializer
|
||||||
search_fields = ["name", "is_superuser"]
|
search_fields = ["name", "is_superuser"]
|
||||||
filterset_fields = ["name", "is_superuser"]
|
filterset_class = GroupFilter
|
||||||
ordering = ["name"]
|
ordering = ["name"]
|
||||||
|
|
||||||
def _filter_queryset_for_list(self, queryset: QuerySet) -> QuerySet:
|
def _filter_queryset_for_list(self, queryset: QuerySet) -> QuerySet:
|
||||||
|
|
18
schema.yml
18
schema.yml
|
@ -1932,6 +1932,24 @@ paths:
|
||||||
name: is_superuser
|
name: is_superuser
|
||||||
schema:
|
schema:
|
||||||
type: boolean
|
type: boolean
|
||||||
|
- in: query
|
||||||
|
name: members_by_pk
|
||||||
|
schema:
|
||||||
|
type: array
|
||||||
|
items:
|
||||||
|
type: integer
|
||||||
|
explode: true
|
||||||
|
style: form
|
||||||
|
- in: query
|
||||||
|
name: members_by_username
|
||||||
|
schema:
|
||||||
|
type: array
|
||||||
|
items:
|
||||||
|
type: string
|
||||||
|
description: Required. 150 characters or fewer. Letters, digits and @/./+/-/_
|
||||||
|
only.
|
||||||
|
explode: true
|
||||||
|
style: form
|
||||||
- in: query
|
- in: query
|
||||||
name: name
|
name: name
|
||||||
schema:
|
schema:
|
||||||
|
|
Reference in New Issue