Ejemplo n.º 1
0
    def _add_admin(self, **kwargs):
        netid = kwargs['net_id']
        regid = kwargs['reg_id']
        self._log.info('ADD: %s is %s in %s' % (netid,
                                                kwargs['role'],
                                                kwargs['account_id']))

        try:
            User.objects.get(reg_id=regid)
        except User.DoesNotExist:
            try:
                person = get_person_by_netid(netid)

                self._log.info('Provisioning admin: %s (%s)' % (
                    person.uwnetid, person.uwregid))

                try:
                    user = get_user_by_sis_id(person.uwregid)
                except DataFailureException as err:
                    if err.status == 404:
                        user = create_user(CanvasUser(
                            name=user_fullname(person),
                            login_id=person.uwnetid,
                            sis_user_id=person.uwregid,
                            email=user_email(person)))

                User.objects.add_user(person)

            except Exception, err:
                self._log.info('Skipped admin: %s (%s)' % (netid, err))
                return
Ejemplo n.º 2
0
    def POST(self, request, **kwargs):
        try:
            rep = json.loads(request.read())

            if 'gmail_id' in rep:
                gmail_id = rep.get('gmail_id', '').strip()
                person = get_person_by_gmail_id(gmail_id)
                user = create_user(person)
                return HttpResponse()
            else:
                net_id = netid_from_request(rep)
                user = User.objects.get(net_id=net_id)
                return self.json_response('{"error":"User already exists"}',
                                          status=409)
        except User.DoesNotExist:
            try:
                user = User.objects.add_user(get_person_by_netid(net_id),
                                             priority=PRIORITY_IMMEDIATE)
                return HttpResponse()

            except Exception as err:
                return self.json_response('{"error": "%s"}' % err, status=400)

        except Exception as err:
            return self.json_response('{"error": "%s"}' % err, status=400)
Ejemplo n.º 3
0
    def _api_import_users(self, import_id, users, role,
                          section, section_only, notify_users):
        try:
            imp = AddUsersImport.objects.get(id=import_id)
            imp.import_pid = os.getpid()
            imp.save()

            # reflect importer enrollments privilege
            enroll_api = Enrollments(as_user=imp.importer_id)

            for u in users:
                try:
                    canvas_user = get_user_by_sis_id(u.regid)
                except DataFailureException as ex:
                    if ex.status == 404:
                        self._log.info(
                            'CREATE USER "%s" login: %s reg_id: %s' % (
                                u.name, u.login, u.regid))

                        # add user as "admin" on behalf of importer
                        canvas_user = create_user(CanvasUser(
                            name=u.name,
                            login_id=u.login,
                            sis_user_id=u.regid,
                            email=u.email))
                    else:
                        raise Exception(
                            'Cannot create user %s: %s' % (u.login, ex))

                enroll_params = {
                    'role_id': role.role_id,
                    'enrollment_state': 'active',
                    'limit_privileges_to_course_section': section_only,
                    'notify': notify_users
                }

                if section.section_id and int(section.section_id) > 0:
                    enroll_params['course_section_id'] = section.section_id

                self._log.info(
                    '%s ADDING %s (%s) TO %s: %s '
                    '(%s) AS %s (%s) - O:%s, N:%s' % (
                        imp.importer, canvas_user.login_id,
                        canvas_user.user_id, section.course_id,
                        section.sis_section_id, section.section_id, role.label,
                        role.role_id,
                        enroll_params['limit_privileges_to_course_section'],
                        enroll_params['notify']))

                enroll_api.enroll_user(section.course_id, canvas_user.user_id,
                                       role.base_role_type, enroll_params)

                imp.imported += 1
                imp.save()

        except Exception as ex:
            self._log.error('EXCEPTION: %s' % (ex))
            imp.import_error = "exception: %s" % (ex)
            imp.save()

        sys.exit(0)