Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
E
edx-platform
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
edx
edx-platform
Commits
5df1ffeb
Commit
5df1ffeb
authored
Dec 15, 2014
by
Will Daly
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add header row to email opt in list; add default date
parent
8a685cec
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
33 additions
and
15 deletions
+33
-15
openedx/core/djangoapps/user_api/management/commands/email_opt_in_list.py
+10
-4
openedx/core/djangoapps/user_api/management/tests/test_email_opt_in_list.py
+23
-11
No files found.
openedx/core/djangoapps/user_api/management/commands/email_opt_in_list.py
View file @
5df1ffeb
...
...
@@ -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_date
time
"
]
# 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_date
time
`
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_date
time
=
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_date
time"
:
pref_set_datetime
if
pref_set_datetime
else
self
.
DEFAULT_DATETIME_STR
,
})
row_count
+=
1
...
...
openedx/core/djangoapps/user_api/management/tests/test_email_opt_in_list.py
View file @
5df1ffeb
...
...
@@ -45,9 +45,11 @@ class EmailOptInListTest(ModuleStoreTestCase):
"full_name"
,
"course_id"
,
"is_opted_in_for_email"
,
"preference_set_date"
"preference_set_date
time
"
]
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_date
time
(
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 date
time
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_date
time
"
:
(
self
.
_latest_pref_set_date
time
(
self
.
user
)
if
kwargs
.
get
(
"expect_pref_datetime"
,
True
)
else
""
else
self
.
DEFAULT_DATETIME_STR
)
},
output
)
},
output
[
1
:]
)
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment