def get(self, request): partner = get_partner_for_site(request) sku = request.GET.get('sku', None) code = request.GET.get('code', None) if not sku: return HttpResponseBadRequest(_('No SKU provided.')) if code: voucher, __ = get_voucher_from_code(code=code) else: voucher = None try: product = StockRecord.objects.get(partner=partner, partner_sku=sku).product course_key = product.attr.course_key api = EdxRestApiClient( get_lms_enrollment_base_api_url(), oauth_access_token=request.user.access_token, append_slash=False ) logger.debug( 'Getting enrollment information for [%s] in [%s].', request.user.username, course_key ) status = api.enrollment(','.join([request.user.username, course_key])).get() username = request.user.username seat_type = mode_for_seat(product) if status and status.get('mode') == seat_type and status.get('is_active'): logger.warning( 'User [%s] attempted to repurchase the [%s] seat of course [%s]', username, seat_type, course_key ) return HttpResponseBadRequest(_('You are already enrolled in {course}.').format( course=product.course.name)) except StockRecord.DoesNotExist: return HttpResponseBadRequest(_('SKU [{sku}] does not exist.').format(sku=sku)) except (ConnectionError, SlumberBaseException, Timeout) as ex: logger.exception( 'Failed to retrieve enrollment details for [%s] in course [%s], Because of [%s]', request.user.username, course_key, ex, ) return HttpResponseBadRequest(_('An error occurred while retrieving enrollment details. Please try again.')) purchase_info = request.strategy.fetch_for_product(product) if not purchase_info.availability.is_available_to_buy: return HttpResponseBadRequest(_('Product [{product}] not available to buy.').format(product=product.title)) prepare_basket(request, product, voucher) return HttpResponseRedirect(reverse('basket:summary'), status=303)
class EnrollmentApiClient(object): def __init__(self): access_token, __ = get_access_token() self.client = EdxRestApiClient(ENROLLMENT_API_URL, jwt=access_token, append_slash=False) def get_enrollment_status(self, username, course_id): """ Retrieve the enrollment status for given user in a given course. """ param = '{username},{course_id}'.format(username=username, course_id=course_id) return self.client.enrollment(param).get()
def is_user_already_enrolled(self, request, seat): """ Check if a user is already enrolled in the course. Calls the LMS enrollment API endpoint and sends the course ID and username query parameters and returns the status of the user's enrollment in the course. Arguments: request (WSGIRequest): the request from which the LMS enrollment API endpoint is created. seat (Product): the seat for which the check is done if the user is enrolled in. Returns: A boolean value if the user is enrolled in the course or not. Raises: ConnectionError, SlumberBaseException and Timeout for failures in establishing a connection with the LMS enrollment API endpoint. """ course_key = seat.attr.course_key try: api = EdxRestApiClient( request.site.siteconfiguration.build_lms_url( '/api/enrollment/v1'), oauth_access_token=self.access_token, append_slash=False) status = api.enrollment(','.join([self.username, course_key])).get() except (ConnectionError, SlumberBaseException, Timeout) as ex: log.exception( 'Failed to retrieve enrollment details for [%s] in course [%s], because of [%s]', self.username, course_key, ex, ) raise ex seat_type = mode_for_seat(seat) if status and status.get('mode') == seat_type and status.get( 'is_active'): return True return False
def is_user_already_enrolled(self, request, seat): """ Check if a user is already enrolled in the course. Calls the LMS enrollment API endpoint and sends the course ID and username query parameters and returns the status of the user's enrollment in the course. Arguments: request (WSGIRequest): the request from which the LMS enrollment API endpoint is created. seat (Product): the seat for which the check is done if the user is enrolled in. Returns: A boolean value if the user is enrolled in the course or not. Raises: ConnectionError, SlumberBaseException and Timeout for failures in establishing a connection with the LMS enrollment API endpoint. """ course_key = seat.attr.course_key try: api = EdxRestApiClient( request.site.siteconfiguration.build_lms_url('/api/enrollment/v1'), oauth_access_token=self.access_token, append_slash=False ) status = api.enrollment(','.join([self.username, course_key])).get() except (ConnectionError, SlumberBaseException, Timeout) as ex: log.exception( 'Failed to retrieve enrollment details for [%s] in course [%s], Because of [%s]', self.username, course_key, ex, ) raise ex seat_type = mode_for_seat(seat) if status and status.get('mode') == seat_type and status.get('is_active'): return True return False
def get(self, request): partner = get_partner_for_site(request) sku = request.GET.get('sku', None) code = request.GET.get('code', None) if not sku: return HttpResponseBadRequest(_('No SKU provided.')) if code: voucher, __ = get_voucher_and_products_from_code(code=code) else: voucher = None try: product = StockRecord.objects.get(partner=partner, partner_sku=sku).product except StockRecord.DoesNotExist: return HttpResponseBadRequest(_('SKU [{sku}] does not exist.').format(sku=sku)) # If the product isn't available then there's no reason to continue with the basket addition purchase_info = request.strategy.fetch_for_product(product) if not purchase_info.availability.is_available_to_buy: msg = _('Product [{product}] not available to buy.').format(product=product.title) return HttpResponseBadRequest(msg) # If the product is not an Enrollment Code, we check to see if the user is already # enrolled to prevent double-enrollment and/or accidental coupon usage if product.get_product_class().name != ENROLLMENT_CODE_PRODUCT_CLASS_NAME: course_key = product.attr.course_key # Submit a query to the LMS Enrollment API try: api = EdxRestApiClient( get_lms_enrollment_base_api_url(), oauth_access_token=request.user.access_token, append_slash=False ) logger.debug( 'Getting enrollment information for [%s] in [%s].', request.user.username, course_key ) status = api.enrollment(','.join([request.user.username, course_key])).get() except (ConnectionError, SlumberBaseException, Timeout) as ex: logger.exception( 'Failed to retrieve enrollment details for [%s] in course [%s], Because of [%s]', request.user.username, course_key, ex, ) msg = _('An error occurred while retrieving enrollment details. Please try again.') return HttpResponseBadRequest(msg) # Enrollment API response received, now perform the actual enrollment check username = request.user.username seat_type = mode_for_seat(product) if status and status.get('mode') == seat_type and status.get('is_active'): logger.warning( 'User [%s] attempted to repurchase the [%s] seat of course [%s]', username, seat_type, course_key ) msg = _('You are already enrolled in {course}.').format(course=product.course.name) return HttpResponseBadRequest(msg) # At this point we're either adding an Enrollment Code product to the basket, # or the user is adding a Seat product for which they are not already enrolled prepare_basket(request, product, voucher) return HttpResponseRedirect(reverse('basket:summary'), status=303)