Skip to content

Commit

Permalink
fix: Minimal response for notebook list api (#19386)
Browse files Browse the repository at this point in the history
  • Loading branch information
benjackwhite authored Dec 18, 2023
1 parent 0d70afb commit fec0553
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 7 deletions.
3 changes: 2 additions & 1 deletion frontend/src/lib/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ import {
IntegrationType,
MediaUploadResponse,
NewEarlyAccessFeatureType,
NotebookListItemType,
NotebookNodeResource,
NotebookType,
OrganizationFeatureFlags,
Expand Down Expand Up @@ -1596,7 +1597,7 @@ const api = {
offset?: number
limit?: number
} = {}
): Promise<CountedPaginatedResponse<NotebookType>> {
): Promise<CountedPaginatedResponse<NotebookListItemType>> {
// TODO attrs could be a union of types like NotebookNodeRecordingAttributes
const apiRequest = new ApiRequest().notebooks()
const { contains, ...queryParams } = objectClean(params)
Expand Down
31 changes: 25 additions & 6 deletions posthog/api/notebook.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from typing import Dict, List, Optional, Any
from typing import Dict, List, Optional, Any, Type
from django.db.models import Q
import structlog
from django.db import transaction
Expand All @@ -17,6 +17,7 @@
from rest_framework.response import Response
from rest_framework.decorators import action
from rest_framework.permissions import IsAuthenticated
from rest_framework.serializers import BaseSerializer

from posthog.api.forbid_destroy_model import ForbidDestroyModel
from posthog.api.routing import StructuredViewSetMixin
Expand Down Expand Up @@ -76,7 +77,26 @@ def log_notebook_activity(
)


class NotebookSerializer(serializers.ModelSerializer):
class NotebookMinimalSerializer(serializers.ModelSerializer):
created_by = UserBasicSerializer(read_only=True)
last_modified_by = UserBasicSerializer(read_only=True)

class Meta:
model = Notebook
fields = [
"id",
"short_id",
"title",
"deleted",
"created_at",
"created_by",
"last_modified_at",
"last_modified_by",
]
read_only_fields = fields


class NotebookSerializer(NotebookMinimalSerializer):
class Meta:
model = Notebook
fields = [
Expand All @@ -101,9 +121,6 @@ class Meta:
"last_modified_by",
]

created_by = UserBasicSerializer(read_only=True)
last_modified_by = UserBasicSerializer(read_only=True)

def create(self, validated_data: Dict, *args, **kwargs) -> Notebook:
request = self.context["request"]
team = self.context["get_team"]()
Expand Down Expand Up @@ -223,7 +240,6 @@ def update(self, instance: Notebook, validated_data: Dict, **kwargs) -> Notebook
)
class NotebookViewSet(StructuredViewSetMixin, ForbidDestroyModel, viewsets.ModelViewSet):
queryset = Notebook.objects.all()
serializer_class = NotebookSerializer
permission_classes = [
IsAuthenticated,
ProjectMembershipNecessaryPermissions,
Expand All @@ -235,6 +251,9 @@ class NotebookViewSet(StructuredViewSetMixin, ForbidDestroyModel, viewsets.Model
include_in_docs = DEBUG
lookup_field = "short_id"

def get_serializer_class(self) -> Type[BaseSerializer]:
return NotebookMinimalSerializer if self.action == "list" else NotebookSerializer

def get_queryset(self) -> QuerySet:
queryset = super().get_queryset()

Expand Down

0 comments on commit fec0553

Please sign in to comment.