From 89b40d639d2150af09c75a530b1ae2180d8b35e0 Mon Sep 17 00:00:00 2001
From: Nathan Chapman
Date: Sun, 8 May 2022 12:14:21 -0600
Subject: [PATCH] Add ability for customer to add their own shipping address
---
.../storefront/address_create_form.html | 19 ++++++++++++++
.../templates/storefront/customer_detail.html | 7 +++++-
.../templates/storefront/order_form.html | 1 +
src/storefront/urls.py | 3 ++-
src/storefront/views.py | 25 +++++++++++++++++--
5 files changed, 51 insertions(+), 4 deletions(-)
create mode 100644 src/storefront/templates/storefront/address_create_form.html
diff --git a/src/storefront/templates/storefront/address_create_form.html b/src/storefront/templates/storefront/address_create_form.html
new file mode 100644
index 0000000..80dd853
--- /dev/null
+++ b/src/storefront/templates/storefront/address_create_form.html
@@ -0,0 +1,19 @@
+{% extends 'base.html' %}
+
+{% block content %}
+
+
+
+
+{% endblock %}
diff --git a/src/storefront/templates/storefront/customer_detail.html b/src/storefront/templates/storefront/customer_detail.html
index 5f2ff72..52cba99 100644
--- a/src/storefront/templates/storefront/customer_detail.html
+++ b/src/storefront/templates/storefront/customer_detail.html
@@ -30,8 +30,13 @@
{% endwith %}
+
+
+ Your addresses
+
+ + New address
+
-
All addresses
{% for address in customer.addresses.all %}
diff --git a/src/storefront/templates/storefront/order_form.html b/src/storefront/templates/storefront/order_form.html
index 037e91d..ff960ce 100644
--- a/src/storefront/templates/storefront/order_form.html
+++ b/src/storefront/templates/storefront/order_form.html
@@ -15,6 +15,7 @@
Shipping address
+ {{shipping_address.email}}
{{shipping_address.first_name}}
{{shipping_address.last_name}}
diff --git a/src/storefront/urls.py b/src/storefront/urls.py
index bb576d8..7764fd5 100644
--- a/src/storefront/urls.py
+++ b/src/storefront/urls.py
@@ -33,7 +33,8 @@ urlpatterns = [
# path('delete/', views.CustomerDeleteView.as_view(), name='customer-delete'),
path('orders//', views.OrderDetailView.as_view(), name='order-detail'),
- path('addresses//update/', views.AddressUpdateView.as_view(), name='address-update'),
+ path('addresses/new/', views.CustomerAddressCreateView.as_view(), name='customer-address-create'),
+ path('addresses//update/', views.CustomerAddressUpdateView.as_view(), name='address-update'),
])),
]
diff --git a/src/storefront/views.py b/src/storefront/views.py
index a2a31cf..c585712 100644
--- a/src/storefront/views.py
+++ b/src/storefront/views.py
@@ -309,6 +309,7 @@ class CustomerUpdateView(LoginRequiredMixin, UpdateView):
def get_success_url(self):
return reverse('storefront:customer-detail', kwargs={'pk': self.object.pk})
+
class OrderDetailView(LoginRequiredMixin, DetailView):
model = Order
template_name = 'storefront/order_detail.html'
@@ -319,7 +320,27 @@ class OrderDetailView(LoginRequiredMixin, DetailView):
context['customer'] = User.objects.get(pk=self.kwargs['pk'])
return context
-class AddressUpdateView(LoginRequiredMixin, UpdateView):
+class CustomerAddressCreateView(LoginRequiredMixin, CreateView):
+ model = Address
+ template_name = 'storefront/address_create_form.html'
+ form_class = AccountAddressForm
+
+ def get_context_data(self, **kwargs):
+ context = super().get_context_data(**kwargs)
+ context['customer'] = User.objects.get(pk=self.kwargs['pk'])
+ return context
+
+ def form_valid(self, form):
+ customer = User.objects.get(pk=self.kwargs['pk'])
+ self.object = form.save()
+ customer.addresses.add(self.object)
+
+ return super().form_valid(form)
+
+ def get_success_url(self):
+ return reverse('storefront:customer-detail', kwargs={'pk': self.kwargs['pk']})
+
+class CustomerAddressUpdateView(LoginRequiredMixin, UpdateView):
model = Address
pk_url_kwarg = 'address_pk'
template_name = 'storefront/address_form.html'
@@ -331,7 +352,7 @@ class AddressUpdateView(LoginRequiredMixin, UpdateView):
return context
def get_success_url(self):
- return reverse('storefront:customer-detail',kwargs={'pk': self.kwargs['pk']})
+ return reverse('storefront:customer-detail', kwargs={'pk': self.kwargs['pk']})