aboutsummaryrefslogtreecommitdiffstats
path: root/ctrack/organisations/management/commands/generate_people.py
blob: 53ece2190345244828a29d450bc3e0bb941ef392 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
from django.core.management import BaseCommand
from django.core.management import CommandParser

from ctrack.organisations.tests.factories import PersonFactory
from ctrack.organisations.tests.factories import RoleFactory
from ctrack.organisations.tests.factories import UserFactory


class Command(BaseCommand):
    help = """
    Creates a bunch of people and organisations for them to work in.

    Also creates users and roles as these are required fields.
    """

    def add_arguments(self, parser: CommandParser) -> None:
        parser.add_argument("number", nargs=1, type=int)

    def handle(self, *args, **options):
        number = options["number"][0]
        user = UserFactory.create()  # we need to have at least one user for the updated_by field
        role = RoleFactory.create()  # because we have a many-to-many relationship with Role, we need to create one and pass it in
        PersonFactory.create_batch(number, role=role, updated_by=user,
                                   predecessor__predecessor=None)  # we do this so we don't get a loop
        self.stdout.write(
            self.style.SUCCESS(
                f"Created {number} Person object[s]! Go forth and multiply."
            )
        )