Add basic query to find items

This commit is contained in:
Nathan Chapman 2023-06-19 22:40:59 -06:00
parent 027ad28394
commit 81f825c389
6 changed files with 60 additions and 1 deletions

View File

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

View File

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

View File

@ -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}'

View File

@ -27,6 +27,7 @@ class ProductVariantUpdateForm(forms.ModelForm):
'track_inventory',
'stock',
'sorting',
'order_limit',
'image'
]

View File

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

View File

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