Skip to content
Snippets Groups Projects
models.py 54.4 KiB
Newer Older
from math import isnan
from django.urls import reverse
from django.core.exceptions import ValidationError
from django.utils import six
from django.utils.encoding import python_2_unicode_compatible
from django.utils.translation import ugettext_lazy as _
from model_utils.managers import InheritanceManager

from django.contrib.auth import get_user_model
from django.contrib.contenttypes.models import ContentType
from guardian.models import GroupObjectPermission
import logging; log = logging.getLogger(__name__)
Brian Moe's avatar
Brian Moe committed
import os
Brian Moe's avatar
Brian Moe committed
import glue
import glue.ligolw
import glue.ligolw.utils
import glue.ligolw.table
import glue.ligolw.lsctables
from glue.ligolw.ligolw import LIGOLWContentHandler
Brian Moe's avatar
Brian Moe committed
from glue.lal import LIGOTimeGPS

from core.models import AutoIncrementModel, CleanSaveModel
from core.models import LogBase, m2mThroughBase
Tanner Prestegard's avatar
Tanner Prestegard committed
from core.time_utils import posixToGpsTime

from django.conf import settings
try:
    from StringIO import StringIO
except ImportError:  # python >= 3
    from io import StringIO
from .managers import ProductionPipelineManager, ExternalPipelineManager


SERVER_TZ = pytz.timezone(settings.TIME_ZONE)

# Let's say we start here on schema versions
#
# 1.0 -> 1.1   changed EventLog.comment from CharField(length=200) -> TextField
#
schema_version = "1.1"
Brian Moe's avatar
Brian Moe committed

@python_2_unicode_compatible
Brian Moe's avatar
Brian Moe committed
class Group(models.Model):
    name = models.CharField(max_length=20)

    def __str__(self):
        return six.text_type(self.name)
Brian Moe's avatar
Brian Moe committed

@python_2_unicode_compatible
    PIPELINE_TYPE_EXTERNAL = 'E'
    PIPELINE_TYPE_OTHER = 'O'
    PIPELINE_TYPE_SEARCH_OTHER = 'SO'
    PIPELINE_TYPE_SEARCH_PRODUCTION = 'SP'
    PIPELINE_TYPE_CHOICES = (
        (PIPELINE_TYPE_EXTERNAL, 'external'),
        (PIPELINE_TYPE_OTHER, 'other'),
        (PIPELINE_TYPE_SEARCH_OTHER, 'non-production search'),
        (PIPELINE_TYPE_SEARCH_PRODUCTION, 'production search'),
    )
    name = models.CharField(max_length=100)
    # Are submissions allowed for this pipeline?
    enabled = models.BooleanField(default=True)
    # Pipeline type
    pipeline_type = models.CharField(max_length=2,
        choices=PIPELINE_TYPE_CHOICES)

    # Add custom managers; must manually define 'objects' as well
    objects = models.Manager()
    production_objects = ProductionPipelineManager()
    external_objects = ExternalPipelineManager()

    class Meta:
        permissions = (
            ('manage_pipeline', 'Can enable or disable pipeline'),
        )
    def __str__(self):
        return six.text_type(self.name)

class PipelineLog(models.Model):
    PIPELINE_LOG_ACTION_DISABLE = 'D'
    PIPELINE_LOG_ACTION_ENABLE = 'E'
    PIPELINE_LOG_ACTION_CHOICES = (
        (PIPELINE_LOG_ACTION_DISABLE, 'disable'),
        (PIPELINE_LOG_ACTION_ENABLE, 'enable'),
    )
    creator = models.ForeignKey(UserModel)
    pipeline = models.ForeignKey(Pipeline)
    created = models.DateTimeField(auto_now_add=True)
    action = models.CharField(max_length=10,
        choices=PIPELINE_LOG_ACTION_CHOICES)


@python_2_unicode_compatible
    name = models.CharField(max_length=100)
    description = models.TextField(blank=True)
    # XXX Need any additional fields? Like a PI email? Or perhaps even fk?

    def __str__(self):
        return six.text_type(self.name)

# Label color will be used in CSS, see
# https://www.w3schools.com/colors/colors_names.asp for
# allowed color choices
@python_2_unicode_compatible
class Label(models.Model):
    name = models.CharField(max_length=20, unique=True)
    # XXX really, does this belong here? probably not.
    defaultColor = models.CharField(max_length=20, unique=False,
        default="black")
    description = models.TextField(blank=False)
    # protected = True means that the Label should not be "writeable": i.e.,
    # users should not be able to directly apply or remove it.  This is useful
    # for labels that are added and removed as part of a process, like
    # signoffs, for examples.
    protected = models.BooleanField(default=False)
    def __str__(self):
        return six.text_type(self.name)
Brian Moe's avatar
Brian Moe committed

    class ProtectedLabelError(Exception):
        # To be raised when an attempt is made to apply or remove a
        # protected label to/from an event or superevent
        pass

    class RelatedSignoffExistsError(Exception):
        # To be raised when an attempt is made to apply a "signoff request"
        # label (like ADVREQ, H1OPS, etc.) when a signoff of that type already
        # exists (example: an advocate signoff exists and ADVOK or ADVNO is
        # applied, but a user tries to apply 'ADVREQ')
        pass
@python_2_unicode_compatible
Brian Moe's avatar
Brian Moe committed
class Event(models.Model):

    objects = InheritanceManager() # Queries can return subclasses, if available.

#    ANALYSIS_TYPE_CHOICES = (
#        ("LM",  "LowMass"),
#        ("HM",  "HighMass"),
#        ("GRB", "GRB"),
#        ("RD",  "Ringdown"),
#        ("OM",  "Omega"),
#        ("Q",   "Q"),
#        ("X",   "X"),
#        ("CWB", "CWB"),
#        ("MBTA", "MBTAOnline"),
#        ("HWINJ", "HardwareInjection"),
#    )
    DEFAULT_EVENT_NEIGHBORHOOD = (-5,5)
Brian Moe's avatar
Brian Moe committed
    created = models.DateTimeField(auto_now_add=True)
    group = models.ForeignKey(Group)
    #uid = models.CharField(max_length=20, default="")  # XXX deprecated.  should be removed.
    #analysisType = models.CharField(max_length=20, choices=ANALYSIS_TYPE_CHOICES)
    # Events aren't required to be part of a superevent. If the superevent is
    # deleted, don't delete the event; just set this FK to null.
    superevent = models.ForeignKey('superevents.Superevent', null=True,
        related_name='events', on_delete=models.SET_NULL)
    # Note: a default value is needed only during the schema migration
    # that creates this column. After that, we can safely remove it.
    # The presence or absence of the default value has no effect on the DB
    # tables, so removing it does not necessitate a migration.
    pipeline = models.ForeignKey(Pipeline) 
    search = models.ForeignKey(Search, null=True)
Brian Moe's avatar
Brian Moe committed
    instruments = models.CharField(max_length=20, default="")
    nevents = models.PositiveIntegerField(null=True)
Brian Moe's avatar
Brian Moe committed
    far = models.FloatField(null=True)
Brian Moe's avatar
Brian Moe committed
    likelihood = models.FloatField(null=True)

    # NOT from coinc_event, but so, so common.
Loading
Loading full blame...