Skip to content

Commit

Permalink
Update donor model
Browse files Browse the repository at this point in the history
  • Loading branch information
tudoramariei committed Feb 1, 2024
1 parent 134bd74 commit c374da6
Show file tree
Hide file tree
Showing 4 changed files with 104 additions and 0 deletions.
77 changes: 77 additions & 0 deletions backend/donations/management/commands/generate_donations.py
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!"))
18 changes: 18 additions & 0 deletions backend/donations/migrations/0007_donor_address.py
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"),
),
]
1 change: 1 addition & 0 deletions backend/donations/models/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,7 @@ class Donor(models.Model):
max_length=100,
db_index=True,
)
address = models.JSONField(verbose_name=_("address"), blank=True, null=False, default=dict)

# originally: tel
phone = models.CharField(verbose_name=_("telephone"), blank=True, null=False, default="", max_length=30)
Expand Down
8 changes: 8 additions & 0 deletions backend/donations/views/ngo.py
Original file line number Diff line number Diff line change
Expand Up @@ -323,6 +323,14 @@ def get_post_value(arg, add_to_error_list=True):
last_name=donor_dict["last_name"],
city=donor_dict["city"],
county=donor_dict["county"],
address={
"street": donor_dict["street"],
"number": donor_dict["number"],
"bl": donor_dict["bl"],
"sc": donor_dict["sc"],
"et": donor_dict["et"],
"ap": donor_dict["ap"],
},
email=donor_dict["email"],
phone=donor_dict["tel"],
is_anonymous=donor_dict["anonymous"],
Expand Down

0 comments on commit c374da6

Please sign in to comment.