Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add a flag to hide course prices in enrollment page when zero #6

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,12 @@ Unreleased
----------
* nothing

[3.42.9]
--------
feat: add a flag to hide course prices in enrollment page when zero

[3.42.8]
--------
feat: add additional fields to EnterpriseCourseEnrollmentViewSet

[3.42.7]
Expand Down
2 changes: 1 addition & 1 deletion enterprise/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@
Your project description goes here.
"""

__version__ = "3.42.8"
__version__ = "3.42.9"

default_app_config = "enterprise.apps.EnterpriseConfig"
1 change: 1 addition & 0 deletions enterprise/admin/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -398,6 +398,7 @@ class Meta:
"enable_audit_data_reporting",
"replace_sensitive_sso_username",
"hide_course_original_price",
"hide_course_price_when_zero",
"enable_portal_code_management_screen",
"enable_portal_subscription_management_screen",
"enable_learner_portal",
Expand Down
23 changes: 23 additions & 0 deletions enterprise/migrations/0155_auto_20230706_0810.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# Generated by Django 3.2.15 on 2023-07-06 08:10

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('enterprise', '0154_alter_systemwideenterpriseuserroleassignment_unique_together'),
]

operations = [
migrations.AddField(
model_name='enterprisecustomer',
name='hide_course_price_when_zero',
field=models.BooleanField(default=False, help_text='Specify whether course cost should be hidden in the landing page when the final price is zero.'),
),
migrations.AddField(
model_name='historicalenterprisecustomer',
name='hide_course_price_when_zero',
field=models.BooleanField(default=False, help_text='Specify whether course cost should be hidden in the landing page when the final price is zero.'),
),
]
5 changes: 5 additions & 0 deletions enterprise/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -386,6 +386,11 @@ class Meta:
help_text=_("The email address where learner's reply to enterprise emails will be delivered.")
)

hide_course_price_when_zero = models.BooleanField(
default=False,
help_text=_("Specify whether course cost should be hidden in the landing page when the final price is zero.")
)

@property
def enterprise_customer_identity_provider(self):
"""
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,19 +77,21 @@ <h2>{{ confirmation_text }}</h2>

<label for="radio{{ forloop.counter0 }}">
<strong class="title">{{ course_mode.title }}</strong>
<span class="price">
{{ price_text }}:
{% if course_mode.final_price and course_mode.original_price != course_mode.final_price %}
{% if hide_course_original_price %}
{{ course_mode.final_price }}
{% if not course_mode.hide_price %}
<span class="price">
{{ price_text }}:
{% if course_mode.final_price and course_mode.original_price != course_mode.final_price %}
{% if hide_course_original_price %}
{{ course_mode.final_price }}
{% else %}
<strike>{{ course_mode.original_price }}</strike> {{ course_mode.final_price }}
<div>{{discount_text|safe }}</div>
{% endif %}
{% else %}
<strike>{{ course_mode.original_price }}</strike> {{ course_mode.final_price }}
<div>{{discount_text|safe }}</div>
{{ course_mode.original_price }}
{% endif %}
{% else %}
{{ course_mode.original_price }}
{% endif %}
</span>
</span>
{% endif %}
<span class="description">{{ course_mode.description }}</span>
</label>
</div>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ <h1 id="modal-header-text-{{ index }}" class="modal-header-text">{{ course_title
<ul class="details-list">
{% if premium_modes|length > 0 %}
{% with course_mode=premium_modes.0 %}
{% if course_mode.original_price %}
{% if course_mode.original_price and not course_mode.hide_price %}
<li>
<div class="detail-container">
<div class="detail-title-container">
Expand Down
24 changes: 24 additions & 0 deletions enterprise/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -2302,3 +2302,27 @@ def parse_lms_api_datetime(datetime_string, datetime_format=LMS_API_DATETIME_FOR
def localized_utcnow():
"""Helper function to return localized utcnow()."""
return pytz.UTC.localize(datetime.datetime.utcnow()) # pylint: disable=no-value-for-parameter


def hide_price_when_zero(enterprise_customer, course_modes):
"""
Adds a "hide_price" flag to the course modes if price is zero and "Hide course price when zero" flag is set.

Arguments:
enterprise_customer: The EnterpriseCustomer that the enrollemnt is being done.
course_modes: iterable with dictionaries containing a required 'final_price' key
"""
if not enterprise_customer.hide_course_price_when_zero:
return course_modes

for mode in course_modes:
mode['hide_price'] = False
try:
numbers = re.findall(r'\d+', mode['final_price'])
mode['hide_price'] = int(''.join(numbers)) == 0
except ValueError:
LOGGER.warning(
'hide_price_when_zero: Could not convert price of course mode "%s" to int.',
mode['title']
)
return course_modes
4 changes: 4 additions & 0 deletions enterprise/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@
get_enterprise_customer_user,
get_platform_logo_url,
get_program_type_description,
hide_price_when_zero,
is_course_run_enrollable,
localized_utcnow,
track_enrollment,
Expand Down Expand Up @@ -1522,6 +1523,9 @@ def get_enterprise_course_enrollment_page(
# Filter audit course modes.
course_modes = filter_audit_course_modes(enterprise_customer, course_modes)

# Set a flag to hide the $0 when the customer doesn't want it to be shown
course_modes = hide_price_when_zero(enterprise_customer, course_modes)

# Allows automatic assignment to a cohort upon enrollment.
cohort = request.GET.get('cohort')
# Add a message to the message display queue if the learner
Expand Down
2 changes: 1 addition & 1 deletion requirements/dev.txt
Original file line number Diff line number Diff line change
Expand Up @@ -415,7 +415,7 @@ pillow==9.0.1
# -r requirements/doc.txt
# -r requirements/test-master.txt
# -r requirements/test.txt
pip-tools==6.5.1
pip-tools==6.6.2
# via -r requirements/dev.in
pkginfo==1.8.2
# via twine
Expand Down
28 changes: 28 additions & 0 deletions tests/test_enterprise/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
enroll_licensed_users_in_courses,
get_idiff_list,
get_platform_logo_url,
hide_price_when_zero,
is_pending_user,
parse_lms_api_datetime,
serialize_notification_content,
Expand Down Expand Up @@ -334,3 +335,30 @@ def expected_email_item(user, activation_links):

expected_email_items = [expected_email_item(user, activation_links) for user in users]
assert email_items == expected_email_items

@ddt.data(True, False)
def test_hide_course_price_when_zero(self, hide_price):
customer = factories.EnterpriseCustomerFactory()
zero_modes = [
{"final_price": "$0"},
{"final_price": "$0.000"},
{"final_price": "Rs. 0.00"},
{"final_price": "0.00 EURO"},
]
non_zero_modes = [
{"final_price": "$100"},
{"final_price": "$73.50"},
{"final_price": "Rs.8000.00"},
{"final_price": "4000 Euros"},
]
customer.hide_course_price_when_zero = hide_price

processed_zero_modes = hide_price_when_zero(customer, zero_modes)
processed_non_zero_modes = hide_price_when_zero(customer, non_zero_modes)

if hide_price:
self.assertTrue(all(mode["hide_price"] for mode in processed_zero_modes))
self.assertFalse(all(mode["hide_price"] for mode in processed_non_zero_modes))
else:
self.assertEqual(zero_modes, processed_zero_modes)
self.assertEqual(non_zero_modes, processed_non_zero_modes)
1 change: 1 addition & 0 deletions tests/test_utilities.py
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,7 @@ def setUp(self):
"system_wide_role_assignments",
"reply_to",
"hide_labor_market_data",
"hide_course_price_when_zero",
]
),
(
Expand Down