Commit 7d279efd by Brian Wilson

add functionality for exporting EAD files to Pearson (and add dump_all option to CDD-dumping).

parent 83fdab79
import csv import csv
import uuid
from collections import OrderedDict from collections import OrderedDict
from datetime import datetime from datetime import datetime
from os.path import isdir
from fs.path import pathjoin
from optparse import make_option
from django.core.management.base import BaseCommand from django.core.management.base import BaseCommand
from student.models import TestCenterUser from student.models import TestCenterUser
from os.path import isdir
from fs.path import pathjoin
class Command(BaseCommand): class Command(BaseCommand):
CSV_TO_MODEL_FIELDS = OrderedDict([ CSV_TO_MODEL_FIELDS = OrderedDict([
# Skipping optional field CandidateID # Skipping optional field CandidateID
("ClientCandidateID", "client_candidate_id"), ("ClientCandidateID", "client_candidate_id"),
...@@ -37,9 +38,17 @@ class Command(BaseCommand): ...@@ -37,9 +38,17 @@ class Command(BaseCommand):
("LastUpdate", "user_updated_at"), # in UTC, so same as what we store ("LastUpdate", "user_updated_at"), # in UTC, so same as what we store
]) ])
args = '<output_file>' option_list = BaseCommand.option_list + (
make_option(
'--dump_all',
action='store_true',
dest='dump_all',
),
)
args = '<output_file_or_dir>'
help = """ help = """
Export user information from TestCenterUser model into a tab delimited Export user demographic information from TestCenterUser model into a tab delimited
text file with a format that Pearson expects. text file with a format that Pearson expects.
""" """
def handle(self, *args, **kwargs): def handle(self, *args, **kwargs):
...@@ -47,10 +56,6 @@ class Command(BaseCommand): ...@@ -47,10 +56,6 @@ class Command(BaseCommand):
print Command.help print Command.help
return return
# use options to set these:
dump_all = False
# self.reset_sample_data()
# update time should use UTC in order to be comparable to the user_updated_at # update time should use UTC in order to be comparable to the user_updated_at
# field # field
uploaded_at = datetime.utcnow() uploaded_at = datetime.utcnow()
...@@ -71,10 +76,11 @@ class Command(BaseCommand): ...@@ -71,10 +76,11 @@ class Command(BaseCommand):
# otherwise convert unicode objects to ascii. # otherwise convert unicode objects to ascii.
def ensure_encoding(value): def ensure_encoding(value):
if isinstance(value, unicode): if isinstance(value, unicode):
return value.encode('iso-8859-1'); return value.encode('iso-8859-1')
else: else:
return value; return value
dump_all = kwargs['dump_all']
with open(destfile, "wb") as outfile: with open(destfile, "wb") as outfile:
writer = csv.DictWriter(outfile, writer = csv.DictWriter(outfile,
...@@ -94,97 +100,4 @@ class Command(BaseCommand): ...@@ -94,97 +100,4 @@ class Command(BaseCommand):
tcu.save() tcu.save()
def reset_sample_data(self):
def make_sample(**kwargs):
data = dict((model_field, kwargs.get(model_field, ""))
for model_field in Command.CSV_TO_MODEL_FIELDS.values())
return TestCenterUser(**data)
def generate_id():
return "edX{:012}".format(uuid.uuid4().int % (10**12))
# TestCenterUser.objects.all().delete()
samples = [
make_sample(
client_candidate_id=generate_id(),
first_name="Jack",
last_name="Doe",
middle_name="C",
address_1="11 Cambridge Center",
address_2="Suite 101",
city="Cambridge",
state="MA",
postal_code="02140",
country="USA",
phone="(617)555-5555",
phone_country_code="1",
user_updated_at=datetime.utcnow()
),
make_sample(
client_candidate_id=generate_id(),
first_name="Clyde",
last_name="Smith",
middle_name="J",
suffix="Jr.",
salutation="Mr.",
address_1="1 Penny Lane",
city="Honolulu",
state="HI",
postal_code="96792",
country="USA",
phone="555-555-5555",
phone_country_code="1",
user_updated_at=datetime.utcnow()
),
make_sample(
client_candidate_id=generate_id(),
first_name="Patty",
last_name="Lee",
salutation="Dr.",
address_1="P.O. Box 555",
city="Honolulu",
state="HI",
postal_code="96792",
country="USA",
phone="808-555-5555",
phone_country_code="1",
user_updated_at=datetime.utcnow()
),
make_sample(
client_candidate_id=generate_id(),
first_name="Jimmy",
last_name="James",
address_1="2020 Palmer Blvd.",
city="Springfield",
state="MA",
postal_code="96792",
country="USA",
phone="917-555-5555",
phone_country_code="1",
extension="2039",
fax="917-555-5556",
fax_country_code="1",
company_name="ACME Traps",
user_updated_at=datetime.utcnow()
),
make_sample(
client_candidate_id=generate_id(),
first_name="Yeong-Un",
last_name="Seo",
address_1="Duryu, Lotte 101",
address_2="Apt 55",
city="Daegu",
country="KOR",
phone="917-555-5555",
phone_country_code="011",
user_updated_at=datetime.utcnow()
),
]
for tcu in samples:
tcu.save()
\ No newline at end of file
import csv import csv
import uuid from collections import OrderedDict
from collections import defaultdict, OrderedDict
from datetime import datetime from datetime import datetime
from os.path import isdir
from fs.path import pathjoin
from optparse import make_option
from django.core.management.base import BaseCommand, CommandError from django.core.management.base import BaseCommand
from student.models import TestCenterUser from student.models import TestCenterRegistration
def generate_id():
return "{:012}".format(uuid.uuid4().int % (10**12))
class Command(BaseCommand): class Command(BaseCommand):
args = '<output_file>'
CSV_TO_MODEL_FIELDS = OrderedDict([
('AuthorizationTransactionType', 'authorization_transaction_type'),
('AuthorizationID', 'authorization_id'),
('ClientAuthorizationID', 'client_authorization_id'),
('ClientCandidateID', 'client_candidate_id'),
('ExamAuthorizationCount', 'exam_authorization_count'),
('ExamSeriesCode', 'exam_series_code'),
('Accommodations', 'accommodation_code'),
('EligibilityApptDateFirst', 'eligibility_appointment_date_first'),
('EligibilityApptDateLast', 'eligibility_appointment_date_last'),
("LastUpdate", "user_updated_at"), # in UTC, so same as what we store
])
args = '<output_file_or_dir>'
help = """ help = """
Export user information from TestCenterUser model into a tab delimited Export user registration information from TestCenterRegistration model into a tab delimited
text file with a format that Pearson expects. text file with a format that Pearson expects.
""" """
FIELDS = [
'AuthorizationTransactionType', option_list = BaseCommand.option_list + (
'AuthorizationID', make_option(
'ClientAuthorizationID', '--dump_all',
'ClientCandidateID', action='store_true',
'ExamAuthorizationCount', dest='dump_all',
'ExamSeriesCode', ),
'EligibilityApptDateFirst', make_option(
'EligibilityApptDateLast', '--force_add',
'LastUpdate', action='store_true',
] dest='force_add',
),
)
def handle(self, *args, **kwargs): def handle(self, *args, **kwargs):
if len(args) < 1: if len(args) < 1:
print Command.help print Command.help
return return
# self.reset_sample_data() # update time should use UTC in order to be comparable to the user_updated_at
# field
uploaded_at = datetime.utcnow()
# if specified destination is an existing directory, then
# create a filename for it automatically. If it doesn't exist,
# or exists as a file, then we will just write to it.
# Name will use timestamp -- this is UTC, so it will look funny,
# but it should at least be consistent with the other timestamps
# used in the system.
dest = args[0]
if isdir(dest):
destfile = pathjoin(dest, uploaded_at.strftime("ead-%Y%m%d-%H%M%S.dat"))
else:
destfile = dest
dump_all = kwargs['dump_all']
with open(args[0], "wb") as outfile: with open(destfile, "wb") as outfile:
writer = csv.DictWriter(outfile, writer = csv.DictWriter(outfile,
Command.FIELDS, Command.CSV_TO_MODEL_FIELDS,
delimiter="\t", delimiter="\t",
quoting=csv.QUOTE_MINIMAL, quoting=csv.QUOTE_MINIMAL,
extrasaction='ignore') extrasaction='ignore')
writer.writeheader() writer.writeheader()
for tcu in TestCenterUser.objects.order_by('id')[:5]: for tcr in TestCenterRegistration.objects.order_by('id'):
record = defaultdict( if dump_all or tcr.needs_uploading:
lambda: "", record = dict((csv_field, getattr(tcr, model_field))
AuthorizationTransactionType="Add", for csv_field, model_field
ClientAuthorizationID=generate_id(), in Command.CSV_TO_MODEL_FIELDS.items())
ClientCandidateID=tcu.client_candidate_id, record["LastUpdate"] = record["LastUpdate"].strftime("%Y/%m/%d %H:%M:%S")
ExamAuthorizationCount="1", if kwargs['force_add']:
ExamSeriesCode="6002x001", record['AuthorizationTransactionType'] = 'Add'
EligibilityApptDateFirst="2012/12/15",
EligibilityApptDateLast="2012/12/30",
LastUpdate=datetime.utcnow().strftime("%Y/%m/%d %H:%M:%S")
)
writer.writerow(record)
writer.writerow(record)
tcr.uploaded_at = uploaded_at
tcr.save()
def reset_sample_data(self):
def make_sample(**kwargs):
data = dict((model_field, kwargs.get(model_field, ""))
for model_field in Command.CSV_TO_MODEL_FIELDS.values())
return TestCenterUser(**data)
# TestCenterUser.objects.all().delete()
samples = [
make_sample(
client_candidate_id=generate_id(),
first_name="Jack",
last_name="Doe",
middle_name="C",
address_1="11 Cambridge Center",
address_2="Suite 101",
city="Cambridge",
state="MA",
postal_code="02140",
country="USA",
phone="(617)555-5555",
phone_country_code="1",
user_updated_at=datetime.utcnow()
),
make_sample(
client_candidate_id=generate_id(),
first_name="Clyde",
last_name="Smith",
middle_name="J",
suffix="Jr.",
salutation="Mr.",
address_1="1 Penny Lane",
city="Honolulu",
state="HI",
postal_code="96792",
country="USA",
phone="555-555-5555",
phone_country_code="1",
user_updated_at=datetime.utcnow()
),
make_sample(
client_candidate_id=generate_id(),
first_name="Patty",
last_name="Lee",
salutation="Dr.",
address_1="P.O. Box 555",
city="Honolulu",
state="HI",
postal_code="96792",
country="USA",
phone="808-555-5555",
phone_country_code="1",
user_updated_at=datetime.utcnow()
),
make_sample(
client_candidate_id=generate_id(),
first_name="Jimmy",
last_name="James",
address_1="2020 Palmer Blvd.",
city="Springfield",
state="MA",
postal_code="96792",
country="USA",
phone="917-555-5555",
phone_country_code="1",
extension="2039",
fax="917-555-5556",
fax_country_code="1",
company_name="ACME Traps",
user_updated_at=datetime.utcnow()
),
make_sample(
client_candidate_id=generate_id(),
first_name="Yeong-Un",
last_name="Seo",
address_1="Duryu, Lotte 101",
address_2="Apt 55",
city="Daegu",
country="KOR",
phone="917-555-5555",
phone_country_code="011",
user_updated_at=datetime.utcnow()
),
]
for tcu in samples:
tcu.save()
\ No newline at end of file
...@@ -406,6 +406,18 @@ class TestCenterRegistration(models.Model): ...@@ -406,6 +406,18 @@ class TestCenterRegistration(models.Model):
def client_candidate_id(self): def client_candidate_id(self):
return self.testcenter_user.client_candidate_id return self.testcenter_user.client_candidate_id
@property
def authorization_transaction_type(self):
if self.uploaded_at is None:
return 'Add'
else:
return 'Update'
@property
def exam_authorization_count(self):
# TODO: figure out if this should really go in the database (with a default value).
return 1
@staticmethod @staticmethod
def create(testcenter_user, exam, accommodation_request): def create(testcenter_user, exam, accommodation_request):
registration = TestCenterRegistration(testcenter_user = testcenter_user) registration = TestCenterRegistration(testcenter_user = testcenter_user)
......
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