aboutsummaryrefslogtreecommitdiffstats
path: root/pyblackbird_cc/payments/signals.py
blob: 24fc1d804a749cad77fca0811bd97eff52dfb245 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
from datetime import timedelta

from allauth.account.signals import user_signed_up
from django.db import transaction
from django.dispatch import receiver
from django.utils import timezone

from .models import ShoppingCart, Subscription
from .models import SubscriptionPlan


@receiver(user_signed_up)
def assign_default_subscription(sender, request, user, **kwargs):
    with transaction.atomic():
        # Get or create the free plan subscription
        free_plan, _ = SubscriptionPlan.objects.get_or_create(
            name="Free Plan",
            defaults={
                "price": 0,
                "description": "Free plan description",
                "allowed_downloads": 10,
            },
        )

        # Create a SubscriptionPlan for the new user
        Subscription.objects.create(
            user=user,
            plan=free_plan,
            is_active=True,
            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)