From 7e6147f7146ecd78317064e54aaab4471949ead5 Mon Sep 17 00:00:00 2001
From: Cali Stenson <cstenson@edx.org>
Date: Thu, 21 Feb 2019 17:52:58 -0500
Subject: [PATCH] Add price information to enrollments and create
 dashboard_metadata for experiments.

REVEM-174
---
 common/djangoapps/course_modes/models.py        |  2 +-
 common/djangoapps/student/models.py             |  6 +++++-
 common/djangoapps/student/views/dashboard.py    | 17 ++++++++++++++---
 lms/djangoapps/experiments/utils.py             | 12 ++++++++++++
 lms/templates/dashboard.html                    |  1 +
 .../experiments/dashboard_metadata.html         | 14 ++++++++++++++
 .../{ => experiments}/user_metadata.html        |  0
 lms/templates/main.html                         |  4 ++--
 8 files changed, 49 insertions(+), 7 deletions(-)
 create mode 100644 lms/templates/experiments/dashboard_metadata.html
 rename lms/templates/{ => experiments}/user_metadata.html (100%)

diff --git a/common/djangoapps/course_modes/models.py b/common/djangoapps/course_modes/models.py
index 608534e4827..f95f94feb2b 100644
--- a/common/djangoapps/course_modes/models.py
+++ b/common/djangoapps/course_modes/models.py
@@ -475,7 +475,7 @@ class CourseMode(models.Model):
     @classmethod
     def min_course_price_for_verified_for_currency(cls, course_id, currency):  # pylint: disable=invalid-name
         """
-        Returns the minimum price of the course int he appropriate currency over all the
+        Returns the minimum price of the course in the appropriate currency over all the
         course's *verified*, non-expired modes.
 
         Assuming all verified courses have a minimum price of >0, this value should always
diff --git a/common/djangoapps/student/models.py b/common/djangoapps/student/models.py
index d1826b683fc..49f036f9408 100644
--- a/common/djangoapps/student/models.py
+++ b/common/djangoapps/student/models.py
@@ -54,7 +54,7 @@ from edx_django_utils.cache import RequestCache
 import lms.lib.comment_client as cc
 from student.signals import UNENROLL_DONE, ENROLL_STATUS_CHANGE, ENROLLMENT_TRACK_UPDATED
 from lms.djangoapps.certificates.models import GeneratedCertificate
-from course_modes.models import CourseMode
+from course_modes.models import CourseMode, get_cosmetic_display_price
 from courseware.models import (
     CourseDynamicUpgradeDeadlineConfiguration,
     DynamicUpgradeDeadlineConfiguration,
@@ -1075,6 +1075,10 @@ class CourseEnrollment(models.Model):
         on_delete=models.DO_NOTHING,
     )
 
+    @property
+    def course_price(self):
+        return get_cosmetic_display_price(self.course)
+
     @property
     def course_id(self):
         return self._course_id
diff --git a/common/djangoapps/student/views/dashboard.py b/common/djangoapps/student/views/dashboard.py
index fe3f1393468..15fb9dc1c57 100644
--- a/common/djangoapps/student/views/dashboard.py
+++ b/common/djangoapps/student/views/dashboard.py
@@ -22,7 +22,7 @@ from six import text_type, iteritems
 
 import track.views
 from bulk_email.models import BulkEmailFlag, Optout  # pylint: disable=import-error
-from course_modes.models import CourseMode
+from course_modes.models import CourseMode, get_cosmetic_display_price
 from courseware.access import has_access
 from edxmako.shortcuts import render_to_response, render_to_string
 from entitlements.models import CourseEntitlement
@@ -42,7 +42,7 @@ from openedx.core.djangoapps.waffle_utils import WaffleFlag, WaffleFlagNamespace
 from openedx.core.djangoapps.user_api.accounts.utils import is_secondary_email_feature_enabled_for_user
 from openedx.core.djangolib.markup import HTML, Text
 from openedx.features.enterprise_support.api import get_dashboard_consent_notification
-from openedx.features.enterprise_support.utils import is_enterprise_learner
+from lms.djangoapps.experiments.utils import get_experiment_dashboard_metadata_context
 from openedx.features.journals.api import journals_enabled
 from shoppingcart.api import order_history
 from shoppingcart.models import CourseRegistrationCode, DonationConfiguration
@@ -60,6 +60,13 @@ from xmodule.modulestore.django import modulestore
 
 log = logging.getLogger("edx.student")
 
+experiments_namespace = WaffleFlagNamespace(name=u'student.experiments')
+#TODO START: Delete waffle flag as part of REVEM-204.
+dashboard_metadata_flag = WaffleFlag(experiments_namespace,
+                                     u'dashboard_metadata',
+                                     flag_undefined_default=False)
+#TODO END: REVEM-204
+
 
 def get_org_black_and_whitelist_for_site():
     """
@@ -695,7 +702,7 @@ def student_dashboard(request):
     inverted_programs = meter.invert_programs()
 
     urls, programs_data = {}, {}
-    bundles_on_dashboard_flag = WaffleFlag(WaffleFlagNamespace(name=u'student.experiments'), u'bundles_on_dashboard')
+    bundles_on_dashboard_flag = WaffleFlag(experiments_namespace, u'bundles_on_dashboard')
 
     # TODO: Delete this code and the relevant HTML code after testing LEARNER-3072 is complete
     if bundles_on_dashboard_flag.is_enabled() and inverted_programs and inverted_programs.items():
@@ -869,6 +876,10 @@ def student_dashboard(request):
         'empty_dashboard_message': empty_dashboard_message,
         'recovery_email_message': recovery_email_message,
         'recovery_email_activation_message': recovery_email_activation_message,
+        # TODO START: Clean up REVEM-205 & REVEM-204.
+        # The below context is for experiments in dashboard_metadata
+        'course_prices': get_experiment_dashboard_metadata_context(course_enrollments) if dashboard_metadata_flag.is_enabled() else None,
+        # TODO END: Clean up REVEM-205 & REVEM-204.
     }
 
     if ecommerce_service.is_enabled(request.user):
diff --git a/lms/djangoapps/experiments/utils.py b/lms/djangoapps/experiments/utils.py
index 58213f22dc4..3e45ed8f645 100644
--- a/lms/djangoapps/experiments/utils.py
+++ b/lms/djangoapps/experiments/utils.py
@@ -191,3 +191,15 @@ def get_experiment_user_metadata_context(course, user):
         'program_key_fields': program_key,
         # TODO: clean up as part of REVEM-199 (END)
     }
+
+
+#TODO START: Clean up REVEM-205
+def get_experiment_dashboard_metadata_context(enrollments):
+    """
+    Given a list of enrollments return a dict of course ids with their prices.
+    Utility function for experimental metadata. See experiments/dashboard_metadata.html.
+    :param enrollments:
+    :return: dict of courses: course price for dashboard metadata
+    """
+    return {str(enrollment.course): enrollment.course_price for enrollment in enrollments}
+#TODO END: Clean up REVEM-205
diff --git a/lms/templates/dashboard.html b/lms/templates/dashboard.html
index 9cfa2beb479..5daf43a627d 100644
--- a/lms/templates/dashboard.html
+++ b/lms/templates/dashboard.html
@@ -361,3 +361,4 @@ from student.models import CourseEnrollment
 </div>
 
 <%include file="dashboard/_dashboard_entitlement_unenrollment_modal.html"/>
+<%include file="/experiments/dashboard_metadata.html" args='course_prices=course_prices'/>
diff --git a/lms/templates/experiments/dashboard_metadata.html b/lms/templates/experiments/dashboard_metadata.html
new file mode 100644
index 00000000000..ecdbe48b8d9
--- /dev/null
+++ b/lms/templates/experiments/dashboard_metadata.html
@@ -0,0 +1,14 @@
+<%page args="course_prices" expression_filter="h"/>
+<%!
+from openedx.core.djangolib.js_utils import dump_js_escaped_json
+%>
+
+<!-- TODO START: Clean up REVEM-205.-->
+<%
+dashboard_metadata = { 'course_prices': course_prices }
+%>
+
+<script type="application/json" id="dashboard-metadata">
+    ${dashboard_metadata | n , dump_js_escaped_json}
+</script>
+<!-- TODO END: Clean up REVEM-205.-->
diff --git a/lms/templates/user_metadata.html b/lms/templates/experiments/user_metadata.html
similarity index 100%
rename from lms/templates/user_metadata.html
rename to lms/templates/experiments/user_metadata.html
diff --git a/lms/templates/main.html b/lms/templates/main.html
index 41f031f08e4..6647b81c83b 100644
--- a/lms/templates/main.html
+++ b/lms/templates/main.html
@@ -7,6 +7,7 @@
 
 ## Pages currently use v1 styling by default. Once the Pattern Library
 ## rollout has been completed, this default can be switched to v2.
+<%page expression_filter="h"/>
 <%! main_css = "style-main-v1" %>
 
 
@@ -128,14 +129,13 @@ from pipeline_mako import render_require_js_path_overrides
   <%block name="head_extra"/>
 
   <%include file="/courseware/experiments.html"/>
-  <%include file="user_metadata.html"/>
+  <%include file="/experiments/user_metadata.html"/>
   <%static:optional_include_mako file="head-extra.html" is_theming_enabled="True" />
 
   <%include file="widgets/optimizely.html" />
   <%include file="widgets/segment-io.html" />
 
   <meta name="path_prefix" content="${EDX_ROOT_URL}">
-  
   <% google_site_verification_id = configuration_helpers.get_value('GOOGLE_SITE_VERIFICATION_ID', settings.GOOGLE_SITE_VERIFICATION_ID) %>
   % if google_site_verification_id:
     <meta name="google-site-verification" content="${google_site_verification_id}" />
-- 
GitLab