From 74f6380e9e2e90ec14b273d970fae5064ee99ce9 Mon Sep 17 00:00:00 2001 From: Chris Dodge Date: Thu, 22 Jan 2015 12:27:39 -0500 Subject: [PATCH] Change coupon CSV download to only include counts for coupons redeemed all the way through purchase updated the failed unit test case and removed the unnessary import --- lms/djangoapps/instructor/tests/test_api.py | 2 +- lms/djangoapps/instructor/views/api.py | 13 ++++--------- lms/djangoapps/instructor_analytics/basic.py | 6 ++++-- 3 files changed, 9 insertions(+), 12 deletions(-) diff --git a/lms/djangoapps/instructor/tests/test_api.py b/lms/djangoapps/instructor/tests/test_api.py index b45926f187..e2cf283537 100644 --- a/lms/djangoapps/instructor/tests/test_api.py +++ b/lms/djangoapps/instructor/tests/test_api.py @@ -3406,7 +3406,7 @@ class TestCourseRegistrationCodes(ModuleStoreTestCase): self.assertEqual(response.status_code, 200, response.content) # filter all the coupons for coupon in Coupon.objects.all(): - self.assertIn('"{code}","{course_id}","{discount}","0","{description}","{expiration_date}"'.format( + self.assertIn('"{code}","{course_id}","{discount}","0","{description}","{expiration_date}","True"'.format( code=coupon.code, course_id=coupon.course_id, discount=coupon.percentage_discount, diff --git a/lms/djangoapps/instructor/views/api.py b/lms/djangoapps/instructor/views/api.py index e50d45dbda..ece0296753 100644 --- a/lms/djangoapps/instructor/views/api.py +++ b/lms/djangoapps/instructor/views/api.py @@ -18,7 +18,6 @@ from django.views.decorators.cache import cache_control from django.core.exceptions import ValidationError, PermissionDenied from django.core.mail.message import EmailMessage from django.db import IntegrityError -from django.db.models import Q from django.core.urlresolvers import reverse from django.core.validators import validate_email from django.utils.translation import ugettext as _ @@ -1044,16 +1043,12 @@ def get_coupon_codes(request, course_id): # pylint: disable=unused-argument Respond with csv which contains a summary of all Active Coupons. """ course_id = SlashSeparatedCourseKey.from_deprecated_string(course_id) - active_coupons = Coupon.objects.filter( - Q(course_id=course_id), - Q(is_active=True), - Q(expiration_date__gt=datetime.datetime.now(pytz.UTC)) | - Q(expiration_date__isnull=True) - ) + coupons = Coupon.objects.filter(course_id=course_id) + query_features = [ - 'code', 'course_id', 'percentage_discount', 'code_redeemed_count', 'description', 'expiration_date' + 'code', 'course_id', 'percentage_discount', 'code_redeemed_count', 'description', 'expiration_date', 'is_active' ] - coupons_list = instructor_analytics.basic.coupon_codes_features(query_features, active_coupons) + coupons_list = instructor_analytics.basic.coupon_codes_features(query_features, coupons) header, data_rows = instructor_analytics.csvs.format_dictlist(coupons_list, query_features) return instructor_analytics.csvs.create_csv_response('Coupons.csv', header, data_rows) diff --git a/lms/djangoapps/instructor_analytics/basic.py b/lms/djangoapps/instructor_analytics/basic.py index 7d8e45a5e6..8249acb2c5 100644 --- a/lms/djangoapps/instructor_analytics/basic.py +++ b/lms/djangoapps/instructor_analytics/basic.py @@ -33,7 +33,7 @@ SALE_ORDER_FEATURES = ('id', 'company_name', 'company_contact_name', 'company_co AVAILABLE_FEATURES = STUDENT_FEATURES + PROFILE_FEATURES COURSE_REGISTRATION_FEATURES = ('code', 'course_id', 'created_by', 'created_at') -COUPON_FEATURES = ('code', 'course_id', 'percentage_discount', 'description', 'expiration_date') +COUPON_FEATURES = ('code', 'course_id', 'percentage_discount', 'description', 'expiration_date', 'is_active') def sale_order_record_features(course_id, features): @@ -225,7 +225,9 @@ def coupon_codes_features(features, coupons_list): coupon_features = [x for x in COUPON_FEATURES if x in features] coupon_dict = dict((feature, getattr(coupon, feature)) for feature in coupon_features) - coupon_dict['code_redeemed_count'] = coupon.couponredemption_set.all().count() + coupon_dict['code_redeemed_count'] = coupon.couponredemption_set.filter( + order__status="purchased" + ).count() # we have to capture the redeemed_by value in the case of the downloading and spent registration # codes csv. In the case of active and generated registration codes the redeemed_by value will be None.