Added Faker to create_example_data to generate more realistic objects
This commit is contained in:
parent
7a5d42f586
commit
b72129d627
|
@ -1,15 +1,18 @@
|
||||||
import random
|
import random
|
||||||
import string
|
|
||||||
|
|
||||||
from django.core.management.base import BaseCommand
|
from django.core.management.base import BaseCommand
|
||||||
from django.db import IntegrityError
|
from django.db import IntegrityError
|
||||||
from idhub.models import Event, Rol, Service, UserRol
|
from idhub.models import Event, Rol, Service, UserRol
|
||||||
from idhub_auth.models import User
|
from idhub_auth.models import User
|
||||||
|
|
||||||
|
from faker import Faker
|
||||||
|
|
||||||
DEFAULT_OBJECTS_CREATED = 30
|
DEFAULT_OBJECTS_CREATED = 30
|
||||||
RANDOM_STRING_LENGTH = 30
|
RANDOM_STRING_LENGTH = 30
|
||||||
EMAIL_RANDOM_STRING_LENGTH = 10
|
EMAIL_RANDOM_STRING_LENGTH = 10
|
||||||
|
|
||||||
|
fake = Faker()
|
||||||
|
|
||||||
|
|
||||||
class Command(BaseCommand):
|
class Command(BaseCommand):
|
||||||
"""
|
"""
|
||||||
|
@ -109,7 +112,7 @@ class Command(BaseCommand):
|
||||||
try:
|
try:
|
||||||
Event.objects.create(
|
Event.objects.create(
|
||||||
type=random.randint(1, 30),
|
type=random.randint(1, 30),
|
||||||
message=create_random_string(),
|
message=fake.paragraph(nb_sentences=3),
|
||||||
user=user
|
user=user
|
||||||
)
|
)
|
||||||
created_event_amount += 1
|
created_event_amount += 1
|
||||||
|
@ -120,13 +123,12 @@ class Command(BaseCommand):
|
||||||
def create_users(self, amount):
|
def create_users(self, amount):
|
||||||
created_user_amount = 0
|
created_user_amount = 0
|
||||||
for value in range(0, amount):
|
for value in range(0, amount):
|
||||||
email = create_random_string(EMAIL_RANDOM_STRING_LENGTH) + "@example.org"
|
email = fake.email()
|
||||||
try:
|
try:
|
||||||
User.objects.create(
|
User.objects.create(
|
||||||
email=email,
|
email=email,
|
||||||
# Could be improved, maybe using Faker
|
first_name=fake.first_name(),
|
||||||
first_name=create_random_string(random.randint(5, 10)),
|
last_name=fake.last_name()
|
||||||
last_name=create_random_string(random.randint(5, 10))
|
|
||||||
)
|
)
|
||||||
self.created_users.append(email)
|
self.created_users.append(email)
|
||||||
created_user_amount += 1
|
created_user_amount += 1
|
||||||
|
@ -139,7 +141,7 @@ class Command(BaseCommand):
|
||||||
"""Superusers can only be created from the specific command"""
|
"""Superusers can only be created from the specific command"""
|
||||||
created_superuser_amount = 0
|
created_superuser_amount = 0
|
||||||
for value in range(0, amount):
|
for value in range(0, amount):
|
||||||
email = create_random_string(EMAIL_RANDOM_STRING_LENGTH)
|
email = fake.email()
|
||||||
try:
|
try:
|
||||||
User.objects.create_superuser(email)
|
User.objects.create_superuser(email)
|
||||||
created_superuser_amount += 1
|
created_superuser_amount += 1
|
||||||
|
@ -150,12 +152,11 @@ class Command(BaseCommand):
|
||||||
def create_services(self, amount):
|
def create_services(self, amount):
|
||||||
created_service_amount = 0
|
created_service_amount = 0
|
||||||
for value in range(0, amount):
|
for value in range(0, amount):
|
||||||
domain = create_random_string(random.randint(5, 15))
|
domain = fake.text(max_nb_chars=200)
|
||||||
try:
|
try:
|
||||||
service = Service.objects.create(
|
service = Service.objects.create(
|
||||||
domain=domain,
|
domain=domain,
|
||||||
description=create_random_string(
|
description=fake.text(max_nb_chars=250)
|
||||||
random.randint(50, 100))
|
|
||||||
)
|
)
|
||||||
self.created_services.append(domain)
|
self.created_services.append(domain)
|
||||||
try:
|
try:
|
||||||
|
@ -173,12 +174,11 @@ class Command(BaseCommand):
|
||||||
def create_roles(self, amount):
|
def create_roles(self, amount):
|
||||||
created_role_amount = 0
|
created_role_amount = 0
|
||||||
for value in range(0, amount):
|
for value in range(0, amount):
|
||||||
# Could be improved, maybe using Faker
|
name = fake.job()
|
||||||
name = create_random_string(random.randint(5, 10))
|
|
||||||
try:
|
try:
|
||||||
Rol.objects.create(name=name,
|
Rol.objects.create(name=name,
|
||||||
description=create_random_string(
|
description=fake.text(max_nb_chars=250)
|
||||||
random.randint(50, 100)))
|
)
|
||||||
created_role_amount += 1
|
created_role_amount += 1
|
||||||
except IntegrityError:
|
except IntegrityError:
|
||||||
self.stdout.write("Couldn't create role " + name)
|
self.stdout.write("Couldn't create role " + name)
|
||||||
|
@ -211,8 +211,3 @@ class Command(BaseCommand):
|
||||||
|
|
||||||
def create_superuser(self, email, password):
|
def create_superuser(self, email, password):
|
||||||
User.objects.create_superuser(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))
|
|
||||||
|
|
|
@ -13,3 +13,4 @@ jsonref==1.1.0
|
||||||
pyld==2.0.3
|
pyld==2.0.3
|
||||||
more-itertools==10.1.0
|
more-itertools==10.1.0
|
||||||
dj-database-url==2.1.0
|
dj-database-url==2.1.0
|
||||||
|
faker==21.0.0
|
||||||
|
|
Loading…
Reference in New Issue