From 0dd19b2d5b3a2cd571e04dc86c5b873540d44311 Mon Sep 17 00:00:00 2001 From: Nathan Chapman Date: Sat, 5 Nov 2022 11:27:48 -0600 Subject: [PATCH] Add cart stock limits --- src/core/models.py | 26 ++++++++++++++++++- src/dashboard/templates/dashboard/config.html | 12 +++++++++ .../templates/dashboard/order_detail.html | 2 +- src/dashboard/urls.py | 5 ++++ .../templates/storefront/cart_detail.html | 1 + 5 files changed, 44 insertions(+), 2 deletions(-) diff --git a/src/core/models.py b/src/core/models.py index a9a32d4..54eaebd 100644 --- a/src/core/models.py +++ b/src/core/models.py @@ -71,6 +71,14 @@ class ProductCategory(models.Model): verbose_name_plural = 'Product Categories' +class ProductManager(models.Manager): + def get_queryset(self): + return super().get_queryset().prefetch_related( + 'productphoto_set', + 'options' + ) + + class Product(models.Model): category = models.ForeignKey( ProductCategory, @@ -92,6 +100,8 @@ class Product(models.Model): created_at = models.DateTimeField(auto_now_add=True) updated_at = models.DateTimeField(auto_now=True) + objects = ProductManager() + def __str__(self): return self.name @@ -124,6 +134,12 @@ class ProductVariant(models.Model): on_delete=models.CASCADE, related_name='variants' ) + image = models.ForeignKey( + 'ProductPhoto', + on_delete=models.SET_NULL, + related_name='+', + null=True + ) name = models.CharField(max_length=255) sku = models.CharField(max_length=255, unique=True) stripe_id = models.CharField(max_length=255, blank=True) @@ -145,6 +161,7 @@ class ProductVariant(models.Model): null=True, validators=[MinValueValidator(0)] ) + sorting = models.PositiveIntegerField(blank=True, null=True) created_at = models.DateTimeField(auto_now_add=True) updated_at = models.DateTimeField(auto_now=True) @@ -155,7 +172,7 @@ class ProductVariant(models.Model): return f'{self.product}: {self.name}' class Meta: - ordering = ['weight'] + ordering = ['sorting', 'weight'] class ProductOption(models.Model): @@ -491,6 +508,13 @@ class SiteSettings(SingletonBase): related_name='+', on_delete=models.SET_NULL ) + free_shipping_min = models.DecimalField( + max_digits=settings.DEFAULT_MAX_DIGITS, + decimal_places=settings.DEFAULT_DECIMAL_PLACES, + blank=True, + null=True, + help_text='Minimum dollar amount in the cart subtotal to qualify for free shipping' + ) def __str__(self): return 'Site Settings' diff --git a/src/dashboard/templates/dashboard/config.html b/src/dashboard/templates/dashboard/config.html index 5e56eb0..70bf292 100644 --- a/src/dashboard/templates/dashboard/config.html +++ b/src/dashboard/templates/dashboard/config.html @@ -23,5 +23,17 @@ {% endfor %} + +
+
+

Site Settings

+ Edit +
+
+

USPS User ID: {{ site_settings.usps_user_id }}

+

Default shipping rate: {{ site_settings.default_shipping_rate }}

+

Free shipping min: ${{ site_settings.free_shipping_min }}

+
+
{% endblock %} diff --git a/src/dashboard/templates/dashboard/order_detail.html b/src/dashboard/templates/dashboard/order_detail.html index c530b70..6cd9a93 100644 --- a/src/dashboard/templates/dashboard/order_detail.html +++ b/src/dashboard/templates/dashboard/order_detail.html @@ -30,7 +30,7 @@ {{product.sku}} {{item.quantity}} - ${{item.variant.price}} + ${{item.unit_price}} ${{item.get_total}} {% endwith %} diff --git a/src/dashboard/urls.py b/src/dashboard/urls.py index dc2d70c..2b0b180 100644 --- a/src/dashboard/urls.py +++ b/src/dashboard/urls.py @@ -12,6 +12,11 @@ urlpatterns = [ views.DashboardConfigView.as_view(), name='config' ), + path( + 'settings//update/', + views.SiteSettingsUpdateView.as_view(), + name='settings-update' + ), path( 'catalog/', views.CatalogView.as_view(), diff --git a/src/storefront/templates/storefront/cart_detail.html b/src/storefront/templates/storefront/cart_detail.html index 9ff2cc8..f5e0451 100644 --- a/src/storefront/templates/storefront/cart_detail.html +++ b/src/storefront/templates/storefront/cart_detail.html @@ -54,6 +54,7 @@

+
Free shipping on orders over ${{ site_settings.free_shipping_min|floatformat:"2" }}