Skip to content

Commit

Permalink
Merge pull request #557 from edx/hammad/ENT-1860
Browse files Browse the repository at this point in the history
ENT-1860 | 500 error observed during Data Sharing Consent
  • Loading branch information
HammadAhmadWaqas authored Sep 3, 2019
2 parents 44a1643 + 23fd546 commit d048550
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 13 deletions.
48 changes: 35 additions & 13 deletions enterprise/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
from django.contrib.auth.decorators import login_required
from django.core.exceptions import ImproperlyConfigured, PermissionDenied
from django.core.urlresolvers import reverse
from django.db import transaction
from django.db import IntegrityError, transaction
from django.http import Http404
from django.shortcuts import redirect, render
from django.utils.decorators import method_decorator
Expand Down Expand Up @@ -468,6 +468,21 @@ def get_page_language_context_data(

return context_data

@staticmethod
def create_enterprise_course_enrollment(request, consent_record, course_id):
"""Create EnterpriseCustomerUser and EnterpriseCourseEnrollment record if not already exists."""
enterprise_customer_user, __ = EnterpriseCustomerUser.objects.get_or_create(
enterprise_customer=consent_record.enterprise_customer,
user_id=request.user.id
)
enterprise_customer_user.update_session(request)
__, created = EnterpriseCourseEnrollment.objects.get_or_create(
enterprise_customer_user=enterprise_customer_user,
course_id=course_id,
)
if created:
track_enrollment('data-consent-page-enrollment', request.user.id, course_id, request.path)

@method_decorator(login_required)
def get(self, request): # pylint: disable=too-many-statements
"""
Expand Down Expand Up @@ -768,18 +783,25 @@ def post(self, request):
consent_provided = bool(request.POST.get('data_sharing_consent', False))
if defer_creation is None and consent_record.consent_required():
if course_id:
enterprise_customer_user, __ = EnterpriseCustomerUser.objects.get_or_create(
enterprise_customer=consent_record.enterprise_customer,
user_id=request.user.id
)
enterprise_customer_user.update_session(request)
__, created = EnterpriseCourseEnrollment.objects.get_or_create(
enterprise_customer_user=enterprise_customer_user,
course_id=course_id,
)
if created:
track_enrollment('data-consent-page-enrollment', request.user.id, course_id, request.path)

try:
self.create_enterprise_course_enrollment(request, consent_record, course_id)
except IntegrityError:
error_code = 'ENTGDS009'
log_message = (
'[Enterprise DSC API] IntegrityError while creating EnterpriseCourseEnrollment.'
'Course: {course_id}, '
'Program: {program_uuid}, '
'EnterpriseCustomer: {enterprise_customer_uuid}, '
'User: {user_id}, '
'ErrorCode: {error_code}'.format(
course_id=course_id,
program_uuid=program_uuid,
enterprise_customer_uuid=enterprise_uuid,
user_id=request.user.id,
error_code=error_code,
)
)
LOGGER.exception(log_message)
consent_record.granted = consent_provided
consent_record.save()

Expand Down
1 change: 1 addition & 0 deletions enterprise/views_error_codes.txt
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ ENTGDS005 - Required request values missing for action to be carried out
ENTGDS006 - The course or program with a given id does not exist
ENTGDS007 - There is no consent record, or consent is not required
ENTGDS008 - No record found for Enterprise customer
ENTGDS009 - IntegrityError while creating EnterpriseCourseEnrollment. Record already exists

ENTHCE000 - No course modes for the goven course id

Expand Down

0 comments on commit d048550

Please sign in to comment.