Skip to content

Commit

Permalink
🚚(!reset) Move new Enrollment viewset and filters in dedicated module
Browse files Browse the repository at this point in the history
If approved, I will rework all commits to integrate those changes in the right
previous commit.
  • Loading branch information
jbpenrath committed Mar 21, 2024
1 parent f10fe1b commit f23ffa1
Show file tree
Hide file tree
Showing 6 changed files with 96 additions and 63 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
from joanie.core.api.base import NestedGenericViewSet, SerializerPerActionMixin
from joanie.core.authentication import SessionAuthenticationWithAuthenticateHeader

from .enrollment import EnrollmentViewSet


# pylint: disable=too-many-ancestors
class OrganizationViewSet(viewsets.ModelViewSet):
Expand Down Expand Up @@ -550,28 +552,3 @@ def destroy(self, request, *args, **kwargs):
) from error

return super().destroy(request, *args, **kwargs)


class EnrollmentViewSet(
SerializerPerActionMixin,
mixins.ListModelMixin,
mixins.RetrieveModelMixin,
mixins.UpdateModelMixin,
viewsets.GenericViewSet,
):
"""
Admin Enrollment ViewSet
"""

authentication_classes = [SessionAuthenticationWithAuthenticateHeader]
permission_classes = [permissions.IsAdminUser & permissions.DjangoModelPermissions]
serializer_classes = {
"list": serializers.AdminEnrollmentLightSerializer,
}
default_serializer_class = serializers.AdminEnrollmentSerializer
queryset = models.Enrollment.objects.all().select_related(
"course_run", "course_run__course", "user", "certificate"
)
filterset_class = filters.EnrollmentAdminFilterSet
filter_backends = [DjangoFilterBackend, OrderingFilter]
ordering_fields = ["created_on"]
41 changes: 41 additions & 0 deletions src/backend/joanie/core/api/admin/enrollment.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
"""
Admin API Enrollment Endpoints
"""

from django_filters.rest_framework import DjangoFilterBackend
from rest_framework import (
mixins,
permissions,
viewsets,
)
from rest_framework.filters import OrderingFilter

from joanie.core import filters, models, serializers
from joanie.core.api.base import SerializerPerActionMixin
from joanie.core.authentication import SessionAuthenticationWithAuthenticateHeader


# pylint: disable=too-many-ancestors
class EnrollmentViewSet(
SerializerPerActionMixin,
mixins.ListModelMixin,
mixins.RetrieveModelMixin,
mixins.UpdateModelMixin,
viewsets.GenericViewSet,
):
"""
Admin Enrollment ViewSet
"""

authentication_classes = [SessionAuthenticationWithAuthenticateHeader]
permission_classes = [permissions.IsAdminUser & permissions.DjangoModelPermissions]
serializer_classes = {
"list": serializers.AdminEnrollmentLightSerializer,
}
default_serializer_class = serializers.AdminEnrollmentSerializer
queryset = models.Enrollment.objects.all().select_related(
"course_run", "course_run__course", "user", "certificate"
)
filterset_class = filters.EnrollmentAdminFilterSet
filter_backends = [DjangoFilterBackend, OrderingFilter]
ordering_fields = ["created_on"]
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
from joanie.core import enums, models
from joanie.core.filters.base import MultipleValueFilter

from .enrollment import EnrollmentAdminFilterSet


class OrganizationAdminFilterSet(filters.FilterSet):
"""
Expand Down Expand Up @@ -290,41 +292,3 @@ def filter_by_query(self, queryset, _name, value):
| Q(owner__first_name__icontains=value)
| Q(owner__last_name__icontains=value)
).distinct()


class EnrollmentAdminFilterSet(filters.FilterSet):
"""
EnrollmentAdminViewSetFilter allows to filter this resource
through a full search text query, course_run_ids, user_ids, ids, active state
and state.
"""

class Meta:
model = models.Enrollment
fields: List[str] = ["query"]

query = filters.CharFilter(method="filter_by_query")
course_run_ids = filters.ModelMultipleChoiceFilter(
queryset=models.CourseRun.objects.all().only("pk"),
field_name="course_run",
distinct=True,
)
user_ids = filters.ModelMultipleChoiceFilter(
queryset=models.User.objects.all().only("pk"),
field_name="user",
distinct=True,
)
ids = MultipleValueFilter(field_class=fields.UUIDField, field_name="id")
is_active = filters.BooleanFilter()
state = filters.ChoiceFilter(choices=enums.ENROLLMENT_STATE_CHOICES)

def filter_by_query(self, queryset, _name, value):
"""
Filter resource by looking for title which contains provided value in
"query" query parameter.
"""
return queryset.filter(
Q(course_run__translations__title__icontains=value)
| Q(course_run__resource_link__icontains=value)
| Q(course_run__course__code__icontains=value)
).distinct()
51 changes: 51 additions & 0 deletions src/backend/joanie/core/filters/admin/enrollment.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
"""
Admin Enrollment API Resource Filters
"""

from typing import List

from django.db.models import Q
from django.forms import fields

from django_filters import rest_framework as filters

from joanie.core import enums, models
from joanie.core.filters.base import MultipleValueFilter


class EnrollmentAdminFilterSet(filters.FilterSet):
"""
EnrollmentAdminFilterSet allows to filter this resource
through a full search text query, course_run_ids, user_ids, ids, active state
and state.
"""

class Meta:
model = models.Enrollment
fields: List[str] = ["query"]

query = filters.CharFilter(method="filter_by_query")
course_run_ids = filters.ModelMultipleChoiceFilter(
queryset=models.CourseRun.objects.all().only("pk"),
field_name="course_run",
distinct=True,
)
user_ids = filters.ModelMultipleChoiceFilter(
queryset=models.User.objects.all().only("pk"),
field_name="user",
distinct=True,
)
ids = MultipleValueFilter(field_class=fields.UUIDField, field_name="id")
is_active = filters.BooleanFilter()
state = filters.ChoiceFilter(choices=enums.ENROLLMENT_STATE_CHOICES)

def filter_by_query(self, queryset, _name, value):
"""
Filter resource by looking for title which contains provided value in
"query" query parameter.
"""
return queryset.filter(
Q(course_run__translations__title__icontains=value)
| Q(course_run__resource_link__icontains=value)
| Q(course_run__course__code__icontains=value)
).distinct()
File renamed without changes.

0 comments on commit f23ffa1

Please sign in to comment.