Skip to content

Commit dde096a

Browse files
committed
🐛(backend) add discounted price to course glimpse template
Discounted price were missing in course glimpse template.
1 parent cfad257 commit dde096a

4 files changed

Lines changed: 82 additions & 1 deletion

File tree

‎CHANGELOG.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ Versioning](https://semver.org/spec/v2.0.0.html).
1313
- Add `discounted_price`, `discount`, `certificate_discounted_price` and
1414
`certificate_discount` fields to `CourseRun` model
1515
- Add search indexation for new `CourseRun` model fields
16+
- Add `discounted_price` to the course glimpse template
1617

1718
### Changed
1819

‎src/richie/apps/courses/models/course.py‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -683,6 +683,15 @@ def price(self):
683683
return self.best_course_run.price
684684
return None
685685

686+
@property
687+
def discounted_price(self):
688+
"""
689+
Returns the discounted price of the course run with the best state.
690+
"""
691+
if self.best_course_run:
692+
return self.best_course_run.discounted_price
693+
return None
694+
686695
@property
687696
def certificate_offer(self):
688697
"""
@@ -701,6 +710,15 @@ def certificate_price(self):
701710
return self.best_course_run.certificate_price
702711
return None
703712

713+
@property
714+
def discounted_certificate_price(self):
715+
"""
716+
Returns the discounted certificate price of the course run with the best state.
717+
"""
718+
if self.best_course_run:
719+
return self.best_course_run.discounted_certificate_price
720+
return None
721+
704722
def copy_relations(self, oldinstance, language=None):
705723
"""
706724
This method is called for 2 types of copying:

‎src/richie/apps/courses/templates/courses/cms/fragment_course_glimpse.html‎

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,14 @@
117117
<use href="#icon-offer-{{course.offer|default:'free'}}" />
118118
</svg>
119119
{% if course.offer != 'free' and course.price %}
120-
<span class="offer__price">{{ course.price|currency:course.price_currency }}</span>
120+
{% if course.discounted_price %}
121+
<div class="offer_prices">
122+
<span class="offer__price offer__price--striked">{{ course.price|currency:course.price_currency }}</span>
123+
<span class="offer__price offer__price--discounted">{{ course.discounted_price|currency:course.price_currency }}</span>
124+
</div>
125+
{% else %}
126+
<span class="offer__price">{{ course.price|currency:course.price_currency }}</span>
127+
{% endif %}
121128
{% endif %}
122129
</div>
123130
</div>

‎tests/apps/courses/test_cms_plugins_course.py‎

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -462,6 +462,7 @@ def test_cms_plugins_course_glimpse_offer(self):
462462
direct_course=course,
463463
price=100.00,
464464
price_currency="EUR",
465+
discounted_price=None,
465466
)
466467

467468
# Define a page to display the Course Glimpse
@@ -500,6 +501,60 @@ def test_cms_plugins_course_glimpse_offer(self):
500501
else:
501502
self.assertCountEqual(course_glimpse.cssselect(".offer__price"), [])
502503

504+
def test_cms_plugins_course_glimpse_offer_discount(self):
505+
"""
506+
The course glimpse should render the discounted offer details
507+
(price ,discounted_price and right icon)
508+
"""
509+
course = CourseFactory()
510+
course_page = course.extended_object
511+
run = CourseRunFactory(
512+
direct_course=course,
513+
price=100.00,
514+
price_currency="EUR",
515+
discounted_price=80.00,
516+
)
517+
518+
# Define a page to display the Course Glimpse
519+
page = create_i18n_page({"en": "A page"})
520+
placeholder = page.placeholders.get(slot="maincontent")
521+
add_plugin(placeholder, CoursePlugin, "en", **{"page": course_page})
522+
page.publish("en")
523+
url = page.get_absolute_url(language="en")
524+
525+
for offer in CourseRunOffer:
526+
with self.subTest(offer=offer):
527+
# Clear cache between each sub tests
528+
cache.clear()
529+
# Update the course run offer then publish the related course page
530+
run.offer = offer
531+
run.save()
532+
self.assertTrue(course_page.publish("en"))
533+
534+
response = self.client.get(url)
535+
html = lxml.html.fromstring(response.content.decode("utf-8"))
536+
course_glimpse = html.cssselect(".course-glimpse")[0]
537+
538+
self.assertIn(
539+
f"course-glimpse--offer-{offer}", course_glimpse.get("class")
540+
)
541+
542+
# Check the offer icon
543+
icon = course_glimpse.cssselect(".offer__icon")[0]
544+
icon_symbol = icon.cssselect("use")[0]
545+
self.assertEqual(icon_symbol.get("href"), f"#icon-offer-{offer}")
546+
547+
# Check the offer price
548+
if offer != CourseRunOffer.FREE:
549+
price = course_glimpse.cssselect(".offer__price--striked")[0]
550+
self.assertEqual(price.text_content(), "€100.00")
551+
discounted_price = course_glimpse.cssselect(
552+
".offer__price--discounted"
553+
)[0]
554+
self.assertEqual(discounted_price.text_content(), "€80.00")
555+
else:
556+
self.assertCountEqual(course_glimpse.cssselect(".offer__price"), [])
557+
503558
def test_cms_plugins_course_glimpse_no_offer(self):
504559
"""
505560
The course glimpse should render the offer free icon

0 commit comments

Comments
 (0)