44. Extra adjustments before payment system
def finish_order(request, order_id) :
if request.method == "POST" :
error = None
data = request.POST.dict()
total = data.get("total")
order = Order.objects.get(id=order_id) #? used get because it is only one order
if float(total) != float(order.total_cost) : #? transformed to float to avoid comparisson errors
error = "conflicting_cost" #? in case the user tries to manipulate the html
if not "adress" in data :
error = "adress"
else :
id_adress = data.get("adress")
adress = Adres.objects.get(id=id_adress)
order.adress = adress
if not request.user.is_authenticated :
email = data.get("email")
try :
validate_email(email)
except ValidationError :
error = "email"
if not error :
clients = Client.objects.filter(email=email)
if clients:
order.client = clients[0]
else:
order.client.email = email
order.client.save()
if error :
addresses = Adres.objects.filter(client=order.client) #? filters all adresses associated with the client
context = {"error" : error, "order" : order, "addresses" : addresses}
return render(request,"checkout.html", context)
return redirect("checkout")
else :
return redirect("store")
Last updated