Esempio n. 1
0
def get_enterprise_customer_cache_key(uuid, username=settings.ENTERPRISE_SERVICE_WORKER_USERNAME):
    """The cache key used to get cached Enterprise Customer data."""
    return get_cache_key(
        resource='enterprise-customer',
        resource_id=uuid,
        username=username,
    )
Esempio n. 2
0
def get_data_consent_share_cache_key(user_id, course_id):
    """
        Returns cache key for data sharing consent needed against user_id and course_id
    """

    return get_cache_key(type='data_sharing_consent_needed',
                         user_id=user_id,
                         course_id=course_id)
Esempio n. 3
0
 def _assert_get_enterprise_customer(self, api_client, enterprise_api_data_for_mock):
     """
     DRY method to verify caching for get enterprise customer method.
     """
     cache_key = get_cache_key(
         resource='enterprise-customer',
         resource_id=enterprise_api_data_for_mock['uuid'],
         username=settings.ENTERPRISE_SERVICE_WORKER_USERNAME,
     )
     self.mock_get_enterprise_customer(enterprise_api_data_for_mock['uuid'], enterprise_api_data_for_mock, 200)
     self._assert_get_enterprise_customer_with_cache(api_client, enterprise_api_data_for_mock, cache_key)
Esempio n. 4
0
def get_data_consent_share_cache_key(user_id, course_id, enterprise_customer_uuid=None):
    """
        Returns cache key for data sharing consent needed against user_id, course_id and enterprise_customer_uuid
    """
    cache_key_params = dict(
        type='data_sharing_consent_needed',
        user_id=user_id,
        course_id=course_id,
    )

    if enterprise_customer_uuid:
        cache_key_params['enterprise_customer_uuid'] = enterprise_customer_uuid

    return get_cache_key(**cache_key_params)
Esempio n. 5
0
    def test_get_order_attribute_from_ecommerce(self, mock_ecommerce_api_client):
        """
        Assert that the get_order_attribute_from_ecommerce method returns order details if it's already cached,
        without calling ecommerce.
        """
        order_details = {"number": self.ORDER_NUMBER, "vouchers": [{"end_datetime": '2025-09-25T00:00:00Z'}]}
        cache_key = get_cache_key(user_id=self.user.id, order_number=self.ORDER_NUMBER)
        TieredCache.set_all_tiers(cache_key, order_details, 60)

        self.enrollment.attributes.create(
            enrollment=self.enrollment,
            namespace='order',
            name='order_number',
            value=self.ORDER_NUMBER
        )
        assert self.enrollment.get_order_attribute_from_ecommerce("vouchers") == order_details["vouchers"]
        mock_ecommerce_api_client.assert_not_called()
    def get_cached_module_engagement_count(self):
        """
            Caches Module Engagement records count for specific enterprise.
        """
        enterprise_id = self.kwargs.get('enterprise_customer')
        cache_key = get_cache_key(
            resource='module_engagement_count',
            resource_id=enterprise_id,
        )
        module_engagement_count_cache = TieredCache.get_cached_response(
            cache_key)
        if module_engagement_count_cache.is_found:
            return module_engagement_count_cache.value

        queryset = self._get_queryset()
        count = queryset.count()
        TieredCache.set_all_tiers(cache_key, count,
                                  settings.ENGAGEMENT_CACHE_TIMEOUT)
        return count
    def cached_enterprise_learns(self):
        """
        Caches Enterprise Learns if cache found, else get fresh copy and returns.
        """
        enterprise_id = self.kwargs.get('enterprise_customer')
        cache_key = get_cache_key(
            resource='enterprise_users',
            resource_id=enterprise_id,
        )
        enterprise_users_cache = TieredCache.get_cached_response(cache_key)
        if enterprise_users_cache.is_found:
            return enterprise_users_cache.value

        enterprise_users = list(
            EnterpriseUser.objects.filter(enterprise_id=self.kwargs.get(
                'enterprise_customer')).values_list('user_username',
                                                    flat=True))
        TieredCache.set_all_tiers(cache_key, enterprise_users,
                                  settings.ENGAGEMENT_CACHE_TIMEOUT)
        return enterprise_users
Esempio n. 8
0
def get_is_enterprise_cache_key(user_id):
    """
        Returns cache key for the enterprise learner validation method needed against user_id.
    """
    return get_cache_key(type='is_enterprise_learner', user_id=user_id)
Esempio n. 9
0
    def get_api_access_request(self, user):
        """
        Get ApiAccessRequests made by the given user.

        Arguments:
            user (User): Django User.

        Returns:
            (dict): ApiAccessRequests for the given user.

        Examples:
            >> user = User.objects.get(username='******')
            >> lms_api_client.get_api_access_requests(user)
            {
                "id": 1,
                "created": "2017-09-25T08:37:05.872566Z",
                "modified": "2017-09-25T08:37:47.412496Z",
                "user": 5,
                "status": "approved",
                "website": "https://example.com/",
                "reason": "Example Reason",
                "company_name": "Example Inc",
                "company_address": "Example Address",
                "site": 1,
                "contacted": True
            }
        """
        resource = 'api-admin/api/v1/api_access_request/'
        query_parameters = {'user__username': user.username}

        cache_key = get_cache_key(username=user.username, resource=resource)
        cached_api_access_request = cache.get(cache_key)

        if cached_api_access_request is SENTINEL_NO_RESULT:
            return None

        if cached_api_access_request:
            return cached_api_access_request

        api_access_request = None
        try:
            results = getattr(self.client,
                              resource).get(**query_parameters)['results']
            if results:
                if len(results) > 1:
                    logger.warning(
                        'Multiple ApiAccessRequest models returned from LMS API for user [%s].',
                        user.username,
                    )
                api_access_request = results[0]
                cache.set(cache_key, api_access_request, ONE_HOUR)
            else:
                cache.set(cache_key, SENTINEL_NO_RESULT, ONE_HOUR)
                logger.info('No results for ApiAccessRequest for user [%s].',
                            user.username)

        except (SlumberBaseException, ConnectionError, Timeout,
                KeyError) as exception:
            cache.set(cache_key, SENTINEL_NO_RESULT, ONE_MINUTE)
            logger.exception(
                '%s: Failed to fetch ApiAccessRequest from LMS for user [%s].',
                exception.__class__.__name__, user.username)

        return api_access_request