From d60dba14c8cff6bc86d6f89533cfe6d718ca5573 Mon Sep 17 00:00:00 2001 From: Jennifer Richards Date: Thu, 19 Dec 2024 16:55:14 -0400 Subject: [PATCH] feat: retrieve single rfc, including text (#8346) * feat: retrieve single rfc Use RFC number instead of doc PK as id * feat: include text in single-rfc response * chore: drop doc id from api response --- ietf/doc/api.py | 12 ++++++++---- ietf/doc/serializers.py | 13 ++++++++++++- 2 files changed, 20 insertions(+), 5 deletions(-) diff --git a/ietf/doc/api.py b/ietf/doc/api.py index 663ee15ef7..671d1686df 100644 --- a/ietf/doc/api.py +++ b/ietf/doc/api.py @@ -13,7 +13,7 @@ from ietf.name.models import StreamName from ietf.utils.timezone import RPC_TZINFO from .models import Document, DocEvent, RelatedDocument -from .serializers import RfcMetadataSerializer, RfcStatus +from .serializers import RfcMetadataSerializer, RfcStatus, RfcSerializer class RfcLimitOffsetPagination(LimitOffsetPagination): @@ -50,8 +50,9 @@ class RfcFilter(filters.FilterSet): class RfcViewSet(ListModelMixin, RetrieveModelMixin, GenericViewSet): permission_classes: list[BasePermission] = [] + lookup_field = "rfc_number" queryset = ( - Document.objects.filter(type_id="rfc") + Document.objects.filter(type_id="rfc", rfc_number__isnull=False) .annotate( published_datetime=Subquery( DocEvent.objects.filter( @@ -81,9 +82,12 @@ class RfcViewSet(ListModelMixin, RetrieveModelMixin, GenericViewSet): ), ) ) # default ordering - RfcFilter may override - - serializer_class = RfcMetadataSerializer pagination_class = RfcLimitOffsetPagination filter_backends = [filters.DjangoFilterBackend, drf_filters.SearchFilter] filterset_class = RfcFilter search_fields = ["title", "abstract"] + + def get_serializer_class(self): + if self.action == "retrieve": + return RfcSerializer + return RfcMetadataSerializer diff --git a/ietf/doc/serializers.py b/ietf/doc/serializers.py index 4a4cda9f2d..d5f7bddd05 100644 --- a/ietf/doc/serializers.py +++ b/ietf/doc/serializers.py @@ -114,6 +114,8 @@ class RelatedRfcSerializer(serializers.Serializer): class RfcMetadataSerializer(serializers.ModelSerializer): + """Serialize metadata of an RFC""" + number = serializers.IntegerField(source="rfc_number") published = serializers.DateField() status = RfcStatusSerializer(source="*") @@ -128,7 +130,6 @@ class RfcMetadataSerializer(serializers.ModelSerializer): class Meta: model = Document fields = [ - "id", "number", "title", "published", @@ -152,3 +153,13 @@ def get_identifiers(self, doc: Document): DocIdentifier(type="doi", value=f"10.17487/RFC{doc.rfc_number:04d}") ) return DocIdentifierSerializer(instance=identifiers, many=True).data + + +class RfcSerializer(RfcMetadataSerializer): + """Serialize an RFC, including its metadata and text content if available""" + + text = serializers.CharField(allow_null=True) + + class Meta: + model = RfcMetadataSerializer.Meta.model + fields = RfcMetadataSerializer.Meta.fields + ["text"]