Merge pull request #22598 from edx/REV-1061

[REV-1061] Set discount on order based on discount that was on basket
This commit is contained in:
Matthew Piatetsky
2020-01-01 22:53:01 -05:00
committed by GitHub
3 changed files with 28 additions and 8 deletions

View File

@@ -44,7 +44,7 @@ DISCOUNT_APPLICABILITY_FLAG = WaffleFlag(
)
DISCOUNT_APPLICABILITY_HOLDBACK = 'first_purchase_discount_holdback'
REV1008_EXPERIMENT_ID = 15
REV1008_EXPERIMENT_ID = 16
def get_discount_expiration_date(user, course):

View File

@@ -19,11 +19,10 @@ from web_fragments.fragment import Fragment
from openedx.features.discounts.applicability import (
can_receive_discount,
get_discount_expiration_date,
discount_percentage
discount_percentage,
REV1008_EXPERIMENT_ID
)
REV1008_EXPERIMENT_ID = 15
def offer_banner_wrapper(user, block, view, frag, context): # pylint: disable=W0613
"""

View File

@@ -14,6 +14,7 @@ from rest_framework.permissions import IsAdminUser
from rest_framework.response import Response
from rest_framework.views import APIView
from experiments.models import ExperimentData
from openedx.core.djangoapps.content.course_overviews.models import CourseOverview
from openedx.core.djangoapps.cors_csrf.decorators import ensure_csrf_cookie_cross_domain
from openedx.core.djangoapps.oauth_dispatch.jwt import create_jwt_for_user
@@ -21,7 +22,7 @@ from openedx.core.lib.api.authentication import OAuth2AuthenticationAllowInactiv
from openedx.core.lib.api.permissions import ApiKeyHeaderPermissionIsAuthenticated
from openedx.core.lib.api.view_utils import DeveloperErrorViewMixin
from .applicability import can_receive_discount, discount_percentage
from .applicability import can_receive_discount, discount_percentage, REV1008_EXPERIMENT_ID
class CourseUserDiscount(DeveloperErrorViewMixin, APIView):
@@ -32,7 +33,7 @@ class CourseUserDiscount(DeveloperErrorViewMixin, APIView):
**Example Requests**
GET /api/discounts/v1/course/{course_key_string}
GET /api/discounts/course/{course_key_string}
**Response Values**
@@ -74,6 +75,15 @@ class CourseUserDiscount(DeveloperErrorViewMixin, APIView):
discount_applicable = can_receive_discount(user=request.user, course=course)
discount_percent = discount_percentage(course)
payload = {'discount_applicable': discount_applicable, 'discount_percent': discount_percent}
# Record whether the last basket loaded for this course had a discount
ExperimentData.objects.update_or_create(
user=request.user,
experiment_id=REV1008_EXPERIMENT_ID,
key='discount_' + str(course),
value=discount_applicable
)
return Response({
'discount_applicable': discount_applicable,
'jwt': create_jwt_for_user(request.user, additional_claims=payload)})
@@ -93,7 +103,7 @@ class CourseUserDiscountWithUserParam(DeveloperErrorViewMixin, APIView):
**Example Requests**
GET /api/discounts/v1/user/{user_id}/course/{course_key_string}
GET /api/discounts/user/{user_id}/course/{course_key_string}
**Response Values**
@@ -134,9 +144,20 @@ class CourseUserDiscountWithUserParam(DeveloperErrorViewMixin, APIView):
course_key = CourseKey.from_string(course_key_string)
course = CourseOverview.get_from_id(course_key)
user = User.objects.get(id=user_id)
discount_applicable = can_receive_discount(user=user, course=course)
# Below code in try/except is temporarily replacing this call
# discount_applicable = can_receive_discount(user=user, course=course)
# Only show a discount on the order if the last basket loaded for this course had a discount
# Do not check any of the discount requirements
try:
discount_applicable = ExperimentData.objects.get(
user=user, experiment_id=REV1008_EXPERIMENT_ID, key='discount_' + str(course)
).value == 'True'
except ExperimentData.DoesNotExist:
discount_applicable = False
discount_percent = discount_percentage(course)
payload = {'discount_applicable': discount_applicable, 'discount_percent': discount_percent}
return Response({
'discount_applicable': discount_applicable,
'jwt': create_jwt_for_user(request.user, additional_claims=payload)})