-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
🚚(!reset) Move new Enrollment viewset and filters in dedicated module
If approved, I will rework all commits to integrate those changes in the right previous commit.
- Loading branch information
Showing
6 changed files
with
96 additions
and
63 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.