-
Notifications
You must be signed in to change notification settings - Fork 3.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add mgmt cmd to generate anonymized ID mapping #663
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
60 changes: 60 additions & 0 deletions
60
common/djangoapps/student/management/commands/anonymized_id_mapping.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
# -*- coding: utf8 -*- | ||
"""Dump username,unique_id_for_user pairs as CSV. | ||
|
||
Give instructors easy access to the mapping from anonymized IDs to user IDs | ||
with a simple Django management command to generate a CSV mapping. To run, use | ||
the following: | ||
|
||
rake django-admin[anonymized_id_mapping,x,y,z] | ||
|
||
[Naturally, substitute the appropriate values for x, y, and z. (I.e., | ||
lms, dev, and MITx/6.002x/Circuits)]""" | ||
|
||
import csv | ||
|
||
from django.contrib.auth.models import User | ||
from django.core.management.base import BaseCommand, CommandError | ||
|
||
from student.models import unique_id_for_user | ||
|
||
|
||
class Command(BaseCommand): | ||
"""Add our handler to the space where django-admin looks up commands.""" | ||
|
||
# It appears that with the way Rake invokes these commands, we can't | ||
# have more than one arg passed through...annoying. | ||
args = ("course_id", ) | ||
|
||
help = """Export a CSV mapping usernames to anonymized ids | ||
|
||
Exports a CSV document mapping each username in the specified course to | ||
the anonymized, unique user ID. | ||
""" | ||
|
||
def handle(self, *args, **options): | ||
if len(args) != 1: | ||
raise CommandError("Usage: unique_id_mapping %s" % | ||
" ".join(("<%s>" % arg for arg in Command.args))) | ||
|
||
course_id = args[0] | ||
|
||
# Generate the output filename from the course ID. | ||
# Change slashes to dashes first, and then append .csv extension. | ||
output_filename = course_id.replace('/', '-') + ".csv" | ||
|
||
# Figure out which students are enrolled in the course | ||
students = User.objects.filter(courseenrollment__course_id=course_id) | ||
if len(students) == 0: | ||
self.stdout.write("No students enrolled in %s" % course_id) | ||
return | ||
|
||
# Write mapping to output file in CSV format with a simple header | ||
try: | ||
with open(output_filename, 'wb') as output_file: | ||
csv_writer = csv.writer(output_file) | ||
csv_writer.writerow(("User ID", "Anonymized user ID")) | ||
for student in students: | ||
csv_writer.writerow((student.id, unique_id_for_user(student))) | ||
except IOError: | ||
raise CommandError("Error writing to file: %s" % output_filename) | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You can invoke w/ multiple arguments using
rake 'django-admin[lms,dev,arg1 arg2 arg3]'
.Also, you can use
./manage.py lms --settings dev command arg1 arg2 arg3
, now.