def test_get_enterprise_customer_for_user(self): """ Test `get_enterprise_customer_for_user` helper method. """ faker = FakerFactory.create() provider_id = faker.slug() user = UserFactory() ecu = EnterpriseCustomerUserFactory(user_id=user.id, ) EnterpriseCustomerIdentityProviderFactory( enterprise_customer=ecu.enterprise_customer, provider_id=provider_id, ) # Assert that correct enterprise customer is returned self.assertEqual( utils.get_enterprise_customer_for_user(auth_user=user), ecu.enterprise_customer, ) # Assert that None is returned if user is not associated with any enterprise customer self.assertEqual( utils.get_enterprise_customer_for_user(auth_user=UserFactory()), None, )
def transmit_single_learner_data(username, course_run_id): """ Task to send single learner data to each linked integrated channel. Arguments: username (str): The username of the learner whose data it should send. course_run_id (str): The course run id of the course it should send data for. """ user = User.objects.get(username=username) LOGGER.info( '[Integrated Channel] Single learner data transmission started.' ' Course: {course_run}, Username: {username}'.format( course_run=course_run_id, username=username)) enterprise_customer = get_enterprise_customer_for_user(user) channel_utils = IntegratedChannelCommandUtils() # Transmit the learner data to each integrated channelStarting Export for channel in channel_utils.get_integrated_channels({ 'channel': None, 'enterprise_customer': enterprise_customer.uuid }): integrated_channel = INTEGRATED_CHANNEL_CHOICES[ channel.channel_code()].objects.get(pk=channel.pk) LOGGER.info( '[Integrated Channel] Processing learner for transmission. Configuration: {configuration},' ' User: {user_id}'.format(configuration=integrated_channel, user_id=user.id)) integrated_channel.transmit_single_learner_data( learner_to_transmit=user, course_run_id=course_run_id, completed_date=timezone.now(), grade='Pass', is_passing=True)
def wrapper(request, *args, **kwargs): """ Checks for an enterprise customer associated with the user, calls the view function if one exists, raises PermissionDenied if not. """ user = request.user enterprise_customer = get_enterprise_customer_for_user(user) if enterprise_customer: args = args + (enterprise_customer,) return view(request, *args, **kwargs) else: raise PermissionDenied( 'User {username} is not associated with an EnterpriseCustomer.'.format( username=user.username ) )
def update_enterprise_courses(self, request, catalog_id): """ This method adds enterprise specific metadata for each course. We are adding following field in all the courses. tpa_hint: a string for identifying Identity Provider. """ courses = [] enterprise_customer = utils.get_enterprise_customer_for_user(request.user) global_context = { 'tpa_hint': enterprise_customer and enterprise_customer.identity_provider, 'enterprise_id': enterprise_customer and enterprise_customer.uuid, 'catalog_id': catalog_id, } for course in self.data['results']: courses.append( self.update_course(course, catalog_id, enterprise_customer, global_context) ) self.data['results'] = courses
def transmit_single_subsection_learner_data(username, course_run_id, subsection_id, grade): """ Task to send a single assessment level learner data record to each linked integrated channel. This task is fired off when an enterprise learner completes a subsection of their course, and as such only sends the data for that sub- section. Arguments: username (str): The username of the learner whose data it should send. course_run_id (str): The course run id of the course it should send data for. subsection_id (str): The subsection id that the learner completed and whose grades are being reported. grade (str): The grade received, used to ensure we are not sending duplicate transmissions. """ start = time.time() user = User.objects.get(username=username) enterprise_customer = get_enterprise_customer_for_user(user) channel_utils = IntegratedChannelCommandUtils() # Transmit the learner data to each integrated channelStarting Export for channel in channel_utils.get_integrated_channels({ 'channel': None, 'enterprise_customer': enterprise_customer.uuid }): integrated_channel = INTEGRATED_CHANNEL_CHOICES[ channel.channel_code()].objects.get(pk=channel.pk) integrated_channel.transmit_single_subsection_learner_data( learner_to_transmit=user, course_run_id=course_run_id, grade=grade, subsection_id=subsection_id) duration = time.time() - start LOGGER.info( '[Integrated Channel] Single learner data transmission task finished.' ' Course: {course_run}, Duration: {duration}, Username: {username}'. format(username=username, course_run=course_run_id, duration=duration))
def process_request(self, request): """ Perform the following checks 1. Check that the user is authenticated and belongs to an enterprise customer. 2. Check that the enterprise customer has a language set via the `default_language` column on EnterpriseCustomer model. 3. Check that user has not set a language via its account settings page. If all the above checks are satisfied then set request._anonymous_user_cookie_lang to the `default_language` of EnterpriseCustomer model instance. This attribute will later be used by the `LanguagePreferenceMiddleware` middleware for setting the user preference. Since, this middleware relies on `LanguagePreferenceMiddleware` so it must always be followed by `LanguagePreferenceMiddleware`. Otherwise, it will not work. """ # If the user is logged in, check for their language preference and user's enterprise configuration. # Also check for real user, if current user is a masquerading user. user_pref, current_user = None, None if hasattr(request, 'user'): current_user = getattr(request.user, 'real_user', request.user) if current_user and current_user.is_authenticated: enterprise_customer = get_enterprise_customer_for_user( current_user) if enterprise_customer and enterprise_customer.default_language: # Get the user's language preference try: user_pref = get_user_preference(current_user, LANGUAGE_KEY) except (UserAPIRequestError, UserAPIInternalError): # Ignore errors related to user preferences not found. pass # If user's language preference is not set and enterprise customer has a default language configured # then set the default language as the learner's language if not user_pref and not is_request_from_mobile_app(request): # pylint: disable=protected-access request._anonymous_user_cookie_lang = enterprise_customer.default_language