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

← Back

+

Create Address

+
+
+
+ {% csrf_token %} + {{ form.as_p }} +

+ +

+
+
+
+{% 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']})