Refactor validation code + Add product_id to context

This commit is contained in:
PCoder 2018-09-26 09:15:24 +02:00
parent 508360472a
commit 4575ff60ec

View file

@ -279,8 +279,7 @@ class PaymentOrderView(FormView):
product = None product = None
try: try:
product = GenericProduct.objects.get( product = GenericProduct.objects.get(
product_name= id=request.POST['generic_payment_form-product_name']
request.POST['generic_payment_form-product_name']
) )
except GenericProduct.DoesNotExist as dne: except GenericProduct.DoesNotExist as dne:
logger.error( logger.error(
@ -341,37 +340,11 @@ class PaymentOrderView(FormView):
) )
if generic_payment_form.is_valid(): if generic_payment_form.is_valid():
logger.debug("Generic payment form is valid.") logger.debug("Generic payment form is valid.")
product = None product = generic_payment_form.cleaned_data.get(
try: 'product_name'
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()
gp_details = { gp_details = {
"product_name": generic_payment_form.cleaned_data.get( "product_name": product.product_name,
'product_name'
),
"amount": generic_payment_form.cleaned_data.get( "amount": generic_payment_form.cleaned_data.get(
'amount' 'amount'
), ),
@ -381,14 +354,10 @@ class PaymentOrderView(FormView):
"description": generic_payment_form.cleaned_data.get( "description": generic_payment_form.cleaned_data.get(
'description' 'description'
), ),
"product_id": product.id
} }
if (product.get_actual_price() != gp_details['amount'] or
product.isSubscription != # gp_details['product_id'] = product.id
(True if gp_details["recurring"] else False)):
raise ValidationError(
_("Product parameters do not match")
)
gp_details['product_id'] = product.id
request.session["generic_payment_details"] = ( request.session["generic_payment_details"] = (
gp_details gp_details
) )
@ -858,6 +827,7 @@ class OrderConfirmationView(DetailView):
'description': gp_details['description'], 'description': gp_details['description'],
'recurring': gp_details['recurring'], 'recurring': gp_details['recurring'],
'product_name': gp_details['product_name'], 'product_name': gp_details['product_name'],
'product_id': gp_details['product_id'],
'order_id': order.id 'order_id': order.id
} }