-
-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
134bd74
commit c374da6
Showing
4 changed files
with
104 additions
and
0 deletions.
There are no files selected for viewing
77 changes: 77 additions & 0 deletions
77
backend/donations/management/commands/generate_donations.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
import random | ||
import string | ||
from typing import Any, Dict, List | ||
|
||
from django.core.management import BaseCommand | ||
from faker import Faker | ||
from localflavor.ro.ro_counties import COUNTIES_CHOICES | ||
|
||
from donations.models.main import Donor, Ngo | ||
|
||
fake = Faker("ro_RO") | ||
|
||
|
||
class Command(BaseCommand): | ||
help = "Generate fake donation forms" | ||
|
||
def add_arguments(self, parser): | ||
parser.add_argument( | ||
"total_donations", | ||
type=int, | ||
help="How many donations to create", | ||
default=10, | ||
) | ||
parser.add_argument( | ||
"--org", | ||
type=int, | ||
help="The ID of the organization to generate donations for", | ||
default=None, | ||
) | ||
|
||
def handle(self, *args, **options): | ||
total_donations = options["total_donations"] | ||
target_org = options.get("org", None) | ||
self.stdout.write(f"Generating {total_donations} donations") | ||
|
||
# create a list of all the NGOs | ||
if not target_org: | ||
ngos = list(Ngo.objects.filter(is_active=True)) | ||
else: | ||
ngos = [Ngo.objects.get(id=target_org)] | ||
|
||
generated_donations: List[Dict[str, Any]] = [] | ||
while len(generated_donations) < total_donations: | ||
# pick a random NGO | ||
ngo = ngos[random.randint(0, len(ngos) - 1)] | ||
|
||
# generate a random donor | ||
donor = { | ||
"ngo": ngo, | ||
"first_name": fake.first_name(), | ||
"last_name": fake.last_name(), | ||
"initial": fake.first_name()[0], | ||
"cnp": fake.ssn(), | ||
"email": fake.email(), | ||
"phone": fake.phone_number(), | ||
"address": { | ||
"street": fake.street_address(), | ||
"number": fake.building_number(), | ||
"bl": random.choice(["", random.randint(1, 20)]), | ||
"sc": random.choice(["", random.choice(string.ascii_uppercase)]), | ||
"et": random.choice(["", random.randint(1, 20)]), | ||
"ap": random.choice(["", random.randint(1, 200)]), | ||
}, | ||
"city": fake.city(), | ||
"county": COUNTIES_CHOICES[random.randint(0, len(COUNTIES_CHOICES) - 1)][1], | ||
"income_type": "wage", | ||
} | ||
|
||
# generate a random donation | ||
generated_donations.append(donor) | ||
|
||
# write to the database | ||
self.stdout.write(self.style.SUCCESS("Writing to the database...")) | ||
|
||
Donor.objects.bulk_create([Donor(**donation) for donation in generated_donations]) | ||
|
||
self.stdout.write(self.style.SUCCESS("Done!")) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
# Generated by Django 4.2.9 on 2024-02-01 14:39 | ||
|
||
from django.db import migrations, models | ||
|
||
|
||
class Migration(migrations.Migration): | ||
|
||
dependencies = [ | ||
("donations", "0006_alter_donor_pdf_file_alter_ngo_prefilled_form"), | ||
] | ||
|
||
operations = [ | ||
migrations.AddField( | ||
model_name="donor", | ||
name="address", | ||
field=models.JSONField(blank=True, default=dict, verbose_name="address"), | ||
), | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters