diff options
author | Matthew Lemon <y@yulqen.org> | 2024-09-14 15:54:21 +0100 |
---|---|---|
committer | Matthew Lemon <y@yulqen.org> | 2024-09-14 15:54:21 +0100 |
commit | f312ec6d4a577e71d76cf85632a198b0c1913703 (patch) | |
tree | eccd268d4432c32b9fee6665e0bbba87967b6fef | |
parent | d2abe12612108b94135dcb0701614dc216fb92d2 (diff) |
wip: adding shopping card functionality
-rw-r--r-- | pyblackbird_cc/conftest.py | 6 | ||||
-rw-r--r-- | pyblackbird_cc/payments/migrations/0007_remove_cartitem_quantity_and_more.py | 23 | ||||
-rw-r--r-- | pyblackbird_cc/payments/models.py | 14 | ||||
-rw-r--r-- | pyblackbird_cc/payments/signals.py | 9 | ||||
-rw-r--r-- | pyblackbird_cc/payments/tests/test_models.py | 24 |
5 files changed, 69 insertions, 7 deletions
diff --git a/pyblackbird_cc/conftest.py b/pyblackbird_cc/conftest.py index 966a3e5..ef8b62a 100644 --- a/pyblackbird_cc/conftest.py +++ b/pyblackbird_cc/conftest.py @@ -1,5 +1,6 @@ import pytest +from pyblackbird_cc.resources.factories import ResourceModelFactory from pyblackbird_cc.users.models import User from pyblackbird_cc.users.tests.factories import UserFactory @@ -12,3 +13,8 @@ def _media_storage(settings, tmpdir) -> None: @pytest.fixture() def user(db) -> User: return UserFactory() + + +@pytest.fixture() +def resource(): + return ResourceModelFactory() diff --git a/pyblackbird_cc/payments/migrations/0007_remove_cartitem_quantity_and_more.py b/pyblackbird_cc/payments/migrations/0007_remove_cartitem_quantity_and_more.py new file mode 100644 index 0000000..e976059 --- /dev/null +++ b/pyblackbird_cc/payments/migrations/0007_remove_cartitem_quantity_and_more.py @@ -0,0 +1,23 @@ +# Generated by Django 5.0.4 on 2024-09-14 14:40 + +from django.conf import settings +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('payments', '0006_subscription_plan'), + migrations.swappable_dependency(settings.AUTH_USER_MODEL), + ] + + operations = [ + migrations.RemoveField( + model_name='cartitem', + name='quantity', + ), + migrations.AddConstraint( + model_name='subscription', + constraint=models.UniqueConstraint(fields=('user', 'plan'), name='unique_user_plan'), + ), + ] diff --git a/pyblackbird_cc/payments/models.py b/pyblackbird_cc/payments/models.py index 174e3fe..81bb1c8 100644 --- a/pyblackbird_cc/payments/models.py +++ b/pyblackbird_cc/payments/models.py @@ -26,21 +26,25 @@ class ShoppingCart(models.Model): def __str__(self): return f"Shopping Cart for {self.user.username}" + def add_resource(self, resource: Resource): + item, created = CartItem.objects.get_or_create( + cart=self, + resource=resource, + ) + if not created: + item.save() + class CartItem(models.Model): cart = models.ForeignKey(ShoppingCart, on_delete=models.CASCADE, related_name="items") resource = models.ForeignKey(Resource, on_delete=models.CASCADE) - quantity = models.PositiveIntegerField(default=1) added_at = models.DateTimeField(auto_now_add=True) class Meta: unique_together = ("cart", "resource") def __str__(self): - return f"{self.quantity} of {self.resource.name} in {self.cart.user.username}'s cart" - - def get_total_price(self): - return self.quantity * self.resource.price + return f"{self.resource.name} in {self.cart.user.username}'s cart" class SubscriptionPlan(models.Model): diff --git a/pyblackbird_cc/payments/signals.py b/pyblackbird_cc/payments/signals.py index e660440..24fc1d8 100644 --- a/pyblackbird_cc/payments/signals.py +++ b/pyblackbird_cc/payments/signals.py @@ -5,7 +5,7 @@ from django.db import transaction from django.dispatch import receiver from django.utils import timezone -from .models import Subscription +from .models import ShoppingCart, Subscription from .models import SubscriptionPlan @@ -30,3 +30,10 @@ def assign_default_subscription(sender, request, user, **kwargs): start_date=timezone.now(), end_date=timezone.now() + timedelta(days=365), # Example: 30 days ) + + +@receiver(user_signed_up) +def assign_user_a_shopping_cart(sender, request, user, **kwargs): + with transaction.atomic(): + # Create a ShoppingCart for the new user + ShoppingCart.objects.create(user=user) diff --git a/pyblackbird_cc/payments/tests/test_models.py b/pyblackbird_cc/payments/tests/test_models.py index 65e7484..fd26fea 100644 --- a/pyblackbird_cc/payments/tests/test_models.py +++ b/pyblackbird_cc/payments/tests/test_models.py @@ -4,7 +4,7 @@ from django.contrib.auth import get_user_model from django.db import IntegrityError from django.test import RequestFactory -from pyblackbird_cc.payments.models import Subscription +from pyblackbird_cc.payments.models import ShoppingCart, Subscription from pyblackbird_cc.payments.models import SubscriptionPlan User = get_user_model() @@ -64,3 +64,25 @@ def test_user_signup_assigns_free_subscription(user_data): # Additional assertions can be added here to check other properties # of the SubscriptionPlan or Subscription as needed + + +@pytest.mark.django_db +def test_shopping_cart_is_created_when_user_is_created(user_data): + user = User.objects.create_user(**user_data) # type: ignore + request = RequestFactory().get("/") + user_signed_up.send(sender=user.__class__, request=request, user=user) + shopping_cart = ShoppingCart.objects.get(user=user) + assert shopping_cart is not None + + +# When the user adds a `Resource` to their cart, create a new `CartItem` instance and associate it with the user's `ShoppingCart` and the selected `Resource`. + + +@pytest.mark.django_db +def test_cart_item_is_created_when_resource_is_added_to_cart(user_data, resource): + user = User.objects.create_user(**user_data) # type: ignore + request = RequestFactory().get("/") + user_signed_up.send(sender=user.__class__, request=request, user=user) + users_cart = ShoppingCart.objects.get(user=user) + users_cart.add_resource(resource) + assert users_cart.items.count() == 1 # type: ignore |