124 lines
4.5 KiB
Python
124 lines
4.5 KiB
Python
# management/commands/create_plans.py
|
|
from decimal import Decimal
|
|
from django.core.management.base import BaseCommand
|
|
from plans.models import Plan, Quota, PlanQuota, Pricing, PlanPricing
|
|
|
|
|
|
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 all plan-related data..."))
|
|
Plan.objects.all().delete()
|
|
Quota.objects.all().delete()
|
|
Pricing.objects.all().delete()
|
|
# Note: Deleting plans and quotas should cascade to related objects like PlanQuota and PlanPricing.
|
|
self.stdout.write(self.style.SUCCESS("Data reset complete."))
|
|
else:
|
|
self.stdout.write(
|
|
self.style.NOTICE("Creating or updating default plans and quotas...")
|
|
)
|
|
|
|
# Create or get quotas
|
|
users_quota, created_u = Quota.objects.get_or_create(
|
|
codename="Users", defaults={"name": "Users", "unit": "number"}
|
|
)
|
|
if created_u:
|
|
self.stdout.write(self.style.SUCCESS('Created quota: "Users"'))
|
|
|
|
cars_quota, created_c = Quota.objects.get_or_create(
|
|
codename="Cars", defaults={"name": "Cars", "unit": "number"}
|
|
)
|
|
if created_c:
|
|
self.stdout.write(self.style.SUCCESS('Created quota: "Cars"'))
|
|
|
|
# Create or get plans
|
|
basic_plan, created_bp = Plan.objects.get_or_create(
|
|
name="Basic",
|
|
defaults={"description": "basic plan", "available": True, "visible": True},
|
|
)
|
|
if created_bp:
|
|
self.stdout.write(self.style.SUCCESS('Created plan: "Basic"'))
|
|
|
|
pro_plan, created_pp = Plan.objects.get_or_create(
|
|
name="Pro",
|
|
defaults={"description": "Pro plan", "available": True, "visible": True},
|
|
)
|
|
if created_pp:
|
|
self.stdout.write(self.style.SUCCESS('Created plan: "Pro"'))
|
|
|
|
enterprise_plan, created_ep = Plan.objects.get_or_create(
|
|
name="Enterprise",
|
|
defaults={
|
|
"description": "Enterprise plan",
|
|
"available": True,
|
|
"visible": True,
|
|
},
|
|
)
|
|
if created_ep:
|
|
self.stdout.write(self.style.SUCCESS('Created plan: "Enterprise"'))
|
|
|
|
# Assign quotas to plans using get_or_create to prevent duplicates
|
|
PlanQuota.objects.get_or_create(
|
|
plan=basic_plan, quota=users_quota, defaults={"value": 10000000}
|
|
)
|
|
PlanQuota.objects.get_or_create(
|
|
plan=basic_plan, quota=cars_quota, defaults={"value": 10000000}
|
|
)
|
|
|
|
# Pro plan quotas
|
|
PlanQuota.objects.get_or_create(
|
|
plan=pro_plan, quota=users_quota, defaults={"value": 10000000}
|
|
)
|
|
PlanQuota.objects.get_or_create(
|
|
plan=pro_plan, quota=cars_quota, defaults={"value": 10000000}
|
|
)
|
|
|
|
# Enterprise plan quotas
|
|
PlanQuota.objects.get_or_create(
|
|
plan=enterprise_plan, quota=users_quota, defaults={"value": 10000000}
|
|
)
|
|
PlanQuota.objects.get_or_create(
|
|
plan=enterprise_plan, quota=cars_quota, defaults={"value": 10000000}
|
|
)
|
|
|
|
# Create or get pricing
|
|
basic_pricing, created_bp_p = Pricing.objects.get_or_create(
|
|
name="3 Months", defaults={"period": 90}
|
|
)
|
|
pro_pricing, created_pp_p = Pricing.objects.get_or_create(
|
|
name="6 Months", defaults={"period": 180}
|
|
)
|
|
enterprise_pricing, created_ep_p = Pricing.objects.get_or_create(
|
|
name="1 Year", defaults={"period": 365}
|
|
)
|
|
|
|
# Assign pricing to plans
|
|
PlanPricing.objects.get_or_create(
|
|
plan=basic_plan,
|
|
pricing=basic_pricing,
|
|
defaults={"price": Decimal("2997.00")},
|
|
)
|
|
PlanPricing.objects.get_or_create(
|
|
plan=pro_plan, pricing=pro_pricing, defaults={"price": Decimal("5395.00")}
|
|
)
|
|
PlanPricing.objects.get_or_create(
|
|
plan=enterprise_plan,
|
|
pricing=enterprise_pricing,
|
|
defaults={"price": Decimal("9590.00")},
|
|
)
|
|
|
|
self.stdout.write(
|
|
self.style.SUCCESS(
|
|
"Subscription plans structure successfully created or updated."
|
|
)
|
|
)
|