def test_enrollments(self): with self.settings( RESTCLIENTS_SWS_DAO_CLASS='restclients.dao_implementation.sws.File', RESTCLIENTS_PWS_DAO_CLASS='restclients.dao_implementation.pws.File'): user = PWS().get_person_by_netid('javerage') csv = Collector() self.assertEquals(len(csv.enrollments), 0) self.assertEquals(csv.add(EnrollmentCSV(section_id='abc', person=user, role='Student', status='active')), True) self.assertEquals(len(csv.enrollments), 1) section = get_section_by_label('2013,winter,DROP_T,100/B') for registration in get_all_registrations_by_section(section): self.assertEquals(csv.add(EnrollmentCSV(registration=registration)), True) section = get_section_by_label('2013,spring,TRAIN,101/A') for user in section.get_instructors(): self.assertEquals(csv.add(EnrollmentCSV(section=section, instructor=user, status='active')), True) self.assertEquals(len(csv.enrollments), 4) self.assertEquals(csv.has_data(), True)
def generate_unused_course_csv(term_sis_id): csvdata = Collector() report_data = get_unused_course_report_data(term_sis_id) header = report_data.pop(0) for row in csv.reader(report_data): if not len(row): continue course_id = row[1] if course_id is None or not len(course_id): continue status = row[4] if status == 'unpublished': kwargs = {'course_id': course_id, 'short_name': row[2], 'long_name': row[3], 'account_id': None, 'term_id': term_sis_id, 'status': 'deleted'} csvdata.add(CourseCSV(**kwargs)) return csvdata.write()
def test_admins(self): formatter = AdminCSV('user_id', 'account_id', 'admin', 'active') csv = Collector() self.assertEquals(len(csv.admins), 0) self.assertEquals(csv.add(formatter), True) self.assertEquals(len(csv.admins), 1) self.assertEquals(csv.has_data(), True)
def test_users(self): user = PWS().get_person_by_netid('javerage') csv = Collector() self.assertEquals(len(csv.users), 0) self.assertEquals(csv.add(UserCSV(user, 'active')), True) self.assertEquals(len(csv.users), 1) self.assertEquals(csv.add(UserCSV(user, 'active')), False) self.assertEquals(csv.has_data(), True)
def test_accounts(self): formatter = AccountCSV('account_id', 'parent_id', Curriculum()) csv = Collector() self.assertEquals(len(csv.accounts), 0) self.assertEquals(csv.add(formatter), True) self.assertEquals(len(csv.accounts), 1) self.assertEquals(csv.add(formatter), False) self.assertEquals(csv.has_data(), True)
def test_terms(self): section = get_section_by_label('2013,summer,TRAIN,101/A') formatter = TermCSV(section) csv = Collector() self.assertEquals(len(csv.terms), 0) self.assertEquals(csv.add(formatter), True) self.assertEquals(len(csv.terms), 1) self.assertEquals(csv.add(formatter), False) self.assertEquals(csv.has_data(), True)
def test_accounts(self): context = Curriculum(full_name='abc') formatter = AccountCSV('account_id', 'parent_id', context) csv = Collector() self.assertEquals(len(csv.accounts), 0) self.assertEquals(csv.add(formatter), True) self.assertEquals(len(csv.accounts), 1) self.assertEquals(csv.add(formatter), False) self.assertEquals(csv.has_data(), True)
def test_courses(self): section = get_section_by_label('2013,spring,TRAIN,101/A') section.course_campus = 'PCE' formatter = CourseCSV(section=section) csv = Collector() self.assertEquals(len(csv.courses), 0) self.assertEquals(csv.add(formatter), True) self.assertEquals(len(csv.courses), 1) self.assertEquals(csv.add(formatter), False) self.assertEquals(csv.has_data(), True)
def test_users(self): with self.settings( RESTCLIENTS_PWS_DAO_CLASS='restclients.dao_implementation.pws.File'): user = PWS().get_person_by_netid('javerage') csv = Collector() self.assertEquals(len(csv.users), 0) self.assertEquals(csv.add(UserCSV(user, 'active')), True) self.assertEquals(len(csv.users), 1) self.assertEquals(csv.add(UserCSV(user, 'active')), False) self.assertEquals(csv.has_data(), True)
def test_terms(self): with self.settings( RESTCLIENTS_SWS_DAO_CLASS='restclients.dao_implementation.sws.File', RESTCLIENTS_PWS_DAO_CLASS='restclients.dao_implementation.pws.File'): section = get_section_by_label('2013,summer,TRAIN,101/A') formatter = TermCSV(section) csv = Collector() self.assertEquals(len(csv.terms), 0) self.assertEquals(csv.add(formatter), True) self.assertEquals(len(csv.terms), 1) self.assertEquals(csv.add(formatter), False) self.assertEquals(csv.has_data(), True)
def test_enrollments(self): user = PWS().get_person_by_netid('javerage') csv = Collector() self.assertEquals(len(csv.enrollments), 0) self.assertEquals( csv.add( EnrollmentCSV(section_id='abc', person=user, role='Student', status='active')), True) self.assertEquals(len(csv.enrollments), 1) section = get_section_by_label('2013,winter,DROP_T,100/B') for registration in get_registrations_by_section(section): self.assertEquals( csv.add(EnrollmentCSV(registration=registration)), True) self.assertEquals(len(csv.enrollments), 3) section = get_section_by_label('2013,spring,TRAIN,101/A') for user in section.get_instructors(): self.assertEquals( csv.add( EnrollmentCSV(section=section, instructor=user, status='active')), True) # Duplicate self.assertEquals( csv.add( EnrollmentCSV(section=section, instructor=user, status='active')), False) self.assertEquals(len(csv.enrollments), 5) self.assertEquals(csv.has_data(), True) # Ad-hoc enrollment self.assertEquals( csv.add( EnrollmentCSV(course_id='course_123', section_id='section_123', person=user, role='Observer', status='active')), True) self.assertEquals(len(csv.enrollments), 6) # Duplicate self.assertEquals( csv.add( EnrollmentCSV(course_id='course_123', section_id='section_123', person=user, role='Observer', status='active')), False)
def test_create_filepath(self, mock_os, mock_stat): with self.settings(SIS_IMPORT_CSV_FILEPATH_COLLISIONS_MAX=1): csv = Collector() root = '' path = csv.create_filepath(root) mock_os.makedirs.assert_called_with(path) mock_os.chmod.assert_called_with(path, csv.dirmode) with self.settings(SIS_IMPORT_CSV_FILEPATH_COLLISIONS_MAX=0): csv = Collector() root = '' self.assertRaises(EnvironmentError, csv.create_filepath, root)
def test_courses(self): with self.settings( RESTCLIENTS_SWS_DAO_CLASS='restclients.dao_implementation.sws.File', RESTCLIENTS_PWS_DAO_CLASS='restclients.dao_implementation.pws.File', LMS_OWNERSHIP_SUBACCOUNT={'PCE_NONE': 'pce_none_account'}): section = get_section_by_label('2013,spring,TRAIN,101/A') section.course_campus = 'PCE' formatter = CourseCSV(section=section) csv = Collector() self.assertEquals(len(csv.courses), 0) self.assertEquals(csv.add(formatter), True) self.assertEquals(len(csv.courses), 1) self.assertEquals(csv.add(formatter), False) self.assertEquals(csv.has_data(), True)
def test_sections(self): with self.settings( RESTCLIENTS_SWS_DAO_CLASS='restclients.dao_implementation.sws.File', RESTCLIENTS_PWS_DAO_CLASS='restclients.dao_implementation.pws.File'): section = get_section_by_label('2013,spring,TRAIN,101/A') formatter = SectionCSV(section=section) csv = Collector() self.assertEquals(len(csv.sections), 0) self.assertEquals(csv.add(formatter), True) self.assertEquals(len(csv.sections), 1) self.assertEquals(csv.add(formatter), False) self.assertEquals(csv.add(SectionCSV(section_id='abc', course_id='abc', name='abc', status='active')), True) self.assertEquals(len(csv.sections), 2) self.assertEquals(csv.has_data(), True)
def test_write_files(self, mock_open): # Test empty csv = Collector() self.assertEquals(csv.has_data(), False) self.assertEquals(csv.write_files(), None) # Test with data csv = Collector() csv.enrollments.append(1) self.assertEquals(csv.has_data(), True) with self.settings(SIS_IMPORT_CSV_DEBUG=False): path = csv.write_files() mock_open.assert_called_with(path + '/enrollments.csv', mode='w') self.assertEquals(csv.has_data(), False)
def handle(self, *args, **options): file_path = options.get('file_path') workshop_name = options.get('workshop_name') term_sis_id = options.get('term_sis_id') account_sis_id = options.get('account_sis_id') with open(file_path, 'r') as infile: file_data = infile.read() netids = file_data.splitlines() csvdata = Collector() for netid in netids: try: person = get_person_by_netid(netid.strip()) except UserPolicyException as err: print("Skipped user '{}': {}".format(netid, err)) continue if not csvdata.add(UserCSV(person)): continue course_sis_id = '-'.join([ term_sis_id, re.sub(r'[^\w]', '-', workshop_name.lower()), person.uwnetid ]) short_name = '{} {}'.format(date.today().year, workshop_name) long_name = '{} Sandbox'.format(short_name) csvdata.add( CourseCSV(course_id=course_sis_id, short_name=short_name, long_name=long_name, account_id=account_sis_id, term_id=term_sis_id, status='active')) csvdata.add( EnrollmentCSV(course_id=course_sis_id, person=person, role=get_instructor_sis_import_role(), status=ENROLLMENT_ACTIVE)) csv_path = csvdata.write_files() if csv_path: imp = Import(priority=Course.PRIORITY_DEFAULT, csv_type='course', csv_path=csv_path) imp.save() imp.import_csv()
def handle(self, *args, **options): if not len(args): raise CommandError("Usage: create_workshop_courses <path> " "<workshop_name><term_sis_id>") file_path = args[0] workshop_name = args[1] term_sis_id = args[2] account_sis_id = 'course-request-sandbox' with open(file_path, 'r') as infile: file_data = infile.read() netids = file_data.splitlines() csvdata = Collector() for netid in netids: try: person = get_person_by_netid(netid.strip()) except UserPolicyException as err: print "Skipped user %s: %s" % (netid, err) continue if not csvdata.add(UserCSV(person)): continue course_sis_id = '%s-%s-%s' % ( term_sis_id, re.sub(r'[^\w]', '-', workshop_name.lower()), person.uwnetid) short_name = '%s %s' % (workshop_name, date.today().year) long_name = '%s Sandbox' % short_name csvdata.add(CourseCSV( course_id=course_sis_id, short_name=short_name, long_name=long_name, account_id=account_sis_id, term_id=term_sis_id, status='active')) csvdata.add(EnrollmentCSV( course_id=course_sis_id, person=person, role=Enrollment.INSTRUCTOR_ROLE, status='active')) csv_path = csvdata.write() if csv_path: imp = Import(priority=PRIORITY_DEFAULT, csv_type='course', csv_path=csv_path) imp.save() imp.import_csv()
def test_write_files(self, mock_open, mock_os, mock_stat): # Test empty csv = Collector() self.assertEquals(csv.has_data(), False) self.assertEquals(csv.write_files(), None) # Test with data csv = Collector() csv.enrollments.append(1) self.assertEquals(csv.has_data(), True) path = csv.write_files() mock_os.path.join.assert_called_with(path, 'enrollments.csv') mock_open.assert_called_with(path, 'w') mock_os.chmod.assert_called_with(path, csv.filemode) self.assertEquals(csv.has_data(), False)
def test_sections(self): section = get_section_by_label('2013,spring,TRAIN,101/A') formatter = SectionCSV(section=section) csv = Collector() self.assertEquals(len(csv.sections), 0) self.assertEquals(csv.add(formatter), True) self.assertEquals(len(csv.sections), 1) self.assertEquals(csv.add(formatter), False) self.assertEquals( csv.add( SectionCSV(section_id='abc', course_id='abc', name='abc', status='active')), True) self.assertEquals(len(csv.sections), 2) self.assertEquals(csv.has_data(), True)
def __init__(self, items=[]): self.data = Collector() self.queue_id = None self.invalid_users = {} self.items = items self.logger = getLogger(__name__)
def test_xlists(self): csv = Collector() self.assertEquals(len(csv.xlists), 0) self.assertEquals(csv.add(XlistCSV('abc', 'def')), True) self.assertEquals(len(csv.xlists), 1) self.assertEquals(csv.has_data(), True)
def test_invalid_format(self): csv = Collector() self.assertRaises(TypeError, csv.add, InvalidFormat) self.assertEquals(csv.has_data(), False)
class Builder(object): def __init__(self, items=[]): self.data = Collector() self.queue_id = None self.invalid_users = {} self.items = items self.logger = getLogger(__name__) def _init_build(self, **kwargs): return def _process(self, item): raise NotImplementedError def _write(self): return self.data.write_files() def build(self, **kwargs): self._init_build(**kwargs) for item in self.items: self._process(item) return self._write() def add_user_data_for_person(self, person, force=False): """ Creates a line of user data for the passed person. If force is not true, the data will only be created if the person has not been provisioned. Returns True for valid users, False otherwise. """ if person.uwregid in self.invalid_users: return False try: valid_net_id(person.uwnetid) except UserPolicyException as err: self.invalid_users[person.uwregid] = True self.logger.info("Skip user {}: {}".format(person.uwregid, err)) return False if force is True: self.data.add(UserCSV(person)) else: user = User.objects.get_user(person) if user.provisioned_date is None: if (self.data.add(UserCSV(person)) and user.queue_id is None): user.queue_id = self.queue_id user.save() return True def add_teacher_enrollment_data(self, section, person, status='active'): """ Generates one teacher enrollment for the passed section and person. """ if self.add_user_data_for_person(person): self.data.add( EnrollmentCSV(section=section, instructor=person, status=status)) def add_student_enrollment_data(self, registration): """ Generates one student enrollment for the passed registration. """ if self.add_user_data_for_person(registration.person): self.data.add(EnrollmentCSV(registration=registration)) def add_group_enrollment_data(self, login_id, section_id, role, status): """ Generates one enrollment for the passed group member. """ try: person = get_person_by_netid(login_id) if self.add_user_data_for_person(person): self.data.add( EnrollmentCSV(section_id=section_id, person=person, role=role, status=status)) except InvalidLoginIdException: try: person = get_person_by_gmail_id(login_id) if status == ENROLLMENT_ACTIVE: self.data.add(UserCSV(person)) self.data.add( EnrollmentCSV(section_id=section_id, person=person, role=role, status=status)) except InvalidLoginIdException as ex: self.logger.info("Skip group member {}: {}".format( login_id, ex)) def add_registrations_by_section(self, section): try: for registration in get_registrations_by_section(section): self.add_student_enrollment_data(registration) except DataFailureException as ex: self.logger.info("Skip enrollments for section {}: {}".format( section.section_label(), ex)) def get_section_resource_by_id(self, section_id): """ Fetch the section resource for the passed section ID, and add to queue. """ try: section = get_section_by_id(section_id) Course.objects.add_to_queue(section, self.queue_id) return section except (ValueError, CoursePolicyException, DataFailureException) as ex: Course.objects.remove_from_queue(section_id, ex) self.logger.info("Skip section {}: {}".format(section_id, ex)) raise
class Builder(object): def __init__(self, items=[]): self.data = Collector() self.queue_id = None self.invalid_users = {} self.items = items self.logger = getLogger(__name__) def _init_build(self, **kwargs): return def _process(self, item): raise NotImplementedError def _write(self): return self.data.write_files() def build(self, **kwargs): self._init_build(**kwargs) for item in self.items: self._process(item) return self._write() def add_user_data_for_person(self, person, force=False): """ Creates a line of user data for the passed person. If force is not true, the data will only be created if the person has not been provisioned. Returns True for valid users, False otherwise. """ if person.uwregid in self.invalid_users: return False try: valid_net_id(person.uwnetid) except UserPolicyException as err: self.invalid_users[person.uwregid] = True self.logger.info("Skip user %s: %s" % (person.uwregid, err)) return False if force is True: self.data.add(UserCSV(person)) else: user = User.objects.add_user(person) if user.provisioned_date is None: if (self.data.add(UserCSV(person)) and user.queue_id is None): user.queue_id = self.queue_id user.save() return True def add_teacher_enrollment_data(self, section, person, status='active'): """ Generates one teacher enrollment for the passed section and person. """ if self.add_user_data_for_person(person): self.data.add(EnrollmentCSV( section=section, instructor=person, status=status)) def add_student_enrollment_data(self, registration): """ Generates one student enrollment for the passed registration. """ if self.add_user_data_for_person(registration.person): self.data.add(EnrollmentCSV(registration=registration)) def add_group_enrollment_data(self, member, section_id, role, status): """ Generates one enrollment for the passed group member. """ if member.is_uwnetid(): person = get_person_by_netid(member.name) if self.add_user_data_for_person(person): self.data.add(EnrollmentCSV( section_id=section_id, person=person, role=role, status=status)) elif member.is_eppn(): if status == Enrollment.ACTIVE_STATUS and hasattr(member, 'login'): person = get_person_by_gmail_id(member.login) self.data.add(UserCSV(person)) else: person = get_person_by_gmail_id(member.name) self.data.add(EnrollmentCSV( section_id=section_id, person=person, role=role, status=status)) def get_section_resource_by_id(self, section_id): """ Fetch the section resource for the passed section ID. """ try: return get_section_by_id(section_id) except DataFailureException as err: data = json.loads(err.msg) Course.objects.remove_from_queue(section_id, "%s: %s %s" % ( err.url, err.status, data["StatusDescription"])) self.logger.info("Skip section %s: %s %s" % ( section_id, err.status, data["StatusDescription"])) raise except ValueError as err: Course.objects.remove_from_queue(section_id, err) self.logger.info("Skip section %s: %s" % (section_id, err)) raise