Merge branch 'release/0.5.0'

This commit is contained in:
Nathan Chapman 2022-03-14 08:58:12 -06:00
commit 8ad80e751b
9 changed files with 48 additions and 19 deletions

View File

@ -1,5 +1,6 @@
import logging import logging
from decimal import Decimal from decimal import Decimal
from PIL import Image
from measurement.measures import Weight from measurement.measures import Weight
from django.db import models from django.db import models
@ -28,7 +29,7 @@ logger = logging.getLogger(__name__)
class ProductManager(models.Manager): class ProductManager(models.Manager):
def get_queryset(self): def get_queryset(self):
return super().get_queryset().annotate( 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): def __str__(self):
return self.product.name 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) 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): class Order(models.Model):
@ -155,12 +173,15 @@ class Order(models.Model):
created_at = models.DateTimeField(auto_now_add=True, editable=False) created_at = models.DateTimeField(auto_now_add=True, editable=False)
updated_at = models.DateTimeField(auto_now=True) updated_at = models.DateTimeField(auto_now=True)
objects = OrderManager()
def get_total_quantity(self): def get_total_quantity(self):
return sum([line.quantity for line in self]) return sum([line.quantity for line in self])
def get_absolute_url(self): def get_absolute_url(self):
return reverse('dashboard:order-detail', kwargs={'pk': self.pk}) return reverse('dashboard:order-detail', kwargs={'pk': self.pk})
class Transaction(models.Model): class Transaction(models.Model):
status = models.CharField( status = models.CharField(
max_length=32, max_length=32,

View File

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

View File

@ -4,7 +4,7 @@
{% block content %} {% block content %}
<article> <article>
<header class="object__header"> <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> <a href="edit" class="action-button">Edit</a>
</header> </header>
<section class="product__detail"> <section class="product__detail">
@ -15,8 +15,9 @@
<h1>{{product.name}}</h1> <h1>{{product.name}}</h1>
<p>{{product.description}}</p> <p>{{product.description}}</p>
<p>$<strong>{{product.price}}</strong></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>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> </div>
</section> </section>
</article> </article>

View File

@ -42,7 +42,7 @@ class DashboardHomeView(TemplateView):
).count() ).count()
context['todays_sales'] = Order.objects.filter( context['todays_sales'] = Order.objects.filter(
created_at__date=today created_at__date=today
).aggregate(total=Sum('total_net_amount'))['total'] or 0 ).aggregate(total=Sum('total_net_amount'))['total']
return context return context
class OrderListView(ListView): class OrderListView(ListView):

View File

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

View File

@ -13,16 +13,20 @@ class Cart:
cart = self.session[settings.CART_SESSION_ID] = {} cart = self.session[settings.CART_SESSION_ID] = {}
self.cart = cart 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) product_id = str(product.id)
if product_id not in self.cart: if product_id not in self.cart:
self.cart[product_id] = { self.cart[product_id] = {
'quantity': 0, 'quantity': 0,
'roast': roast, 'roast': roast,
'other': other, 'other': other,
'customer_note': customer_note,
'price': str(product.price) 'price': str(product.price)
} }
elif product_id in self.cart:
self.cart[product_id].update({
'roast': roast,
'other': other,
})
if update_quantity: if update_quantity:
self.cart[product_id]['quantity'] = quantity self.cart[product_id]['quantity'] = quantity
else: else:
@ -32,7 +36,7 @@ class Cart:
def save(self): def save(self):
self.session[settings.CART_SESSION_ID] = self.cart self.session[settings.CART_SESSION_ID] = self.cart
self.session.modified = True 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): def remove(self, product):
product_id = str(product.id) product_id = str(product.id)
@ -102,7 +106,7 @@ class Cart:
OrderLine( OrderLine(
order=order, order=order,
product=item['product'], product=item['product'],
customer_note=item['customer_note'], customer_note=f'{item["roast"]} {item["other"]}',
unit_price=item['price'], unit_price=item['price'],
quantity=item['quantity'], quantity=item['quantity'],
tax_rate=2, tax_rate=2,

View File

@ -8,15 +8,15 @@ from accounts import STATE_CHOICES
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
class AddToCartForm(forms.Form): class AddToCartForm(forms.Form):
WHOLE = 'WHOLE' WHOLE = 'Whole Beans'
ESPRESSO = 'ESPRESSO' ESPRESSO = 'Espresso'
CONE_DRIP = 'CONE_DRIP' CONE_DRIP = 'Cone Drip'
BASKET_DRIP = 'BASKET_DRIP' BASKET_DRIP = 'Basket Drip'
FRENCH_PRESS = 'FRENCH_PRESS' FRENCH_PRESS = 'French Press'
STOVETOP_ESPRESSO = 'STOVETOP_ESPRESSO' STOVETOP_ESPRESSO = 'Stovetop Espresso (Moka Pot)'
AEROPRESS = 'AEROPRESS' AEROPRESS = 'AeroPress'
PERCOLATOR = 'PERCOLATOR' PERCOLATOR = 'Percolator'
OTHER = 'OTHER' OTHER = 'Other'
ROAST_CHOICES = [ ROAST_CHOICES = [
(WHOLE, 'Whole Beans'), (WHOLE, 'Whole Beans'),
(ESPRESSO, 'Espresso'), (ESPRESSO, 'Espresso'),

View File

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

View File

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