Exemple #1
0
def get_user_program_info(user, edx_client):
    """
    Provides a detailed serialization all of a User's enrolled Programs with enrollment/grade info

    Args:
        user (User): A User
        edx_client (EdxApi): An EdxApi instance

    Returns:
        list: Enrolled Program information
    """
    # update cache
    # NOTE: this part can be moved to an asynchronous task
    if edx_client is not None:
        try:
            for cache_type in CachedEdxDataApi.SUPPORTED_CACHES:
                CachedEdxDataApi.update_cache_if_expired(
                    user, edx_client, cache_type)
        except InvalidCredentialStored:
            # this needs to raise in order to force the user re-login
            raise
        except:  # pylint: disable=bare-except
            log.exception('Impossible to refresh edX cache')

    response_data = {
        "programs": [],
        "is_edx_data_fresh": CachedEdxDataApi.are_all_caches_fresh(user)
    }
    all_programs = (Program.objects.filter(
        live=True, programenrollment__user=user).prefetch_related(
            'course_set__courserun_set'))
    for program in all_programs:
        mmtrack_info = get_mmtrack(user, program)
        response_data['programs'].append(get_info_for_program(mmtrack_info))
    return response_data
Exemple #2
0
def get_user_program_info(user, edx_client):
    """
    Provides a detailed serialization all of a User's enrolled Programs with enrollment/grade info

    Args:
        user (User): A User
        edx_client (EdxApi): An EdxApi instance

    Returns:
        list: Enrolled Program information
    """
    # update cache
    # NOTE: this part can be moved to an asynchronous task
    if edx_client is not None:
        try:
            for cache_type in CachedEdxDataApi.SUPPORTED_CACHES:
                CachedEdxDataApi.update_cache_if_expired(user, edx_client, cache_type)
        except InvalidCredentialStored:
            # this needs to raise in order to force the user re-login
            raise
        except:  # pylint: disable=bare-except
            log.exception('Impossible to refresh edX cache')

    response_data = {
        "programs": [],
        "is_edx_data_fresh": CachedEdxDataApi.are_all_caches_fresh(user)
    }
    all_programs = (
        Program.objects.filter(live=True, programenrollment__user=user).prefetch_related('course_set__courserun_set')
    )
    for program in all_programs:
        mmtrack_info = get_mmtrack(user, program)
        response_data['programs'].append(get_info_for_program(mmtrack_info))
    return response_data
 def test_are_all_caches_fresh(self, cache_type):
     """Test for are_all_caches_fresh"""
     assert UserCacheRefreshTime.objects.filter(user=self.user).exists() is False
     assert CachedEdxDataApi.are_all_caches_fresh(self.user) is False
     now = now_in_utc()
     yesterday = now - timedelta(days=1)
     user_cache = UserCacheRefreshTimeFactory.create(
         user=self.user,
         enrollment=now,
         certificate=now,
         current_grade=now,
     )
     assert CachedEdxDataApi.are_all_caches_fresh(self.user) is True
     setattr(user_cache, cache_type, yesterday)
     user_cache.save()
     assert CachedEdxDataApi.are_all_caches_fresh(self.user) is False
     setattr(user_cache, cache_type, now)
     user_cache.save()
     assert CachedEdxDataApi.are_all_caches_fresh(self.user) is True
 def test_are_all_caches_fresh(self, cache_type):
     """Test for are_all_caches_fresh"""
     assert UserCacheRefreshTime.objects.filter(
         user=self.user).exists() is False
     assert CachedEdxDataApi.are_all_caches_fresh(self.user) is False
     now = now_in_utc()
     yesterday = now - timedelta(days=1)
     user_cache = UserCacheRefreshTimeFactory.create(
         user=self.user,
         enrollment=now,
         certificate=now,
         current_grade=now,
     )
     assert CachedEdxDataApi.are_all_caches_fresh(self.user) is True
     setattr(user_cache, cache_type, yesterday)
     user_cache.save()
     assert CachedEdxDataApi.are_all_caches_fresh(self.user) is False
     setattr(user_cache, cache_type, now)
     user_cache.save()
     assert CachedEdxDataApi.are_all_caches_fresh(self.user) is True