本文整理汇总了Python中edx_rest_api_client.client.EdxRestApiClient.enrollment方法的典型用法代码示例。如果您正苦于以下问题:Python EdxRestApiClient.enrollment方法的具体用法?Python EdxRestApiClient.enrollment怎么用?Python EdxRestApiClient.enrollment使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类edx_rest_api_client.client.EdxRestApiClient
的用法示例。
在下文中一共展示了EdxRestApiClient.enrollment方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: get
# 需要导入模块: from edx_rest_api_client.client import EdxRestApiClient [as 别名]
# 或者: from edx_rest_api_client.client.EdxRestApiClient import enrollment [as 别名]
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)
示例2: is_user_already_enrolled
# 需要导入模块: from edx_rest_api_client.client import EdxRestApiClient [as 别名]
# 或者: from edx_rest_api_client.client.EdxRestApiClient import enrollment [as 别名]
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
示例3: get
# 需要导入模块: from edx_rest_api_client.client import EdxRestApiClient [as 别名]
# 或者: from edx_rest_api_client.client.EdxRestApiClient import enrollment [as 别名]
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)