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
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
}