@login_required
@enforce_shopping_cart_enabled
def use_code(request):
 code = request.POST['code']
 coupons = Coupon.objects.filter(Q(code=code), Q(is_active=True), (Q(expiration_date__gt=datetime.datetime.now(pytz.UTC)) | Q(expiration_date__isnull=True)))
 if (not coupons):
  try:
   course_reg = CourseRegistrationCode.objects.get(code=code)
  except CourseRegistrationCode.DoesNotExist:
   return HttpResponseNotFound(_("Discount does not exist against code '{code}'.").format(code=code))
  return use_registration_code(course_reg, request.user)
 return use_coupon_code(coupons, request.user)

@ login_required @ enforce_shopping_cart_enabled def use_code ( request ) : code = request . POST [ 'code' ] coupons = Coupon . objects . filter ( Q ( code = code ) , Q ( is_active = True ) , ( Q ( expiration_date__gt = datetime . datetime . now ( pytz . UTC ) ) | Q ( expiration_date__isnull = True ) ) ) if ( not coupons ) : try : course_reg = CourseRegistrationCode . objects . get ( code = code ) except CourseRegistrationCode . DoesNotExist : return HttpResponseNotFound ( _ ( "Discount does not exist against code '{code}'." ) . format ( code = code ) ) return use_registration_code ( course_reg , request . user ) return use_coupon_code ( coupons , request . user )
valid code can be either coupon or registration code .