Commit 7d690dde by Sanford Student

adding request cache for milestones

parent ba54ad19
...@@ -9,13 +9,19 @@ from django.utils.translation import ugettext as _ ...@@ -9,13 +9,19 @@ from django.utils.translation import ugettext as _
from opaque_keys import InvalidKeyError from opaque_keys import InvalidKeyError
from opaque_keys.edx.keys import CourseKey from opaque_keys.edx.keys import CourseKey
from milestones import api as milestones_api
from milestones.exceptions import InvalidMilestoneRelationshipTypeException
from milestones.models import MilestoneRelationshipType
from openedx.core.djangoapps.content.course_overviews.models import CourseOverview from openedx.core.djangoapps.content.course_overviews.models import CourseOverview
from xmodule.modulestore.django import modulestore from xmodule.modulestore.django import modulestore
import request_cache
NAMESPACE_CHOICES = { NAMESPACE_CHOICES = {
'ENTRANCE_EXAM': 'entrance_exams' 'ENTRANCE_EXAM': 'entrance_exams'
} }
REQUEST_CACHE_NAME = "milestones"
def get_namespace_choices(): def get_namespace_choices():
""" """
...@@ -49,7 +55,6 @@ def add_prerequisite_course(course_key, prerequisite_course_key): ...@@ -49,7 +55,6 @@ def add_prerequisite_course(course_key, prerequisite_course_key):
""" """
if not is_prerequisite_courses_enabled(): if not is_prerequisite_courses_enabled():
return None return None
from milestones import api as milestones_api
milestone_name = _('Course {course_id} requires {prerequisite_course_id}').format( milestone_name = _('Course {course_id} requires {prerequisite_course_id}').format(
course_id=unicode(course_key), course_id=unicode(course_key),
prerequisite_course_id=unicode(prerequisite_course_key) prerequisite_course_id=unicode(prerequisite_course_key)
...@@ -73,7 +78,6 @@ def remove_prerequisite_course(course_key, milestone): ...@@ -73,7 +78,6 @@ def remove_prerequisite_course(course_key, milestone):
""" """
if not is_prerequisite_courses_enabled(): if not is_prerequisite_courses_enabled():
return None return None
from milestones import api as milestones_api
milestones_api.remove_course_milestone( milestones_api.remove_course_milestone(
course_key, course_key,
milestone, milestone,
...@@ -89,7 +93,6 @@ def set_prerequisite_courses(course_key, prerequisite_course_keys): ...@@ -89,7 +93,6 @@ def set_prerequisite_courses(course_key, prerequisite_course_keys):
""" """
if not is_prerequisite_courses_enabled(): if not is_prerequisite_courses_enabled():
return None return None
from milestones import api as milestones_api
#remove any existing requirement milestones with this pre-requisite course as requirement #remove any existing requirement milestones with this pre-requisite course as requirement
course_milestones = milestones_api.get_course_milestones(course_key=course_key, relationship="requires") course_milestones = milestones_api.get_course_milestones(course_key=course_key, relationship="requires")
if course_milestones: if course_milestones:
...@@ -123,7 +126,6 @@ def get_pre_requisite_courses_not_completed(user, enrolled_courses): # pylint: ...@@ -123,7 +126,6 @@ def get_pre_requisite_courses_not_completed(user, enrolled_courses): # pylint:
if not is_prerequisite_courses_enabled(): if not is_prerequisite_courses_enabled():
return {} return {}
from milestones import api as milestones_api
pre_requisite_courses = {} pre_requisite_courses = {}
for course_key in enrolled_courses: for course_key in enrolled_courses:
...@@ -185,8 +187,6 @@ def fulfill_course_milestone(course_key, user): ...@@ -185,8 +187,6 @@ def fulfill_course_milestone(course_key, user):
""" """
if not settings.FEATURES.get('MILESTONES_APP', False): if not settings.FEATURES.get('MILESTONES_APP', False):
return None return None
from milestones import api as milestones_api
from milestones.exceptions import InvalidMilestoneRelationshipTypeException
try: try:
course_milestones = milestones_api.get_course_milestones(course_key=course_key, relationship="fulfills") course_milestones = milestones_api.get_course_milestones(course_key=course_key, relationship="fulfills")
except InvalidMilestoneRelationshipTypeException: except InvalidMilestoneRelationshipTypeException:
...@@ -203,7 +203,6 @@ def remove_course_milestones(course_key, user, relationship): ...@@ -203,7 +203,6 @@ def remove_course_milestones(course_key, user, relationship):
""" """
if not settings.FEATURES.get('MILESTONES_APP', False): if not settings.FEATURES.get('MILESTONES_APP', False):
return None return None
from milestones import api as milestones_api
course_milestones = milestones_api.get_course_milestones(course_key=course_key, relationship=relationship) course_milestones = milestones_api.get_course_milestones(course_key=course_key, relationship=relationship)
for milestone in course_milestones: for milestone in course_milestones:
milestones_api.remove_user_milestone({'id': user.id}, milestone) milestones_api.remove_user_milestone({'id': user.id}, milestone)
...@@ -216,7 +215,6 @@ def get_required_content(course, user): ...@@ -216,7 +215,6 @@ def get_required_content(course, user):
""" """
required_content = [] required_content = []
if settings.FEATURES.get('MILESTONES_APP', False): if settings.FEATURES.get('MILESTONES_APP', False):
from milestones.exceptions import InvalidMilestoneRelationshipTypeException
# Get all of the outstanding milestones for this course, for this user # Get all of the outstanding milestones for this course, for this user
try: try:
milestone_paths = get_course_milestones_fulfillment_paths( milestone_paths = get_course_milestones_fulfillment_paths(
...@@ -241,7 +239,6 @@ def milestones_achieved_by_user(user, namespace): ...@@ -241,7 +239,6 @@ def milestones_achieved_by_user(user, namespace):
""" """
if not settings.FEATURES.get('MILESTONES_APP', False): if not settings.FEATURES.get('MILESTONES_APP', False):
return None return None
from milestones import api as milestones_api
return milestones_api.get_user_milestones({'id': user.id}, namespace) return milestones_api.get_user_milestones({'id': user.id}, namespace)
...@@ -262,7 +259,6 @@ def seed_milestone_relationship_types(): ...@@ -262,7 +259,6 @@ def seed_milestone_relationship_types():
""" """
if not settings.FEATURES.get('MILESTONES_APP', False): if not settings.FEATURES.get('MILESTONES_APP', False):
return None return None
from milestones.models import MilestoneRelationshipType
MilestoneRelationshipType.objects.create(name='requires') MilestoneRelationshipType.objects.create(name='requires')
MilestoneRelationshipType.objects.create(name='fulfills') MilestoneRelationshipType.objects.create(name='fulfills')
...@@ -291,7 +287,6 @@ def add_milestone(milestone_data): ...@@ -291,7 +287,6 @@ def add_milestone(milestone_data):
""" """
if not settings.FEATURES.get('MILESTONES_APP', False): if not settings.FEATURES.get('MILESTONES_APP', False):
return None return None
from milestones import api as milestones_api
return milestones_api.add_milestone(milestone_data) return milestones_api.add_milestone(milestone_data)
...@@ -301,7 +296,6 @@ def get_milestones(namespace): ...@@ -301,7 +296,6 @@ def get_milestones(namespace):
""" """
if not settings.FEATURES.get('MILESTONES_APP', False): if not settings.FEATURES.get('MILESTONES_APP', False):
return [] return []
from milestones import api as milestones_api
return milestones_api.get_milestones(namespace) return milestones_api.get_milestones(namespace)
...@@ -311,7 +305,6 @@ def get_milestone_relationship_types(): ...@@ -311,7 +305,6 @@ def get_milestone_relationship_types():
""" """
if not settings.FEATURES.get('MILESTONES_APP', False): if not settings.FEATURES.get('MILESTONES_APP', False):
return {} return {}
from milestones import api as milestones_api
return milestones_api.get_milestone_relationship_types() return milestones_api.get_milestone_relationship_types()
...@@ -321,7 +314,6 @@ def add_course_milestone(course_id, relationship, milestone): ...@@ -321,7 +314,6 @@ def add_course_milestone(course_id, relationship, milestone):
""" """
if not settings.FEATURES.get('MILESTONES_APP', False): if not settings.FEATURES.get('MILESTONES_APP', False):
return None return None
from milestones import api as milestones_api
return milestones_api.add_course_milestone(course_id, relationship, milestone) return milestones_api.add_course_milestone(course_id, relationship, milestone)
...@@ -331,7 +323,6 @@ def get_course_milestones(course_id): ...@@ -331,7 +323,6 @@ def get_course_milestones(course_id):
""" """
if not settings.FEATURES.get('MILESTONES_APP', False): if not settings.FEATURES.get('MILESTONES_APP', False):
return [] return []
from milestones import api as milestones_api
return milestones_api.get_course_milestones(course_id) return milestones_api.get_course_milestones(course_id)
...@@ -341,7 +332,6 @@ def add_course_content_milestone(course_id, content_id, relationship, milestone) ...@@ -341,7 +332,6 @@ def add_course_content_milestone(course_id, content_id, relationship, milestone)
""" """
if not settings.FEATURES.get('MILESTONES_APP', False): if not settings.FEATURES.get('MILESTONES_APP', False):
return None return None
from milestones import api as milestones_api
return milestones_api.add_course_content_milestone(course_id, content_id, relationship, milestone) return milestones_api.add_course_content_milestone(course_id, content_id, relationship, milestone)
...@@ -351,12 +341,31 @@ def get_course_content_milestones(course_id, content_id, relationship, user_id=N ...@@ -351,12 +341,31 @@ def get_course_content_milestones(course_id, content_id, relationship, user_id=N
""" """
if not settings.FEATURES.get('MILESTONES_APP', False): if not settings.FEATURES.get('MILESTONES_APP', False):
return [] return []
from milestones import api as milestones_api
if user_id is None:
return milestones_api.get_course_content_milestones(course_id, content_id, relationship)
request_cache_dict = request_cache.get_cache(REQUEST_CACHE_NAME)
if user_id not in request_cache_dict:
request_cache_dict[user_id] = milestones_api.get_course_content_milestones(
course_key=course_id,
user={"id": user_id}
)
milestones_for_content = []
if relationship == "requires":
for milestone in request_cache_dict[user_id]:
if milestone["content_id"] == content_id and milestone["requirements"]:
milestones_for_content.append(milestone)
if relationship == "fulfills":
for milestone in request_cache_dict[user_id]:
if milestone["namespace"].contains(content_id) and milestone["requirements"]:
milestones_for_content.append(milestone)
return milestones_api.get_course_content_milestones( return milestones_api.get_course_content_milestones(
course_id, course_id,
content_id, content_id,
relationship, relationship,
{'id': user_id} if user_id else None user={"id": user_id}
) )
...@@ -366,7 +375,6 @@ def remove_course_content_user_milestones(course_key, content_key, user, relatio ...@@ -366,7 +375,6 @@ def remove_course_content_user_milestones(course_key, content_key, user, relatio
""" """
if not settings.FEATURES.get('MILESTONES_APP', False): if not settings.FEATURES.get('MILESTONES_APP', False):
return [] return []
from milestones import api as milestones_api
course_content_milestones = milestones_api.get_course_content_milestones(course_key, content_key, relationship) course_content_milestones = milestones_api.get_course_content_milestones(course_key, content_key, relationship)
for milestone in course_content_milestones: for milestone in course_content_milestones:
...@@ -379,7 +387,6 @@ def remove_content_references(content_id): ...@@ -379,7 +387,6 @@ def remove_content_references(content_id):
""" """
if not settings.FEATURES.get('MILESTONES_APP', False): if not settings.FEATURES.get('MILESTONES_APP', False):
return None return None
from milestones import api as milestones_api
return milestones_api.remove_content_references(content_id) return milestones_api.remove_content_references(content_id)
...@@ -398,7 +405,6 @@ def get_course_milestones_fulfillment_paths(course_id, user_id): ...@@ -398,7 +405,6 @@ def get_course_milestones_fulfillment_paths(course_id, user_id):
""" """
if not settings.FEATURES.get('MILESTONES_APP', False): if not settings.FEATURES.get('MILESTONES_APP', False):
return None return None
from milestones import api as milestones_api
return milestones_api.get_course_milestones_fulfillment_paths( return milestones_api.get_course_milestones_fulfillment_paths(
course_id, course_id,
user_id user_id
...@@ -411,5 +417,13 @@ def add_user_milestone(user, milestone): ...@@ -411,5 +417,13 @@ def add_user_milestone(user, milestone):
""" """
if not settings.FEATURES.get('MILESTONES_APP', False): if not settings.FEATURES.get('MILESTONES_APP', False):
return None return None
from milestones import api as milestones_api
return milestones_api.add_user_milestone(user, milestone) return milestones_api.add_user_milestone(user, milestone)
def remove_user_milestone(user, milestone):
"""
Client API operation adapter/wrapper
"""
if not settings.FEATURES.get('MILESTONES_APP', False):
return None
return milestones_api.remove_user_milestone(user, milestone)
...@@ -7,10 +7,9 @@ import json ...@@ -7,10 +7,9 @@ import json
from collections import defaultdict from collections import defaultdict
from django.contrib.auth.models import User from django.contrib.auth.models import User
from xmodule.modulestore.django import modulestore from xmodule.modulestore.django import modulestore
from milestones import api as milestones_api
from openedx.core.lib.gating import api as gating_api from openedx.core.lib.gating import api as gating_api
from lms.djangoapps.grades.module_grades import get_module_score from lms.djangoapps.grades.module_grades import get_module_score
from util import milestones_helpers
log = logging.getLogger(__name__) log = logging.getLogger(__name__)
...@@ -81,6 +80,6 @@ def evaluate_prerequisite(course, prereq_content_key, user_id): ...@@ -81,6 +80,6 @@ def evaluate_prerequisite(course, prereq_content_key, user_id):
) )
if score >= min_score: if score >= min_score:
milestones_api.add_user_milestone({'id': user_id}, prereq_milestone) milestones_helpers.add_user_milestone({'id': user_id}, prereq_milestone)
else: else:
milestones_api.remove_user_milestone({'id': user_id}, prereq_milestone) milestones_helpers.remove_user_milestone({'id': user_id}, prereq_milestone)
...@@ -9,7 +9,6 @@ from opaque_keys.edx.keys import UsageKey ...@@ -9,7 +9,6 @@ from opaque_keys.edx.keys import UsageKey
from xmodule.modulestore.django import modulestore from xmodule.modulestore.django import modulestore
from openedx.core.lib.gating.exceptions import GatingValidationError from openedx.core.lib.gating.exceptions import GatingValidationError
log = logging.getLogger(__name__) log = logging.getLogger(__name__)
# This is used to namespace gating-specific milestones # This is used to namespace gating-specific milestones
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment