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
|
from itertools import chain
from django.conf import settings
from django.db import models
from django.db.models import Q
from django.utils.text import slugify
from myuser.models import TeamUser
ES_YEAR_LENGTH = 3
class Common(models.Model):
date_created = models.DateTimeField(auto_now_add=True)
last_modified = models.DateTimeField(auto_now=True)
class Meta:
abstract = True
class RegulatoryRole(Common):
name = models.CharField(max_length=64, null=False, blank=False)
description = models.TextField(max_length=1024)
def __str__(self):
return self.name
class Meta:
verbose_name_plural = "Regulatory Roles"
class Person(Common):
"External person, rather than MOD at this point."
first_name = models.CharField(max_length=64, null=False, blank=False)
last_name = models.CharField(max_length=64, null=False, blank=False)
organisation = models.ForeignKey("Organisation", null=False, blank=False, on_delete=models.CASCADE)
email = models.EmailField(null=True, blank=True)
mobile = models.CharField(max_length=64, null=True, blank=True)
regulatory_role = models.ManyToManyField(RegulatoryRole)
def __str__(self):
return f"{self.first_name} {self.last_name}"
class Meta:
verbose_name_plural = "People"
class RegulatedEntityType(Common):
name = models.CharField(max_length=128, null=False, blank=False)
def __str__(self):
return self.name
class Meta:
verbose_name_plural = "Regulated Entity Types"
class Organisation(Common):
name = models.CharField(max_length=128, blank=False)
slug = models.SlugField(max_length=128, blank=True)
is_regulated_entity = models.BooleanField(default=False)
entitytype = models.ForeignKey(RegulatedEntityType, null=True, blank=True, on_delete=models.CASCADE)
# Responsible Person
rp = models.ManyToManyField(
Person,
blank=True,
related_name="responsible_person",
verbose_name="Responsible Person",
)
# Accountable Person
ap = models.ManyToManyField(
Person,
blank=True,
related_name="accountable_person",
verbose_name="Accountable Person",
)
# Information Holder
ih = models.ManyToManyField(
Person,
blank=True,
related_name="information_holder",
verbose_name="Information Holder",
)
def __str__(self):
return self.name
def save(self, *args, **kwargs):
self.slug = slugify(self.name)
super().save(*args, **kwargs)
def lead_team(self):
teams = {ot.team for ot in self.lead_inspector.all()}
if not teams:
return "NA"
if len(teams) > 1:
if len(set([t.name for t in teams])) > 1:
return "Shared Lead"
else:
return teams.pop()
else:
return teams.pop()
class Meta:
verbose_name_plural = "Organisations"
class EngagementType(Common):
name = models.CharField(max_length=56)
description = models.TextField(null=True, blank=True)
def __str__(self):
return self.name
class Meta:
verbose_name_plural = "Engagement Types"
class EngagementManager(models.Manager):
def sp(self):
# TODO fix this - returns duplicates
qs = self.filter(officers__team__name="Submarines and Propulsion")
return qs.exclude(Q(engagement_type__name="INSPECTION") | Q(engagement_type__name="ASSESSMENT"))
def sp_regulatory(self):
"Assessments and Inspectons only"
qs = self.filter(officers__team__name="Submarines and Propulsion")
return qs.filter(
Q(engagement_type__name="INSPECTION")
| Q(engagement_type__name="ASSESSMENT")
| Q(engagement_type__name="L1RIF")
| Q(engagement_type__name="L2RIF")
| Q(engagement_type__name="L3RIF")
| Q(engagement_type__name="L4RIF")
| Q(engagement_type__name="SAMPLING")
)
def tr(self):
return self.filter(officers__team__name="Transport")
class Engagement(Common):
proposed_start_date = models.DateField(null=False, blank=False)
proposed_end_date = models.DateField(null=True, blank=True)
engagement_type = models.ForeignKey(EngagementType, on_delete=models.CASCADE)
external_party = models.ForeignKey(Organisation, on_delete=models.CASCADE)
officers = models.ManyToManyField(TeamUser)
objects = EngagementManager()
friendly_types = {
"INSPECTION": "Inspection",
"ASSESSMENT": "Assessment",
"SAMPLING": "Sampling",
"L1RIF": "L1 RIF",
"L2RIF": "L2 RIF",
"L3RIF": "L3 RIF",
"L4RIF": "L4 RIF",
}
def get_officers(self):
officers_from_effort = list(chain.from_iterable([x.officers.all() for x in self.effort.all()]))
return [o.fullname() for o in set(officers_from_effort + list(self.officers.all()))]
return "toss"
# return [" ".join([x.first_name, x.last_name]) for x in self.officers.all()]
def total_planning_effort(self):
p_effort = self.engagementeffort_set.all().filter(is_planned=True)
return sum([x.effort_total_hours() for x in p_effort])
def total_planning_effort_per_officer(self):
# TODO - check this algorithm. It's not quite right is it?
p_effort = self.engagementeffort_set.all().filter(is_planned=True)
offs = sum([x.officers.count() for x in p_effort])
return sum([x.effort_total_hours() for x in p_effort]) / offs
def total_effort(self):
return 0
def friendly_type(self):
return self.friendly_types[self.engagement_type.name]
def dscs(self):
"Return all declared DSCs as part of REGULATION effort"
dscs = set()
for ee in EngagementEffort.objects.filter(engagement=self):
for si in ee.sub_instruments.all():
dscs.add(si)
return dscs
def __str__(self):
return f"{self.engagement_type.name} at {self.external_party} ({self.proposed_start_date})"
class Meta:
ordering = ("proposed_start_date",)
class EngagementEffort(Common):
choices = (
("TRAVEL", "Travel"),
("PLANNING", "Planning"),
("REGULATION", "Regulation (On-site or Remote)"),
("DISCUSSION", "Discussion"),
("REPORTING", "Reporting"),
)
is_planned = models.BooleanField(null=True, blank=True, verbose_name="Planned", default=True)
effort_type = models.CharField(max_length=32, choices=choices, verbose_name="Effort Type")
proposed_start_date = models.DateTimeField()
proposed_end_date = models.DateTimeField()
engagement = models.ForeignKey(Engagement, on_delete=models.CASCADE, related_name="effort")
officers = models.ManyToManyField(settings.AUTH_USER_MODEL)
sub_instruments = models.ManyToManyField("instruments.SubInstrument", blank=True, related_name="effort")
notes = models.TextField(null=True, blank=True)
class Meta:
verbose_name_plural = "Engagement Effort"
ordering = ("proposed_start_date",)
def get_officers(self):
return [" ".join([x.first_name, x.last_name]) for x in self.officers.all()]
def effort_total_hours(self):
"Returns total effort for this engagement."
delta = self.proposed_end_date - self.proposed_start_date
return delta.seconds / 60 / 60
def effort_total_planned_hours(self):
"Returns total planned effort for this engagement."
if self.is_planned:
delta = self.proposed_end_date - self.proposed_start_date
return delta.seconds / 60 / 60
else:
return 0
def effort_actual(self):
"Returns effort that where is_planned is false."
if not self.is_planned:
delta = self.proposed_end_date - self.proposed_start_date
return delta.seconds / 60 / 60
else:
return 0
def effort_per_officer_hours(self):
"Returns effort in hours per officer"
delta = (self.proposed_end_date - self.proposed_start_date) / self.officers.count()
return delta.seconds / 60 / 60
def __str__(self):
return f"{self.effort_type} effort for {self.engagement}: {self.proposed_end_date - self.proposed_start_date}"
class RegulatoryCycle(Common):
start_date = models.DateField(null=False, blank=False)
end_date = models.DateField(null=False, blank=False)
description = models.TextField(max_length=1024, null=True, blank=True)
def get_year(self):
return str(self.start_date.year)
def __str__(self):
return f"Regulatory Cycle: {self.get_year()}"
class EngagementStrategy(Common):
STATUS = (
("DRAFT", "Draft"),
("SUBMITTED", "Submitted"),
("APPROVED", "Approved"),
("REJECTED", "Rejected"),
)
organisation = models.ForeignKey(Organisation, on_delete=models.CASCADE)
start_year = models.ForeignKey(RegulatoryCycle, on_delete=models.CASCADE, related_name="start_year")
end_year = models.ForeignKey(RegulatoryCycle, on_delete=models.CASCADE, related_name="end_year")
description = models.TextField(max_length=1024, null=True, blank=True)
inspector_sign_off = models.DateField(null=True, blank=True)
owned_by = models.ForeignKey(
settings.AUTH_USER_MODEL, on_delete=models.CASCADE, related_name="owned_engagement_strategies"
)
reviewed_by = models.ForeignKey(
settings.AUTH_USER_MODEL,
null=True,
blank=True,
on_delete=models.CASCADE,
related_name="reviewed_engagement_strategies",
)
management_sign_off = models.DateField(null=True, blank=True)
status = models.CharField(max_length=32, choices=STATUS, default=STATUS[0][0])
def get_end_year(self) -> int:
return self.start_year.start_date.year + (ES_YEAR_LENGTH - 1)
def __str__(self):
return (
f"Engagement Strategy ({self.start_year.start_date.year}-{self.get_end_year()}) - {self.organisation.name}"
)
class Meta:
ordering = ("start_year",)
unique_together = ("start_year", "organisation")
verbose_name_plural = "Engagement Strategies"
|