Skip to content

Commit

Permalink
feat: add audit gate to CourseChatView
Browse files Browse the repository at this point in the history
  • Loading branch information
ilee2u committed Nov 18, 2024
1 parent 20045dd commit 95206e7
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 8 deletions.
27 changes: 25 additions & 2 deletions learning_assistant/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,15 @@
from jinja2 import BaseLoader, Environment
from opaque_keys import InvalidKeyError

from learning_assistant.constants import ACCEPTED_CATEGORY_TYPES, CATEGORY_TYPE_MAP
from datetime import datetime

from learning_assistant.constants import AUDIT_TRIAL_MAX_DAYS, ACCEPTED_CATEGORY_TYPES, CATEGORY_TYPE_MAP
from learning_assistant.data import LearningAssistantCourseEnabledData
from learning_assistant.models import LearningAssistantCourseEnabled, LearningAssistantMessage
from learning_assistant.models import (
LearningAssistantAuditTrial,
LearningAssistantCourseEnabled,
LearningAssistantMessage,
)
from learning_assistant.platform_imports import (
block_get_children,
block_leaf_filter,
Expand Down Expand Up @@ -224,3 +230,20 @@ def get_message_history(courserun_key, user, message_count):
message_history = list(LearningAssistantMessage.objects.filter(
course_id=courserun_key, user=user).order_by('-created')[:message_count])[::-1]
return message_history


def check_if_audit_trial_is_expired(user_id):
"""
Given a user (User), get the corresponding LearningAssistantAuditTrial trial object,
or create one if one does not exist yet.
"""
audit_trial, created = LearningAssistantAuditTrial.objects.get_or_create(user_id)

# If the trial was just created, then it definitely isn't expired, so return False
if created:
return False

# If the user's trial is expired, return True. Else, return False
if (datetime.now() - audit_trial.start_date) < AUDIT_TRIAL_MAX_DAYS:
return True
return False
2 changes: 2 additions & 0 deletions learning_assistant/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,5 @@
"html": "TEXT",
"video": "VIDEO",
}

AUDIT_TRIAL_MAX_DAYS = 14
19 changes: 13 additions & 6 deletions learning_assistant/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
pass

from learning_assistant.api import (
check_if_audit_trial_is_expired,
get_course_id,
get_message_history,
learning_assistant_enabled,
Expand Down Expand Up @@ -68,18 +69,24 @@ def post(self, request, course_run_id):
data={'detail': 'Learning assistant not enabled for course.'}
)

# If user does not have an enrollment record, or is not staff, they should not have access
# If user does not have a verified enrollment record, or is not staff, they should not have full access
user_role = get_user_role(request.user, courserun_key)
enrollment_object = CourseEnrollment.get_enrollment(request.user, courserun_key)
enrollment_mode = enrollment_object.mode if enrollment_object else None
if (
(enrollment_mode not in CourseMode.VERIFIED_MODES)
# NOTE: Will there ever be a case where the user has a course mod that's
# in neither VERIFIED_MODES nor AUDIT_MODES that we need to worry about?
enrollment_mode not in CourseMode.VERIFIED_MODES
and enrollment_mode in CourseMode.AUDIT_MODES
and not user_role_is_staff(user_role)
):
return Response(
status=http_status.HTTP_403_FORBIDDEN,
data={'detail': 'Must be staff or have valid enrollment.'}
)
# If user has an audit enrollment record, get or create their trial
user_audit_trial_expired = check_if_audit_trial_is_expired(user_id=request.user.id)
if user_audit_trial_expired:
return Response(
status=http_status.HTTP_403_FORBIDDEN,
data={'detail': 'Must be staff or have valid enrollment.'}
)

unit_id = request.query_params.get('unit_id')

Expand Down

0 comments on commit 95206e7

Please sign in to comment.