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 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 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()
Exemple #4
0
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