<commit_before>from __future__ import unicode_literals, absolute_import
from django.utils import timezone
from django.utils.safestring import mark_safe
from django.contrib.postgres.fields import JSONField
from django.contrib.gis.db import models

from mptt.models import MPTTModel, TreeForeignKey
from dateutil.relativedelta import relativedelta
from datetime import timedelta

from tracking import models as tracking

class Function(models.Model):
    SERVICE_CHOICES = (
        (1, "Service 1"),
        (2, "Service 2"),
        (3, "Service 3"),
        (4, "Service 4"),
        (5, "Service 5"),
        (6, "Service 6"),
        (7, "Service 7"),
        (8, "Service 8"),
        (9, "Common Service 9 - OIM"),
        (10, "Common Service 10 - ODG"),
        (11, "Common Service 11 - ???"),
    )
    SERVICE_CHOICES_DICT = dict(SERVICE_CHOICES)
    service = models.PositiveSmallIntegerField(choices=SERVICE_CHOICES, default=4)
    name = models.CharField(max_length=256, unique=True)

    def __str__(self):
        return self.name

class Process(models.Model):
    name = models.CharField(max_length=256, unique=True)
    function = models.ForeignKey(Function, on_delete=models.PROTECT, null=True, blank=True)
    mtd = models.DurationField(help_text="Maximum Tolerable Downtime (days hh:mm:ss)", default=timedelta(days=14))
    rto = models.DurationField(help_text="Recovery Time Objective (days hh:mm:ss)", default=timedelta(days=7))
    rpo = models.DurationField(help_text="Recovery Point Objective/Data Loss Interval (days hh:mm:ss)", default=timedelta(hours=24))
    users = models.PositiveSmallIntegerField(default=1)
    #TODO: Add validation for durationfields


    def __str__(self):
        return self.name

class Location(models.Model):
    name = models.CharField(max_length=256, unique=True)
    manager = models.ForeignKey(tracking.DepartmentUser, on_delete=models.PROTECT, null=True, blank=True)
    address = models.TextField(unique=True)
    pobox = models.TextField(blank=True)
    phone = models.CharField(max_length=128, null=True, blank=True)
    fax = models.CharField(max_length=128, null=True, blank=True)
    email = models.CharField(max_length=128, null=True, blank=True)
    point = models.PointField(null=True, blank=True)
    url = models.CharField(max_length=2000, help_text="URL to webpage with more information", null=True, blank=True)
    bandwidth_url = models.CharField(max_length=2000, help_text="URL to prtg graph of bw utilisation", null=True, blank=True)

    def __str__(self):
        return "{} ({})".format(self.name, self.address)

    def as_dict(self):
        return {k: getattr(self, k) for k in ("name", "address", "pobox", "phone", "fax", "email") if getattr(self, k)}

    def save(self, *args, **kwargs):
        for orgunit in self.orgunit_set.all():
            orgunit.save()
        super(Location, self).save(*args, **kwargs)

    class Meta:
        ordering = ('name',)


class SecondaryLocation(models.Model):
    location = models.ForeignKey(Location)
    name = models.CharField(max_length=256, unique=True)
    manager = models.ForeignKey(tracking.DepartmentUser, on_delete=models.PROTECT, null=True, blank=True)
    phone = models.CharField(max_length=128, null=True, blank=True)
    fax = models.CharField(max_length=128, null=True, blank=True)
    email = models.CharField(max_length=128, null=True, blank=True)

    def __str__(self):
        return self.name

    def save(self, *args, **kwargs):
        for orgunit in self.orgunit_set.all():
            orgunit.save()
        super(Location, self).save(*args, **kwargs)

    def as_dict(self):
        return {k: getattr(self, k) for k in ("name", "phone", "fax", "email") if getattr(self, k)}


class OrgUnit(MPTTModel):
    TYPE_CHOICES = (
        (0, "Department"),
        (1, "Division"),
        (2, "Branch"),
        (3, "Region"),
        (4, "Cost Centre"),
        (5, "Office"),
        (6, "District"),
        (7, "Section"),
    )
    TYPE_CHOICES_DICT = dict(TYPE_CHOICES)
    unit_type = models.PositiveSmallIntegerField(choices=TYPE_CHOICES, default=4)
    ad_guid = models.CharField(max_length=48, unique=True, null=True, editable=False)
    ad_dn = models.CharField(max_length=512, unique=True, null=True, editable=False)
    name = models.CharField(max_length=256, unique=True)
    acronym = models.CharField(max_length=16, null=True, blank=True)
    manager = models.ForeignKey(tracking.DepartmentUser, on_delete=models.PROTECT, null=True, blank=True)
    parent = TreeForeignKey('self', on_delete=models.PROTECT, null=True, blank=True, related_name='children', db_index=True)
    details = JSONField(null=True, blank=True)
    location = models.ForeignKey(Location, on_delete=models.PROTECT, null=True, blank=True)
    secondary_location = models.ForeignKey(SecondaryLocation, on_delete=models.PROTECT, null=True, blank=True)

    def cc(self):
        try:<commit_msg>Fix bug in SecondaryLocation save method.<commit_after>from __future__ import unicode_literals, absolute_import
from django.utils import timezone
from django.utils.safestring import mark_safe
from django.contrib.postgres.fields import JSONField
from django.contrib.gis.db import models

from mptt.models import MPTTModel, TreeForeignKey
from dateutil.relativedelta import relativedelta
from datetime import timedelta

from tracking import models as tracking

class Function(models.Model):
    SERVICE_CHOICES = (
        (1, "Service 1"),
        (2, "Service 2"),
        (3, "Service 3"),
        (4, "Service 4"),
        (5, "Service 5"),
        (6, "Service 6"),
        (7, "Service 7"),
        (8, "Service 8"),
        (9, "Common Service 9 - OIM"),
        (10, "Common Service 10 - ODG"),
        (11, "Common Service 11 - ???"),
    )
    SERVICE_CHOICES_DICT = dict(SERVICE_CHOICES)
    service = models.PositiveSmallIntegerField(choices=SERVICE_CHOICES, default=4)
    name = models.CharField(max_length=256, unique=True)

    def __str__(self):
        return self.name

class Process(models.Model):
    name = models.CharField(max_length=256, unique=True)
    function = models.ForeignKey(Function, on_delete=models.PROTECT, null=True, blank=True)
    mtd = models.DurationField(help_text="Maximum Tolerable Downtime (days hh:mm:ss)", default=timedelta(days=14))
    rto = models.DurationField(help_text="Recovery Time Objective (days hh:mm:ss)", default=timedelta(days=7))
    rpo = models.DurationField(help_text="Recovery Point Objective/Data Loss Interval (days hh:mm:ss)", default=timedelta(hours=24))
    users = models.PositiveSmallIntegerField(default=1)
    #TODO: Add validation for durationfields


    def __str__(self):
        return self.name

class Location(models.Model):
    name = models.CharField(max_length=256, unique=True)
    manager = models.ForeignKey(tracking.DepartmentUser, on_delete=models.PROTECT, null=True, blank=True)
    address = models.TextField(unique=True)
    pobox = models.TextField(blank=True)
    phone = models.CharField(max_length=128, null=True, blank=True)
    fax = models.CharField(max_length=128, null=True, blank=True)
    email = models.CharField(max_length=128, null=True, blank=True)
    point = models.PointField(null=True, blank=True)
    url = models.CharField(max_length=2000, help_text="URL to webpage with more information", null=True, blank=True)
    bandwidth_url = models.CharField(max_length=2000, help_text="URL to prtg graph of bw utilisation", null=True, blank=True)

    def __str__(self):
        return "{} ({})".format(self.name, self.address)

    def as_dict(self):
        return {k: getattr(self, k) for k in ("name", "address", "pobox", "phone", "fax", "email") if getattr(self, k)}

    def save(self, *args, **kwargs):
        for orgunit in self.orgunit_set.all():
            orgunit.save()
        super(Location, self).save(*args, **kwargs)

    class Meta:
        ordering = ('name',)


class SecondaryLocation(models.Model):
    location = models.ForeignKey(Location)
    name = models.CharField(max_length=256, unique=True)
    manager = models.ForeignKey(tracking.DepartmentUser, on_delete=models.PROTECT, null=True, blank=True)
    phone = models.CharField(max_length=128, null=True, blank=True)
    fax = models.CharField(max_length=128, null=True, blank=True)
    email = models.CharField(max_length=128, null=True, blank=True)

    def __str__(self):
        return self.name

    def save(self, *args, **kwargs):
        for orgunit in self.orgunit_set.all():
            orgunit.save()
        super(SecondaryLocation, self).save(*args, **kwargs)

    def as_dict(self):
        return {k: getattr(self, k) for k in ("name", "phone", "fax", "email") if getattr(self, k)}


class OrgUnit(MPTTModel):
    TYPE_CHOICES = (
        (0, "Department"),
        (1, "Division"),
        (2, "Branch"),
        (3, "Region"),
        (4, "Cost Centre"),
        (5, "Office"),
        (6, "District"),
        (7, "Section"),
    )
    TYPE_CHOICES_DICT = dict(TYPE_CHOICES)
    unit_type = models.PositiveSmallIntegerField(choices=TYPE_CHOICES, default=4)
    ad_guid = models.CharField(max_length=48, unique=True, null=True, editable=False)
    ad_dn = models.CharField(max_length=512, unique=True, null=True, editable=False)
    name = models.CharField(max_length=256, unique=True)
    acronym = models.CharField(max_length=16, null=True, blank=True)
    manager = models.ForeignKey(tracking.DepartmentUser, on_delete=models.PROTECT, null=True, blank=True)
    parent = TreeForeignKey('self', on_delete=models.PROTECT, null=True, blank=True, related_name='children', db_index=True)
    details = JSONField(null=True, blank=True)
    location = models.ForeignKey(Location, on_delete=models.PROTECT, null=True, blank=True)
    secondary_location = models.ForeignKey(SecondaryLocation, on_delete=models.PROTECT, null=True, blank=True)

    def cc(self):
        try:
{'fraction_non_alphanumeric': 0.13039086960913038, 'fraction_numerical': 0.01817998182001818, 'mean_word_length': 4.085772984078069, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '70564', 'n_tokens_mistral': 3334, 'n_tokens_neox': 3125, 'n_words': 708}