diff --git a/learning_assistant/api.py b/learning_assistant/api.py index 55c73c1..701fca0 100644 --- a/learning_assistant/api.py +++ b/learning_assistant/api.py @@ -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, @@ -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 diff --git a/learning_assistant/constants.py b/learning_assistant/constants.py index 7027a28..92e3717 100644 --- a/learning_assistant/constants.py +++ b/learning_assistant/constants.py @@ -14,3 +14,5 @@ "html": "TEXT", "video": "VIDEO", } + +AUDIT_TRIAL_MAX_DAYS = 14 diff --git a/learning_assistant/views.py b/learning_assistant/views.py index b1b5c03..672f063 100644 --- a/learning_assistant/views.py +++ b/learning_assistant/views.py @@ -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, @@ -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')