From d1b60e9b3e84cdf2fbed75484bec442e19f92cc7 Mon Sep 17 00:00:00 2001 From: Daniel Ursache Dogariu Date: Wed, 11 Mar 2026 14:36:12 +0200 Subject: [PATCH] Improve fake/demo data generator randomization --- .../management/commands/generate_orgs.py | 13 +++---- .../commands/generate_other_causes.py | 5 ++- backend/utils/random.py | 36 +++++++++++++++++++ backend/utils/staging.py | 4 +-- 4 files changed, 47 insertions(+), 11 deletions(-) create mode 100644 backend/utils/random.py diff --git a/backend/donations/management/commands/generate_orgs.py b/backend/donations/management/commands/generate_orgs.py index fd2e87a2..1bd7d001 100644 --- a/backend/donations/management/commands/generate_orgs.py +++ b/backend/donations/management/commands/generate_orgs.py @@ -8,6 +8,7 @@ from localflavor.ro.ro_counties import COUNTIES_CHOICES from donations.models.ngos import Cause, Ngo +from utils.random import random_33p, random_75p from utils.text.cleanup import clean_slug, strip_accents fake = Faker("ro_RO") @@ -844,13 +845,13 @@ def handle(self, *args, **options): password=owner_email.split("@")[0], first_name=first_name, last_name=last_name, - is_verified=random.choice([True, False]), + is_verified=random_75p(), ) owner.save() except IntegrityError: continue - should_not_have_ngo = random.choice(range(0, 6)) == 3 + should_not_have_ngo = random_33p() if create_user_only or not create_valid and should_not_have_ngo: continue @@ -863,8 +864,8 @@ def handle(self, *args, **options): "phone": fake.phone_number(), "email": random.choice([owner_email, fake.email()]), "website": fake.url(), - "is_active": create_valid or random.choice([True, False]), - "has_online_tax_account": create_valid or random.choice([True, False]), + "is_active": create_valid or random_75p(), + "has_online_tax_account": create_valid or random_75p(), "ngohub_org_id": random.choice([random.randint(1, 9999), None]) if create_ngohub_id else None, } try: @@ -879,7 +880,7 @@ def handle(self, *args, **options): owner.ngo = org owner.save() - ignore_cause = not create_valid or random.choice(range(0, 6)) == 3 + ignore_cause = not create_valid or random_33p() if ignore_cause: continue @@ -888,7 +889,7 @@ def handle(self, *args, **options): ngo_cause = Cause.objects.create( ngo=org, is_main=True, - allow_online_collection=org.has_online_tax_account and random.choice(range(0, 6)) == 3, + allow_online_collection=org.has_online_tax_account and random_75p(), slug=kebab_case_name, name=org_name, description=fake.paragraph(nb_sentences=3, variable_nb_sentences=True), diff --git a/backend/donations/management/commands/generate_other_causes.py b/backend/donations/management/commands/generate_other_causes.py index af8137d0..b4d33ea1 100644 --- a/backend/donations/management/commands/generate_other_causes.py +++ b/backend/donations/management/commands/generate_other_causes.py @@ -5,6 +5,7 @@ from faker import Faker from donations.models.ngos import Cause, CauseVisibilityChoices, Ngo +from utils.random import random_75p fake = Faker("ro_RO") @@ -301,9 +302,7 @@ def handle(self, *args, **options): ] ) - allow_online_collection = ( - ngo.has_online_tax_account if not ngo.has_online_tax_account else random.choice(range(0, 6)) == 3 - ) + allow_online_collection = ngo.has_online_tax_account and random_75p() causes.append( Cause( ngo=ngo, diff --git a/backend/utils/random.py b/backend/utils/random.py new file mode 100644 index 00000000..1a1fcaf6 --- /dev/null +++ b/backend/utils/random.py @@ -0,0 +1,36 @@ +import random + + +def random_25p(): + """ + 25% chance of True + """ + return random.choice((True, False, False, False)) + + +def random_33p(): + """ + 33% chance of True + """ + return random.choice((True, False, False)) + + +def random_50p(): + """ + 50% chance of True + """ + return random.choice((True, False)) + + +def random_66p(): + """ + 66% chance of True + """ + return random.choice((True, True, False)) + + +def random_75p(): + """ + 75% chance of True + """ + return random.choice((True, True, True, False)) diff --git a/backend/utils/staging.py b/backend/utils/staging.py index 5ea4319e..5e8f5fc6 100644 --- a/backend/utils/staging.py +++ b/backend/utils/staging.py @@ -67,7 +67,7 @@ def reset_staging(generate_orgs_count=0, generate_causes_count=0, generate_donat logger.info("Deleted all users except the default admins") # Generate new demo organizations - management.call_command("generate_orgs", generate_orgs_count, valid=True, ngohub=False) + management.call_command("generate_orgs", generate_orgs_count, ngohub=False) logger.info("Generated %d demo organizations", generate_orgs_count) # Generate new demo causes @@ -88,7 +88,7 @@ def schedule_reset_staging(request): raise PermissionDenied logger.info("Scheduling a staging environment reset") - async_task(reset_staging, generate_orgs_count=20, generate_causes_count=25, generate_donations_count=100) + async_task(reset_staging, generate_orgs_count=25, generate_causes_count=30, generate_donations_count=150) messages.add_message( request,