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, RegulatoryCycle, RegulatoryRole, ) from instruments.models import Instrument, SubInstrument from myuser.models import Team, TeamUser def duration_formatter(d1: datetime.date, d2: datetime.date) -> str: trip_delta = d2 - d1 if trip_delta.days == 0: # this is a single day event return f"{d1.strftime("%d %B %Y")} (1 day)" else: # if months are different... if d1.month != d2.month: # we need to format this differently return f"{d1.strftime("%d %B")} - {d2.strftime("%d %B %Y")} ({trip_delta.days +1} days)" else: return f"{d1.strftime("%d")}-{d2.strftime("%d %B %Y")} ({trip_delta.days + 1} days)" 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, ) # Some Regulatory Cycles RegulatoryCycle.objects.create(start_date="2022-01-01", end_date="2022-12-31") RegulatoryCycle.objects.create(start_date="2023-01-01", end_date="2023-12-31") RegulatoryCycle.objects.create(start_date="2024-01-01", end_date="2024-12-31") # 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