From c374da69037b3d0655a38e66b840ef17214a6c41 Mon Sep 17 00:00:00 2001 From: Tudor Amariei Date: Thu, 1 Feb 2024 17:44:32 +0200 Subject: [PATCH] Update donor model --- .../management/commands/generate_donations.py | 77 +++++++++++++++++++ .../migrations/0007_donor_address.py | 18 +++++ backend/donations/models/main.py | 1 + backend/donations/views/ngo.py | 8 ++ 4 files changed, 104 insertions(+) create mode 100644 backend/donations/management/commands/generate_donations.py create mode 100644 backend/donations/migrations/0007_donor_address.py diff --git a/backend/donations/management/commands/generate_donations.py b/backend/donations/management/commands/generate_donations.py new file mode 100644 index 00000000..0529cba8 --- /dev/null +++ b/backend/donations/management/commands/generate_donations.py @@ -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!")) diff --git a/backend/donations/migrations/0007_donor_address.py b/backend/donations/migrations/0007_donor_address.py new file mode 100644 index 00000000..48b80853 --- /dev/null +++ b/backend/donations/migrations/0007_donor_address.py @@ -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"), + ), + ] diff --git a/backend/donations/models/main.py b/backend/donations/models/main.py index 877864c3..bac03626 100644 --- a/backend/donations/models/main.py +++ b/backend/donations/models/main.py @@ -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) diff --git a/backend/donations/views/ngo.py b/backend/donations/views/ngo.py index bfa079e7..de33cd1e 100644 --- a/backend/donations/views/ngo.py +++ b/backend/donations/views/ngo.py @@ -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"],