def get_eligibility_for_user(request): """ **User Cases** Retrieve user eligibility against course. **Parameters:** * course_key (unicode): Identifier of course. * username (unicode): Username of current User. **Example Usage:** GET /api/credit/v1/eligibility?username=user&course_key=edX/Demo_101/Fall "response": { "course_key": "edX/Demo_101/Fall", "deadline": "2015-10-23" } **Responses:** * 200 OK: The request was created successfully. * 404 Not Found: The provider does not exist. """ course_key = request.GET.get("course_key", None) username = request.GET.get("username", None) return JsonResponse( api.get_eligibilities_for_user(username=username, course_key=course_key))
def get_eligibility_for_user(request): """ **User Cases** Retrieve user eligibility against course. **Parameters:** * course_key (unicode): Identifier of course. * username (unicode): Username of current User. **Example Usage:** GET /api/credit/v1/eligibility?username=user&course_key=edX/Demo_101/Fall "response": { "course_key": "edX/Demo_101/Fall", "deadline": "2015-10-23" } **Responses:** * 200 OK: The request was created successfully. * 404 Not Found: The provider does not exist. """ course_key = request.GET.get("course_key", None) username = request.GET.get("username", None) return JsonResponse(api.get_eligibilities_for_user(username=username, course_key=course_key))
def test_eligibility_disabled_course(self): # Configure a credit eligibility for a disabled course credit_course = self.add_credit_course() credit_course.enabled = False credit_course.save() CreditEligibility.objects.create(course=credit_course, username="******") # The user should NOT be eligible for credit is_eligible = api.is_user_eligible_for_credit("staff", credit_course.course_key) self.assertFalse(is_eligible) # The eligibility should NOT show up in the user's list of eligibilities eligibilities = api.get_eligibilities_for_user("staff") self.assertEqual(eligibilities, [])
def test_eligibility_expired(self): # Configure a credit eligibility that expired yesterday credit_course = self.add_credit_course() CreditEligibility.objects.create( course=credit_course, username="******", deadline=datetime.datetime.now(pytz.UTC) - datetime.timedelta(days=1) ) # The user should NOT be eligible for credit is_eligible = api.is_user_eligible_for_credit("staff", credit_course.course_key) self.assertFalse(is_eligible) # The eligibility should NOT show up in the user's list of eligibilities eligibilities = api.get_eligibilities_for_user("staff") self.assertEqual(eligibilities, [])
def test_eligibility_expired(self): # Configure a credit eligibility that expired yesterday credit_course = self.add_credit_course() CreditEligibility.objects.create( course=credit_course, username="******", deadline=datetime.datetime.now(pytz.UTC) - datetime.timedelta(days=1) ) # The user should NOT be eligible for credit is_eligible = api.is_user_eligible_for_credit("staff", credit_course.course_key) self.assertFalse(is_eligible) # The eligibility should NOT show up in the user's list of eligibilities eligibilities = api.get_eligibilities_for_user("staff") self.assertEqual(eligibilities, [])
def test_eligibility_disabled_course(self): # Configure a credit eligibility for a disabled course credit_course = self.add_credit_course() credit_course.enabled = False credit_course.save() CreditEligibility.objects.create( course=credit_course, username="******", ) # The user should NOT be eligible for credit is_eligible = api.is_user_eligible_for_credit("staff", credit_course.course_key) self.assertFalse(is_eligible) # The eligibility should NOT show up in the user's list of eligibilities eligibilities = api.get_eligibilities_for_user("staff") self.assertEqual(eligibilities, [])
def _credit_statuses(user, course_enrollments): """ Retrieve the status for credit courses. A credit course is a course for which a user can purchased college credit. The current flow is: 1. User becomes eligible for credit (submits verifications, passes the course, etc.) 2. User purchases credit from a particular credit provider. 3. User requests credit from the provider, usually creating an account on the provider's site. 4. The credit provider notifies us whether the user's request for credit has been accepted or rejected. The dashboard is responsible for communicating the user's state in this flow. Arguments: user (User): The currently logged-in user. course_enrollments (list[CourseEnrollment]): List of enrollments for the user. Returns: dict The returned dictionary has keys that are `CourseKey`s and values that are dictionaries with: * eligible (bool): True if the user is eligible for credit in this course. * deadline (datetime): The deadline for purchasing and requesting credit for this course. * purchased (bool): Whether the user has purchased credit for this course. * provider_name (string): The display name of the credit provider. * provider_status_url (string): A URL the user can visit to check on their credit request status. * request_status (string): Either "pending", "approved", or "rejected" * error (bool): If true, an unexpected error occurred when retrieving the credit status, so the user should contact the support team. Example: >>> _credit_statuses(user, course_enrollments) { CourseKey.from_string("edX/DemoX/Demo_Course"): { "course_key": "edX/DemoX/Demo_Course", "eligible": True, "deadline": 2015-11-23 00:00:00 UTC, "purchased": True, "provider_name": "Hogwarts", "provider_status_url": "http://example.com/status", "request_status": "pending", "error": False } } """ from openedx.core.djangoapps.credit import api as credit_api # Feature flag off if not settings.FEATURES.get("ENABLE_CREDIT_ELIGIBILITY"): return {} request_status_by_course = { request["course_key"]: request["status"] for request in credit_api.get_credit_requests_for_user(user.username) } credit_enrollments = { enrollment.course_id: enrollment for enrollment in course_enrollments if enrollment.mode == "credit" } # When a user purchases credit in a course, the user's enrollment # mode is set to "credit" and an enrollment attribute is set # with the ID of the credit provider. We retrieve *all* such attributes # here to minimize the number of database queries. purchased_credit_providers = { attribute.enrollment.course_id: attribute.value for attribute in CourseEnrollmentAttribute.objects.filter( namespace="credit", name="provider_id", enrollment__in=list(credit_enrollments.values()) ).select_related("enrollment") } provider_info_by_id = { provider["id"]: provider for provider in credit_api.get_credit_providers() } statuses = {} for eligibility in credit_api.get_eligibilities_for_user(user.username): course_key = CourseKey.from_string(text_type(eligibility["course_key"])) providers_names = get_credit_provider_attribute_values(course_key, 'display_name') status = { "course_key": text_type(course_key), "eligible": True, "deadline": eligibility["deadline"], "purchased": course_key in credit_enrollments, "provider_name": make_providers_strings(providers_names), "provider_status_url": None, "provider_id": None, "request_status": request_status_by_course.get(course_key), "error": False, } # If the user has purchased credit, then include information about the credit # provider from which the user purchased credit. # We retrieve the provider's ID from the an "enrollment attribute" set on the user's # enrollment when the user's order for credit is fulfilled by the E-Commerce service. if status["purchased"]: provider_id = purchased_credit_providers.get(course_key) if provider_id is None: status["error"] = True log.error( u"Could not find credit provider associated with credit enrollment " u"for user %s in course %s. The user will not be able to see their " u"credit request status on the student dashboard. This attribute should " u"have been set when the user purchased credit in the course.", user.id, course_key ) else: provider_info = provider_info_by_id.get(provider_id, {}) status["provider_name"] = provider_info.get("display_name") status["provider_status_url"] = provider_info.get("status_url") status["provider_id"] = provider_id if not status["provider_name"] and not status["provider_status_url"]: status["error"] = True log.error( u"Could not find credit provider info for [%s] in [%s]. The user will not " u"be able to see their credit request status on the student dashboard.", provider_id, provider_info_by_id ) statuses[course_key] = status return statuses
def _credit_statuses(user, course_enrollments): """ Retrieve the status for credit courses. A credit course is a course for which a user can purchased college credit. The current flow is: 1. User becomes eligible for credit (submits verifications, passes the course, etc.) 2. User purchases credit from a particular credit provider. 3. User requests credit from the provider, usually creating an account on the provider's site. 4. The credit provider notifies us whether the user's request for credit has been accepted or rejected. The dashboard is responsible for communicating the user's state in this flow. Arguments: user (User): The currently logged-in user. course_enrollments (list[CourseEnrollment]): List of enrollments for the user. Returns: dict The returned dictionary has keys that are `CourseKey`s and values that are dictionaries with: * eligible (bool): True if the user is eligible for credit in this course. * deadline (datetime): The deadline for purchasing and requesting credit for this course. * purchased (bool): Whether the user has purchased credit for this course. * provider_name (string): The display name of the credit provider. * provider_status_url (string): A URL the user can visit to check on their credit request status. * request_status (string): Either "pending", "approved", or "rejected" * error (bool): If true, an unexpected error occurred when retrieving the credit status, so the user should contact the support team. Example: >>> _credit_statuses(user, course_enrollments) { CourseKey.from_string("edX/DemoX/Demo_Course"): { "course_key": "edX/DemoX/Demo_Course", "eligible": True, "deadline": 2015-11-23 00:00:00 UTC, "purchased": True, "provider_name": "Hogwarts", "provider_status_url": "http://example.com/status", "request_status": "pending", "error": False } } """ from openedx.core.djangoapps.credit import api as credit_api # Feature flag off if not settings.FEATURES.get("ENABLE_CREDIT_ELIGIBILITY"): return {} request_status_by_course = { request["course_key"]: request["status"] for request in credit_api.get_credit_requests_for_user(user.username) } credit_enrollments = { enrollment.course_id: enrollment for enrollment in course_enrollments if enrollment.mode == "credit" } # When a user purchases credit in a course, the user's enrollment # mode is set to "credit" and an enrollment attribute is set # with the ID of the credit provider. We retrieve *all* such attributes # here to minimize the number of database queries. purchased_credit_providers = { attribute.enrollment.course_id: attribute.value for attribute in CourseEnrollmentAttribute.objects.filter( namespace="credit", name="provider_id", enrollment__in=credit_enrollments.values() ).select_related("enrollment") } provider_info_by_id = { provider["id"]: provider for provider in credit_api.get_credit_providers() } statuses = {} for eligibility in credit_api.get_eligibilities_for_user(user.username): course_key = CourseKey.from_string(text_type(eligibility["course_key"])) providers_names = get_credit_provider_display_names(course_key) status = { "course_key": text_type(course_key), "eligible": True, "deadline": eligibility["deadline"], "purchased": course_key in credit_enrollments, "provider_name": make_providers_strings(providers_names), "provider_status_url": None, "provider_id": None, "request_status": request_status_by_course.get(course_key), "error": False, } # If the user has purchased credit, then include information about the credit # provider from which the user purchased credit. # We retrieve the provider's ID from the an "enrollment attribute" set on the user's # enrollment when the user's order for credit is fulfilled by the E-Commerce service. if status["purchased"]: provider_id = purchased_credit_providers.get(course_key) if provider_id is None: status["error"] = True log.error( u"Could not find credit provider associated with credit enrollment " u"for user %s in course %s. The user will not be able to see his or her " u"credit request status on the student dashboard. This attribute should " u"have been set when the user purchased credit in the course.", user.id, course_key ) else: provider_info = provider_info_by_id.get(provider_id, {}) status["provider_name"] = provider_info.get("display_name") status["provider_status_url"] = provider_info.get("status_url") status["provider_id"] = provider_id statuses[course_key] = status return statuses