120 lines
3.8 KiB
Python
120 lines
3.8 KiB
Python
# management/commands/create_plans.py
|
|
from django.core.management.base import BaseCommand
|
|
from plans.models import Plan, Quota, Pricing, PlanPricing
|
|
from decimal import Decimal
|
|
from django.db.models import Q
|
|
|
|
|
|
class Command(BaseCommand):
|
|
help = "Create basic subscription plans structure"
|
|
|
|
def add_arguments(self, parser):
|
|
parser.add_argument(
|
|
"--reset",
|
|
action="store_true",
|
|
help="Delete existing plans and quotas before creating new ones",
|
|
)
|
|
|
|
def handle(self, *args, **options):
|
|
if options["reset"]:
|
|
self.stdout.write(self.style.WARNING("Resetting existing plans data..."))
|
|
Plan.objects.all().delete()
|
|
Quota.objects.filter(
|
|
Q(codename="basic") | Q(codename="pro") | Q(codename="premium")
|
|
).delete()
|
|
|
|
# Ensure no existing plans are marked as default
|
|
|
|
Plan.objects.all().delete()
|
|
Quota.objects.all().delete()
|
|
Pricing.objects.all().delete()
|
|
|
|
# Create core quotas
|
|
basic_quota, _ = Quota.objects.update_or_create(
|
|
codename="basic",
|
|
defaults={
|
|
"name": "Basic Features",
|
|
"description": "Essential platform access",
|
|
"is_boolean": True,
|
|
},
|
|
)
|
|
|
|
pro_quota, _ = Quota.objects.update_or_create(
|
|
codename="pro",
|
|
defaults={
|
|
"name": "Pro Features",
|
|
"description": "Advanced functionality",
|
|
"is_boolean": True,
|
|
},
|
|
)
|
|
|
|
premium_quota, _ = Quota.objects.update_or_create(
|
|
codename="premium",
|
|
defaults={
|
|
"name": "Premium Features",
|
|
"description": "Full platform access",
|
|
"is_boolean": True,
|
|
},
|
|
)
|
|
|
|
# Create pricing period
|
|
monthly_pricing, _ = Pricing.objects.update_or_create(
|
|
name="Monthly", defaults={"period": 30}
|
|
)
|
|
|
|
# Define plan structure
|
|
plans = [
|
|
{
|
|
"name": "Basic",
|
|
"description": "Entry-level plan",
|
|
"price": Decimal("0.00"),
|
|
"period": None,
|
|
"quotas": [basic_quota],
|
|
"default": True,
|
|
},
|
|
{
|
|
"name": "Pro",
|
|
"description": "Professional plan",
|
|
"price": Decimal("29.00"),
|
|
"period": 30,
|
|
"quotas": [basic_quota, pro_quota],
|
|
"default": False,
|
|
},
|
|
{
|
|
"name": "Premium",
|
|
"description": "Full access plan",
|
|
"price": Decimal("99.00"),
|
|
"period": 30,
|
|
"quotas": [basic_quota, pro_quota, premium_quota],
|
|
"default": None,
|
|
},
|
|
]
|
|
|
|
# Create plans and associations
|
|
for plan_data in plans:
|
|
plan, created = Plan.objects.update_or_create(
|
|
name=plan_data["name"],
|
|
defaults={
|
|
"description": plan_data["description"],
|
|
"default": plan_data.get("default", False),
|
|
"available": True,
|
|
"visible": True,
|
|
},
|
|
)
|
|
|
|
# Set quotas
|
|
plan.quotas.set(plan_data["quotas"])
|
|
|
|
# Create pricing if applicable
|
|
if plan_data["price"] > 0:
|
|
PlanPricing.objects.update_or_create(
|
|
plan=plan,
|
|
pricing=monthly_pricing,
|
|
defaults={"price": plan_data["price"], "visible": True},
|
|
)
|
|
|
|
status = "Created" if created else "Updated"
|
|
self.stdout.write(self.style.SUCCESS(f"{status} {plan.name} plan"))
|
|
|
|
self.stdout.write(self.style.SUCCESS("Successfully created plans structure"))
|