mirror of
https://github.com/i701/sarlink-portal-api.git
synced 2025-07-07 12:16:30 +00:00
60 lines
1.8 KiB
Python
60 lines
1.8 KiB
Python
# billing/management/commands/seed_billing.py
|
|
|
|
import random
|
|
from django.core.management.base import BaseCommand
|
|
from django.utils import timezone
|
|
from faker import Faker
|
|
from billing.models import Topup
|
|
from api.models import User
|
|
|
|
|
|
class Command(BaseCommand):
|
|
help = "Seeds topup models with dummy data."
|
|
|
|
def add_arguments(self, parser):
|
|
parser.add_argument(
|
|
"--number",
|
|
type=int,
|
|
default=10,
|
|
help="The number of topups to create.",
|
|
)
|
|
|
|
def handle(self, *args, **options):
|
|
number = options["number"]
|
|
fake = Faker()
|
|
|
|
users = User.objects.all()
|
|
if not users.exists():
|
|
self.stdout.write(
|
|
self.style.ERROR(
|
|
"No users found. Please seed users first (e.g., python manage.py seed_users)."
|
|
)
|
|
)
|
|
return
|
|
|
|
self.stdout.write(self.style.NOTICE(f"Seeding {number} topups..."))
|
|
|
|
for _ in range(number):
|
|
random_user = random.choice(users)
|
|
|
|
expires_at_date = timezone.now() + timezone.timedelta(
|
|
minutes=10,
|
|
)
|
|
print(
|
|
f"Creating topup for user {getattr(random_user, 'id', None)} expires at: {expires_at_date}"
|
|
)
|
|
Topup.objects.create(
|
|
amount=fake.pydecimal(
|
|
left_digits=4,
|
|
right_digits=2,
|
|
positive=True,
|
|
min_value=100.00,
|
|
max_value=5000.00,
|
|
),
|
|
user=random_user,
|
|
updated_at=timezone.now(),
|
|
expires_at=expires_at_date,
|
|
)
|
|
|
|
self.stdout.write(self.style.SUCCESS(f"Successfully seeded {number} topups."))
|