From 1c4186a7b61cb8b79a5d44e9a88cc9035640869d Mon Sep 17 00:00:00 2001 From: Matt Bertrand Date: Tue, 18 Jun 2024 12:46:31 -0400 Subject: [PATCH 1/2] Make resource.prices = any published run prices if there is no next run --- learning_resources/models.py | 13 +++++++++++- learning_resources/models_test.py | 34 +++++++++++++++++++++++++++++++ 2 files changed, 46 insertions(+), 1 deletion(-) diff --git a/learning_resources/models.py b/learning_resources/models.py index 72bbdb4780..b4092e00fe 100644 --- a/learning_resources/models.py +++ b/learning_resources/models.py @@ -2,6 +2,7 @@ from decimal import Decimal +from django.contrib.admin.utils import flatten from django.contrib.auth.models import User from django.contrib.postgres.fields import ArrayField from django.db import models @@ -256,7 +257,17 @@ def prices(self) -> list[Decimal]: LearningResourceType.program.name, ]: next_run = self.next_run - return next_run.prices if next_run else [] + return ( + next_run.prices + if next_run and next_run.prices + else sorted( + set( + flatten( + [run.prices for run in self.runs.filter(published=True)] + ) + ) + ) + ) else: return [Decimal(0.00)] diff --git a/learning_resources/models_test.py b/learning_resources/models_test.py index 4f0db4d45c..3316d4348b 100644 --- a/learning_resources/models_test.py +++ b/learning_resources/models_test.py @@ -1,10 +1,16 @@ """Tests for learning_resources.models""" +from datetime import timedelta +from decimal import Decimal + import pytest +from django.contrib.admin.utils import flatten +from django.db.models import F from learning_resources.constants import LearningResourceType from learning_resources.factories import ( CourseFactory, + LearningResourceRunFactory, ProgramFactory, ) @@ -48,3 +54,31 @@ def test_course_creation(): assert resource.offered_by is not None assert resource.runs.count() == course.runs.count() assert resource.prices == resource.next_run.prices + + +def test_course_prices_current_no_next(): + """Test that course.prices == published run prices if no next run""" + course = CourseFactory.create() + resource = course.learning_resource + resource.runs.update(start_date=F("start_date") - timedelta(days=3650)) + unpub_run = LearningResourceRunFactory.create( + learning_resource=resource, published=False, prices=[Decimal("987654.32")] + ) + resource.refresh_from_db() + assert resource.next_run is None + # Prices should be from any published run if no next run + assert resource.prices == sorted( + set(flatten([run.prices for run in resource.runs.filter(published=True)])) + ) + assert len(resource.prices) > 0 + assert unpub_run.prices[0] not in resource.prices + + +def test_course_prices_unpublished_runs(): + """Test that course.prices == [] if no published run""" + course = CourseFactory.create() + resource = course.learning_resource + resource.runs.update(published=False) + resource.refresh_from_db() + assert resource.next_run is None + assert resource.prices == [] From 0fde87a8e216f2471c6d298a33ae1569d67d6af5 Mon Sep 17 00:00:00 2001 From: Matt Bertrand Date: Tue, 18 Jun 2024 13:17:01 -0400 Subject: [PATCH 2/2] most recent run --- learning_resources/models.py | 16 ++++------------ learning_resources/models_test.py | 12 +++++------- 2 files changed, 9 insertions(+), 19 deletions(-) diff --git a/learning_resources/models.py b/learning_resources/models.py index b4092e00fe..0f0769dbaa 100644 --- a/learning_resources/models.py +++ b/learning_resources/models.py @@ -2,7 +2,6 @@ from decimal import Decimal -from django.contrib.admin.utils import flatten from django.contrib.auth.models import User from django.contrib.postgres.fields import ArrayField from django.db import models @@ -256,18 +255,11 @@ def prices(self) -> list[Decimal]: LearningResourceType.course.name, LearningResourceType.program.name, ]: - next_run = self.next_run - return ( - next_run.prices - if next_run and next_run.prices - else sorted( - set( - flatten( - [run.prices for run in self.runs.filter(published=True)] - ) - ) - ) + next_run = ( + self.next_run + or self.runs.filter(published=True).order_by("-start_date").first() ) + return next_run.prices if next_run and next_run.prices else [] else: return [Decimal(0.00)] diff --git a/learning_resources/models_test.py b/learning_resources/models_test.py index 3316d4348b..823105e1a4 100644 --- a/learning_resources/models_test.py +++ b/learning_resources/models_test.py @@ -4,7 +4,6 @@ from decimal import Decimal import pytest -from django.contrib.admin.utils import flatten from django.db.models import F from learning_resources.constants import LearningResourceType @@ -57,7 +56,7 @@ def test_course_creation(): def test_course_prices_current_no_next(): - """Test that course.prices == published run prices if no next run""" + """Test that course.prices == most recent run prices if no next run""" course = CourseFactory.create() resource = course.learning_resource resource.runs.update(start_date=F("start_date") - timedelta(days=3650)) @@ -65,13 +64,12 @@ def test_course_prices_current_no_next(): learning_resource=resource, published=False, prices=[Decimal("987654.32")] ) resource.refresh_from_db() + most_recent_run = resource.runs.filter(published=True).order_by("start_date").last() + assert most_recent_run != unpub_run assert resource.next_run is None - # Prices should be from any published run if no next run - assert resource.prices == sorted( - set(flatten([run.prices for run in resource.runs.filter(published=True)])) - ) + # Prices should be from most recent published run if no next run + assert resource.prices == most_recent_run.prices assert len(resource.prices) > 0 - assert unpub_run.prices[0] not in resource.prices def test_course_prices_unpublished_runs():