Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

draft: docs: add page_number_paginate_serializer #24821

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 43 additions & 0 deletions openedx/core/apidocs.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,3 +53,46 @@ class PageOfInnerSerializer(serializers.Serializer):

PageOfInnerSerializer.__name__ = f'PageOf{inner_serializer_class.__name__}'
return PageOfInnerSerializer


def page_number_paginate_serializer(inner_serializer_class):
"""
Create a page-number-paginated version of a serializer.

This is hacky workaround for an edx-api-doc-tools issue described here:
https://github.com/edx/api-doc-tools/issues/32

It assumes we are using page-number-style pagination and assumes a specific
schema for the pages. It should be removed once we address the underlying issue.

Arguments:
inner_serializer_class (type): A subclass of ``Serializer``.

Returns: type
A subclass of ``Serializer`` to model the schema of a page of a page-number-
paginated endpoint.
"""
class PageOfInnerSerializer(serializers.Serializer):
"""
A serializer for a page of a page-number-paginated list of ``inner_serializer_class``.
"""
# pylint: disable=abstract-method
count = serializers.IntegerField(
help_text="Total number of objects across all pages."
)
previous = serializers.URLField(
required=False,
help_text="Link to the previous page or results, or null if this is the first.",
)
next = serializers.URLField(
required=False,
help_text="Link to the next page of results, or null if this is the last.",
)
results = serializers.ListField(
child=inner_serializer_class(),
help_text="The list of result objects on this page.",
)

PageOfInnerSerializer.__name__ = 'PageOf{}'.format(inner_serializer_class.__name__)
return PageOfInnerSerializer