# Path: statusboard/models.py
# class Incident(TimeStampedModel):
#     name = models.CharField(max_length=255, verbose_name=_("name"))
#     services = models.ManyToManyField(
#         "Service",
#         blank=True,
#         related_name="incidents",
#         related_query_name="incident",
#         verbose_name=_("services"),
#     )
#     occurred = models.DateTimeField(
#         default=timezone.now, verbose_name=_("occurred")
#     )
#     closed = models.BooleanField(default=False)
#     objects = IncidentManager()
# 
#     def updates_by_ctime(self):
#         return self.updates.order_by("-created")
# 
#     def should_be_closed(self):
#         try:
#             return self.updates.latest("created").status == 3
#         except IncidentUpdate.DoesNotExist:
#             return False
# 
#     def __str__(self):
#         return self.name
# 
#     class Meta:
#         verbose_name = _("incident")
#         verbose_name_plural = _("incidents")
# 
# Path: statusboard/models.py
# class Service(TimeStampedModel):
#     name = models.CharField(
#         max_length=255, unique=True, verbose_name=_("name")
#     )
#     description = models.TextField(blank=True, verbose_name=_("description"))
#     href = models.URLField(blank=True)
#     status = models.IntegerField(
#         choices=SERVICE_STATUSES, verbose_name=_("status")
#     )
#     priority = models.PositiveIntegerField(
#         default=0, verbose_name=_("priority")
#     )
#     groups = models.ManyToManyField(
#         "ServiceGroup",
#         blank=True,
#         related_name="services",
#         related_query_name="service",
#         verbose_name=_("groups"),
#     )
#     objects = ServiceManager()
# 
#     def __str__(self):
#         return self.name
# 
#     def __init__(self, *args, **kwargs):
#         super(Service, self).__init__(*args, **kwargs)
#         self._status = self.status
# 
#     def save(self, *args, **kwargs):
#         # When the instance is saved, the original status is updated
#         super(Service, self).save(*args, **kwargs)
#         self._status = self.status
# 
#     class Meta:
#         verbose_name = _("service")
#         verbose_name_plural = _("services")
# 
# Path: statusboard/models.py
# class ServiceGroup(TimeStampedModel):
#     name = models.CharField(
#         max_length=255, unique=True, verbose_name=_("name")
#     )
#     priority = models.PositiveIntegerField(
#         default=0, verbose_name=_("priority")
#     )
#     collapse = models.IntegerField(
#         choices=SERVICEGROUP_COLLAPSE_OPTIONS, default=0
#     )
#     objects = ServiceGroupManager()
# 
#     def worst_service(self):
#         return self.services.all().latest("status")
# 
#     def collapsed(self):
#         """Check if the service group should collapse or not.
# 
#         Return true if a group doesn't have any service."""
#         if self.collapse == 0:
#             return False
#         elif self.collapse == 1:
#             return True
#         else:
#             return not self.services.exclude(status=0).exists()
# 
#     def is_empty_group(self):
#         return not self.services.all().exists()
# 
#     def __str__(self):
#         return self.name
# 
#     class Meta:
#         verbose_name = _("service group")
#         verbose_name_plural = _("service groups")
# 
# Path: statusboard/models.py
# class IncidentUpdate(TimeStampedModel):
#     incident = models.ForeignKey(
#         Incident,
#         related_name="updates",
#         on_delete=models.deletion.CASCADE,
#         related_query_name="update",
#         verbose_name=_("incident"),
#     )
#     status = models.IntegerField(
#         choices=INCIDENT_STATUSES, verbose_name=_("status")
#     )
#     description = models.TextField(verbose_name=_("description"))
# 
#     def __str__(self):
#         return "Update {} {}".format(self.incident.name, self.modified)
# 
#     class Meta:
#         verbose_name = _("incident update")
#         verbose_name_plural = _("incident updates")
# 
# Path: statusboard/models.py
# class Maintenance(TimeStampedModel):
#     scheduled = models.DateTimeField(verbose_name=_("scheduled start"))
#     name = models.CharField(max_length=255, verbose_name=_("name"))
#     description = models.TextField(verbose_name=_("description"))
# 
#     def __str__(self):
#         return self.name
# 
#     class Meta:
#         verbose_name = _("maintenance")
#         verbose_name_plural = _("maintenances")
# 
# Path: statusboard/models.py
# SERVICE_STATUSES = (
#     (0, _("Operational")),
#     (1, _("Performance issues")),
#     (2, _("Partial outage")),
#     (3, _("Major outage")),
# )


from django import forms
from django.forms import inlineformset_factory
from django.utils.translation import ugettext_lazy as _
from .models import Incident
from .models import Service
from .models import ServiceGroup
from .models import IncidentUpdate
from .models import Maintenance
from .models import SERVICE_STATUSES
            (
                "Update the status of involved services "
                "(an empty value means that they will be left unaltered)"
            )
        ),
        required=False,
    )

    class Meta:
        model = Incident
        fields = ["name", "occurred", "services", "service_status"]

    def __init__(self, *args, **kwargs):
        super(IncidentForm, self).__init__(*args, **kwargs)
        self.fields["services"].queryset = Service.objects.order_by("name")

    def save(self, commit=True):
        model = super(IncidentForm, self).save(commit=False)
        status = self.cleaned_data["service_status"]

        if commit:
            model.save()
            self.save_m2m()
            if status not in (None, ""):
                model.services.update(status=status)

        return model


IncidentUpdateFormSet = inlineformset_factory(

# Path: statusboard/models.py
# class Incident(TimeStampedModel):
#     name = models.CharField(max_length=255, verbose_name=_("name"))
#     services = models.ManyToManyField(
#         "Service",
#         blank=True,
#         related_name="incidents",
#         related_query_name="incident",
#         verbose_name=_("services"),
#     )
#     occurred = models.DateTimeField(
#         default=timezone.now, verbose_name=_("occurred")
#     )
#     closed = models.BooleanField(default=False)
#     objects = IncidentManager()
# 
#     def updates_by_ctime(self):
#         return self.updates.order_by("-created")
# 
#     def should_be_closed(self):
#         try:
#             return self.updates.latest("created").status == 3
#         except IncidentUpdate.DoesNotExist:
#             return False
# 
#     def __str__(self):
#         return self.name
# 
#     class Meta:
#         verbose_name = _("incident")
#         verbose_name_plural = _("incidents")
# 
# Path: statusboard/models.py
# class Service(TimeStampedModel):
#     name = models.CharField(
#         max_length=255, unique=True, verbose_name=_("name")
#     )
#     description = models.TextField(blank=True, verbose_name=_("description"))
#     href = models.URLField(blank=True)
#     status = models.IntegerField(
#         choices=SERVICE_STATUSES, verbose_name=_("status")
#     )
#     priority = models.PositiveIntegerField(
#         default=0, verbose_name=_("priority")
#     )
#     groups = models.ManyToManyField(
#         "ServiceGroup",
#         blank=True,
#         related_name="services",
#         related_query_name="service",
#         verbose_name=_("groups"),
#     )
#     objects = ServiceManager()
# 
#     def __str__(self):
#         return self.name
# 
#     def __init__(self, *args, **kwargs):
#         super(Service, self).__init__(*args, **kwargs)
#         self._status = self.status
# 
#     def save(self, *args, **kwargs):
#         # When the instance is saved, the original status is updated
#         super(Service, self).save(*args, **kwargs)
#         self._status = self.status
# 
#     class Meta:
#         verbose_name = _("service")
#         verbose_name_plural = _("services")
# 
# Path: statusboard/models.py
# class ServiceGroup(TimeStampedModel):
#     name = models.CharField(
#         max_length=255, unique=True, verbose_name=_("name")
#     )
#     priority = models.PositiveIntegerField(
#         default=0, verbose_name=_("priority")
#     )
#     collapse = models.IntegerField(
#         choices=SERVICEGROUP_COLLAPSE_OPTIONS, default=0
#     )
#     objects = ServiceGroupManager()
# 
#     def worst_service(self):
#         return self.services.all().latest("status")
# 
#     def collapsed(self):
#         """Check if the service group should collapse or not.
# 
#         Return true if a group doesn't have any service."""
#         if self.collapse == 0:
#             return False
#         elif self.collapse == 1:
#             return True
#         else:
#             return not self.services.exclude(status=0).exists()
# 
#     def is_empty_group(self):
#         return not self.services.all().exists()
# 
#     def __str__(self):
#         return self.name
# 
#     class Meta:
#         verbose_name = _("service group")
#         verbose_name_plural = _("service groups")
# 
# Path: statusboard/models.py
# class IncidentUpdate(TimeStampedModel):
#     incident = models.ForeignKey(
#         Incident,
#         related_name="updates",
#         on_delete=models.deletion.CASCADE,
#         related_query_name="update",
#         verbose_name=_("incident"),
#     )
#     status = models.IntegerField(
#         choices=INCIDENT_STATUSES, verbose_name=_("status")
#     )
#     description = models.TextField(verbose_name=_("description"))
# 
#     def __str__(self):
#         return "Update {} {}".format(self.incident.name, self.modified)
# 
#     class Meta:
#         verbose_name = _("incident update")
#         verbose_name_plural = _("incident updates")
# 
# Path: statusboard/models.py
# class Maintenance(TimeStampedModel):
#     scheduled = models.DateTimeField(verbose_name=_("scheduled start"))
#     name = models.CharField(max_length=255, verbose_name=_("name"))
#     description = models.TextField(verbose_name=_("description"))
# 
#     def __str__(self):
#         return self.name
# 
#     class Meta:
#         verbose_name = _("maintenance")
#         verbose_name_plural = _("maintenances")
# 
# Path: statusboard/models.py
# SERVICE_STATUSES = (
#     (0, _("Operational")),
#     (1, _("Performance issues")),
#     (2, _("Partial outage")),
#     (3, _("Major outage")),
# )


# Path: statusboard/forms.py
from django import forms
from django.forms import inlineformset_factory
from django.utils.translation import ugettext_lazy as _
from .models import Incident
from .models import Service
from .models import ServiceGroup
from .models import IncidentUpdate
from .models import Maintenance
from .models import SERVICE_STATUSES

            (
                "Update the status of involved services "
                "(an empty value means that they will be left unaltered)"
            )
        ),
        required=False,
    )

    class Meta:
        model = Incident
        fields = ["name", "occurred", "services", "service_status"]

    def __init__(self, *args, **kwargs):
        super(IncidentForm, self).__init__(*args, **kwargs)
        self.fields["services"].queryset = Service.objects.order_by("name")

    def save(self, commit=True):
        model = super(IncidentForm, self).save(commit=False)
        status = self.cleaned_data["service_status"]

        if commit:
            model.save()
            self.save_m2m()
            if status not in (None, ""):
                model.services.update(status=status)

        return model


IncidentUpdateFormSet = inlineformset_factory(

    Incident, IncidentUpdate, fields=("status", "description"), extra=1