Cleanup and remove deprecated RequestCache Django app

ARCH-223
This commit is contained in:
Nimisha Asthagiri
2018-08-30 14:32:29 -04:00
committed by Robert Raposa
parent 53d8a04b88
commit 700a902b68
54 changed files with 595 additions and 600 deletions

View File

@@ -18,7 +18,7 @@ from opaque_keys.edx.keys import CourseKey
from opaque_keys.edx.django.models import CourseKeyField
from openedx.core.djangoapps.content.course_overviews.models import CourseOverview
from openedx.core.djangoapps.request_cache.middleware import ns_request_cached
from openedx.core.lib.cache_utils import request_cached
Mode = namedtuple('Mode',
[
@@ -311,7 +311,7 @@ class CourseMode(models.Model):
return [mode.to_tuple() for mode in found_course_modes]
@classmethod
@ns_request_cached(CACHE_NAMESPACE)
@request_cached(CACHE_NAMESPACE)
def modes_for_course(cls, course_id, include_expired=False, only_selectable=True):
"""
Returns a list of the non-expired modes for a given course id

View File

@@ -11,7 +11,7 @@ from django_comment_common.models import (
Role
)
from openedx.core.djangoapps.course_groups.cohorts import get_legacy_discussion_settings
from openedx.core.djangoapps.request_cache.middleware import request_cached
from openedx.core.lib.cache_utils import request_cached
from .models import CourseDiscussionSettings
@@ -110,7 +110,7 @@ def are_permissions_roles_seeded(course_id):
return True
@request_cached
@request_cached()
def get_course_discussion_settings(course_key):
try:
course_discussion_settings = CourseDiscussionSettings.objects.get(course_id=course_key)

View File

@@ -11,9 +11,9 @@ from django.conf import settings
from mako.exceptions import TopLevelLookupException
from mako.lookup import TemplateLookup
from openedx.core.djangoapps.request_cache.middleware import request_cached
from openedx.core.djangoapps.theming.helpers import get_template as themed_template
from openedx.core.djangoapps.theming.helpers import get_template_path_with_theme, strip_site_theme_templates_path
from openedx.core.lib.cache_utils import request_cached
from . import LOOKUP
@@ -148,7 +148,7 @@ def add_lookup(namespace, directory, package=None, prepend=False):
templates.add_directory(directory, prepend=prepend)
@request_cached
@request_cached()
def lookup_template(namespace, name):
"""
Look up a Mako template by namespace and name.

View File

@@ -22,8 +22,8 @@ Methods for creating RequestContext for using with Mako templates.
from crum import get_current_request
from django.template import RequestContext
from openedx.core.djangoapps.request_cache import get_cache
from util.request import safe_get_host
from edx_django_utils.cache import RequestCache
from openedx.core.lib.request_utils import safe_get_host
def get_template_request_context(request=None):
@@ -38,7 +38,7 @@ def get_template_request_context(request=None):
if request is None:
return None
request_cache_dict = get_cache('edxmako')
request_cache_dict = RequestCache('edxmako').data
cache_key = "request_context"
if cache_key in request_cache_dict:
return request_cache_dict[cache_key]

View File

@@ -50,6 +50,7 @@ from six import text_type
from slumber.exceptions import HttpClientError, HttpServerError
from user_util import user_util
from edx_django_utils.cache import RequestCache
import lms.lib.comment_client as cc
from student.signals import UNENROLL_DONE, ENROLL_STATUS_CHANGE, ENROLLMENT_TRACK_UPDATED
from lms.djangoapps.certificates.models import GeneratedCertificate
@@ -62,7 +63,6 @@ from courseware.models import (
from enrollment.api import _default_course_mode
from openedx.core.djangoapps.content.course_overviews.models import CourseOverview
from openedx.core.djangoapps.request_cache import clear_cache, get_cache
from openedx.core.djangoapps.site_configuration import helpers as configuration_helpers
from openedx.core.djangoapps.xmodule_django.models import NoneToEmptyManager
from openedx.core.djangolib.model_mixins import DeletableByUserValue
@@ -2069,7 +2069,7 @@ class CourseEnrollment(models.Model):
"""
# before populating the cache with another bulk set of data,
# remove previously cached entries to keep memory usage low.
clear_cache(cls.MODE_CACHE_NAMESPACE)
RequestCache(cls.MODE_CACHE_NAMESPACE).clear()
records = cls.objects.filter(user__in=users, course_id=course_key).select_related('user')
cache = cls._get_mode_active_request_cache()
@@ -2080,9 +2080,9 @@ class CourseEnrollment(models.Model):
@classmethod
def _get_mode_active_request_cache(cls):
"""
Returns the request-specific cache for CourseEnrollment
Returns the request-specific cache for CourseEnrollment as dict.
"""
return get_cache(cls.MODE_CACHE_NAMESPACE)
return RequestCache(cls.MODE_CACHE_NAMESPACE).data
@classmethod
def _get_enrollment_in_request_cache(cls, user, course_key):

View File

@@ -10,7 +10,7 @@ from collections import defaultdict
from django.contrib.auth.models import User
from opaque_keys.edx.django.models import CourseKeyField
from openedx.core.djangoapps.request_cache import get_cache
from openedx.core.lib.cache_utils import get_cache
from student.models import CourseAccessRole
log = logging.getLogger(__name__)

View File

@@ -24,6 +24,7 @@ from student.models import (
UserProfile,
get_retired_email_by_email
)
from openedx.core.lib.request_utils import safe_get_host
from student.tests.factories import PendingEmailChangeFactory, RegistrationFactory, UserFactory
from student.views import (
SETTING_CHANGE_INITIATED,
@@ -33,7 +34,6 @@ from student.views import (
)
from student.views import generate_activation_email_context, send_reactivation_email_for_user
from third_party_auth.views import inactive_user_view
from util.request import safe_get_host
from util.testing import EventTestMixin

View File

@@ -5,7 +5,7 @@ from opaque_keys import InvalidKeyError
from opaque_keys.edx.keys import CourseKey
from six import text_type
from util.request import COURSE_REGEX
from openedx.core.lib.request_utils import COURSE_REGEX
log = logging.getLogger(__name__)

View File

@@ -4,7 +4,7 @@ used in event tracking.
"""
from uuid import UUID, uuid4
from openedx.core.djangoapps.request_cache import get_cache
from openedx.core.lib.cache_utils import get_cache
def get_event_transaction_id():

View File

@@ -9,7 +9,7 @@ from functools import wraps
from django.db import DEFAULT_DB_ALIAS, DatabaseError, Error, transaction
from openedx.core.djangoapps.request_cache import get_cache
from openedx.core.lib.cache_utils import get_cache
OUTER_ATOMIC_CACHE_NAME = 'db.outer_atomic'

View File

@@ -11,8 +11,8 @@ from milestones.services import MilestonesService
from opaque_keys import InvalidKeyError
from opaque_keys.edx.keys import CourseKey
from openedx.core.djangoapps.request_cache import get_cache
from openedx.core.djangoapps.content.course_overviews.models import CourseOverview
from openedx.core.lib.cache_utils import get_cache
from xmodule.modulestore.django import modulestore
NAMESPACE_CHOICES = {

View File

@@ -1,49 +0,0 @@
""" Utility functions related to HTTP requests """
import re
from django.conf import settings
from opaque_keys import InvalidKeyError
from opaque_keys.edx.keys import CourseKey
from openedx.core.djangoapps.site_configuration import helpers as configuration_helpers
# accommodates course api urls, excluding any course api routes that do not fall under v*/courses, such as v1/blocks.
COURSE_REGEX = re.compile(r'^(.*?/courses/)(?!v[0-9]+/[^/]+){}'.format(settings.COURSE_ID_PATTERN))
def safe_get_host(request):
"""
Get the host name for this request, as safely as possible.
If ALLOWED_HOSTS is properly set, this calls request.get_host;
otherwise, this returns whatever settings.SITE_NAME is set to.
This ensures we will never accept an untrusted value of get_host()
"""
if isinstance(settings.ALLOWED_HOSTS, (list, tuple)) and '*' not in settings.ALLOWED_HOSTS:
return request.get_host()
else:
return configuration_helpers.get_value('site_domain', settings.SITE_NAME)
def course_id_from_url(url):
"""
Extracts the course_id from the given `url`.
"""
if not url:
return None
match = COURSE_REGEX.match(url)
if match is None:
return None
course_id = match.group('course_id')
if course_id is None:
return None
try:
return CourseKey.from_string(course_id)
except InvalidKeyError:
return None

View File

@@ -1,73 +0,0 @@
"""Tests for util.request module."""
import unittest
from django.conf import settings
from django.core.exceptions import SuspiciousOperation
from django.test.client import RequestFactory
from util.request import course_id_from_url, safe_get_host
class ResponseTestCase(unittest.TestCase):
""" Tests for response-related utility functions """
def setUp(self):
super(ResponseTestCase, self).setUp()
self.old_site_name = settings.SITE_NAME
self.old_allowed_hosts = settings.ALLOWED_HOSTS
def tearDown(self):
super(ResponseTestCase, self).tearDown()
settings.SITE_NAME = self.old_site_name
settings.ALLOWED_HOSTS = self.old_allowed_hosts
def test_safe_get_host(self):
""" Tests that the safe_get_host function returns the desired host """
settings.SITE_NAME = 'siteName.com'
factory = RequestFactory()
request = factory.request()
request.META['HTTP_HOST'] = 'www.userProvidedHost.com'
# If ALLOWED_HOSTS is not set properly, safe_get_host should return SITE_NAME
settings.ALLOWED_HOSTS = None
self.assertEqual(safe_get_host(request), "siteName.com")
settings.ALLOWED_HOSTS = ["*"]
self.assertEqual(safe_get_host(request), "siteName.com")
settings.ALLOWED_HOSTS = ["foo.com", "*"]
self.assertEqual(safe_get_host(request), "siteName.com")
# If ALLOWED_HOSTS is set properly, and the host is valid, we just return the user-provided host
settings.ALLOWED_HOSTS = [request.META['HTTP_HOST']]
self.assertEqual(safe_get_host(request), request.META['HTTP_HOST'])
# If ALLOWED_HOSTS is set properly but the host is invalid, we should get a SuspiciousOperation
settings.ALLOWED_HOSTS = ["the_valid_website.com"]
with self.assertRaises(SuspiciousOperation):
safe_get_host(request)
def test_course_id_from_url(self):
""" Test course_id_from_url(). """
self.assertIsNone(course_id_from_url('/login'))
self.assertIsNone(course_id_from_url('/course/edX/maths/2020'))
self.assertIsNone(course_id_from_url('/courses/edX/maths/'))
self.assertIsNone(course_id_from_url('/api/courses/v1/blocks/edX/maths/2020'))
self.assertIsNone(course_id_from_url('/api/courses/v1/blocks/course-v1:incidental+courseid+formatting'))
self.assertIsNone(course_id_from_url('/api/courses/v41/notcourses/course-v1:incidental+courseid+formatting'))
course_id = course_id_from_url('/courses/course-v1:edX+maths+2020')
self.assertCourseIdFieldsMatch(course_id=course_id, org="edX", course='maths', run='2020')
course_id = course_id_from_url('/courses/edX/maths/2020')
self.assertCourseIdFieldsMatch(course_id=course_id, org='edX', course='maths', run='2020')
course_id = course_id_from_url('/api/courses/v1/courses/course-v1:edX+maths+2020')
self.assertCourseIdFieldsMatch(course_id=course_id, org='edX', course='maths', run='2020')
course_id = course_id_from_url('/api/courses/v1/courses/edX/maths/2020')
self.assertCourseIdFieldsMatch(course_id=course_id, org='edX', course='maths', run='2020')
def assertCourseIdFieldsMatch(self, course_id, org, course, run):
""" Asserts that the passed-in course id matches the specified fields"""
self.assertEqual(course_id.org, org)
self.assertEqual(course_id.course, course)
self.assertEqual(course_id.run, run)

View File

@@ -12,7 +12,8 @@ import logging
from opaque_keys.edx.keys import UsageKey
from opaque_keys.edx.locator import BlockUsageLocator
from six import text_type
from openedx.core.lib.cache_utils import memoize_in_request_cache
from openedx.core.lib.cache_utils import request_cached
from xblock.core import XBlock
from xmodule.exceptions import InvalidVersionError
from xmodule.modulestore import ModuleStoreEnum
from xmodule.modulestore.exceptions import (
@@ -641,7 +642,6 @@ class DraftModuleStore(MongoModuleStore):
bulk_record.dirty = True
self.collection.remove({'_id': {'$in': to_be_deleted}}, safe=self.collection.safe)
@memoize_in_request_cache('request_cache')
def has_changes(self, xblock):
"""
Check if the subtree rooted at xblock has any drafts and thus may possibly have changes
@@ -649,6 +649,18 @@ class DraftModuleStore(MongoModuleStore):
:return: True if there are any drafts anywhere in the subtree under xblock (a weaker
condition than for other stores)
"""
return self._cached_has_changes(self.request_cache, xblock)
@request_cached(
# use the XBlock's location value in the cache key
arg_map_function=lambda arg: unicode(arg.location if isinstance(arg, XBlock) else arg),
# use this store's request_cache
request_cache_getter=lambda args, kwargs: args[1],
)
def _cached_has_changes(self, request_cache, xblock):
"""
Internal has_changes method that caches the result.
"""
# don't check children if this block has changes (is not public)
if getattr(xblock, 'is_draft', False):
return True

View File

@@ -7,7 +7,7 @@ from django.conf import settings
from django.utils.translation import ugettext_lazy as _
import logging
from openedx.core.djangoapps.request_cache.middleware import request_cached
from openedx.core.lib.cache_utils import request_cached
from xmodule.partitions.partitions import UserPartition, UserPartitionError, ENROLLMENT_TRACK_PARTITION_ID
from xmodule.modulestore.django import modulestore
@@ -17,7 +17,7 @@ log = logging.getLogger(__name__)
FEATURES = getattr(settings, 'FEATURES', {})
@request_cached
@request_cached()
def get_all_partitions_for_course(course, active_only=False):
"""
A method that returns all `UserPartitions` associated with a course, as a List.

View File

@@ -25,7 +25,7 @@ from lxml import etree
from opaque_keys.edx.locator import AssetLocator
from openedx.core.djangoapps.video_config.models import HLSPlaybackEnabledFlag
from openedx.core.djangoapps.video_pipeline.config.waffle import waffle_flags, DEPRECATE_YOUTUBE
from openedx.core.lib.cache_utils import memoize_in_request_cache
from openedx.core.lib.cache_utils import request_cached
from openedx.core.lib.license import LicenseMixin
from xblock.completable import XBlockCompletionMode
from xblock.core import XBlock
@@ -1053,8 +1053,11 @@ class VideoDescriptor(VideoFields, VideoTranscriptsMixin, VideoStudioViewHandler
"""
return self.runtime.service(self, "request_cache")
@memoize_in_request_cache('request_cache')
def get_cached_val_data_for_course(self, video_profile_names, course_id):
@classmethod
@request_cached(
request_cache_getter=lambda args, kwargs: args[1],
)
def get_cached_val_data_for_course(cls, request_cache, video_profile_names, course_id):
"""
Returns the VAL data for the requested video profiles for the given course.
"""
@@ -1087,7 +1090,11 @@ class VideoDescriptor(VideoFields, VideoTranscriptsMixin, VideoStudioViewHandler
video_profile_names.append('hls')
# get and cache bulk VAL data for course
val_course_data = self.get_cached_val_data_for_course(video_profile_names, self.location.course_key)
val_course_data = self.get_cached_val_data_for_course(
self.request_cache,
video_profile_names,
self.location.course_key,
)
val_video_data = val_course_data.get(self.edx_video_id, {})
# Get the encoded videos if data from VAL is found