Skip to content

Commit

Permalink
chore: move new serializers to v2
Browse files Browse the repository at this point in the history
  • Loading branch information
hamza-56 committed Dec 12, 2024
1 parent 691bafe commit 02d2691
Show file tree
Hide file tree
Showing 9 changed files with 156 additions and 132 deletions.
135 changes: 135 additions & 0 deletions course_discovery/apps/api/v2/serializers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
from course_discovery.apps.course_metadata.search_indexes import documents
from course_discovery.apps.course_metadata.search_indexes.constants import SEARCH_INDEX_ADDITIONAL_FIELDS_V2
from course_discovery.apps.course_metadata.search_indexes.documents import (
CourseDocument,
CourseRunDocument,
LearnerPathwayDocument,
PersonDocument,
ProgramDocument,
)
from course_discovery.apps.course_metadata.search_indexes.serializers.aggregation import (
AggregateSearchListSerializer,
AggregateSearchSerializer,
)
from course_discovery.apps.course_metadata.search_indexes.serializers.common import SortFieldMixin
from course_discovery.apps.course_metadata.search_indexes.serializers.course import CourseSearchDocumentSerializer
from course_discovery.apps.course_metadata.search_indexes.serializers.course_run import (
CourseRunSearchDocumentSerializer,
)
from course_discovery.apps.course_metadata.search_indexes.serializers.learner_pathway import (
LearnerPathwaySearchDocumentSerializer,
)
from course_discovery.apps.course_metadata.search_indexes.serializers.person import PersonSearchDocumentSerializer
from course_discovery.apps.course_metadata.search_indexes.serializers.program import ProgramSearchDocumentSerializer
from course_discovery.apps.edx_elasticsearch_dsl_extensions.serializers import DummyDocument


class CourseRunSearchDocumentSerializerV2(SortFieldMixin, CourseRunSearchDocumentSerializer):
"""
Serializer for Course Run documents, extending the base `CourseRunSearchDocumentSerializer`
to include additional fields for enhanced search functionality, as well as a `sort` field
to provide sorting information from the Elasticsearch response.
This serializer expands the `fields` attribute in the `Meta` class to include additional
fields specified in `SEARCH_INDEX_ADDITIONAL_FIELDS_V2`.
"""

class Meta(CourseRunSearchDocumentSerializer.Meta):
document = CourseRunDocument
fields = CourseRunSearchDocumentSerializer.Meta.fields + SEARCH_INDEX_ADDITIONAL_FIELDS_V2


class CourseSearchDocumentSerializerV2(SortFieldMixin, CourseSearchDocumentSerializer):
"""
Serializer for Course documents, extending the base `CourseSearchDocumentSerializer`
to include additional fields for enhanced search functionality, as well as a `sort` field
to provide sorting information from the Elasticsearch response.
This serializer expands the `fields` attribute in the `Meta` class to include additional
fields specified in `SEARCH_INDEX_ADDITIONAL_FIELDS_V2`.
"""

class Meta(CourseSearchDocumentSerializer.Meta):
document = CourseDocument
fields = CourseSearchDocumentSerializer.Meta.fields + SEARCH_INDEX_ADDITIONAL_FIELDS_V2


class ProgramSearchDocumentSerializerV2(SortFieldMixin, ProgramSearchDocumentSerializer):
"""
Serializer for Program documents, extending the base `ProgramSearchDocumentSerializer`
to include additional fields for enhanced search functionality, as well as a `sort` field
to provide sorting information from the Elasticsearch response.
This serializer expands the `fields` attribute in the `Meta` class to include additional
fields specified in `SEARCH_INDEX_ADDITIONAL_FIELDS_V2`.
"""

class Meta(ProgramSearchDocumentSerializer.Meta):
document = ProgramDocument
fields = ProgramSearchDocumentSerializer.Meta.fields + SEARCH_INDEX_ADDITIONAL_FIELDS_V2


class LearnerPathwaySearchDocumentSerializerV2(SortFieldMixin, LearnerPathwaySearchDocumentSerializer):
"""
Serializer for Learner Pathway documents, extending the base `LearnerPathwaySearchDocumentSerializer`
to include additional fields for enhanced search functionality, as well as a `sort` field
to provide sorting information from the Elasticsearch response.
This serializer expands the `fields` attribute in the `Meta` class to include additional
fields specified in `SEARCH_INDEX_ADDITIONAL_FIELDS_V2`.
"""

class Meta(LearnerPathwaySearchDocumentSerializer.Meta):
document = LearnerPathwayDocument
fields = LearnerPathwaySearchDocumentSerializer.Meta.fields + SEARCH_INDEX_ADDITIONAL_FIELDS_V2


class PersonSearchDocumentSerializerV2(SortFieldMixin, PersonSearchDocumentSerializer):
"""
Serializer for Person documents, extending the base `PersonSearchDocumentSerializer`
to include additional fields for enhanced search functionality, as well as a `sort` field
to provide sorting information from the Elasticsearch response.
This serializer expands the `fields` attribute in the `Meta` class to include additional
fields specified in `SEARCH_INDEX_ADDITIONAL_FIELDS_V2`.
"""

class Meta(PersonSearchDocumentSerializer.Meta):
document = PersonDocument
fields = PersonSearchDocumentSerializer.Meta.fields + SEARCH_INDEX_ADDITIONAL_FIELDS_V2


class AggregateSearchListSerializerV2(AggregateSearchListSerializer):
"""
Extended version of the AggregateSearchListSerializer with updated serializers that support search_after pagination.
This subclass allows for the use of newer serializer versions for the same document types,
which include additional search index fields specifically for version 2.
"""

class Meta(AggregateSearchListSerializer.Meta):
"""
Meta options.
"""

serializers = {
documents.CourseRunDocument: CourseRunSearchDocumentSerializerV2,
documents.CourseDocument: CourseSearchDocumentSerializerV2,
documents.ProgramDocument: ProgramSearchDocumentSerializerV2,
documents.LearnerPathwayDocument: LearnerPathwaySearchDocumentSerializerV2,
documents.PersonDocument: PersonSearchDocumentSerializerV2,
}


class AggregateSearchSerializerV2(AggregateSearchSerializer):
"""
Serializer for aggregated elasticsearch documents.
"""

class Meta(AggregateSearchSerializer.Meta):
"""
Meta options.
"""

list_serializer_class = AggregateSearchListSerializerV2
document = DummyDocument
6 changes: 3 additions & 3 deletions course_discovery/apps/api/v2/views/search.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
"""API v2 Search"""

from course_discovery.apps.api.v1.views.search import AggregateSearchViewSet as AggregateSearchViewSetV1
from course_discovery.apps.course_metadata.search_indexes import serializers as search_indexes_serializers
from course_discovery.apps.api.v2.serializers import AggregateSearchSerializerV2
from course_discovery.apps.edx_elasticsearch_dsl_extensions.search import SearchAfterSearch
from course_discovery.apps.edx_elasticsearch_dsl_extensions.viewsets import SearchAfterPagination

Expand All @@ -11,11 +11,11 @@ class AggregateSearchViewSet(AggregateSearchViewSetV1):
Viewset for searching Elasticsearch documents using search_after pagination.
This viewset extends the functionality of the original AggregateSearchViewSet
by implementing search_after pagination, which allows for efficient pagination
by implementing search_after pagination, which allows for efficient pagination
through large datasets in Elasticsearch.
"""

serializer_class = search_indexes_serializers.AggregateSearchSerializerV2
serializer_class = AggregateSearchSerializerV2
pagination_class = SearchAfterPagination
ordering_fields = {"start": "start", "aggregation_uuid": "aggregation_uuid"}
ordering = ("-start", "aggregation_uuid")
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from .aggregation import (
AggregateFacetSearchSerializer, AggregateSearchModelSerializer, AggregateSearchSerializer,
AggregateSearchSerializerV2, LimitedAggregateSearchSerializer
LimitedAggregateSearchSerializer
)
from .common import DocumentDSLSerializerMixin, ModelObjectDocumentSerializerMixin
from .course import CourseFacetSerializer, CourseSearchDocumentSerializer, CourseSearchModelSerializer
Expand All @@ -14,7 +14,6 @@
'AggregateFacetSearchSerializer',
'LimitedAggregateSearchSerializer',
'AggregateSearchSerializer',
'AggregateSearchSerializerV2',
'CourseSearchDocumentSerializer',
'CourseFacetSerializer',
'CourseSearchModelSerializer',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,16 @@
from course_discovery.apps.api.serializers import COMMON_IGNORED_FIELDS
from course_discovery.apps.course_metadata.search_indexes import documents
from course_discovery.apps.edx_elasticsearch_dsl_extensions.serializers import (
BaseDjangoESDSLFacetSerializer, DummyDocument, MultiDocumentSerializerMixin
BaseDjangoESDSLFacetSerializer,
DummyDocument,
MultiDocumentSerializerMixin,
)

from .course import CourseSearchDocumentSerializer, CourseSearchDocumentSerializerV2, CourseSearchModelSerializer
from .course_run import (
CourseRunSearchDocumentSerializer, CourseRunSearchDocumentSerializerV2, CourseRunSearchModelSerializer
)
from .learner_pathway import (
LearnerPathwaySearchDocumentSerializer, LearnerPathwaySearchDocumentSerializerV2,
LearnerPathwaySearchModelSerializer
)
from .person import PersonSearchDocumentSerializer, PersonSearchDocumentSerializerV2
from .program import ProgramSearchDocumentSerializer, ProgramSearchDocumentSerializerV2, ProgramSearchModelSerializer
from .course import CourseSearchDocumentSerializer, CourseSearchModelSerializer
from .course_run import CourseRunSearchDocumentSerializer, CourseRunSearchModelSerializer
from .learner_pathway import LearnerPathwaySearchDocumentSerializer, LearnerPathwaySearchModelSerializer
from .person import PersonSearchDocumentSerializer
from .program import ProgramSearchDocumentSerializer, ProgramSearchModelSerializer


class AggregateSearchModelSerializer(MultiDocumentSerializerMixin, DocumentSerializer):
Expand Down Expand Up @@ -115,27 +112,6 @@ class Meta:
}


class AggregateSearchListSerializerV2(AggregateSearchListSerializer):
"""
Extended version of the AggregateSearchListSerializer with updated serializers that support search_after pagination.
This subclass allows for the use of newer serializer versions for the same document types,
which include additional search index fields specifically for version 2.
"""

class Meta(AggregateSearchListSerializer.Meta):
"""
Meta options.
"""
serializers = {
documents.CourseRunDocument: CourseRunSearchDocumentSerializerV2,
documents.CourseDocument: CourseSearchDocumentSerializerV2,
documents.ProgramDocument: ProgramSearchDocumentSerializerV2,
documents.LearnerPathwayDocument: LearnerPathwaySearchDocumentSerializerV2,
documents.PersonDocument: PersonSearchDocumentSerializerV2,
}


class AggregateSearchSerializer(DocumentSerializer):
"""
Serializer for aggregated elasticsearch documents.
Expand All @@ -150,14 +126,3 @@ class Meta:
ignore_fields = COMMON_IGNORED_FIELDS


class AggregateSearchSerializerV2(AggregateSearchSerializer):
"""
Serializer for aggregated elasticsearch documents.
"""

class Meta(AggregateSearchSerializer.Meta):
"""
Meta options.
"""
list_serializer_class = AggregateSearchListSerializerV2
document = DummyDocument
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@
from course_discovery.apps.course_metadata.utils import get_course_run_estimated_hours, get_product_skill_names
from course_discovery.apps.edx_elasticsearch_dsl_extensions.serializers import BaseDjangoESDSLFacetSerializer

from ..constants import BASE_SEARCH_INDEX_FIELDS, COMMON_IGNORED_FIELDS, SEARCH_INDEX_ADDITIONAL_FIELDS_V2
from ..constants import BASE_SEARCH_INDEX_FIELDS, COMMON_IGNORED_FIELDS
from ..documents import CourseDocument
from .common import (
DateTimeSerializerMixin, DocumentDSLSerializerMixin, ModelObjectDocumentSerializerMixin, SortFieldMixin
DateTimeSerializerMixin, DocumentDSLSerializerMixin, ModelObjectDocumentSerializerMixin
)

__all__ = ('CourseSearchDocumentSerializer',)
Expand Down Expand Up @@ -220,21 +220,6 @@ class Meta:
)


class CourseSearchDocumentSerializerV2(SortFieldMixin, CourseSearchDocumentSerializer):
"""
Serializer for Course documents, extending the base `CourseSearchDocumentSerializer`
to include additional fields for enhanced search functionality, as well as a `sort` field
to provide sorting information from the Elasticsearch response.
This serializer expands the `fields` attribute in the `Meta` class to include additional
fields specified in `SEARCH_INDEX_ADDITIONAL_FIELDS_V2`.
"""

class Meta(CourseSearchDocumentSerializer.Meta):
document = CourseDocument
fields = CourseSearchDocumentSerializer.Meta.fields + SEARCH_INDEX_ADDITIONAL_FIELDS_V2


# pylint: disable=abstract-method
class CourseFacetSerializer(BaseDjangoESDSLFacetSerializer):
"""
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@
from course_discovery.apps.course_metadata.utils import get_product_skill_names
from course_discovery.apps.edx_elasticsearch_dsl_extensions.serializers import BaseDjangoESDSLFacetSerializer

from ..constants import BASE_SEARCH_INDEX_FIELDS, COMMON_IGNORED_FIELDS, SEARCH_INDEX_ADDITIONAL_FIELDS_V2
from ..constants import BASE_SEARCH_INDEX_FIELDS, COMMON_IGNORED_FIELDS
from ..documents import CourseRunDocument
from .common import DateTimeSerializerMixin, DocumentDSLSerializerMixin, SortFieldMixin
from .common import DateTimeSerializerMixin, DocumentDSLSerializerMixin

__all__ = ('CourseRunSearchDocumentSerializer',)

Expand Down Expand Up @@ -95,21 +95,6 @@ class Meta:
)


class CourseRunSearchDocumentSerializerV2(SortFieldMixin, CourseRunSearchDocumentSerializer):
"""
Serializer for Course Run documents, extending the base `CourseRunSearchDocumentSerializer`
to include additional fields for enhanced search functionality, as well as a `sort` field
to provide sorting information from the Elasticsearch response.
This serializer expands the `fields` attribute in the `Meta` class to include additional
fields specified in `SEARCH_INDEX_ADDITIONAL_FIELDS_V2`.
"""

class Meta(CourseRunSearchDocumentSerializer.Meta):
document = CourseRunDocument
fields = CourseRunSearchDocumentSerializer.Meta.fields + SEARCH_INDEX_ADDITIONAL_FIELDS_V2


# pylint: disable=abstract-method
class CourseRunFacetSerializer(BaseDjangoESDSLFacetSerializer):
"""
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@
from course_discovery.apps.api.serializers import ContentTypeSerializer
from course_discovery.apps.learner_pathway.api.serializers import LearnerPathwaySerializer, LearnerPathwayStepSerializer

from ..constants import BASE_SEARCH_INDEX_FIELDS, COMMON_IGNORED_FIELDS, SEARCH_INDEX_ADDITIONAL_FIELDS_V2
from ..constants import BASE_SEARCH_INDEX_FIELDS, COMMON_IGNORED_FIELDS
from ..documents import LearnerPathwayDocument
from .common import (
DateTimeSerializerMixin, DocumentDSLSerializerMixin, ModelObjectDocumentSerializerMixin, SortFieldMixin
DateTimeSerializerMixin, DocumentDSLSerializerMixin, ModelObjectDocumentSerializerMixin
)

__all__ = ('LearnerPathwaySearchDocumentSerializer',)
Expand Down Expand Up @@ -81,21 +81,6 @@ def to_representation(self, instance):
return super().to_representation(prepared_instance)


class LearnerPathwaySearchDocumentSerializerV2(SortFieldMixin, LearnerPathwaySearchDocumentSerializer):
"""
Serializer for Learner Pathway documents, extending the base `LearnerPathwaySearchDocumentSerializer`
to include additional fields for enhanced search functionality, as well as a `sort` field
to provide sorting information from the Elasticsearch response.
This serializer expands the `fields` attribute in the `Meta` class to include additional
fields specified in `SEARCH_INDEX_ADDITIONAL_FIELDS_V2`.
"""

class Meta(LearnerPathwaySearchDocumentSerializer.Meta):
document = LearnerPathwayDocument
fields = LearnerPathwaySearchDocumentSerializer.Meta.fields + SEARCH_INDEX_ADDITIONAL_FIELDS_V2


class LearnerPathwaySearchModelSerializer(DocumentDSLSerializerMixin, ContentTypeSerializer, LearnerPathwaySerializer):
"""
Serializer for LearnerPathway model elasticsearch document.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

from ..constants import BASE_SEARCH_INDEX_FIELDS, COMMON_IGNORED_FIELDS
from ..documents import PersonDocument
from .common import DocumentDSLSerializerMixin, SortFieldMixin
from .common import DocumentDSLSerializerMixin

__all__ = ('PersonSearchDocumentSerializer',)

Expand Down Expand Up @@ -41,21 +41,6 @@ class Meta:
)


class PersonSearchDocumentSerializerV2(SortFieldMixin, PersonSearchDocumentSerializer):
"""
Serializer for Person documents, extending the base `PersonSearchDocumentSerializer`
to include additional fields for enhanced search functionality, as well as a `sort` field
to provide sorting information from the Elasticsearch response.
This serializer expands the `fields` attribute in the `Meta` class to include additional
fields specified in `SEARCH_INDEX_ADDITIONAL_FIELDS_V2`.
"""

class Meta(PersonSearchDocumentSerializer.Meta):
document = PersonDocument
fields = PersonSearchDocumentSerializer.Meta.fields


# pylint: disable=abstract-method
class PersonFacetSerializer(BaseDjangoESDSLFacetSerializer):
"""
Expand Down
Loading

0 comments on commit 02d2691

Please sign in to comment.