diff --git a/apps/baskets/__init__.py b/apps/baskets/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/apps/baskets/admin.py b/apps/baskets/admin.py new file mode 100644 index 0000000..0f5dfa4 --- /dev/null +++ b/apps/baskets/admin.py @@ -0,0 +1,17 @@ +from django.contrib import admin + +from apps.baskets.models import Basket, BasketProduct + + +class BasketProductInline(admin.TabularInline): + model = BasketProduct + extra = 1 + + +@admin.register(Basket) +class BasketAdmin(admin.ModelAdmin): + list_display = ("user",) + list_filter = ("user",) + search_fields = ("user",) + empty_value_display = "-empty-" + inlines = (BasketProductInline,) diff --git a/apps/baskets/apps.py b/apps/baskets/apps.py new file mode 100644 index 0000000..17029ef --- /dev/null +++ b/apps/baskets/apps.py @@ -0,0 +1,6 @@ +from django.apps import AppConfig + + +class BasketsConfig(AppConfig): + default_auto_field = "django.db.models.BigAutoField" + name = "apps.baskets" diff --git a/apps/baskets/migrations/0001_initial.py b/apps/baskets/migrations/0001_initial.py new file mode 100644 index 0000000..59a2df6 --- /dev/null +++ b/apps/baskets/migrations/0001_initial.py @@ -0,0 +1,84 @@ +# Generated by Django 4.1 on 2023-08-11 19:26 + +import django.db.models.deletion +from django.conf import settings +from django.db import migrations, models + + +class Migration(migrations.Migration): + initial = True + + dependencies = [ + ("products", "0001_initial"), + migrations.swappable_dependency(settings.AUTH_USER_MODEL), + ] + + operations = [ + migrations.CreateModel( + name="Basket", + fields=[ + ( + "id", + models.BigAutoField( + auto_created=True, + primary_key=True, + serialize=False, + verbose_name="ID", + ), + ), + ( + "user", + models.ForeignKey( + on_delete=django.db.models.deletion.CASCADE, + related_name="user", + to=settings.AUTH_USER_MODEL, + verbose_name="User", + ), + ), + ], + options={ + "verbose_name": "Basket", + "verbose_name_plural": "Baskets", + }, + ), + migrations.CreateModel( + name="BasketProduct", + fields=[ + ( + "id", + models.BigAutoField( + auto_created=True, + primary_key=True, + serialize=False, + verbose_name="ID", + ), + ), + ( + "quantity", + models.PositiveIntegerField(verbose_name="Product quantity"), + ), + ( + "basket", + models.OneToOneField( + on_delete=django.db.models.deletion.CASCADE, + related_name="basket", + to="baskets.basket", + verbose_name="Basket", + ), + ), + ( + "product", + models.ForeignKey( + on_delete=django.db.models.deletion.CASCADE, + related_name="product", + to="products.product", + verbose_name="Product", + ), + ), + ], + options={ + "verbose_name": "Product in the basket", + "verbose_name_plural": "Products in the basket", + }, + ), + ] diff --git a/apps/baskets/migrations/0002_basket_basket_products_alter_basketproduct_basket.py b/apps/baskets/migrations/0002_basket_basket_products_alter_basketproduct_basket.py new file mode 100644 index 0000000..bd3b9f5 --- /dev/null +++ b/apps/baskets/migrations/0002_basket_basket_products_alter_basketproduct_basket.py @@ -0,0 +1,31 @@ +# Generated by Django 4.1 on 2023-08-12 20:28 + +import django.db.models.deletion +from django.db import migrations, models + + +class Migration(migrations.Migration): + dependencies = [ + ("products", "0001_initial"), + ("baskets", "0001_initial"), + ] + + operations = [ + migrations.AddField( + model_name="basket", + name="basket_products", + field=models.ManyToManyField( + through="baskets.BasketProduct", to="products.product" + ), + ), + migrations.AlterField( + model_name="basketproduct", + name="basket", + field=models.ForeignKey( + on_delete=django.db.models.deletion.CASCADE, + related_name="basket", + to="baskets.basket", + verbose_name="Basket", + ), + ), + ] diff --git a/apps/baskets/migrations/__init__.py b/apps/baskets/migrations/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/apps/baskets/models.py b/apps/baskets/models.py new file mode 100644 index 0000000..e426c45 --- /dev/null +++ b/apps/baskets/models.py @@ -0,0 +1,36 @@ +from django.contrib.auth import get_user_model +from django.db import models +from django.utils.translation import gettext_lazy as _ + +from apps.products.models import Product + + +class Basket(models.Model): + user = models.ForeignKey( + get_user_model(), on_delete=models.CASCADE, related_name="user", verbose_name=_("User") + ) + basket_products = models.ManyToManyField(Product, through="BasketProduct") + + class Meta: + verbose_name = _("Basket") + verbose_name_plural = _("Baskets") + + def __str__(self): + return f"{self.user}" + + +class BasketProduct(models.Model): + basket = models.ForeignKey( + Basket, on_delete=models.CASCADE, related_name="basket", verbose_name=_("Basket") + ) + product = models.ForeignKey( + Product, on_delete=models.CASCADE, related_name="product", verbose_name=_("Product") + ) + quantity = models.PositiveIntegerField(verbose_name=_("Product quantity")) + + class Meta: + verbose_name = _("Product in the basket") + verbose_name_plural = _("Products in the basket") + + def __str__(self): + return f"{self.quantity} x {self.product} in Basket for User: {self.basket.user}" diff --git a/config/settings.py b/config/settings.py index ad03236..cfad3d7 100644 --- a/config/settings.py +++ b/config/settings.py @@ -39,6 +39,7 @@ "apps.products", "apps.users", "apps.orders", + "apps.baskets", ] INSTALLED_APPS = DJANGO_APPS + THIRD_PARTY_APPS + LOCAL_APPS