summaryrefslogtreecommitdiffstats
path: root/engagements/utils.py
blob: 2c4ff7207a5e766a99f4f77c16cb93253fc5fcfd (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
import random
from collections import defaultdict
from datetime import date, datetime

from faker import Faker

from engagements.models import (
    Engagement,
    EngagementEffort,
    EngagementType,
    Organisation,
    Person,
    RegulatedEntityType,
    RegulatoryRole,
)
from instruments.models import Instrument, SubInstrument
from myuser.models import Team, TeamUser


def populate_database():
    out = defaultdict(list)
    fake = Faker(locale="en_GB")

    # Users and teams
    TeamUser.objects.all().delete()
    Team.objects.all().delete()
    teams = ["Submarines and Propulsion", "Transport"]
    Team.objects.all().delete()
    tdefnuc = Team.objects.create(name=teams[0])
    u1 = TeamUser.objects.create_superuser(
        email="lemon@lemon.com",
        password="lemonlemon",
    )
    u1.first_name = "Matthew"
    u1.last_name = "Lemon"
    u1.team = tdefnuc
    u1.designation = "LI8"
    u1.is_active = True
    u1.save()
    out["superuser"] = u1

    desigs = ["LI1", "LI2", "LI3", "LI4", "LI5", "LI6"]
    for p in range(6):
        first_name = fake.first_name()
        last_name = fake.last_name()
        u = TeamUser.objects.create_superuser(
            email=f"{first_name.lower()}@theregulator.com",
            password="fakepassword",
        )
        u.first_name = first_name
        u.last_name = last_name
        u.team = Team.objects.create(name=random.choice(teams))
        u.designation = desigs[p]
        out["users"].append(u)
        u.save()

    # RegulatoryRoles
    RegulatoryRole.objects.all().delete()
    RegulatoryRole.objects.create(
        name="Responsible Person",
        description="The Regulated Person charged with managing, etc",
    )
    RegulatoryRole.objects.create(name="Accountable Person")
    RegulatoryRole.objects.create(
        name="Information Holder",
        description="A regulated person who must ensure etc.",
    )
    # RegulatedEntityTypes
    RegulatedEntityType.objects.all().delete()
    ret1 = RegulatedEntityType.objects.create(name="Site")
    RegulatedEntityType.objects.create(name="Operation")
    RegulatedEntityType.objects.create(name="Distrubuted Site")
    # Organisations
    Organisation.objects.all().delete()
    for _ in range(10):
        o = Organisation.objects.create(name=fake.company(), is_regulated_entity=True, entitytype=ret1)
        if random.choice([1, 2, 3]) == 2:
            u1.lead_for.add(o)
            u1.save()
        out["orgs"].append(o)
    o1 = Organisation.objects.create(name="MOD", is_regulated_entity=False)
    out["orgs"].append(o1)
    # Instruments
    Instrument.objects.all().delete()
    j = Instrument.objects.create(
        name="JSP 628",
        long_title="Security Regulation of the DNE",
        designator="JSP628",
        owner=o1,
    )
    # Create the DSCs
    Faker.seed(0)
    SubInstrument.objects.all().delete()
    for n in range(1, 17):
        si = SubInstrument.objects.create(
            title=f"DSC {str(n)} - Title {n}",
            itype="DSC",
            parent=j,
            short=f"DSC {n}",
            description=fake.paragraph(nb_sentences=2),
            rationale=fake.paragraph(nb_sentences=3),
        )
        out["sub_instruments"].append(si)
    for d in SubInstrument.objects.filter(itype="DSC"):
        i = SubInstrument.objects.create(
            title=f"DSTAIG {d.pk} - Title {d.pk}",
            is_guidance=True,
            itype="DSTAIG",
            parent=j,
            description=fake.paragraph(nb_sentences=2),
            rationale=fake.paragraph(nb_sentences=3),
        )
        out["sub_instruments"].append(i)
        i.relative.add(d)

    # EngagementType
    EngagementType.objects.all().delete()
    EngagementType.objects.create(name="EMAIL", description=fake.paragraph(4))
    EngagementType.objects.create(name="MEETING", description=fake.paragraph(4))
    EngagementType.objects.create(name="BRIEFING", description=fake.paragraph(4))
    EngagementType.objects.create(name="TEAMSCALL", description=fake.paragraph(4))
    EngagementType.objects.create(name="PHONECALL", description=fake.paragraph(4))
    EngagementType.objects.create(name="L1RIF", description=fake.paragraph(4))
    EngagementType.objects.create(name="L2RIF", description=fake.paragraph(4))
    EngagementType.objects.create(name="L3RIF", description=fake.paragraph(4))
    EngagementType.objects.create(name="L4RIF", description=fake.paragraph(4))
    EngagementType.objects.create(name="SAMPLING", description=fake.paragraph(4))
    EngagementType.objects.create(name="INSPECTION", description=fake.paragraph(4))
    EngagementType.objects.create(name="ASSESSMENT", description=fake.paragraph(4))

    # People
    Person.objects.all().delete()
    o_pks = [o.pk for o in Organisation.objects.all()]
    for _ in range(5):
        p = Person(
            first_name=fake.first_name(),
            last_name=fake.last_name(),
            organisation=Organisation.objects.get(pk=random.choice(o_pks)),
            mobile=fake.cellphone_number(),
        )
        p.email = f"{p.first_name.lower()}@{p.organisation.slug}.com"
        p.save()
        out["people"].append(p)

    # Engagement
    Engagement.objects.all().delete()
    d1 = date(2022, 5, 10)
    d2 = date(2022, 5, 12)
    # users = get_user_model()
    et = EngagementType.objects.get(name="INSPECTION")
    ep = Organisation.objects.first()
    e = Engagement.objects.create(
        proposed_start_date=d1,
        proposed_end_date=d2,
        engagement_type=et,
        external_party=ep,
    )
    e.officers.add(u1)
    e.save()
    out["engagements"].append(e)

    # Effort
    EngagementEffort.objects.all().delete()
    d1 = datetime(2022, 4, 10, 10, 0, 0)
    d2 = datetime(2022, 4, 10, 14, 15, 0)
    d3 = datetime(2022, 4, 10, 12, 0, 0)
    d4 = datetime(2022, 4, 10, 13, 0, 0)  # 1 hour between d3 and d4
    ef = EngagementEffort.objects.create(
        is_planned=True,
        effort_type="PLANNING",
        proposed_start_date=d1,
        proposed_end_date=d2,
        engagement=e,
    )
    EngagementEffort.objects.create(
        is_planned=True,
        effort_type="TRAVEL",
        proposed_start_date=d3,
        proposed_end_date=d4,
        engagement=e,
    )
    ef.officers.add(u1)
    ef.sub_instruments.add(out["sub_instruments"][0])
    ef.save()
    out["engagement_effort"].append(ef)
    return out