def generate_offer_html(user, course): """ Create the actual HTML object with the offer text in it. Returns a openedx.core.djangolib.markup.HTML object, or None if the user should not be shown an offer message. """ if user and not user.is_anonymous and course: now = datetime.now(tz=pytz.UTC).strftime(u"%Y-%m-%d %H:%M:%S%z") saw_banner = ExperimentData.objects.filter( user=user, experiment_id=REV1008_EXPERIMENT_ID, key=str(course)) if not saw_banner: ExperimentData.objects.create(user=user, experiment_id=REV1008_EXPERIMENT_ID, key=str(course), value=now) discount_expiration_date = get_discount_expiration_date(user, course) if (discount_expiration_date and can_receive_discount( user=user, course=course, discount_expiration_date=discount_expiration_date)): # Translator: xgettext:no-python-format offer_message = _( u'{banner_open} Upgrade by {discount_expiration_date} and save {percentage}% ' u'[{strikeout_price}]{span_close}{br}Use code {b_open}{code}{b_close} at checkout! ' u'{a_open}Upgrade Now{a_close}{div_close}') message_html = HTML(offer_message).format( a_open=HTML(u'<a href="{upgrade_link}">').format( upgrade_link=verified_upgrade_deadline_link( user=user, course=course)), a_close=HTML('</a>'), b_open=HTML('<b>'), code=Text('BIENVENIDOAEDX') if get_language() == 'es-419' else Text('EDXWELCOME'), b_close=HTML('</b>'), br=HTML('<br>'), banner_open=HTML( '<div class="first-purchase-offer-banner" role="note">' '<span class="first-purchase-offer-banner-bold">'), discount_expiration_date=discount_expiration_date.strftime( u'%B %d'), percentage=discount_percentage(course), span_close=HTML('</span>'), div_close=HTML('</div>'), strikeout_price=HTML( format_strikeout_price(user, course, check_for_discount=False)[0])) return message_html return None
def test_happy_path(self): assert utils.generate_offer_data(self.user, self.overview) == { 'code': 'EDXWELCOME', 'expiration_date': get_discount_expiration_date(self.user, self.overview), 'original_price': '$149', 'discounted_price': '$126.65', 'percentage': 15, 'upgrade_url': '/dashboard' }
def get_first_purchase_offer_banner_fragment(user, course): """ Return an HTML Fragment with First Purcahse Discount message, which has the discount_expiration_date, price, discount percentage and a link to upgrade. """ if user and not user.is_anonymous and course: now = datetime.now(tz=pytz.UTC) stop_bucketing_into_discount_experiment = datetime( 2019, 11, 22, 0, 0, 0, 0, pytz.UTC) saw_banner = ExperimentData.objects.filter( user=user, experiment_id=REV1008_EXPERIMENT_ID, key=str(course)) if not saw_banner and not now > stop_bucketing_into_discount_experiment: ExperimentData.objects.create( user=user, experiment_id=REV1008_EXPERIMENT_ID, key=str(course), value=now.strftime(u"%Y-%m-%d %H:%M:%S%z")) discount_expiration_date = get_discount_expiration_date(user, course) if (discount_expiration_date and can_receive_discount( user=user, course=course, discount_expiration_date=discount_expiration_date)): # Translator: xgettext:no-python-format offer_message = _( u'{banner_open} Upgrade by {discount_expiration_date} and save {percentage}% ' u'[{strikeout_price}]{span_close}{br}Discount will be automatically applied at checkout. ' u'{a_open}Upgrade Now{a_close}{div_close}') return Fragment( HTML(offer_message).format( a_open=HTML(u'<a href="{upgrade_link}">').format( upgrade_link=verified_upgrade_deadline_link( user=user, course=course)), a_close=HTML('</a>'), br=HTML('<br>'), banner_open=HTML( '<div class="first-purchase-offer-banner" role="note">' '<span class="first-purchase-offer-banner-bold">'), discount_expiration_date=discount_expiration_date.strftime( u'%B %d'), percentage=discount_percentage(course), span_close=HTML('</span>'), div_close=HTML('</div>'), strikeout_price=HTML( format_strikeout_price(user, course, check_for_discount=False)[0]))) return None
def generate_offer_data(user, course): """ Create a dictionary of information about the current discount offer. Used by serializers to pass onto frontends and by the LMS locally to generate HTML for template rendering. Returns a dictionary of data, or None if no offer is applicable. """ if not user or not course or user.is_anonymous: return None ExperimentData.objects.get_or_create( user=user, experiment_id=REV1008_EXPERIMENT_ID, key=str(course), defaults={ 'value': datetime.now(tz=pytz.UTC).strftime('%Y-%m-%d %H:%M:%S%z'), }, ) expiration_date = get_discount_expiration_date(user, course) if not expiration_date: return None if not can_receive_discount( user, course, discount_expiration_date=expiration_date): return None original, discounted, percentage = _get_discount_prices( user, course, assume_discount=True) return { 'code': 'BIENVENIDOAEDX' if get_language() == 'es-419' else 'EDXWELCOME', 'expiration_date': expiration_date, 'original_price': original, 'discounted_price': discounted, 'percentage': percentage, 'upgrade_url': verified_upgrade_deadline_link(user, course=course), }
def test_first_purchase_offer_banner_display(self, applicability, percentage, can_receive_discount_mock, discount_percentage_mock): """ Ensure first purchase offer banner displays correctly """ can_receive_discount_mock.return_value = applicability discount_percentage_mock.return_value = percentage user = self.create_user_for_course(self.course, CourseUserType.ENROLLED) now_time = datetime.now(tz=UTC).strftime(u"%Y-%m-%d %H:%M:%S%z") ExperimentData.objects.create(user=user, experiment_id=REV1008_EXPERIMENT_ID, key=str(self.course), value=now_time) self.client.login(username=user.username, password=self.TEST_PASSWORD) url = course_home_url(self.course) response = self.client.get(url) discount_expiration_date = get_discount_expiration_date( user, self.course).strftime(u'%B %d') upgrade_link = verified_upgrade_deadline_link(user=user, course=self.course) bannerText = u'''<div class="first-purchase-offer-banner" role="note"> <span class="first-purchase-offer-banner-bold"><b> Upgrade by {discount_expiration_date} and save {percentage}% [{strikeout_price}]</b></span> <br>Use code <b>EDXWELCOME</b> at checkout! <a id="welcome" href="{upgrade_link}">Upgrade Now</a> </div>'''.format( discount_expiration_date=discount_expiration_date, percentage=percentage, strikeout_price=HTML( format_strikeout_price(user, self.course, check_for_discount=False)[0]), upgrade_link=upgrade_link) if applicability: self.assertContains(response, bannerText, html=True) else: self.assertNotContains(response, bannerText, html=True)
def get_first_purchase_offer_banner_fragment(user, course): """ Return an HTML Fragment with First Purcahse Discount message, which has the discount_expiration_date, price, discount percentage and a link to upgrade. """ if user and course: discount_expiration_date = get_discount_expiration_date(user, course) if (discount_expiration_date and can_receive_discount( user=user, course=course, discount_expiration_date=discount_expiration_date)): # Translator: xgettext:no-python-format offer_message = _( u'{banner_open} Upgrade by {discount_expiration_date} and save {percentage}% ' u'[{strikeout_price}]{span_close}{br}Discount will be automatically applied at checkout. ' u'{a_open}Upgrade Now{a_close}{div_close}') return Fragment( HTML(offer_message).format( a_open=HTML(u'<a href="{upgrade_link}">').format( upgrade_link=verified_upgrade_deadline_link( user=user, course=course)), a_close=HTML('</a>'), br=HTML('<br>'), banner_open=HTML( '<div class="first-purchase-offer-banner"><span class="first-purchase-offer-banner-bold">' ), discount_expiration_date=discount_expiration_date.strftime( u'%B %d'), percentage=discount_percentage(), span_close=HTML('</span>'), div_close=HTML('</div>'), strikeout_price=HTML( format_strikeout_price(user, course, check_for_discount=False)[0]))) return None