From d781b0e65e3d80fb3a6fa1185da295aa2d48c74a Mon Sep 17 00:00:00 2001
From: Tanner Prestegard <tanner.prestegard@ligo.org>
Date: Thu, 12 Jul 2018 10:45:01 -0500
Subject: [PATCH] Adding category utilities

Adding utils to Superevent and Event models for comparing
categories and deciding if an Event is the correct category to be
part of a superevent.
---
 gracedb/events/models.py      | 18 ++++++++++++++++
 gracedb/superevents/models.py | 39 +++++++++++++++++++++++++++++++++++
 2 files changed, 57 insertions(+)

diff --git a/gracedb/events/models.py b/gracedb/events/models.py
index c72a3fe07..cca02a8b8 100644
--- a/gracedb/events/models.py
+++ b/gracedb/events/models.py
@@ -193,6 +193,24 @@ class Event(models.Model):
         nodes.append(hdf.read())
         return os.path.join(settings.GRACEDB_DATA_DIR, *nodes)
 
+    def is_test(self):
+        return self.group.name == 'Test'
+
+    def is_mdc(self):
+        return (self.search and self.search.name == 'MDC' and
+                self.group.name != 'Test')
+
+    def is_production(self):
+        return not (self.is_test() or self.is_mdc())
+
+    def get_event_category(self):
+        if self.is_test():
+            return 'Test'
+        elif self.is_mdc():
+            return 'MDC'
+        else:
+            return 'Production'
+
     def ligoApproved(self):
         return self.approval_set.filter(approvingCollaboration='L').count()
 
diff --git a/gracedb/superevents/models.py b/gracedb/superevents/models.py
index 27b25a3cd..80d2509ef 100644
--- a/gracedb/superevents/models.py
+++ b/gracedb/superevents/models.py
@@ -208,6 +208,40 @@ class Superevent(CleanSaveModel, ModelToDictMixin, AutoIncrementModel):
         # Call base class delete
         super(Superevent, self).delete(*args, **kwargs)
 
+    def event_compatible(self, event):
+        """
+        Check whether an event is of the correct type to be part of this
+        superevent
+        """
+        return self.__class__.event_category_check(event, self.category)
+
+    @classmethod
+    def event_category_check(cls, event, superevent_category):
+        """
+        Given a superevent type, check whether an event could be added to such
+        a superevent
+        """
+        if (superevent_category == cls.SUPEREVENT_CATEGORY_TEST and
+            not event.is_test()):
+            return False
+        elif (superevent_category == cls.SUPEREVENT_CATEGORY_MDC and
+              not event.is_mdc()):
+            return False
+        elif (superevent_category == cls.SUPEREVENT_CATEGORY_PRODUCTION and
+              (event.is_test() or event.is_mdc())):
+            return False
+        else:
+            return True
+
+    def is_production(self):
+        return self.category == self.SUPEREVENT_CATEGORY_PRODUCTION
+
+    def is_test(self):
+        return self.category == self.SUPEREVENT_CATEGORY_TEST
+
+    def is_mdc(self):
+        return self.category == self.SUPEREVENT_CATEGORY_MDC
+
     def confirm_as_gw(self):
         """
         Sets is_gw to True, calculates the gw_date_number in the database, and
@@ -415,6 +449,11 @@ class Superevent(CleanSaveModel, ModelToDictMixin, AutoIncrementModel):
         # one that datetime can't parse or that the regex won't match
         pass
 
+    class EventTypeMismatchError(Exception):
+        # To be raised when an attempt is made to add an event with an
+        # incompatible type
+        pass
+
 
 class Log(CleanSaveModel, LogBase, AutoIncrementModel):
     """
-- 
GitLab