diff options
Diffstat (limited to 'alphabetlearning/payments/signals.py')
-rw-r--r-- | alphabetlearning/payments/signals.py | 40 |
1 files changed, 40 insertions, 0 deletions
diff --git a/alphabetlearning/payments/signals.py b/alphabetlearning/payments/signals.py new file mode 100644 index 0000000..50d988b --- /dev/null +++ b/alphabetlearning/payments/signals.py @@ -0,0 +1,40 @@ +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 +from .models import 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) |