aboutsummaryrefslogtreecommitdiffstats
path: root/ctrack/organisations/management/commands/populate_db.py
blob: b10476e7774cbfc390cca6659f3962ffaf34a487 (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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
import random
from random import randint, choice

from django.core.management import BaseCommand
from django.core.management import CommandParser

from ctrack.assessments.models import CAFSelfAssessment, CAFObjective, CAFPrinciple
from ctrack.caf.models import CAF
from ctrack.caf.tests.factories import (
    GradingFactory,
    FileStoreFactory,
    CAFFactory,
    ApplicableSystemFactory,
)
from ctrack.organisations.models import AddressType, Person
from ctrack.organisations.models import Mode
from ctrack.organisations.models import Submode
from ctrack.organisations.tests.factories import AddressFactory
from ctrack.organisations.tests.factories import OrganisationFactory
from ctrack.organisations.tests.factories import PersonFactory
from ctrack.organisations.tests.factories import RoleFactory
from ctrack.organisations.tests.factories import UserFactory
from ctrack.register.tests.factories import EngagementEventFactory
from ctrack.register.tests.factories import EngagementTypeFactory

fnames = [
    "Clock Pylon Systems",
    "Ultramarine Hanglider Navigator",
    "Membranous Floor Heaters",
    "Alan's Wardrobe Hinge Circuits",
    "Marine Sluicegate Extension Pulleys",
    "Ironway Prob Modelling Area",
    "Bufferage Clippers",
    "Slow Gauze Thread Manipulator",
    "Terratoast Piling",
    "Accounting and Warehouse Conducer",
    "Able Hopscotch Mirrors",
    "Jolly Main Legacy Circuitry",
]


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]

        # Set up some reasonable Modes and SubModes
        m1 = Mode.objects.create(descriptor="Rail")
        m2 = Mode.objects.create(descriptor="Maritime")

        sb1 = Submode.objects.create(descriptor="Light Rail", mode=m1)
        sb2 = Submode.objects.create(descriptor="Rail Maintenance", mode=m1)
        sb3 = Submode.objects.create(descriptor="Rail Infrastructure", mode=m1)
        sb4 = Submode.objects.create(descriptor="International Rail", mode=m1)
        sb5 = Submode.objects.create(descriptor="Passenger Port", mode=m2)
        sb6 = Submode.objects.create(descriptor="Freight Port", mode=m2)
        sb7 = Submode.objects.create(descriptor="Shipping Infrastructure", mode=m2)

        submodes = [sb1, sb2, sb3, sb4, sb5, sb6, sb7]

        # we need a User object to completed the updated_by fields in Organisation and Person
        user = (
            UserFactory.create()
        )  # we need to have at least one user for the updated_by field

        # Create 40 Organisation objects
        orgs = [
            OrganisationFactory.create(submode=submodes[randint(0, len(submodes) - 1)])
            for org in range(40)
        ]
        # Create 40 Address objects
        addr_type = AddressType.objects.create(descriptor="Primary Address")
        for org in orgs:
            AddressFactory.create(type=addr_type, organisation=org)

        roles = [
            RoleFactory.create() for x in range(10)
        ]  # because we have a many-to-many relationship with Role, we need to create one and pass it in
        for org in orgs:
            PersonFactory.create(
                role=choice(roles),
                updated_by=user,
                predecessor=None,
                organisation__submode=choice(submodes),
                organisation=org,
            )

        inspector_role = RoleFactory.create(name="Compliance Inspector")

        # set up some EngagementEvents

        p1 = PersonFactory.create(
            role=choice(roles),
            updated_by=user,
            predecessor=None,
            organisation__submode=choice(submodes),
            organisation=org,
        )
        p2 = PersonFactory.create(
            role=choice(roles),
            updated_by=user,
            predecessor=None,
            organisation__submode=choice(submodes),
            organisation=org,
        )
        p3 = PersonFactory.create(
            role=choice(roles),
            updated_by=user,
            predecessor=None,
            organisation__submode=choice(submodes),
            organisation=org,
        )

        regulator_org = OrganisationFactory.create(
            submode=None,
            name="The Regulator",
            designation_type=3,
            registered_company_name="The Regulator - HMG",
            comments="This is the real regulator.",
        )

        inspectors = [
            PersonFactory.create(
                role=inspector_role,
                updated_by=user,
                job_title="Compliance Inspector",
                predecessor=None,
                organisation__submode=None,
                organisation=regulator_org,
            )
            for _ in range(5)
        ]

        etf1 = EngagementTypeFactory(descriptor="Information Notice")
        etf2 = EngagementTypeFactory(descriptor="Designation Letter")
        etf3 = EngagementTypeFactory(
            descriptor="CAF - Initial Submission", enforcement_instrument=False
        )

        EngagementEventFactory.create(type=etf1, user=user, participants=[p1, p2])
        EngagementEventFactory.create(type=etf2, user=user, participants=[p3])

        # Quality gradings
        q_descriptors = ["Q1", "Q2", "Q3", "Q4", "Q5"]
        for g in q_descriptors:
            GradingFactory.create(descriptor=g, type="QUALITY")

        # Confidence gradings
        c_descriptors = ["C1", "C2", "C3", "C4", "C5"]
        for g in c_descriptors:
            GradingFactory.create(descriptor=g, type="CONFIDENCE")

        # File store
        FileStoreFactory.create(physical_location_organisation=orgs[1])

        # Every org gets on CAF for now
        for org in orgs:
            # create a CAF and ApplicableService for it
            self._create_caf_app_service(c_descriptors, org, q_descriptors)

        # CAF submissions - they create EngagementEvents
        # Get a random CAF
        _caf = CAF.objects.get(pk=1)  # we should have one by now
        EngagementEventFactory.create(
            type=etf3, user=user, participants=[inspectors[1], p2], related_caf=_caf
        )

        # We want to create a CAF with a bunch of scoring now...
        _caf2 = CAF.objects.get(pk=1)
        _completer = Person.objects.get(pk=1)
        caf_assessment = CAFSelfAssessment.objects.create(
            caf_id=_caf2.id, completer_id=_completer.id, comments="Random Comments"
        )

        # We want to simulate 4 CAF Objectives
        c_obj_a = CAFObjective.objects.create(name="Objective A: Major Issue A",
                                              description="An important objective to fix the world.", order_id=1)
        c_obj_b = CAFObjective.objects.create(name="Objective B: Major Issue B",
                                              description="An important objective to fix the world.", order_id=2)
        c_obj_c = CAFObjective.objects.create(name="Objective C: Major Issue C",
                                              description="An important objective to fix the world.", order_id=3)
        c_obj_d = CAFObjective.objects.create(name="Objective D: Major Issue D",
                                              description="An important objective to fix the world.", order_id=4)

        # For each Objective, let's create four Principles
        p_a1 = CAFPrinciple.objects.create(
            caf_objective_id=c_obj_a.id,
            designation="A1",
            title="Governance",
            description="When you don't have Governance, you have nothing.",
            order_id=1
        )
        p_a2 = CAFPrinciple.objects.create(
            caf_objective_id=c_obj_a.id,
            designation="A2",
            title="Risk Management",
            description="Don't take a risk, and don't get nowhere.",
            order_id=2
        )
        p_a3 = CAFPrinciple.objects.create(
            caf_objective_id=c_obj_a.id,
            designation="A3",
            title="Asset Management",
            description="Without assets, you have no raw materials to work with.",
            order_id=3
        )
        p_a4 = CAFPrinciple.objects.create(
            caf_objective_id=c_obj_a.id,
            designation="A4",
            title="Supply Chain",
            description="You need to get your stuff from somewhere.",
            order_id=4
        )

        p_b1 = CAFPrinciple.objects.create(
            caf_objective_id=c_obj_b.id,
            designation="B1",
            title="Service Protection & Policies",
            description="Put in place the right protections for a future of security.",
            order_id=1
        )
        p_b2 = CAFPrinciple.objects.create(
            caf_objective_id=c_obj_b.id,
            designation="B2",
            title="Identity and Access Control",
            description="Stop the wrong people getting at your critical assets, okay.",
            order_id=2
        )
        p_b3 = CAFPrinciple.objects.create(
            caf_objective_id=c_obj_b.id,
            designation="B3",
            title="Data Security",
            description="Data is the new oil...",
            order_id=3
        )
        p_b4 = CAFPrinciple.objects.create(
            caf_objective_id=c_obj_b.id,
            designation="B4",
            title="System Security",
            description="If you have complicated systems, they need some sort of security.",
            order_id=4
        )

        # Only two of these
        p_c1 = CAFPrinciple.objects.create(
            caf_objective_id=c_obj_c.id,
            designation="C1",
            title="Security Monitoring",
            description="Monitoring the bits and pieces is the most important aspect of your life.",
            order_id=1
        )
        p_c2 = CAFPrinciple.objects.create(
            caf_objective_id=c_obj_c.id,
            designation="C2",
            title="Proactive Security and Event Discovery",
            description="If we're not proactive, we will get found out eventually.",
            order_id=2
        )

        # Only two of these too
        p_d1 = CAFPrinciple.objects.create(
            caf_objective_id=c_obj_d.id,
            designation="D1",
            title="Response and Recovery Planning",
            description="Responding to the security problems since 1999...",
            order_id=1
        )
        p_d2 = CAFPrinciple.objects.create(
            caf_objective_id=c_obj_d.id,
            designation="D2",
            title="Improvements",
            description="Improving all the things.",
            order_id=2
        )

        # TODO - adapt this so that it records more than just Persons created

        self.stdout.write(
            self.style.SUCCESS(
                f"Created {number} Person object[s]! Go forth and multiply."
            )
        )

    def _create_caf_app_service(self, c_descriptors, org, q_descriptors):
        caf = CAFFactory.create(
            quality_grading__descriptor=random.choice(q_descriptors),
            confidence_grading__descriptor=random.choice(c_descriptors),
            triage_review_date=None,
            triage_review_inspector=None,
        )
        # Each CAF can have up to three systems associated with it
        for _ in range(random.randint(1, 3)):
            ApplicableSystemFactory.create(
                name=random.choice(fnames), organisation=org, caf=caf,
            )