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

feat: preference to advertised runs dates instead of additional metadata #4494

Closed
wants to merge 2 commits 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
42 changes: 38 additions & 4 deletions course_discovery/apps/api/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -703,15 +703,17 @@ 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
Expand All @@ -722,6 +724,38 @@ class Meta:
'product_meta', 'external_course_marketing_type', 'display_on_org_page', 'taxi_form',
)

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.

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, getattr(obj, obj_field_name))

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_date', 'start')

def get_end_date(self, obj):
""" Retrieve the end date """
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, 'registration_deadline', 'enrollment_end')

def update_taxi_form(self, instance, taxi_form):
if instance.taxi_form:
if not taxi_form:
Expand Down
Loading