Skip to content

Commit

Permalink
feat: refatoring
Browse files Browse the repository at this point in the history
  • Loading branch information
varshamenon4 committed Dec 18, 2024
1 parent c91f7d8 commit 9a02d9c
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 13 deletions.
26 changes: 15 additions & 11 deletions learning_assistant/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,15 @@
from edx_django_utils.cache import get_cache_key
from jinja2 import BaseLoader, Environment
from opaque_keys import InvalidKeyError
from pytz import utc

try:
from common.djangoapps.course_modes.models import CourseMode
from common.djangoapps.student.models import CourseEnrollment
except ImportError:
CourseMode = None
# todo: do we need try / catch for this?
try:
from common.djangoapps.course_modes.models import CourseEnrollment
except ImportError:
CourseEnrollment = None
# todo: do we need try / catch for this?

from learning_assistant.constants import ACCEPTED_CATEGORY_TYPES, CATEGORY_TYPE_MAP
from learning_assistant.data import LearningAssistantAuditTrialData, LearningAssistantCourseEnabledData
Expand Down Expand Up @@ -312,19 +311,24 @@ def get_or_create_audit_trial(user):
)


def audit_trial_is_expired(audit_trial_data, courserun_key):
"""
Given a user (User), get or create the corresponding LearningAssistantAuditTrial trial object.
def audit_trial_is_expired(enrollment, audit_trial_data):
"""
course_enrollment = CourseEnrollment.objects.get(user=audit_trial_data.user, course=courserun_key)
Given an enrollment and audit_trial_data, return whether the audit trial is expired as a boolean.
upgrade_deadline = course_enrollment.upgrade_deadline()
Arguments:
* enrollment (CourseEnrollment): the user course enrollment
* audit_trial_data (LearningAssistantAuditTrialData): the data related to the audit trial
Returns:
* audit_trial_is_expired (boolean): whether the audit trial is expired
"""
upgrade_deadline = enrollment.upgrade_deadline

# If the upgrade deadline has passed, return True for expired. Upgrade deadline is an optional attribute of a
# CourseMode, so if it's None, then do not return True.
days_until_upgrade_deadline = datetime.now() - upgrade_deadline if upgrade_deadline else None
days_until_upgrade_deadline = datetime.now(utc) - upgrade_deadline if upgrade_deadline else None
if days_until_upgrade_deadline is not None and days_until_upgrade_deadline >= timedelta(days=0):
return True

# If the user's trial is past its expiry date, return True for expired. Else, return False.
return audit_trial_data is None or audit_trial_data.expiration_date <= datetime.now()
return audit_trial_data is None or audit_trial_data.expiration_date <= datetime.now(utc)
4 changes: 2 additions & 2 deletions learning_assistant/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ def post(self, request, course_run_id):
# next message. Otherwise, return 403
elif enrollment_mode in CourseMode.UPSELL_TO_VERIFIED_MODES: # AUDIT, HONOR
audit_trial = get_or_create_audit_trial(request.user)
is_user_audit_trial_expired = audit_trial_is_expired(audit_trial, courserun_key)
is_user_audit_trial_expired = audit_trial_is_expired(enrollment_object, audit_trial)
if is_user_audit_trial_expired:
return Response(
status=http_status.HTTP_403_FORBIDDEN,
Expand Down Expand Up @@ -383,7 +383,7 @@ def get(self, request, course_run_id):
has_trial_access = (
enrollment_mode in valid_trial_access_modes
and audit_trial
and not audit_trial_is_expired(audit_trial, courserun_key)
and not audit_trial_is_expired(enrollment_object, audit_trial)
)

if (
Expand Down

0 comments on commit 9a02d9c

Please sign in to comment.