mirror of
https://github.com/Crocmagnon/checkout.git
synced 2024-11-22 16:18:03 +01:00
Add unique constraint to baskets
This commit is contained in:
parent
91d64c6ce4
commit
8d51981368
3 changed files with 39 additions and 4 deletions
|
@ -2,6 +2,7 @@ import random
|
||||||
from datetime import timedelta
|
from datetime import timedelta
|
||||||
|
|
||||||
import freezegun
|
import freezegun
|
||||||
|
import numpy as np
|
||||||
from django.core.management import call_command
|
from django.core.management import call_command
|
||||||
from django.core.management.base import BaseCommand
|
from django.core.management.base import BaseCommand
|
||||||
from django.utils.timezone import now
|
from django.utils.timezone import now
|
||||||
|
@ -39,10 +40,19 @@ class Command(BaseCommand):
|
||||||
if random.random() < 0.99:
|
if random.random() < 0.99:
|
||||||
method = random.choices(payment_methods, weights=methods_weights)[0]
|
method = random.choices(payment_methods, weights=methods_weights)[0]
|
||||||
basket = Basket.objects.create(payment_method=method)
|
basket = Basket.objects.create(payment_method=method)
|
||||||
|
items_in_basket = int(random.normalvariate(3, 2))
|
||||||
|
if items_in_basket > len(products):
|
||||||
|
items_in_basket = len(products)
|
||||||
|
if items_in_basket < 1:
|
||||||
|
items_in_basket = 1
|
||||||
|
selected_products = np.random.choice(
|
||||||
|
products,
|
||||||
|
size=items_in_basket,
|
||||||
|
replace=False,
|
||||||
|
p=np.asarray(products_weights) / sum(products_weights),
|
||||||
|
)
|
||||||
items = []
|
items = []
|
||||||
item_count = int(random.normalvariate(3, 2))
|
for product in selected_products:
|
||||||
for _ in range(item_count):
|
|
||||||
product: Product = random.choices(products, weights=products_weights)[0]
|
|
||||||
items.append(
|
items.append(
|
||||||
BasketItem(
|
BasketItem(
|
||||||
product=product,
|
product=product,
|
||||||
|
|
|
@ -0,0 +1,22 @@
|
||||||
|
# Generated by Django 4.0.4 on 2022-05-05 16:46
|
||||||
|
|
||||||
|
import django.db.models.expressions
|
||||||
|
from django.db import migrations, models
|
||||||
|
|
||||||
|
|
||||||
|
class Migration(migrations.Migration):
|
||||||
|
|
||||||
|
dependencies = [
|
||||||
|
("purchase", "0007_alter_basketitem_unit_price_cents"),
|
||||||
|
]
|
||||||
|
|
||||||
|
operations = [
|
||||||
|
migrations.AddConstraint(
|
||||||
|
model_name="basketitem",
|
||||||
|
constraint=models.UniqueConstraint(
|
||||||
|
django.db.models.expressions.F("product"),
|
||||||
|
django.db.models.expressions.F("basket"),
|
||||||
|
name="unique_product_per_basket",
|
||||||
|
),
|
||||||
|
),
|
||||||
|
]
|
|
@ -3,7 +3,7 @@ from __future__ import annotations
|
||||||
import hashlib
|
import hashlib
|
||||||
|
|
||||||
from django.db import models
|
from django.db import models
|
||||||
from django.db.models import Avg, Count, F, Sum
|
from django.db.models import Avg, Count, F, Sum, UniqueConstraint
|
||||||
from django.db.models.functions import Coalesce
|
from django.db.models.functions import Coalesce
|
||||||
from django.urls import reverse
|
from django.urls import reverse
|
||||||
from django.utils.translation import gettext
|
from django.utils.translation import gettext
|
||||||
|
@ -217,3 +217,6 @@ class BasketItem(Model):
|
||||||
class Meta:
|
class Meta:
|
||||||
verbose_name = _("basket item")
|
verbose_name = _("basket item")
|
||||||
verbose_name_plural = _("basket items")
|
verbose_name_plural = _("basket items")
|
||||||
|
constraints = [
|
||||||
|
UniqueConstraint("product", "basket", name="unique_product_per_basket")
|
||||||
|
]
|
||||||
|
|
Loading…
Reference in a new issue