Commit 8ad7a64f by Chris Dodge

when enrolling a student from a PaidCourseRegistration or a RegistrationCode…

when enrolling a student from a PaidCourseRegistration or a RegistrationCode redemption, then note the CourseEnrollment id in the respective tables
parent d4de932c
......@@ -759,6 +759,7 @@ class RegistrationCodeRedemption(models.Model):
registration_code = models.ForeignKey(CourseRegistrationCode, db_index=True)
redeemed_by = models.ForeignKey(User, db_index=True)
redeemed_at = models.DateTimeField(default=datetime.now(pytz.utc), null=True)
course_enrollment = models.ForeignKey(CourseEnrollment, null=True)
@classmethod
def delete_registration_redemption(cls, user, cart):
......@@ -811,6 +812,7 @@ class RegistrationCodeRedemption(models.Model):
"""
code_redemption = RegistrationCodeRedemption(registration_code=course_reg_code, redeemed_by=user)
code_redemption.save()
return code_redemption
class SoftDeleteCouponManager(models.Manager):
......@@ -910,6 +912,7 @@ class PaidCourseRegistration(OrderItem):
"""
course_id = CourseKeyField(max_length=128, db_index=True)
mode = models.SlugField(default=CourseMode.DEFAULT_MODE_SLUG)
course_enrollment = models.ForeignKey(CourseEnrollment, null=True)
@classmethod
def contained_in_order(cls, order, course_id):
......@@ -1004,7 +1007,9 @@ class PaidCourseRegistration(OrderItem):
log.error(msg)
raise PurchasedCallbackException(msg)
CourseEnrollment.enroll(user=self.user, course_key=self.course_id, mode=self.mode)
# enroll in course and link to the enrollment_id
self.course_enrollment = CourseEnrollment.enroll(user=self.user, course_key=self.course_id, mode=self.mode)
self.save()
log.info("Enrolled {0} in paid course {1}, paid ${2}"
.format(self.user.email, self.course_id, self.line_cost)) # pylint: disable=no-member
......
......@@ -499,6 +499,8 @@ class PaidCourseRegistrationTest(ModuleStoreTestCase):
self.assertTrue(CourseEnrollment.is_enrolled(self.user, self.course_key))
reg1 = PaidCourseRegistration.objects.get(id=reg1.id) # reload from DB to get side-effect
self.assertEqual(reg1.status, "purchased")
self.assertIsNotNone(reg1.course_enrollment)
self.assertEqual(reg1.course_enrollment.id, CourseEnrollment.objects.get(user=self.user, course_id=self.course_key).id)
def test_generate_receipt_instructions(self):
"""
......
......@@ -975,8 +975,8 @@ class ShoppingCartViewsTests(ModuleStoreTestCase):
# Two courses in user shopping cart
self.login_user()
PaidCourseRegistration.add_to_order(self.cart, self.course_key)
PaidCourseRegistration.add_to_order(self.cart, self.testing_course.id)
item1 = PaidCourseRegistration.add_to_order(self.cart, self.course_key)
item2 = PaidCourseRegistration.add_to_order(self.cart, self.testing_course.id)
self.assertEquals(self.cart.orderitem_set.count(), 2)
resp = self.client.post(reverse('shoppingcart.views.use_code'), {'code': self.reg_code})
......@@ -997,6 +997,16 @@ class ShoppingCartViewsTests(ModuleStoreTestCase):
course_enrollment = CourseEnrollment.objects.filter(user=self.user)
self.assertEqual(course_enrollment.count(), 2)
# make sure the enrollment_ids were stored in the PaidCourseRegistration items
# refetch them first since they are updated
item1 = PaidCourseRegistration.objects.get(id=item1.id)
self.assertIsNotNone(item1.course_enrollment)
self.assertEqual(item1.course_enrollment.course_id, self.course_key)
item2 = PaidCourseRegistration.objects.get(id=item2.id)
self.assertIsNotNone(item2.course_enrollment)
self.assertEqual(item2.course_enrollment.course_id, self.testing_course.id)
@patch('shoppingcart.views.render_to_response', render_mock)
def test_show_receipt_success_with_valid_reg_code(self):
self.add_course_to_user_cart(self.course_key)
......@@ -1454,7 +1464,9 @@ class RegistrationCodeRedemptionCourseEnrollment(ModuleStoreTestCase):
self.assertTrue('View Course' in response.content)
#now check that the registration code has already been redeemed and user is already registered in the course
RegistrationCodeRedemption.objects.filter(registration_code__code=registration_code)
redemption = RegistrationCodeRedemption.objects.get(registration_code__code=registration_code)
self.assertIsNotNone(redemption.course_enrollment)
response = self.client.get(redeem_url, **{'HTTP_HOST': 'localhost'})
self.assertEquals(len(RegistrationCodeRedemption.objects.filter(registration_code__code=registration_code)), 1)
self.assertTrue("You've clicked a link for an enrollment code that has already been used." in response.content)
......
......@@ -352,8 +352,9 @@ def register_code_redemption(request, registration_code):
course = get_course_by_id(getattr(course_registration, 'course_id'), depth=0)
if reg_code_is_valid and not reg_code_already_redeemed:
#now redeem the reg code.
RegistrationCodeRedemption.create_invoice_generated_registration_redemption(course_registration, request.user)
CourseEnrollment.enroll(request.user, course.id)
redemption = RegistrationCodeRedemption.create_invoice_generated_registration_redemption(course_registration, request.user)
redemption.course_enrollment = CourseEnrollment.enroll(request.user, course.id)
redemption.save()
context = {
'redemption_success': True,
'reg_code': registration_code,
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment