Commit 3a051f35 by Matthew Piatetsky

add first paid seat sku to search

parent c8b72d39
......@@ -58,6 +58,7 @@ COURSE_RUN_SEARCH_FIELDS = (
'number', 'seat_types', 'image_url', 'type', 'level_type', 'availability', 'published', 'partner', 'program_types',
'authoring_organization_uuids', 'subject_uuids', 'staff_uuids', 'mobile_available', 'logo_image_urls',
'aggregation_key', 'min_effort', 'max_effort', 'weeks_to_complete', 'has_enrollable_seats',
'first_enrollable_paid_seat_sku'
)
PROGRAM_FACET_FIELD_OPTIONS = {
......
......@@ -1237,6 +1237,7 @@ class CourseRunSearchSerializerTests(ElasticsearchTestMixin, TestCase):
def test_data(self):
course_run = CourseRunFactory(transcript_languages=LanguageTag.objects.filter(code__in=['en-us', 'zh-cn']),
authoring_organizations=[OrganizationFactory()])
SeatFactory.create(course_run=course_run, type='verified', price=10, sku='ABCDEF')
program = ProgramFactory(courses=[course_run.course])
self.reindex_courses(program)
serializer = self.serialize_course_run(course_run)
......@@ -1276,6 +1277,7 @@ class CourseRunSearchSerializerTests(ElasticsearchTestMixin, TestCase):
'staff_uuids': get_uuids(course_run.staff.all()),
'aggregation_key': 'courserun:{}'.format(course_run.course.key),
'has_enrollable_seats': course_run.has_enrollable_seats,
'first_enrollable_paid_seat_sku': course_run.first_enrollable_paid_seat_sku(),
}
assert serializer.data == expected
......
......@@ -521,6 +521,14 @@ class CourseRun(TimeStampedModel):
"""
return self.seats.exclude(type__in=Seat.SEATS_WITH_PREREQUISITES).filter(price__gt=0.0)
def first_enrollable_paid_seat_sku(self):
seats = list(self._enrollable_paid_seats().order_by('upgrade_deadline'))
if not seats:
# Enrollable paid seats are not available for this CourseRun.
return None
first_enrollable_paid_seat_sku = seats[0].sku
return first_enrollable_paid_seat_sku
def has_enrollable_paid_seats(self):
"""
Return a boolean indicating whether or not enrollable paid Seats (Seats with price > 0 and no prerequisites)
......
......@@ -168,6 +168,7 @@ class CourseRunIndex(BaseCourseIndex, indexes.Indexable):
staff_uuids = indexes.MultiValueField()
subject_uuids = indexes.MultiValueField()
has_enrollable_paid_seats = indexes.BooleanField(null=False)
first_enrollable_paid_seat_sku = 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)
......@@ -180,6 +181,9 @@ class CourseRunIndex(BaseCourseIndex, indexes.Indexable):
def prepare_has_enrollable_paid_seats(self, obj):
return obj.has_enrollable_paid_seats()
def prepare_first_enrollable_paid_seat_sku(self, obj):
return obj.first_enrollable_paid_seat_sku()
def prepare_is_current_and_still_upgradeable(self, obj):
return obj.is_current_and_still_upgradeable()
......
......@@ -232,6 +232,14 @@ class CourseRunTests(TestCase):
factories.SeatFactory.create(course_run=course_run, type=seat_type, price=price)
self.assertEqual(course_run.has_enrollable_paid_seats(), expected_result)
def test_first_enrollable_paid_seat_sku(self):
"""
Verify that first_enrollable_paid_seat_sku returns sku of first paid seat.
"""
course_run = factories.CourseRunFactory.create()
factories.SeatFactory.create(course_run=course_run, type='verified', price=10, sku='ABCDEF')
self.assertEqual(course_run.first_enrollable_paid_seat_sku(), 'ABCDEF')
@ddt.data(
# Case 1: Return None when there are no enrollable paid Seats.
([('audit', 0, None)], '2016-12-31 00:00:00Z', '2016-08-31 00:00:00Z', None),
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment