def __init__(self): self.DROPBOX_API_URL = configuration_helpers.get_value( 'DROPBOX_API_URL', 'https://content.dropboxapi.com/2') self.DROPBOX_FILE_PATH = configuration_helpers.get_value( 'DROPBOX_FILE_PATH', '/courses.txt') self.DROPBOX_TOKEN = configuration_helpers.get_value( 'DROPBOX_TOKEN', 'token')
def __init__(self): self.MIT_HZ_PROVIDER = configuration_helpers.get_value( 'MIT_HZ_PROVIDER', 'samlp', ) self.MIT_HZ_ORG = configuration_helpers.get_value( 'MIT_HZ_ORG', 'Pearson', )
def execute_external_enrollment(data, course): """ Execute an enrollment for the given data and course. Args: data: dict with the enrollment data. course: instance of CourseDescriptor. """ try: controller = course.other_course_settings.get( 'external_platform_target', '') except AttributeError: LOG.error('Course [%s] not configured as external.', str(course.id)) return valid_external_targets = configuration_helpers.get_value( 'VALID_EXTERNAL_TARGETS', []) if controller.lower() not in valid_external_targets: LOG.warning( 'The controller %s is not present in the valid external targets list %s.', controller, valid_external_targets, ) return enrollment_controller = ExternalEnrollmentFactory.get_enrollment_controller( controller=controller, ) enrollment_controller._post_enrollment(data, course.other_course_settings) # pylint: disable=protected-access
def update_external_enrollment(sender, **kwargs): # pylint: disable=unused-argument """ This receiver is attached to enrollment/unenrollments events. It applies the required action when course mode is 'verified'. If kwargs['course_enrollment'].is_active is True, an external 'enrollment' request will be trigger, otherwise the 'unenroll' action will be called. """ if not configuration_helpers.get_value('ENABLE_EXTERNAL_ENROLLMENTS', False): return if kwargs['course_enrollment'].mode == get_course_mode().VERIFIED: LOG.info( 'The event %s has been triggered for course [%s] and user [%s]. Calling external enrollment controller...', 'Enroll' if kwargs['course_enrollment'].is_active else 'Unenroll', kwargs['course_enrollment'].course_id, kwargs['course_enrollment'].user.email, ) data = { 'user_email': kwargs['course_enrollment'].user.email, 'user_name': kwargs['course_enrollment'].user.profile.name, 'course_id': kwargs['course_enrollment'].course_id, 'course_mode': kwargs['course_enrollment'].mode, 'is_active': kwargs['course_enrollment'].is_active, } execute_external_enrollment(data=data, course=get_course_by_id( kwargs['course_enrollment'].course_id))
def _get_random_string(self, length): """ Method that generates and return a random string. """ random_string = ( uuid4().hex[:length] if length == self.USERNAME_SUFFIX_LENGTH else configuration_helpers.get_value('DEFAULT_USER_TESTING_PASSWORD')) return uuid4().hex[:length] if not random_string else random_string
def _create_icc_user(self, data, duplicated_username): """ Method that creates a user in the ICC database based in the current user logged in data. Also takes care of duplicated usernames. """ log_details = { 'url': settings.ICC_CREATE_USER_API_FUNCTION, } icc_user = {} try: user, _ = get_user(email=data.get('user_email')) username = user.username.lower() request_data = { 'wstoken': settings.ICC_API_TOKEN, 'wsfunction': settings.ICC_CREATE_USER_API_FUNCTION, 'users[0][username]': username + self._get_random_string(self.USERNAME_SUFFIX_LENGTH) if duplicated_username else username, 'users[0][password]': self._get_random_string(settings.ICC_HASH_LENGTH), 'users[0][firstname]': user.first_name, 'users[0][lastname]': user.last_name, 'users[0][email]': user.email, 'users[0][auth]': configuration_helpers.get_value( 'ICC_AUTH_METHOD_OVERRIDE', settings.ICC_AUTH_METHOD, ), } log_details['request_payload'] = request_data response = requests.post( url=settings.ICC_BASE_URL, data=request_data, ) except Exception as error: # pylint: disable=broad-except log_details['request_payload']['wstoken'] = 'icc-api-token' log_details['response'] = { 'error': 'Failed to create ICC user. Reason: %s' % str(error) } LOG.error('Failed to create ICC user. Reason: %s', str(error)) EnrollmentRequestLog.objects.create( # pylint: disable=no-member request_type=str(self), details=log_details, ) else: icc_user = self._get_icc_user_from_xml_response( response, 'create_user') return icc_user
def delete_external_enrollment(sender, instance, **kwargs): # pylint: disable=unused-argument """ This receiver is called when the django.db.models.signals.post_delete signal is sent, it will always execute an unenrollment. """ if not configuration_helpers.get_value('ENABLE_EXTERNAL_ENROLLMENTS', False): return data = { 'user_email': instance.user.email, 'course_mode': instance.mode, 'is_active': False, } execute_external_enrollment(data=data, course=get_course_by_id(instance.course.id))
def update_external_enrollment(sender, created, instance, **kwargs): # pylint: disable=unused-argument """ This receiver is called when the django.db.models.signals.post_save signal is sent, it will execute an enrollment or unenrollment based on the value of instance.is_active. """ if (not configuration_helpers.get_value('ENABLE_EXTERNAL_ENROLLMENTS', False) or (created and not instance.is_active)): return data = { 'user_email': instance.user.email, 'course_mode': instance.mode, 'is_active': instance.is_active, } execute_external_enrollment(data=data, course=get_course_by_id(instance.course.id))