Skip to content

Commit

Permalink
refactor v1 structure + add financial page
Browse files Browse the repository at this point in the history
  • Loading branch information
Jenni Whitman committed Oct 31, 2023
1 parent cfed566 commit 44a6962
Show file tree
Hide file tree
Showing 16 changed files with 1,436 additions and 1,292 deletions.
24 changes: 24 additions & 0 deletions cms/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,7 @@ class ProgramPageSerializer(serializers.ModelSerializer):
feature_image_src = serializers.SerializerMethodField()
page_url = serializers.SerializerMethodField()
price = serializers.SerializerMethodField()
financial_assistance_form_url = serializers.SerializerMethodField()

def get_feature_image_src(self, instance):
"""Serializes the source of the feature_image"""
Expand All @@ -155,11 +156,34 @@ def get_page_url(self, instance):
def get_price(self, instance):
return instance.price[0].value["text"] if len(instance.price) > 0 else None

def get_financial_assistance_form_url(self, instance):
"""
Returns URL of the Financial Assistance Form.
"""
financial_assistance_page = FlexiblePricingRequestForm.objects.filter(
selected_program_id=instance.program.id
).live().first()
if (financial_assistance_page is None) and (instance.get_children() is not None):
financial_assistance_page = (
instance.get_children().type(FlexiblePricingRequestForm).live().first()
)
if (financial_assistance_page is None) & (len(instance.program.related_programs) > 0):
financial_assistance_page = FlexiblePricingRequestForm.objects.filter(
selected_program__in=instance.program.related_programs
).first()
return (
f"{instance.get_url()}{financial_assistance_page.slug}/"
if financial_assistance_page
else ""
)


class Meta:
model = models.ProgramPage
fields = [
"feature_image_src",
"page_url",
"financial_assistance_form_url",
"description",
"live",
"length",
Expand Down
103 changes: 102 additions & 1 deletion cms/serializers_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
ProgramPageFactory,
)
from cms.models import FlexiblePricingRequestForm
from cms.serializers import CoursePageSerializer
from cms.serializers import CoursePageSerializer, ProgramPageSerializer
from courses.factories import CourseFactory, ProgramFactory
from main.test_utils import assert_drf_json_equal

Expand Down Expand Up @@ -225,3 +225,104 @@ def test_serialize_course_page_with_flex_price_form_as_child_no_program(
"effort": course_page.effort,
},
)


def test_serialize_program_page(mocker, fully_configured_wagtail, staff_user, mock_context):
fake_image_src = "http://example.com/my.img"
patched_get_wagtail_src = mocker.patch(
"cms.serializers.get_wagtail_img_src", return_value=fake_image_src
)

program = ProgramFactory(page=None)
program_page = ProgramPageFactory(program=program)
financial_assistance_form = FlexiblePricingFormFactory(
selected_program_id=program.id, parent=program_page
)
rf = RequestFactory()
request = rf.get("/")
request.user = staff_user

data = ProgramPageSerializer(
instance=program_page, context=program_page.get_context(request)
).data
assert_drf_json_equal(
data,
{
"feature_image_src": fake_image_src,
"page_url": program_page.url,
"financial_assistance_form_url": f"{program_page.get_url()}{financial_assistance_form.slug}/",
"description": bleach.clean(program_page.description, tags=[], strip=True),
"live": True,
"length": program_page.length,
"effort": program_page.effort,
"price": None,
}
)


def test_serialize_program_page__with_related_financial_form(mocker, fully_configured_wagtail, staff_user,
mock_context):
fake_image_src = "http://example.com/my.img"
patched_get_wagtail_src = mocker.patch(
"cms.serializers.get_wagtail_img_src", return_value=fake_image_src
)

program = ProgramFactory(page=None)
program_page = ProgramPageFactory(program=program)
other_program = ProgramFactory(page=None)
other_program_page = ProgramPageFactory(program=other_program)
financial_assistance_form = FlexiblePricingFormFactory(
selected_program_id=other_program.id, parent=other_program_page
)
program.add_related_program(other_program)
rf = RequestFactory()
request = rf.get("/")
request.user = staff_user

data = ProgramPageSerializer(
instance=program_page, context=program_page.get_context(request)
).data
assert_drf_json_equal(
data,
{
"feature_image_src": fake_image_src,
"page_url": program_page.url,
"financial_assistance_form_url": f"{program_page.get_url()}{financial_assistance_form.slug}/",
"description": bleach.clean(program_page.description, tags=[], strip=True),
"live": True,
"length": program_page.length,
"effort": program_page.effort,
"price": None,
}
)


def test_serialize_program_page__no_financial_form(mocker, fully_configured_wagtail, staff_user,
mock_context):
fake_image_src = "http://example.com/my.img"
patched_get_wagtail_src = mocker.patch(
"cms.serializers.get_wagtail_img_src", return_value=fake_image_src
)

program = ProgramFactory(page=None)
program_page = ProgramPageFactory(program=program)
rf = RequestFactory()
request = rf.get("/")
request.user = staff_user

data = ProgramPageSerializer(
instance=program_page, context=program_page.get_context(request)
).data
assert_drf_json_equal(
data,
{
"feature_image_src": fake_image_src,
"page_url": program_page.url,
"financial_assistance_form_url": "",
"description": bleach.clean(program_page.description, tags=[], strip=True),
"live": True,
"length": program_page.length,
"effort": program_page.effort,
"price": None,
}
)
2 changes: 1 addition & 1 deletion courses/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
ProgramRequirement,
ProgramRequirementNodeType,
)
from courses.serializers.v1 import ProgramRequirementTreeSerializer
from courses.serializers.v1.programs import ProgramRequirementTreeSerializer
from courses.widgets import ProgramRequirementsInput


Expand Down
Loading

0 comments on commit 44a6962

Please sign in to comment.