From b0ecbfc3815cee0aad0391ceb2dad4a2ceb761bf Mon Sep 17 00:00:00 2001 From: Abdul Manan Date: Tue, 27 Aug 2024 19:26:46 +0500 Subject: [PATCH] Added currency field --- course_discovery/apps/api/serializers.py | 5 +++++ course_discovery/apps/course_metadata/models.py | 11 +++++++++++ .../apps/course_metadata/search_indexes.py | 4 ++++ 3 files changed, 20 insertions(+) diff --git a/course_discovery/apps/api/serializers.py b/course_discovery/apps/api/serializers.py index b1672cce07..50ef03ce8c 100644 --- a/course_discovery/apps/api/serializers.py +++ b/course_discovery/apps/api/serializers.py @@ -2207,6 +2207,7 @@ class Meta: class CourseRunSearchSerializer(HaystackSerializer): availability = serializers.SerializerMethodField() first_enrollable_paid_seat_price = serializers.SerializerMethodField() + first_enrollable_paid_seat_currency = serializers.SerializerMethodField() type = serializers.SerializerMethodField() is_enrollable = serializers.SerializerMethodField() @@ -2216,6 +2217,9 @@ def get_availability(self, result): def get_first_enrollable_paid_seat_price(self, result): return result.object.first_enrollable_paid_seat_price + def get_first_enrollable_paid_seat_currency(self, result): + return result.object.first_enrollable_paid_seat_currency + def get_type(self, result): return result.object.type_legacy @@ -2234,6 +2238,7 @@ class Meta: 'enrollment_start', 'first_enrollable_paid_seat_sku', 'first_enrollable_paid_seat_price', + 'first_enrollable_paid_seat_currency', 'full_description', 'go_live_date', 'has_enrollable_seats', diff --git a/course_discovery/apps/course_metadata/models.py b/course_discovery/apps/course_metadata/models.py index f65d9fc3ad..eb98b4a4b9 100644 --- a/course_discovery/apps/course_metadata/models.py +++ b/course_discovery/apps/course_metadata/models.py @@ -1421,6 +1421,17 @@ def first_enrollable_paid_seat_price(self): price = int(seats[0].price) if seats[0].price else None return price + + @property + def first_enrollable_paid_seat_currency(self): + """ + Returns currency code for first enrollable paid seat. + """ + seats = sorted(self._enrollable_paid_seats(), key=self._upgrade_deadline_sort) + if not seats: + return None + + return seats[0].currency.code or None def first_enrollable_paid_seat_sku(self): # Sort in python to avoid an additional request to the database for order_by diff --git a/course_discovery/apps/course_metadata/search_indexes.py b/course_discovery/apps/course_metadata/search_indexes.py index fe78446917..ddf8ecba09 100644 --- a/course_discovery/apps/course_metadata/search_indexes.py +++ b/course_discovery/apps/course_metadata/search_indexes.py @@ -240,6 +240,7 @@ class CourseRunIndex(BaseCourseIndex, indexes.Indexable): has_enrollable_paid_seats = indexes.BooleanField(null=False) first_enrollable_paid_seat_sku = indexes.CharField(null=True) first_enrollable_paid_seat_price = indexes.IntegerField(null=True) + first_enrollable_paid_seat_currency = indexes.CharField(null=True) paid_seat_enrollment_end = indexes.DateTimeField(null=True) license = indexes.MultiValueField(model_attr='license', faceted=True) has_enrollable_seats = indexes.BooleanField(model_attr='has_enrollable_seats', null=False) @@ -288,6 +289,9 @@ def prepare_first_enrollable_paid_seat_sku(self, obj): def prepare_first_enrollable_paid_seat_price(self, obj): return obj.first_enrollable_paid_seat_price + def prepare_first_enrollable_paid_seat_currency(self, obj): + return obj.first_enrollable_paid_seat_currency + def prepare_is_current_and_still_upgradeable(self, obj): return obj.is_current_and_still_upgradeable()