summaryrefslogtreecommitdiffstats
path: root/instruments/models.py
blob: e266fb285cdee3cf2b6775387c218b4f194b45aa (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
from django.db import models

from engagements.models import EngagementEffort, Organisation


class Common(models.Model):
    date_created = models.DateTimeField(auto_now_add=True)
    last_modified = models.DateTimeField(auto_now=True)

    class Meta:
        abstract = True


class Instrument(Common):
    "JSP 628, JSP 440, the ONR ones, etc"
    name = models.CharField(max_length=512, null=False, blank=False)
    long_title = models.CharField(max_length=1024, null=True, blank=True)
    designator = models.CharField(max_length=3, null=True, blank=True)
    owner = models.ForeignKey(Organisation, null=False, blank=False, on_delete=models.CASCADE)
    link = models.URLField(max_length=1024, null=True, blank=True)

    def __str__(self):
        return self.name

    class Meta:
        verbose_name_plural = "Instruments"


class SubInstrument(Common):
    "DSCs, Chapters, Supplements, Leaflets..."
    choices = (
        ("DSC", "Defence Security Condition (DSC)"),
        ("DSYAP", "Defence Security Assessment Principle (DSyAP)"),
        ("DSTAIG", "Defence Security Testing And I Don't Know (DSTAIG)"),
    )
    is_guidance = models.BooleanField(null=True, blank=True, default=False)
    itype = models.CharField(choices=choices, max_length=6, verbose_name="Instrument Type")
    title = models.CharField(max_length=512, null=False, blank=False)
    parent = models.ForeignKey(Instrument, on_delete=models.CASCADE, verbose_name="Parent Instrument")
    description = models.TextField(null=True, blank=True)
    rationale = models.TextField(null=True, blank=True)
    relative = models.ManyToManyField("self")
    short = models.CharField(max_length=10, null=True, blank=True)

    def effort(self):
        return EngagementEffort.objects.filter(sub_instruments__contains=self)

    def effort_for_org(self, org):
        eff = EngagementEffort.objects.filter(engagement__external_party=org).filter(effort_type="REGULATION")
        return sum(e.effort_total_hours() for e in eff)

    def __str__(self):
        return self.title

    class Meta:
        verbose_name_plural = "Sub Instruments"