From b72129d627ad513773b946745503ddcf8f3937de Mon Sep 17 00:00:00 2001 From: Elijah Date: Fri, 15 Dec 2023 17:53:46 +0100 Subject: [PATCH] Added Faker to create_example_data to generate more realistic objects --- .../commands/create_example_data.py | 33 ++++++++----------- requirements.txt | 1 + 2 files changed, 15 insertions(+), 19 deletions(-) diff --git a/idhub/management/commands/create_example_data.py b/idhub/management/commands/create_example_data.py index 54c8385..89aa408 100644 --- a/idhub/management/commands/create_example_data.py +++ b/idhub/management/commands/create_example_data.py @@ -1,15 +1,18 @@ import random -import string from django.core.management.base import BaseCommand from django.db import IntegrityError from idhub.models import Event, Rol, Service, UserRol from idhub_auth.models import User +from faker import Faker + DEFAULT_OBJECTS_CREATED = 30 RANDOM_STRING_LENGTH = 30 EMAIL_RANDOM_STRING_LENGTH = 10 +fake = Faker() + class Command(BaseCommand): """ @@ -109,7 +112,7 @@ class Command(BaseCommand): try: Event.objects.create( type=random.randint(1, 30), - message=create_random_string(), + message=fake.paragraph(nb_sentences=3), user=user ) created_event_amount += 1 @@ -120,13 +123,12 @@ class Command(BaseCommand): def create_users(self, amount): created_user_amount = 0 for value in range(0, amount): - email = create_random_string(EMAIL_RANDOM_STRING_LENGTH) + "@example.org" + email = fake.email() try: User.objects.create( email=email, - # Could be improved, maybe using Faker - first_name=create_random_string(random.randint(5, 10)), - last_name=create_random_string(random.randint(5, 10)) + first_name=fake.first_name(), + last_name=fake.last_name() ) self.created_users.append(email) created_user_amount += 1 @@ -139,7 +141,7 @@ class Command(BaseCommand): """Superusers can only be created from the specific command""" created_superuser_amount = 0 for value in range(0, amount): - email = create_random_string(EMAIL_RANDOM_STRING_LENGTH) + email = fake.email() try: User.objects.create_superuser(email) created_superuser_amount += 1 @@ -150,12 +152,11 @@ class Command(BaseCommand): def create_services(self, amount): created_service_amount = 0 for value in range(0, amount): - domain = create_random_string(random.randint(5, 15)) + domain = fake.text(max_nb_chars=200) try: service = Service.objects.create( domain=domain, - description=create_random_string( - random.randint(50, 100)) + description=fake.text(max_nb_chars=250) ) self.created_services.append(domain) try: @@ -173,12 +174,11 @@ class Command(BaseCommand): def create_roles(self, amount): created_role_amount = 0 for value in range(0, amount): - # Could be improved, maybe using Faker - name = create_random_string(random.randint(5, 10)) + name = fake.job() try: Rol.objects.create(name=name, - description=create_random_string( - random.randint(50, 100))) + description=fake.text(max_nb_chars=250) + ) created_role_amount += 1 except IntegrityError: self.stdout.write("Couldn't create role " + name) @@ -211,8 +211,3 @@ class Command(BaseCommand): def create_superuser(self, email, password): User.objects.create_superuser(email, password) - - -def create_random_string(string_length=RANDOM_STRING_LENGTH): - return ''.join(random.choices(string.ascii_uppercase + string.digits, - k=string_length)) diff --git a/requirements.txt b/requirements.txt index 9b19238..ae069eb 100644 --- a/requirements.txt +++ b/requirements.txt @@ -13,3 +13,4 @@ jsonref==1.1.0 pyld==2.0.3 more-itertools==10.1.0 dj-database-url==2.1.0 +faker==21.0.0