Example #1
0
    def test_sanitize_cache_key(self):
        keys = ['', ' ', 'I am a key. AMA!']

        for key in keys:
            sanitized = sanitize_cache_key(key)
            self.assertLess(len(sanitized), 250)

            # TODO Add a proper assertion to ensure all control characters are removed.
            self.assertNotIn(' ', sanitized)
    def test_sanitize_cache_key(self):
        keys = ["", " ", "I am a key. AMA!"]

        for key in keys:
            sanitized = sanitize_cache_key(key)
            self.assertLess(len(sanitized), 250)

            # TODO Add a proper assertion to ensure all control characters are removed.
            self.assertNotIn(" ", sanitized)
Example #3
0
    def get_courses(self):
        # Check the cache for the user's courses
        key = sanitize_cache_key(u'user_{}_courses'.format(
            self.request.user.pk))
        courses = cache.get(key)

        # If no cached courses, iterate over the data from the course API.
        if not courses:
            courses = []
            page = 1

            while page:
                try:
                    logger.debug('Retrieving page %d of course info...', page)
                    response = self.course_api.courses.get(page=page,
                                                           page_size=100)
                    course_details = response['results']

                    # Cache the information so that it doesn't need to be retrieved later.
                    for course in course_details:
                        course_id = course['id']
                        _key = self._course_detail_cache_key(course_id)
                        cache.set(_key, course)

                    courses += course_details

                    if response['pagination']['next']:
                        page += 1
                    else:
                        page = None
                        logger.debug(
                            'Completed retrieval of course info. Retrieved info for %d courses.',
                            len(courses))
                except HttpClientError as e:
                    logger.error("Unable to retrieve course data: %s", e)
                    page = None
                    break

        cache.set(key, courses)
        return courses
    def get_courses(self):
        # Check the cache for the user's courses
        key = sanitize_cache_key(u'user_{}_courses'.format(self.request.user.pk))
        courses = cache.get(key)

        # If no cached courses, iterate over the data from the course API.
        if not courses:
            courses = []
            page = 1

            while page:
                try:
                    logger.debug('Retrieving page %d of course info...', page)
                    response = self.course_api.get(page=page, page_size=100)
                    course_details = response['results']

                    # Cache the information so that it doesn't need to be retrieved later.
                    for course in course_details:
                        course_id = course['id']
                        _key = self._course_detail_cache_key(course_id)
                        cache.set(_key, course)

                    courses += course_details

                    if response['next']:
                        page += 1
                    else:
                        page = None
                        logger.debug('Completed retrieval of course info. Retrieved info for %d courses.', len(courses))
                except HttpClientError as e:
                    logger.error("Unable to retrieve course data: %s", e)
                    page = None
                    break

        cache.set(key, courses)
        return courses
 def get_cache_key(self, name):
     """ Returns sanitized key for caching. """
     return sanitize_cache_key(u'{}_{}'.format(self.course_id, name))
Example #6
0
 def _course_detail_cache_key(self, course_id):
     return sanitize_cache_key('course_{}_details'.format(course_id))
 def _course_detail_cache_key(self, course_id):
     return sanitize_cache_key(u'course_{}_details'.format(course_id))
 def get_cache_key(self, name):
     """ Returns sanitized key for caching. """
     return sanitize_cache_key(u'{}_{}'.format(self.course_id, name))
 def get_cache_key(self, name):
     return sanitize_cache_key(u'{}_{}'.format(self.course_id, name))
 def get_cache_key(self, name):
     return sanitize_cache_key(u'{}_{}'.format(self.course_id, name))