Add basic query to find items
This commit is contained in:
parent
027ad28394
commit
81f825c389
@ -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),
|
||||||
|
),
|
||||||
|
]
|
||||||
@ -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',
|
||||||
|
),
|
||||||
|
]
|
||||||
@ -192,11 +192,12 @@ class ProductVariant(models.Model):
|
|||||||
validators=[MinValueValidator(0)]
|
validators=[MinValueValidator(0)]
|
||||||
)
|
)
|
||||||
sorting = models.PositiveIntegerField(blank=True, null=True)
|
sorting = models.PositiveIntegerField(blank=True, null=True)
|
||||||
|
order_limit = models.PositiveIntegerField(blank=True, null=True)
|
||||||
|
|
||||||
created_at = models.DateTimeField(auto_now_add=True)
|
created_at = models.DateTimeField(auto_now_add=True)
|
||||||
updated_at = models.DateTimeField(auto_now=True)
|
updated_at = models.DateTimeField(auto_now=True)
|
||||||
|
|
||||||
objects = ProductVariantManager()
|
#objects = ProductVariantManager()
|
||||||
|
|
||||||
def __str__(self):
|
def __str__(self):
|
||||||
return f'{self.product}: {self.name}'
|
return f'{self.product}: {self.name}'
|
||||||
|
|||||||
@ -27,6 +27,7 @@ class ProductVariantUpdateForm(forms.ModelForm):
|
|||||||
'track_inventory',
|
'track_inventory',
|
||||||
'stock',
|
'stock',
|
||||||
'sorting',
|
'sorting',
|
||||||
|
'order_limit',
|
||||||
'image'
|
'image'
|
||||||
]
|
]
|
||||||
|
|
||||||
|
|||||||
@ -195,6 +195,9 @@ class Cart:
|
|||||||
self.items.append(new_item)
|
self.items.append(new_item)
|
||||||
self.save()
|
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):
|
def update_item_quantity(self, item_index, quantity):
|
||||||
self.items[item_index].quantity = quantity
|
self.items[item_index].quantity = quantity
|
||||||
self.save()
|
self.save()
|
||||||
|
|||||||
@ -355,6 +355,8 @@ class OrderCreateView(CreateView):
|
|||||||
|
|
||||||
cart = Cart(request)
|
cart = Cart(request)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
if cart.coupon is not None:
|
if cart.coupon is not None:
|
||||||
try:
|
try:
|
||||||
user = User.objects.get(
|
user = User.objects.get(
|
||||||
@ -362,6 +364,22 @@ class OrderCreateView(CreateView):
|
|||||||
)
|
)
|
||||||
except User.DoesNotExist:
|
except User.DoesNotExist:
|
||||||
user = None
|
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():
|
if user in cart.coupon.users.all():
|
||||||
cart.remove_coupon()
|
cart.remove_coupon()
|
||||||
messages.warning(request, 'Coupon already used.')
|
messages.warning(request, 'Coupon already used.')
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user