def save_registration_code(user, course_id, mode_slug, invoice=None, order=None, invoice_item=None):
 code = random_code_generator()
 matching_coupons = Coupon.objects.filter(code=code, is_active=True)
 if matching_coupons:
  return save_registration_code(user, course_id, mode_slug, invoice=invoice, order=order, invoice_item=invoice_item)
 course_registration = CourseRegistrationCode(code=code, course_id=unicode(course_id), created_by=user, invoice=invoice, order=order, mode_slug=mode_slug, invoice_item=invoice_item)
 try:
  with transaction.atomic():
   course_registration.save()
  return course_registration
 except IntegrityError:
  return save_registration_code(user, course_id, mode_slug, invoice=invoice, order=order, invoice_item=invoice_item)

def save_registration_code ( user , course_id , mode_slug , invoice = None , order = None , invoice_item = None ) : code = random_code_generator ( ) matching_coupons = Coupon . objects . filter ( code = code , is_active = True ) if matching_coupons : return save_registration_code ( user , course_id , mode_slug , invoice = invoice , order = order , invoice_item = invoice_item ) course_registration = CourseRegistrationCode ( code = code , course_id = unicode ( course_id ) , created_by = user , invoice = invoice , order = order , mode_slug = mode_slug , invoice_item = invoice_item ) try : with transaction . atomic ( ) : course_registration . save ( ) return course_registration except IntegrityError : return save_registration_code ( user , course_id , mode_slug , invoice = invoice , order = order , invoice_item = invoice_item )
recursive function that generate a new code every time and saves in the course registration table if validation check passes args: user : the user creating the course registration codes .