From 7d7833f4e7a361102a32e193c130edfb4ecea2e0 Mon Sep 17 00:00:00 2001 From: Muhammad Afaq Shuaib Date: Thu, 21 Nov 2024 20:09:08 +0500 Subject: [PATCH 1/2] feat: preference to advertised runs dates instead of additional metadata in serializer --- course_discovery/apps/api/serializers.py | 40 +++++++++++++++++++++--- 1 file changed, 36 insertions(+), 4 deletions(-) diff --git a/course_discovery/apps/api/serializers.py b/course_discovery/apps/api/serializers.py index cfb7a43988..e346244d45 100644 --- a/course_discovery/apps/api/serializers.py +++ b/course_discovery/apps/api/serializers.py @@ -703,15 +703,15 @@ class AdditionalMetadataSerializer(BaseModelSerializer): facts = FactSerializer(many=True) certificate_info = CertificateInfoSerializer() product_meta = ProductMetaSerializer(required=False, allow_null=True) - start_date = serializers.DateTimeField() - end_date = serializers.DateTimeField() - registration_deadline = serializers.DateTimeField(allow_null=True) + start_date = serializers.SerializerMethodField() + end_date = serializers.SerializerMethodField() + registration_deadline = serializers.SerializerMethodField() variant_id = serializers.UUIDField(allow_null=True) taxi_form = TaxiFormSerializer(required=False, allow_null=True) @classmethod def prefetch_queryset(cls): - return AdditionalMetadata.objects.select_related('facts', 'certificate_info', 'product_meta', 'taxi_form') + return AdditionalMetadata.objects.prefetch_related().select_related('facts', 'certificate_info', 'product_meta', 'taxi_form') class Meta: model = AdditionalMetadata @@ -722,6 +722,38 @@ class Meta: 'product_meta', 'external_course_marketing_type', 'display_on_org_page', 'taxi_form', ) + def get_related_course_data(self, obj, field_name): + """ + Helper method to retrieve data (start_date, end_date, registration_deadline) + from the advertised course run or fallback to the object's field. + + Args: + obj (AdditionalMetadata): The model instance. + field_name (str): Field name to fetch from the advertised course run. + + Returns: + Value of the field from the advertised course run or object's fallback. + """ + related_course = obj.related_courses.filter(draft=True).first() + advertised_course_run = getattr(related_course, 'advertised_course_run', None) + + if related_course and advertised_course_run: + return getattr(advertised_course_run, field_name, None) + + return getattr(obj, field_name, None) + + def get_start_date(self, obj): + """ Retrieve the start date """ + return self.get_related_course_data(obj, 'start') + + def get_end_date(self, obj): + """ Retrieve the end date """ + return self.get_related_course_data(obj, 'end') + + def get_registration_deadline(self, obj): + """ Retrieve the registration deadline """ + return self.get_related_course_data(obj, 'enrollment_end') + def update_taxi_form(self, instance, taxi_form): if instance.taxi_form: if not taxi_form: From 54ff04a0d90339703cb88b17326f8cbe95ce4db4 Mon Sep 17 00:00:00 2001 From: Muhammad Afaq Shuaib Date: Thu, 21 Nov 2024 20:40:58 +0500 Subject: [PATCH 2/2] chore: added a change --- course_discovery/apps/api/serializers.py | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/course_discovery/apps/api/serializers.py b/course_discovery/apps/api/serializers.py index e346244d45..4b583f54e4 100644 --- a/course_discovery/apps/api/serializers.py +++ b/course_discovery/apps/api/serializers.py @@ -711,7 +711,9 @@ class AdditionalMetadataSerializer(BaseModelSerializer): @classmethod def prefetch_queryset(cls): - return AdditionalMetadata.objects.prefetch_related().select_related('facts', 'certificate_info', 'product_meta', 'taxi_form') + return AdditionalMetadata.objects.prefetch_related( + + ).select_related('facts', 'certificate_info', 'product_meta', 'taxi_form') class Meta: model = AdditionalMetadata @@ -722,7 +724,7 @@ class Meta: 'product_meta', 'external_course_marketing_type', 'display_on_org_page', 'taxi_form', ) - def get_related_course_data(self, obj, field_name): + def get_related_course_data(self, obj, obj_field_name, field_name): """ Helper method to retrieve data (start_date, end_date, registration_deadline) from the advertised course run or fallback to the object's field. @@ -738,21 +740,21 @@ def get_related_course_data(self, obj, field_name): advertised_course_run = getattr(related_course, 'advertised_course_run', None) if related_course and advertised_course_run: - return getattr(advertised_course_run, field_name, None) + return getattr(advertised_course_run, field_name, getattr(obj, obj_field_name)) - return getattr(obj, field_name, None) + return getattr(obj, obj_field_name, None) def get_start_date(self, obj): """ Retrieve the start date """ - return self.get_related_course_data(obj, 'start') + return self.get_related_course_data(obj, 'start_date', 'start') def get_end_date(self, obj): """ Retrieve the end date """ - return self.get_related_course_data(obj, 'end') + return self.get_related_course_data(obj, 'end_date', 'end') def get_registration_deadline(self, obj): """ Retrieve the registration deadline """ - return self.get_related_course_data(obj, 'enrollment_end') + return self.get_related_course_data(obj, 'registration_deadline', 'enrollment_end') def update_taxi_form(self, instance, taxi_form): if instance.taxi_form: