From f2003a711f1f4fa15c97e47122e9ddec712340e5 Mon Sep 17 00:00:00 2001 From: Nathan Chapman Date: Sun, 1 May 2022 10:29:59 -0600 Subject: [PATCH] Update USPS shipping handling --- src/storefront/cart.py | 14 +++++++++----- .../templates/storefront/cart_detail.html | 2 +- src/storefront/views.py | 19 ++++++++++++++++--- 3 files changed, 26 insertions(+), 9 deletions(-) diff --git a/src/storefront/cart.py b/src/storefront/cart.py index b2eb9f0..e05afe6 100644 --- a/src/storefront/cart.py +++ b/src/storefront/cart.py @@ -115,17 +115,21 @@ class Cart: return ShippingContainer.VARIABLE def get_shipping_cost(self): - if len(self) > 0: - usps_rate_request = self.build_usps_rate_request() + if len(self) > 0 and self.session.get("shipping_address"): + try: + usps_rate_request = self.build_usps_rate_request() + except TypeError as e: + return Decimal('0.00') usps = USPSApiWithRate(settings.USPS_USER_ID, test=True) validation = usps.get_rate(usps_rate_request) logger.info(validation.result) try: - rate = Decimal(validation.result['RateV4Response']['Package']['Postage']['CommercialRate']) - return rate + rate = validation.result['RateV4Response']['Package']['Postage']['CommercialRate'] + rate = '0.00' except KeyError as e: raise e("USPS Result has no 'Postage'") - return Decimal('0.00') + rate = '0.00' + return Decimal(rate) else: return Decimal('0.00') diff --git a/src/storefront/templates/storefront/cart_detail.html b/src/storefront/templates/storefront/cart_detail.html index 555e307..2b107d2 100644 --- a/src/storefront/templates/storefront/cart_detail.html +++ b/src/storefront/templates/storefront/cart_detail.html @@ -16,7 +16,7 @@

{{product.name}}

-

Grind: {{item.grind}}

+

Grind:

{% for key, value in item.variations.items %}

{{key}}

diff --git a/src/storefront/views.py b/src/storefront/views.py index f3a9b76..625ac6d 100644 --- a/src/storefront/views.py +++ b/src/storefront/views.py @@ -7,7 +7,7 @@ from django.shortcuts import render, reverse, redirect, get_object_or_404 from django.urls import reverse_lazy from django.core.mail import EmailMessage from django.core.exceptions import ObjectDoesNotExist -from django.http import JsonResponse +from django.http import JsonResponse, HttpResponseRedirect from django.views.generic.base import RedirectView, TemplateView from django.views.generic.edit import FormView, CreateView, UpdateView, DeleteView, FormMixin from django.views.generic.detail import DetailView, SingleObjectMixin @@ -15,6 +15,7 @@ from django.views.generic.list import ListView from django.contrib.auth.decorators import login_required from django.contrib.auth.mixins import LoginRequiredMixin, UserPassesTestMixin from django.contrib.messages.views import SuccessMessageMixin +from django.contrib import messages from django.views.decorators.csrf import csrf_exempt from django.views.decorators.http import require_POST from django.forms.models import model_to_dict @@ -193,13 +194,25 @@ class OrderCreateView(CreateView): form_class = OrderCreateForm success_url = reverse_lazy('storefront:payment-done') + def get(self, request, *args, **kwargs): + if not self.request.session.get("shipping_address"): + messages.warning(request, 'Please add a shipping address.') + return HttpResponseRedirect(reverse('storefront:checkout-address')) + else: + return super().get(request, *args, **kwargs) + def get_initial(self): cart = Cart(self.request) + try: + shipping_cost = cart.get_shipping_cost() + except Exception as e: + raise e('Could not get shipping information') + shipping_cost = Decimal('0.00') + initial = { 'total_net_amount': cart.get_total_price(), - 'shipping_total': cart.get_shipping_cost() + 'shipping_total': shipping_cost } - if self.request.session.get('shipping_address'): a = self.request.session.get('shipping_address') user_info = {