Updates to the Course Entitlement API to block Learners from enrolling

in runs that are not available.

[LEARNER-3800]
This commit is contained in:
Albert St. Aubin
2017-12-18 14:35:53 -05:00
parent 5ccd465d85
commit cac22680e2
4 changed files with 193 additions and 42 deletions

View File

@@ -2,21 +2,21 @@
import copy
import datetime
import logging
import pycountry
from dateutil.parser import parse as datetime_parse
import pycountry
from django.conf import settings
from django.core.cache import cache
from django.core.exceptions import ObjectDoesNotExist
from edx_rest_api_client.client import EdxRestApiClient
from opaque_keys.edx.keys import CourseKey
from pytz import UTC
from entitlements.utils import is_course_run_entitlement_fullfillable
from openedx.core.djangoapps.catalog.cache import (PROGRAM_CACHE_KEY_TPL,
SITE_PROGRAM_UUIDS_CACHE_KEY_TPL)
from openedx.core.djangoapps.catalog.models import CatalogIntegration
from openedx.core.lib.edx_api_utils import get_edx_api_data
from openedx.core.lib.token_utils import JwtBuilder
from student.models import CourseEnrollment
logger = logging.getLogger(__name__)
@@ -315,48 +315,20 @@ def get_fulfillable_course_runs_for_entitlement(entitlement, course_runs):
"""
Takes a list of course runs and returns only the course runs, sorted by start date, that:
1) Are currently running or in the future
1) Are currently running or start in the future
2) A user can enroll in
3) A user can upgrade in
4) Are published
5) Are not enrolled in already for an active session
3) A user can upgrade to the entitlement mode
4) Are not already currently enrolled in a session
These are the only sessions that can be selected for an entitlement.
"""
enrollable_sessions = []
enrollments_for_user = CourseEnrollment.enrollments_for_user(entitlement.user).filter(mode=entitlement.mode)
enrolled_sessions = frozenset([str(e.course_id) for e in enrollments_for_user])
# Only show published course runs that can still be enrolled and upgraded
now = datetime.datetime.now(UTC)
for course_run in course_runs:
# Only courses that have not ended will be displayed
run_start = course_run.get('start')
run_end = course_run.get('end')
is_running = run_start and (not run_end or datetime_parse(run_end) > now)
# Only courses that can currently be enrolled in will be displayed
enrollment_start = course_run.get('enrollment_start')
enrollment_end = course_run.get('enrollment_end')
can_enroll = ((not enrollment_start or datetime_parse(enrollment_start) < now)
and (not enrollment_end or datetime_parse(enrollment_end) > now)
and course_run.get('key') not in enrolled_sessions)
# Only upgrade-able courses will be displayed
can_upgrade = False
for seat in course_run.get('seats', []):
if seat.get('type') == entitlement.mode:
upgrade_deadline = seat.get('upgrade_deadline', None)
can_upgrade = not upgrade_deadline or (datetime_parse(upgrade_deadline) > now)
break
# Only published courses will be displayed
is_published = course_run.get('status') == 'published'
if is_running and can_upgrade and can_enroll and is_published:
course_id = CourseKey.from_string(course_run.get('key'))
if is_course_run_entitlement_fullfillable(course_id, now, entitlement):
enrollable_sessions.append(course_run)
enrollable_sessions.sort(key=lambda session: session.get('start'))