Skip to content

Commit

Permalink
Programs API v2 (#1966)
Browse files Browse the repository at this point in the history
* Added tests, fixture and utility functions

* oops, put nplus 1 back & reparameterize the fixture

* reformat

* Add to Programs as well

* reformat

* add v2

* updated per Nathan's feedback

* merge + update for feedback

* moved to v2, versioned tests and serializers as well

* remove random copy pasta

* Example of catalog data fixture

* added v1 and v2 programs. having urls issues

* reformat

* namespace fixed. Paring down programs list a little.

* Fixing programs

* reformat

* reformat

* fix merge

* testing

* fix extra urls for records/program

* less calls

* reformat

* v2 serializer test

* refactor v1 structure + add financial page

* move v2 stuff

* missing import

* reformat

* remove empty file

* reformat

* imports

* how though

* more mocks eaten

* add .live()

* update for additional query due to flex form

* reformat

* test failure

* fmt

* fix patch

---------

Co-authored-by: Nathan Levesque <rhysyngsun@gmail.com>
  • Loading branch information
JenniWhitman and rhysyngsun authored Nov 1, 2023
1 parent 709b0e3 commit 863ea21
Show file tree
Hide file tree
Showing 30 changed files with 1,738 additions and 1,187 deletions.
35 changes: 35 additions & 0 deletions cms/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,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 @@ -164,11 +165,45 @@ 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
)
.live()
.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
107 changes: 106 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,
Expand Down Expand Up @@ -304,3 +304,108 @@ def test_serialized_course_finaid_form_url(
related_fa_page.slug
in serialized_output["financial_assistance_form_url"]
)


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,
},
)
4 changes: 2 additions & 2 deletions courses/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,8 @@ def course_catalog_data(course_catalog_program_count, course_catalog_course_coun
Returns 3 separate lists to simulate what the tests received prior.
Args:
num_courses(int): number of courses to generate.
num_programs(int): number of programs to generate.
course_catalog_course_count(int): number of courses to generate.
course_catalog_program_count(int): number of programs to generate.
"""
programs = []
courses = []
Expand Down
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 import ProgramRequirementTreeSerializer
from courses.serializers.v1.programs import ProgramRequirementTreeSerializer
from courses.widgets import ProgramRequirementsInput


Expand Down
Loading

0 comments on commit 863ea21

Please sign in to comment.