diff --git a/core/migrations/0002_productvariant_max_order_per_customer.py b/core/migrations/0002_productvariant_max_order_per_customer.py new file mode 100644 index 0000000..9a2ad6b --- /dev/null +++ b/core/migrations/0002_productvariant_max_order_per_customer.py @@ -0,0 +1,18 @@ +# Generated by Django 4.1.6 on 2023-06-19 23:07 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('core', '0001_initial'), + ] + + operations = [ + migrations.AddField( + model_name='productvariant', + name='max_order_per_customer', + field=models.PositiveIntegerField(blank=True, null=True), + ), + ] diff --git a/core/migrations/0003_rename_max_order_per_customer_productvariant_order_limit.py b/core/migrations/0003_rename_max_order_per_customer_productvariant_order_limit.py new file mode 100644 index 0000000..9e95f90 --- /dev/null +++ b/core/migrations/0003_rename_max_order_per_customer_productvariant_order_limit.py @@ -0,0 +1,18 @@ +# Generated by Django 4.1.6 on 2023-06-20 02:59 + +from django.db import migrations + + +class Migration(migrations.Migration): + + dependencies = [ + ('core', '0002_productvariant_max_order_per_customer'), + ] + + operations = [ + migrations.RenameField( + model_name='productvariant', + old_name='max_order_per_customer', + new_name='order_limit', + ), + ] diff --git a/core/models.py b/core/models.py index 4235d53..7116ea8 100644 --- a/core/models.py +++ b/core/models.py @@ -192,11 +192,12 @@ class ProductVariant(models.Model): validators=[MinValueValidator(0)] ) sorting = models.PositiveIntegerField(blank=True, null=True) + order_limit = models.PositiveIntegerField(blank=True, null=True) created_at = models.DateTimeField(auto_now_add=True) updated_at = models.DateTimeField(auto_now=True) - objects = ProductVariantManager() + #objects = ProductVariantManager() def __str__(self): return f'{self.product}: {self.name}' diff --git a/dashboard/forms.py b/dashboard/forms.py index 5348f35..ae168ab 100644 --- a/dashboard/forms.py +++ b/dashboard/forms.py @@ -27,6 +27,7 @@ class ProductVariantUpdateForm(forms.ModelForm): 'track_inventory', 'stock', 'sorting', + 'order_limit', 'image' ] diff --git a/storefront/cart.py b/storefront/cart.py index 16f96f8..6f8f2d8 100644 --- a/storefront/cart.py +++ b/storefront/cart.py @@ -195,6 +195,9 @@ class Cart: self.items.append(new_item) self.save() + def get_item_by_pk(self, pk): + return next(i for i, v in enumerate(self) if v.variant.pk == pk) + def update_item_quantity(self, item_index, quantity): self.items[item_index].quantity = quantity self.save() diff --git a/storefront/views.py b/storefront/views.py index f8567b9..34c3c12 100644 --- a/storefront/views.py +++ b/storefront/views.py @@ -355,6 +355,8 @@ class OrderCreateView(CreateView): cart = Cart(request) + + if cart.coupon is not None: try: user = User.objects.get( @@ -362,6 +364,22 @@ class OrderCreateView(CreateView): ) except User.DoesNotExist: user = None + + if user: + variants_ordered = ProductVariant.objects.filter( + pk__in=cart.item_variant_pks, + order_lines__order__customer=user, + order_lines__order__status="fulfilled" + ).values("id", "order_limit").annotate( + num_ordered=Sum("order_lines__quantity") + ).order_by() + + for item in cart: + if item.quantity + + + + if user in cart.coupon.users.all(): cart.remove_coupon() messages.warning(request, 'Coupon already used.')