mirror of
https://github.com/i701/sarlink-portal-api.git
synced 2025-07-07 06:06:31 +00:00
feat(billing): Add management command to seed topup models with dummy data ✨
This commit is contained in:
63
billing/management/commands/seed_topups.py
Normal file
63
billing/management/commands/seed_topups.py
Normal file
@ -0,0 +1,63 @@
|
||||
# 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 (
|
||||
Payment,
|
||||
)
|
||||
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)
|
||||
|
||||
paid_status = fake.boolean(chance_of_getting_true=80)
|
||||
paid_at_date = fake.date_time_this_year() if paid_status else None
|
||||
|
||||
if paid_at_date and timezone.is_naive(paid_at_date):
|
||||
paid_at_date = timezone.make_aware(paid_at_date)
|
||||
|
||||
Topup.objects.create(
|
||||
amount=fake.pydecimal(
|
||||
left_digits=4,
|
||||
right_digits=2,
|
||||
positive=True,
|
||||
min_value=100.00,
|
||||
max_value=5000.00,
|
||||
),
|
||||
paid=paid_status,
|
||||
user=random_user,
|
||||
paid_at=paid_at_date,
|
||||
updated_at=timezone.now(),
|
||||
)
|
||||
|
||||
self.stdout.write(self.style.SUCCESS(f"Successfully seeded {number} topups."))
|
Reference in New Issue
Block a user