Commit e4a8b099 authored by Tanner Prestegard's avatar Tanner Prestegard Committed by GraceDB

Add state change checking to update alerts

Add capability for passing kwargs to AlertIssuer classes'
issue_alerts() method.  We take advantage of this in the update
alerts for checking for FAR changes and NSCAND state changes.
parent 8a8fe4b8
......@@ -5,7 +5,7 @@ class AlertIssuer(object):
serializer_class = None
alert_types = None
def __init__(self, obj, alert_type, *args, **kwargs):
def __init__(self, obj, alert_type):
# Check alert type
if alert_type not in self.alert_types:
raise ValueError('alert_type should be in {0}'.format(
......@@ -18,7 +18,7 @@ class AlertIssuer(object):
def serialize_obj(self):
return self.serializer_class(self.obj).data
def issue_alerts(self):
def issue_alerts(self, **kwargs):
# Should be overridden in derived classes
return NotImplemented
......
......@@ -38,9 +38,9 @@ class AlertIssuerWithParentEvent(AlertIssuerWithParentObject):
# Make sure we have the event "subclass"
return self.obj.event.get_subclass_or_self()
def issue_alerts(self):
def issue_alerts(self, **kwargs):
issue_alerts(self.get_parent_obj(), self.alert_type,
self.serialize_obj(), self.serialize_parent())
self.serialize_obj(), self.serialize_parent(), **kwargs)
class EventAlertIssuer(AlertIssuerWithParentEvent):
......@@ -66,10 +66,10 @@ class EventLabelAlertIssuer(AlertIssuerWithParentEvent):
serializer_class = staticmethod(labelToDict)
alert_types = ['label_added', 'label_removed']
def issue_alerts(self):
def issue_alerts(self, **kwargs):
issue_alerts(self.get_parent_obj(), self.alert_type,
self.serialize_obj(), self.serialize_parent(),
label=self.obj.label)
label=self.obj.label, **kwargs)
class EventVOEventAlertIssuer(AlertIssuerWithParentEvent):
......
......@@ -20,9 +20,9 @@ class AlertIssuerWithParentSuperevent(AlertIssuerWithParentObject):
self.obj.__class__.__name__))
return self.obj.superevent
def issue_alerts(self):
def issue_alerts(self, **kwargs):
issue_alerts(self.get_parent_obj(), self.alert_type,
self.serialize_obj(), self.serialize_parent())
self.serialize_obj(), self.serialize_parent(), **kwargs)
class SupereventAlertIssuer(AlertIssuerWithParentSuperevent):
......@@ -43,10 +43,10 @@ class SupereventLabelAlertIssuer(AlertIssuerWithParentSuperevent):
serializer_class = SupereventLabelSerializer
alert_types = ['label_added', 'label_removed']
def issue_alerts(self):
def issue_alerts(self, **kwargs):
issue_alerts(self.get_parent_obj(), self.alert_type,
self.serialize_obj(), self.serialize_parent(),
label=self.obj.label)
label=self.obj.label, **kwargs)
class SupereventVOEventAlertIssuer(AlertIssuerWithParentSuperevent):
......
......@@ -576,6 +576,10 @@ class EventDetail(InheritPermissionsAPIView):
except Exception, e:
return Response(str(e))
# Compile far and nscand for alerts
old_far = event.far
old_nscand = event.is_ns_candidate()
# messages = []
# if event.group.name != request.data['group']:
# messages += [
......@@ -619,7 +623,8 @@ class EventDetail(InheritPermissionsAPIView):
event.save()
# Issue alert
EventAlertIssuer(event, alert_type='update').issue_alerts()
EventAlertIssuer(event, alert_type='update').issue_alerts(
old_far=old_far, old_nscand=old_nscand)
return Response(status=status.HTTP_202_ACCEPTED)
......
......@@ -142,6 +142,7 @@ def update_superevent(superevent, updater, add_log_message=True,
# Write a log message
# 'if' statement to handle patch requests which don't change entire object
superevent_alert_kwargs = {}
if add_log_message:
updates = ["{name}: {old} -> {new}".format(name=k, old=old_params[k],
new=new_params[k]) for k in new_params.keys()
......@@ -177,9 +178,17 @@ def update_superevent(superevent, updater, add_log_message=True,
EventAlertIssuer(new_params['preferred_event'],
alert_type='selected_as_preferred').issue_alerts()
# If preferred event has changed, compile kwargs for superevent
# alert
superevent_alert_kwargs['old_far'] = \
old_params['preferred_event'].far
superevent_alert_kwargs['old_nscand'] = \
old_params['preferred_event'].is_ns_candidate()
# Superevent alerts
if issue_alert:
SupereventAlertIssuer(superevent, alert_type='update').issue_alerts()
SupereventAlertIssuer(superevent, alert_type='update').issue_alerts(
**superevent_alert_kwargs)
return superevent
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment