From 4575ff60ecb890547032ee3d1d4b546193b8656f Mon Sep 17 00:00:00 2001 From: PCoder Date: Wed, 26 Sep 2018 09:15:24 +0200 Subject: [PATCH] Refactor validation code + Add product_id to context --- datacenterlight/views.py | 48 ++++++++-------------------------------- 1 file changed, 9 insertions(+), 39 deletions(-) diff --git a/datacenterlight/views.py b/datacenterlight/views.py index 736d2e63..66f8c642 100644 --- a/datacenterlight/views.py +++ b/datacenterlight/views.py @@ -279,8 +279,7 @@ class PaymentOrderView(FormView): product = None try: product = GenericProduct.objects.get( - product_name= - request.POST['generic_payment_form-product_name'] + id=request.POST['generic_payment_form-product_name'] ) except GenericProduct.DoesNotExist as dne: logger.error( @@ -341,37 +340,11 @@ class PaymentOrderView(FormView): ) if generic_payment_form.is_valid(): logger.debug("Generic payment form is valid.") - product = None - try: - product = GenericProduct.objects.get( - product_name= - request.POST['generic_payment_form-product_name'] - ) - except GenericProduct.DoesNotExist as dne: - err_msg = _( - "The requested product '{}' does not exist".format( - request.POST[ - 'generic_payment_form-product_name'] - ) - ) - logger.error(err_msg) - raise ValidationError(err_msg) - except GenericProduct.MultipleObjectsReturned as mpe: - logger.error( - "There seem to be more than one product with " - "the name {}".format( - request.POST[ - 'generic_payment_form-product_name'] - ) - ) - product = GenericProduct.objects.all( - product_name= - request.POST['generic_payment_form-product_name'] - ).first() + product = generic_payment_form.cleaned_data.get( + 'product_name' + ) gp_details = { - "product_name": generic_payment_form.cleaned_data.get( - 'product_name' - ), + "product_name": product.product_name, "amount": generic_payment_form.cleaned_data.get( 'amount' ), @@ -381,14 +354,10 @@ class PaymentOrderView(FormView): "description": generic_payment_form.cleaned_data.get( 'description' ), + "product_id": product.id } - if (product.get_actual_price() != gp_details['amount'] or - product.isSubscription != - (True if gp_details["recurring"] else False)): - raise ValidationError( - _("Product parameters do not match") - ) - gp_details['product_id'] = product.id + + # gp_details['product_id'] = product.id request.session["generic_payment_details"] = ( gp_details ) @@ -858,6 +827,7 @@ class OrderConfirmationView(DetailView): 'description': gp_details['description'], 'recurring': gp_details['recurring'], 'product_name': gp_details['product_name'], + 'product_id': gp_details['product_id'], 'order_id': order.id }