Commit 5df1ffeb by Will Daly

Add header row to email opt in list; add default date

parent 8a685cec
......@@ -22,6 +22,7 @@ The command will always use the read replica database if one is configured.
import os.path
import csv
import time
import datetime
import contextlib
import logging
......@@ -48,13 +49,16 @@ class Command(BaseCommand):
"full_name",
"course_id",
"is_opted_in_for_email",
"preference_set_date"
"preference_set_datetime"
]
# Number of records to read at a time when making
# multiple queries over a potentially large dataset.
QUERY_INTERVAL = 1000
# Default datetime if the user has not set a preference
DEFAULT_DATETIME_STR = datetime.datetime(year=2014, month=12, day=1).isoformat(' ')
def handle(self, *args, **options):
"""Execute the command.
......@@ -183,6 +187,8 @@ class Command(BaseCommand):
"""
writer = csv.DictWriter(file_handle, fieldnames=self.OUTPUT_FIELD_NAMES)
writer.writeheader()
cursor = self._db_cursor()
query = (
u"""
......@@ -207,7 +213,7 @@ class Command(BaseCommand):
AND `user_id`=user.`id`
ORDER BY modified DESC
LIMIT 1
) AS `preference_set_date`
) AS `preference_set_datetime`
FROM
student_courseenrollment AS enrollment
LEFT JOIN auth_user AS user ON user.id=enrollment.user_id
......@@ -222,13 +228,13 @@ class Command(BaseCommand):
cursor.execute(query)
row_count = 0
for row in self._iterate_results(cursor):
email, full_name, course_id, is_opted_in, pref_set_date = row
email, full_name, course_id, is_opted_in, pref_set_datetime = row
writer.writerow({
"email": email.encode('utf-8'),
"full_name": full_name.encode('utf-8'),
"course_id": course_id.encode('utf-8'),
"is_opted_in_for_email": is_opted_in if is_opted_in else "True",
"preference_set_date": pref_set_date,
"preference_set_datetime": pref_set_datetime if pref_set_datetime else self.DEFAULT_DATETIME_STR,
})
row_count += 1
......
......@@ -45,9 +45,11 @@ class EmailOptInListTest(ModuleStoreTestCase):
"full_name",
"course_id",
"is_opted_in_for_email",
"preference_set_date"
"preference_set_datetime"
]
DEFAULT_DATETIME_STR = "2014-12-01 00:00:00"
def setUp(self):
self.user = UserFactory.create(
username=self.USER_USERNAME,
......@@ -299,7 +301,7 @@ class EmailOptInListTest(ModuleStoreTestCase):
"""
profile_api.update_email_opt_in(user.username, org, is_opted_in)
def _latest_pref_set_date(self, user):
def _latest_pref_set_datetime(self, user):
"""Retrieve the latest opt-in preference for the user,
across all orgs and preference keys.
......@@ -307,11 +309,11 @@ class EmailOptInListTest(ModuleStoreTestCase):
user (User): The user whos preference was set.
Returns:
ISO-formatted date string or empty string
ISO-formatted datetime string or empty string
"""
pref = UserOrgTag.objects.filter(user=user).order_by("-modified")
return pref[0].modified.isoformat(' ') if len(pref) > 0 else ""
return pref[0].modified.isoformat(' ') if len(pref) > 0 else self.DEFAULT_DATETIME_STR
def _run_command(self, org, other_names=None, only_courses=None, query_interval=None):
"""Execute the management command to generate the email opt-in list.
......@@ -374,8 +376,7 @@ class EmailOptInListTest(ModuleStoreTestCase):
*args: Tuples of (user, course_id, opt_in_pref)
Keyword Arguments:
expect_pref_datetime (bool): If false, expect an empty
string for the preference.
expect_pref_datetime (bool): If false, expect the default datetime.
Returns:
None
......@@ -384,16 +385,27 @@ class EmailOptInListTest(ModuleStoreTestCase):
AssertionError
"""
self.assertEqual(len(output), len(args))
self.assertEqual(len(output), len(args) + 1)
# Check the header row
self.assertEqual({
"email": "email",
"full_name": "full_name",
"course_id": "course_id",
"is_opted_in_for_email": "is_opted_in_for_email",
"preference_set_datetime": "preference_set_datetime"
}, output[0])
# Check data rows
for user, course_id, opt_in_pref in args:
self.assertIn({
"email": user.email.encode('utf-8'),
"full_name": user.profile.name.encode('utf-8'),
"course_id": unicode(course_id).encode('utf-8'),
"is_opted_in_for_email": unicode(opt_in_pref),
"preference_set_date": (
self._latest_pref_set_date(self.user)
"preference_set_datetime": (
self._latest_pref_set_datetime(self.user)
if kwargs.get("expect_pref_datetime", True)
else ""
else self.DEFAULT_DATETIME_STR
)
}, output)
}, output[1:])
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment