From 046060afcaa74fd776c5ae1b903369e7d100ce80 Mon Sep 17 00:00:00 2001 From: Nathan Chapman Date: Mon, 14 Mar 2022 08:57:40 -0600 Subject: [PATCH] Update checkout --- src/core/models.py | 23 ++++++++++++++++++- .../templates/dashboard/dashboard_detail.html | 2 +- .../templates/dashboard/product_detail.html | 5 ++-- src/dashboard/views.py | 2 +- src/static/styles/main.css | 2 +- src/storefront/cart.py | 12 ++++++---- src/storefront/forms.py | 18 +++++++-------- .../templates/storefront/product_detail.html | 1 + src/storefront/views.py | 2 ++ 9 files changed, 48 insertions(+), 19 deletions(-) diff --git a/src/core/models.py b/src/core/models.py index b5c191b..53aa1e7 100644 --- a/src/core/models.py +++ b/src/core/models.py @@ -1,5 +1,6 @@ import logging from decimal import Decimal +from PIL import Image from measurement.measures import Weight from django.db import models @@ -28,7 +29,7 @@ logger = logging.getLogger(__name__) class ProductManager(models.Manager): def get_queryset(self): return super().get_queryset().annotate( - num_ordered=models.Count('order_lines__quantity', distinct=True) + num_ordered=models.Sum('order_lines__quantity') ) @@ -65,6 +66,15 @@ class ProductPhoto(models.Model): def __str__(self): return self.product.name + # def save(self, *args, **kwargs): + # super().save(*args, **kwargs) + + # img = Image.open(self.image.path) + # if img.height > 400 or img.width > 400: + # output_size = (400, 400) + # img.thumbnail(output_size) + # img.save(self.image.path) + @@ -105,6 +115,14 @@ class ShippingMethod(models.Model): type = models.CharField(max_length=30, choices=ShippingMethodType.CHOICES) +class OrderManager(models.Manager): + def with_lines(self): + return self.select_related('lines') + + def with_fulfillment(self): + return self.annotate( + total_quantity_fulfilled=models.Sum('lines__quantity_fulfilled'), + ) class Order(models.Model): @@ -155,12 +173,15 @@ class Order(models.Model): created_at = models.DateTimeField(auto_now_add=True, editable=False) updated_at = models.DateTimeField(auto_now=True) + objects = OrderManager() + def get_total_quantity(self): return sum([line.quantity for line in self]) def get_absolute_url(self): return reverse('dashboard:order-detail', kwargs={'pk': self.pk}) + class Transaction(models.Model): status = models.CharField( max_length=32, diff --git a/src/dashboard/templates/dashboard/dashboard_detail.html b/src/dashboard/templates/dashboard/dashboard_detail.html index 828a38f..1e0decb 100644 --- a/src/dashboard/templates/dashboard/dashboard_detail.html +++ b/src/dashboard/templates/dashboard/dashboard_detail.html @@ -14,7 +14,7 @@
Sales
Today -

${{todays_sales|floatformat:2}}

+

${{todays_sales|default_if_none:"0"|floatformat:2}}

diff --git a/src/dashboard/templates/dashboard/product_detail.html b/src/dashboard/templates/dashboard/product_detail.html index af71550..6c31064 100644 --- a/src/dashboard/templates/dashboard/product_detail.html +++ b/src/dashboard/templates/dashboard/product_detail.html @@ -4,7 +4,7 @@ {% block content %}
-

Product: {{product.name}}

+

Product

Edit
@@ -15,8 +15,9 @@

{{product.name}}

{{product.description}}

${{product.price}}

+

{{product.weight.oz}} oz

Visible in listings: {{product.visible_in_listings|yesno:"Yes,No"}}

-

Ordered {{num_ordered}} times.

+

Ordered {{product.num_ordered|default_if_none:"0"}} time{{ product.num_ordered|pluralize }}.

diff --git a/src/dashboard/views.py b/src/dashboard/views.py index 45138ac..ee8af68 100644 --- a/src/dashboard/views.py +++ b/src/dashboard/views.py @@ -42,7 +42,7 @@ class DashboardHomeView(TemplateView): ).count() context['todays_sales'] = Order.objects.filter( created_at__date=today - ).aggregate(total=Sum('total_net_amount'))['total'] or 0 + ).aggregate(total=Sum('total_net_amount'))['total'] return context class OrderListView(ListView): diff --git a/src/static/styles/main.css b/src/static/styles/main.css index 59afa83..4a32366 100644 --- a/src/static/styles/main.css +++ b/src/static/styles/main.css @@ -192,7 +192,7 @@ img { .product__item { display: grid; - grid-template-columns: repeat(2, 1fr); + grid-template-columns: 2fr 1fr; gap: 0 2rem; } diff --git a/src/storefront/cart.py b/src/storefront/cart.py index 07c7229..a60d424 100644 --- a/src/storefront/cart.py +++ b/src/storefront/cart.py @@ -13,16 +13,20 @@ class Cart: cart = self.session[settings.CART_SESSION_ID] = {} self.cart = cart - def add(self, product, quantity=1, roast='', other='', customer_note='', update_quantity=False): + def add(self, product, quantity=1, roast='', other='', update_quantity=False): product_id = str(product.id) if product_id not in self.cart: self.cart[product_id] = { 'quantity': 0, 'roast': roast, 'other': other, - 'customer_note': customer_note, 'price': str(product.price) } + elif product_id in self.cart: + self.cart[product_id].update({ + 'roast': roast, + 'other': other, + }) if update_quantity: self.cart[product_id]['quantity'] = quantity else: @@ -32,7 +36,7 @@ class Cart: def save(self): self.session[settings.CART_SESSION_ID] = self.cart self.session.modified = True - logger.info(f'\nCart:\n{self.cart}\n\n') + logger.info(f'\nCart:\n{self.cart}\n') def remove(self, product): product_id = str(product.id) @@ -102,7 +106,7 @@ class Cart: OrderLine( order=order, product=item['product'], - customer_note=item['customer_note'], + customer_note=f'{item["roast"]} {item["other"]}', unit_price=item['price'], quantity=item['quantity'], tax_rate=2, diff --git a/src/storefront/forms.py b/src/storefront/forms.py index 53b1c64..82e8994 100644 --- a/src/storefront/forms.py +++ b/src/storefront/forms.py @@ -8,15 +8,15 @@ from accounts import STATE_CHOICES logger = logging.getLogger(__name__) class AddToCartForm(forms.Form): - WHOLE = 'WHOLE' - ESPRESSO = 'ESPRESSO' - CONE_DRIP = 'CONE_DRIP' - BASKET_DRIP = 'BASKET_DRIP' - FRENCH_PRESS = 'FRENCH_PRESS' - STOVETOP_ESPRESSO = 'STOVETOP_ESPRESSO' - AEROPRESS = 'AEROPRESS' - PERCOLATOR = 'PERCOLATOR' - OTHER = 'OTHER' + WHOLE = 'Whole Beans' + ESPRESSO = 'Espresso' + CONE_DRIP = 'Cone Drip' + BASKET_DRIP = 'Basket Drip' + FRENCH_PRESS = 'French Press' + STOVETOP_ESPRESSO = 'Stovetop Espresso (Moka Pot)' + AEROPRESS = 'AeroPress' + PERCOLATOR = 'Percolator' + OTHER = 'Other' ROAST_CHOICES = [ (WHOLE, 'Whole Beans'), (ESPRESSO, 'Espresso'), diff --git a/src/storefront/templates/storefront/product_detail.html b/src/storefront/templates/storefront/product_detail.html index 2f8b974..ff9a9ad 100644 --- a/src/storefront/templates/storefront/product_detail.html +++ b/src/storefront/templates/storefront/product_detail.html @@ -10,6 +10,7 @@

{{product.name}}

{{product.description}}

${{product.price}}

+

{{product.weight.oz}} oz

{% csrf_token %} {{ form.as_p }} diff --git a/src/storefront/views.py b/src/storefront/views.py index a65544c..8bb66b8 100644 --- a/src/storefront/views.py +++ b/src/storefront/views.py @@ -35,6 +35,8 @@ class CartView(TemplateView): item['update_quantity_form'] = AddToCartForm( initial={ 'quantity': item['quantity'], + 'roast': item['roast'], + 'other': item['other'], 'update': True } )