Update checkout

This commit is contained in:
Nathan Chapman 2022-03-14 08:57:40 -06:00
parent 7df5e92720
commit 046060afca
9 changed files with 48 additions and 19 deletions

View File

@ -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,

View File

@ -14,7 +14,7 @@
<div class="sales">
<h5>Sales</h5>
<small>Today</small>
<h3>${{todays_sales|floatformat:2}}</h3>
<h3>${{todays_sales|default_if_none:"0"|floatformat:2}}</h3>
</div>
</section>

View File

@ -4,7 +4,7 @@
{% block content %}
<article>
<header class="object__header">
<h1><img src="{% static "images/cubes.png" %}" alt=""> Product: {{product.name}}</h1>
<h1><img src="{% static "images/cubes.png" %}" alt=""> Product</h1>
<a href="edit" class="action-button">Edit</a>
</header>
<section class="product__detail">
@ -15,8 +15,9 @@
<h1>{{product.name}}</h1>
<p>{{product.description}}</p>
<p>$<strong>{{product.price}}</strong></p>
<p>{{product.weight.oz}} oz</p>
<p>Visible in listings: <strong>{{product.visible_in_listings|yesno:"Yes,No"}}</strong></p>
<p>Ordered {{num_ordered}} times.</p>
<p>Ordered {{product.num_ordered|default_if_none:"0"}} time{{ product.num_ordered|pluralize }}.</p>
</div>
</section>
</article>

View File

@ -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):

View File

@ -192,7 +192,7 @@ img {
.product__item {
display: grid;
grid-template-columns: repeat(2, 1fr);
grid-template-columns: 2fr 1fr;
gap: 0 2rem;
}

View File

@ -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,

View File

@ -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'),

View File

@ -10,6 +10,7 @@
<h1>{{product.name}}</h1>
<p>{{product.description}}</p>
<p>$<strong>{{product.price}}</strong></p>
<p>{{product.weight.oz}} oz</p>
<form method="post" action="{% url 'storefront:cart-add' product.pk %}">
{% csrf_token %}
{{ form.as_p }}

View File

@ -35,6 +35,8 @@ class CartView(TemplateView):
item['update_quantity_form'] = AddToCartForm(
initial={
'quantity': item['quantity'],
'roast': item['roast'],
'other': item['other'],
'update': True
}
)