Commit e67e9096 by ayub-khan Committed by Ayub khan

Learner-780 -Display price of only active courses on prog cert page

parent 44791b98
...@@ -881,6 +881,15 @@ class Program(TimeStampedModel): ...@@ -881,6 +881,15 @@ class Program(TimeStampedModel):
yield seat yield seat
@property @property
def canonical_seats(self):
applicable_seat_types = set(seat_type.slug for seat_type in self.type.applicable_seat_types.all())
for run in self.canonical_course_runs:
for seat in run.seats.all():
if seat.type in applicable_seat_types:
yield seat
@property
def seat_types(self): def seat_types(self):
return set(seat.type for seat in self.seats) return set(seat.type for seat in self.seats)
...@@ -912,7 +921,7 @@ class Program(TimeStampedModel): ...@@ -912,7 +921,7 @@ class Program(TimeStampedModel):
""" """
currencies_with_total = defaultdict() currencies_with_total = defaultdict()
course_map = defaultdict(list) course_map = defaultdict(list)
for seat in self.seats: for seat in self.canonical_seats:
course_uuid = seat.course_run.course.uuid course_uuid = seat.course_run.course.uuid
# Identify the most relevant course_run seat for a course. # Identify the most relevant course_run seat for a course.
# And use the price of the seat to represent the price of the course # And use the price of the seat to represent the price of the course
...@@ -959,7 +968,7 @@ class Program(TimeStampedModel): ...@@ -959,7 +968,7 @@ class Program(TimeStampedModel):
@property @property
def price_ranges(self): def price_ranges(self):
currencies = defaultdict(list) currencies = defaultdict(list)
for seat in self.seats: for seat in self.canonical_seats:
currencies[seat.currency].append(seat.price) currencies[seat.currency].append(seat.price)
total_by_currency = self._get_total_price_by_currency() total_by_currency = self._get_total_price_by_currency()
......
...@@ -452,6 +452,8 @@ class ProgramTests(TestCase): ...@@ -452,6 +452,8 @@ class ProgramTests(TestCase):
currency = Currency.objects.get(code='USD') currency = Currency.objects.get(code='USD')
course_run = factories.CourseRunFactory() course_run = factories.CourseRunFactory()
course_run.course.canonical_course_run = course_run
course_run.course.save()
factories.SeatFactory(type='audit', currency=currency, course_run=course_run, price=0) factories.SeatFactory(type='audit', currency=currency, course_run=course_run, price=0)
factories.SeatFactory(type='credit', currency=currency, course_run=course_run, price=600) factories.SeatFactory(type='credit', currency=currency, course_run=course_run, price=600)
factories.SeatFactory(type='verified', currency=currency, course_run=course_run, price=100) factories.SeatFactory(type='verified', currency=currency, course_run=course_run, price=100)
...@@ -651,6 +653,24 @@ class ProgramTests(TestCase): ...@@ -651,6 +653,24 @@ class ProgramTests(TestCase):
# Verify only canonical course runs are returned in set # Verify only canonical course runs are returned in set
self.assertEqual(set(self.program.canonical_course_runs), set(expected_canonical_runs)) self.assertEqual(set(self.program.canonical_course_runs), set(expected_canonical_runs))
def test_canonical_course_seats(self):
""" Test canonical course seats returns only canonical course run's applicable seats """
currency = Currency.objects.get(code='USD')
course = factories.CourseFactory()
course_runs_same_course = factories.CourseRunFactory.create_batch(3, course=course)
for course_run in course_runs_same_course:
factories.SeatFactory(type='verified', currency=currency, course_run=course_run, price=100)
course.canonical_course_run = course_runs_same_course[0]
course.save()
applicable_seat_types = SeatType.objects.filter(slug__in=['verified'])
program_type = factories.ProgramTypeFactory(applicable_seat_types=applicable_seat_types)
program = factories.ProgramFactory(type=program_type, courses=[course])
self.assertEqual(set(course.canonical_course_run.seats.all()), set(program.canonical_seats))
def test_languages(self): def test_languages(self):
expected_languages = set([course_run.language for course_run in self.course_runs]) expected_languages = set([course_run.language for course_run in self.course_runs])
actual_languages = self.program.languages actual_languages = self.program.languages
...@@ -704,6 +724,8 @@ class ProgramTests(TestCase): ...@@ -704,6 +724,8 @@ class ProgramTests(TestCase):
factories.SeatFactory(type='audit', currency=currency, course_run=course_run, price=0) factories.SeatFactory(type='audit', currency=currency, course_run=course_run, price=0)
factories.SeatFactory(type='verified', currency=currency, course_run=course_run, price=test_price) factories.SeatFactory(type='verified', currency=currency, course_run=course_run, price=test_price)
test_price += 100 test_price += 100
course_run.course.canonical_course_run = course_run
course_run.course.save()
applicable_seat_types = SeatType.objects.filter(slug__in=['verified']) applicable_seat_types = SeatType.objects.filter(slug__in=['verified'])
program_type = factories.ProgramTypeFactory(applicable_seat_types=applicable_seat_types) program_type = factories.ProgramTypeFactory(applicable_seat_types=applicable_seat_types)
...@@ -721,6 +743,8 @@ class ProgramTests(TestCase): ...@@ -721,6 +743,8 @@ class ProgramTests(TestCase):
for course_run in single_course_course_runs: for course_run in single_course_course_runs:
factories.SeatFactory(type='audit', currency=currency, course_run=course_run, price=0) factories.SeatFactory(type='audit', currency=currency, course_run=course_run, price=0)
factories.SeatFactory(type='verified', currency=currency, course_run=course_run, price=10) factories.SeatFactory(type='verified', currency=currency, course_run=course_run, price=10)
course_run.course.canonical_course_run = course_run
course_run.course.save()
day_separation = 1 day_separation = 1
now = datetime.datetime.utcnow() now = datetime.datetime.utcnow()
...@@ -741,7 +765,8 @@ class ProgramTests(TestCase): ...@@ -741,7 +765,8 @@ class ProgramTests(TestCase):
course_run=course_run, course_run=course_run,
price=(day_separation * 100)) price=(day_separation * 100))
day_separation += 1 day_separation += 1
course.canonical_course_run = course_runs_same_course[2]
course.save()
applicable_seat_types = SeatType.objects.filter(slug__in=['verified']) applicable_seat_types = SeatType.objects.filter(slug__in=['verified'])
program_type = factories.ProgramTypeFactory(applicable_seat_types=applicable_seat_types) program_type = factories.ProgramTypeFactory(applicable_seat_types=applicable_seat_types)
...@@ -757,7 +782,7 @@ class ProgramTests(TestCase): ...@@ -757,7 +782,7 @@ class ProgramTests(TestCase):
""" """
program = self.create_program_with_multiple_course_runs() program = self.create_program_with_multiple_course_runs()
expected_price_ranges = [{'currency': 'USD', 'min': Decimal(10), 'max': Decimal(300), 'total': Decimal(130)}] expected_price_ranges = [{'currency': 'USD', 'min': Decimal(10), 'max': Decimal(300), 'total': Decimal(330)}]
self.assertEqual(program.price_ranges, expected_price_ranges) self.assertEqual(program.price_ranges, expected_price_ranges)
def test_price_ranges_with_multiple_course_runs_and_none_dates(self): def test_price_ranges_with_multiple_course_runs_and_none_dates(self):
...@@ -765,9 +790,10 @@ class ProgramTests(TestCase): ...@@ -765,9 +790,10 @@ class ProgramTests(TestCase):
Verifies the price_range property of a program with multiple courses, Verifies the price_range property of a program with multiple courses,
and a course with multiple runs, and some of the dates in the course runs are None and a course with multiple runs, and some of the dates in the course runs are None
""" """
program = self.create_program_with_multiple_course_runs(set_all_dates=False) program = self.create_program_with_multiple_course_runs(set_all_dates=False)
expected_price_ranges = [{'currency': 'USD', 'min': Decimal(10), 'max': Decimal(300), 'total': Decimal(130)}] expected_price_ranges = [{'currency': 'USD', 'min': Decimal(10), 'max': Decimal(300), 'total': Decimal(330)}]
self.assertEqual(program.price_ranges, expected_price_ranges) self.assertEqual(program.price_ranges, expected_price_ranges)
def test_staff(self): def test_staff(self):
......
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