From dc7daccfc8c20de6ee9e8552066cc7700fba5afe Mon Sep 17 00:00:00 2001 From: Calen Pennington Date: Mon, 10 Jun 2019 14:26:42 -0400 Subject: [PATCH] Add strike-out price in LMS courseware --- .../content_type_gating/partitions.py | 16 ++++++++-- .../access_denied_message.html | 7 ++++- .../content_type_gating/tests/test_access.py | 29 +++++++++++++++++++ 3 files changed, 49 insertions(+), 3 deletions(-) diff --git a/openedx/features/content_type_gating/partitions.py b/openedx/features/content_type_gating/partitions.py index b3e8c492da..4c30c66d13 100644 --- a/openedx/features/content_type_gating/partitions.py +++ b/openedx/features/content_type_gating/partitions.py @@ -17,9 +17,11 @@ from web_fragments.fragment import Fragment from course_modes.models import CourseMode from lms.djangoapps.commerce.utils import EcommerceService +from openedx.core.djangoapps.content.course_overviews.models import CourseOverview from openedx.core.lib.mobile_utils import is_request_from_mobile_app from openedx.features.content_type_gating.helpers import CONTENT_GATING_PARTITION_ID, FULL_ACCESS, LIMITED_ACCESS from openedx.features.content_type_gating.models import ContentTypeGatingConfig +from openedx.features.discounts.applicability import discount_percentage, can_receive_discount from xmodule.partitions.partitions import UserPartition, UserPartitionError LOG = logging.getLogger(__name__) @@ -76,7 +78,8 @@ class ContentTypeGatingPartition(UserPartition): """ def access_denied_fragment(self, block, user, user_group, allowed_groups): course_key = self._get_course_key_from_course_block(block) - modes = CourseMode.modes_for_course_dict(course_key) + course = CourseOverview.get_from_id(course_key) + modes = CourseMode.modes_for_course_dict(course=course) verified_mode = modes.get(CourseMode.VERIFIED) if (verified_mode is None or user_group == FULL_ACCESS or user_group in allowed_groups): @@ -84,10 +87,19 @@ class ContentTypeGatingPartition(UserPartition): ecommerce_checkout_link = self._get_checkout_link(user, verified_mode.sku) request = crum.get_current_request() + + if can_receive_discount(user, course): + price_before_discount = verified_mode.min_price + upgrade_price = "{:0.2f}".format(price_before_discount * ((100.0 - discount_percentage()) / 100)) + else: + price_before_discount = None + upgrade_price = verified_mode.min_price + frag = Fragment(render_to_string('content_type_gating/access_denied_message.html', { 'mobile_app': request and is_request_from_mobile_app(request), 'ecommerce_checkout_link': ecommerce_checkout_link, - 'min_price': str(verified_mode.min_price) + 'min_price': upgrade_price, + 'price_before_discount': price_before_discount, })) return frag diff --git a/openedx/features/content_type_gating/templates/content_type_gating/access_denied_message.html b/openedx/features/content_type_gating/templates/content_type_gating/access_denied_message.html index b47de960f5..a215a539d1 100644 --- a/openedx/features/content_type_gating/templates/content_type_gating/access_denied_message.html +++ b/openedx/features/content_type_gating/templates/content_type_gating/access_denied_message.html @@ -12,7 +12,12 @@ {% if not mobile_app and ecommerce_checkout_link %} - {% trans "Upgrade to unlock" %} (${{min_price}} USD) + {% trans "Upgrade to unlock" %} + {% if price_before_discount %} + (${{min_price}} USD ${{price_before_discount}} USD) + {% else %} + (${{min_price}} USD) + {% endif %} {% endif %} diff --git a/openedx/features/content_type_gating/tests/test_access.py b/openedx/features/content_type_gating/tests/test_access.py index 2d8d2d7ad0..c72f8d248a 100644 --- a/openedx/features/content_type_gating/tests/test_access.py +++ b/openedx/features/content_type_gating/tests/test_access.py @@ -20,6 +20,7 @@ from pyquery import PyQuery as pq from six.moves.html_parser import HTMLParser +from course_modes.models import CourseMode from course_api.blocks.api import get_blocks from course_modes.tests.factories import CourseModeFactory from experiments.models import ExperimentData, ExperimentKeyValue @@ -43,6 +44,7 @@ from openedx.core.djangoapps.django_comment_common.models import ( from openedx.core.djangoapps.user_api.tests.factories import UserCourseTagFactory from openedx.core.djangoapps.util.testing import TestConditionalContent from openedx.core.lib.url_utils import quote_slashes +from openedx.features.discounts.applicability import DISCOUNT_APPLICABILITY_FLAG from openedx.features.content_type_gating.helpers import CONTENT_GATING_PARTITION_ID, CONTENT_TYPE_GATE_GROUP_IDS from openedx.features.content_type_gating.models import ContentTypeGatingConfig from openedx.features.content_type_gating.partitions import ContentTypeGatingPartition @@ -780,6 +782,33 @@ class TestProblemTypeAccess(SharedModuleStoreTestCase): request_factory=self.factory, ) + @ddt.data(True, False) + def test_discount_display(self, has_discount): + verified_mode = CourseMode.objects.get(course_id=self.course.id, mode_slug=CourseMode.VERIFIED) + verified_mode.min_price = 100 + verified_mode.save() + + with DISCOUNT_APPLICABILITY_FLAG.override(has_discount): + with patch.object(ContentTypeGatingPartition, '_get_checkout_link', return_value='#'): + block_content = _get_content_from_lms_index( + block=self.blocks_dict['problem'], + user_id=self.audit_user.id, + course=self.course, + request_factory=self.factory, + ) + + assert 'content-paywall' in block_content + assert 'certA_1' in block_content + + if has_discount: + assert '$100' in block_content + assert '$85' in block_content + else: + assert '' not in block_content + assert '$85' not in block_content + assert '$100' in block_content + + @override_settings(FIELD_OVERRIDE_PROVIDERS=( 'openedx.features.content_type_gating.field_override.ContentTypeGatingFieldOverride',