Skip to content

Commit

Permalink
Use BaseValuesViewset more to prevent introspection errors.
Browse files Browse the repository at this point in the history
Add dummy get_queryset and get_serializer_class methods otherwise.
  • Loading branch information
rtibbles committed May 6, 2024
1 parent fe77297 commit 2418619
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 8 deletions.
7 changes: 3 additions & 4 deletions kolibri/core/content/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -1697,9 +1697,7 @@ def mean(data):
return mean


class ContentNodeProgressViewset(
TreeQueryMixin, viewsets.GenericViewSet, ListModelMixin
):
class ContentNodeProgressViewset(TreeQueryMixin, BaseValuesViewset, ListModelMixin):
filter_backends = (DjangoFilterBackend, filters.OrderingFilter)
ordering_fields = ["last_interacted"]
ordering = ("lft", "id")
Expand All @@ -1711,6 +1709,7 @@ class ContentNodeProgressViewset(
# that the pagination object generated by the ContentNodeViewset
# will be used to make subsequent page requests.
pagination_class = OptionalPagination
values = ("content_id", "progress")

def get_queryset(self):
user = self.request.user
Expand All @@ -1737,7 +1736,7 @@ def generate_response(self, request, queryset):
content_id__in=queryset.exclude(kind=content_kinds.TOPIC).values_list(
"content_id", flat=True
),
).values("content_id", "progress")
).values(*self.values)
)
return Response(logs)

Expand Down
12 changes: 10 additions & 2 deletions kolibri/core/content/public_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
from django.db import connection
from django.db.models import Q
from django.http import HttpResponseBadRequest
from rest_framework.generics import get_object_or_404
from rest_framework.response import Response
from rest_framework.serializers import Serializer
from rest_framework.viewsets import GenericViewSet

from kolibri.core.content import models
Expand All @@ -14,6 +14,14 @@


class ImportMetadataViewset(GenericViewSet):
queryset = models.ContentNode.objects.all()

def get_serializer_class(self):
"""
Add this purely to avoid warnings from DRF YASG schema generation.
"""
return Serializer

default_content_schema = CONTENT_SCHEMA_VERSION
min_content_schema = MIN_CONTENT_SCHEMA_VERSION

Expand Down Expand Up @@ -64,7 +72,7 @@ def retrieve(self, request, pk=None):

# Get the model for the target node here - we do this so that we trigger a 404 immediately if the node
# does not exist.
node = get_object_or_404(models.ContentNode.objects.all(), pk=pk)
node = self.get_object()

nodes = node.get_ancestors(include_self=True)

Expand Down
13 changes: 13 additions & 0 deletions kolibri/core/device/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
from rest_framework import viewsets
from rest_framework.permissions import IsAuthenticated
from rest_framework.response import Response
from rest_framework.serializers import Serializer

import kolibri
from .models import DevicePermissions
Expand Down Expand Up @@ -123,6 +124,18 @@ def create(self, request, *args, **kwargs):
class FreeSpaceView(mixins.ListModelMixin, viewsets.GenericViewSet):
permission_classes = (IsAuthenticated,)

def get_serializer_class(self):
"""
Add this purely to avoid warnings from DRF YASG schema generation.
"""
return Serializer

def get_queryset(self):
"""
Add this purely to avoid warnings from DRF YASG schema generation.
"""
return None

def list(self, request):
path = request.query_params.get("path")
if path is None:
Expand Down
4 changes: 3 additions & 1 deletion kolibri/core/discovery/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
from .utils.network.client import NetworkClient
from .utils.network.connections import capture_connection_state
from .utils.network.connections import update_network_location
from kolibri.core.api import BaseValuesViewset
from kolibri.core.api import ValuesViewset
from kolibri.core.device.permissions import NotProvisionedHasPermission
from kolibri.core.utils.urls import reverse_path
Expand Down Expand Up @@ -74,7 +75,8 @@ class StaticNetworkLocationViewSet(NetworkLocationViewSet):
queryset = StaticNetworkLocation.objects.all()


class NetworkLocationFacilitiesView(viewsets.GenericViewSet):
class NetworkLocationFacilitiesView(BaseValuesViewset):
queryset = NetworkLocation.objects.all()
permission_classes = [NetworkLocationPermissions | NotProvisionedHasPermission]

def retrieve(self, request, pk=None):
Expand Down
25 changes: 24 additions & 1 deletion kolibri/core/logger/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
from rest_framework.decorators import action
from rest_framework.exceptions import ValidationError
from rest_framework.response import Response
from rest_framework.serializers import Serializer

from .models import AttemptLog
from .models import ContentSessionLog
Expand Down Expand Up @@ -230,6 +231,18 @@ def to_dict(self):

@method_decorator(csrf_protect, name="dispatch")
class ProgressTrackingViewSet(viewsets.GenericViewSet):
def get_serializer_class(self):
"""
Add this purely to avoid warnings from DRF YASG schema generation.
"""
return Serializer

def get_queryset(self):
"""
Add this purely to avoid warnings from DRF YASG schema generation.
"""
return None

def _precache_dataset_id(self, user):
if user is None or user.is_anonymous:
return
Expand Down Expand Up @@ -919,11 +932,21 @@ def update(self, request, pk=None):


class TotalContentProgressViewSet(viewsets.GenericViewSet):
def get_serializer_class(self):
"""
Add this purely to avoid warnings from DRF YASG schema generation.
"""
return Serializer

def get_queryset(self):
return ContentSummaryLog.objects.filter(user=self.request.user)

def retrieve(self, request, pk=None):
if request.user.is_anonymous or pk != request.user.id:
raise PermissionDenied("Can only access progress data for self")
progress = (
request.user.contentsummarylog_set.annotate(
self.get_queryset()
.annotate(
mastery_progress=Sum(
Case(
When(masterylogs__complete=True, then=Value(1)),
Expand Down
6 changes: 6 additions & 0 deletions kolibri/core/tasks/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,12 @@ def get_fields(self):
class TasksViewSet(viewsets.GenericViewSet):
serializer_class = TasksSerializer

def get_queryset(self):
"""
Add this purely to avoid warnings from DRF YASG schema generation.
"""
return None

def validate_create_req_data(self, request):
"""
Validates the request data received on POST /api/tasks/.
Expand Down

0 comments on commit 2418619

Please sign in to comment.