Commit c5fba5a2 authored by Tanner Prestegard's avatar Tanner Prestegard

Adding functions for managing signoffs

parent 9eb9604d
......@@ -513,6 +513,18 @@ class GraceDb(GsiRest):
def superevent_categories(self):
return self.service_info.get('superevent-categories')
@property
def instruments(self):
return self.service_info.get('instruments')
@property
def signoff_types(self):
return self.service_info.get('signoff-types')
@property
def signoff_statuses(self):
return self.service_info.get('signoff-statuses')
def request(self, method, *args, **kwargs):
if method.lower() in ['post', 'put']:
kwargs['priming_url'] = self._service_url
......@@ -1887,6 +1899,150 @@ class GraceDb(GsiRest):
body = {'action': action}
return self.post(uri, body=body)
def _signoff_helper(self, object_id, action, template, uri_kwargs,
signoff_type=None, instrument=None, status=None, comment=None):
# uri_kwargs should already have the graceid or superevent_id in it
# Validate args
if signoff_type:
signoff_type = self._getCode(signoff_type, self.signoff_types)
if not signoff_type:
raise ValueError("signoff_type must be one of: {0}".format(
", ".join(self.signoff_types)))
if instrument:
instrument = self._getCode(instrument, self.instruments)
if not instrument:
raise ValueError("instrument must be one of: {0}".format(
", ".join(self.instruments)))
if status:
status = self._getCode(status, self.signoff_statuses)
if not status:
raise ValueError("status must be one of: {0}".format(
", ".join(self.signoff_statuses)))
if signoff_type == 'OP' and not instrument:
raise ValueError('Operator signoffs require an instrument')
# Get HTTP method and args
body = {}
if (action == 'create'):
http_method = "POST"
body['signoff_type'] = signoff_type
body['instrument'] = instrument
body['comment'] = comment
body['status'] = status
elif (action == 'update'):
http_method = "PATCH"
uri_kwargs['typeinst'] = signoff_type + instrument
if comment:
body['comment'] = comment
if status:
body['status'] = status
elif (action == 'get'):
http_method = "GET"
if signoff_type:
uri_kwargs['typeinst'] = signoff_type + instrument
elif (action == 'delete'):
http_method = "DELETE"
uri_kwargs['typeinst'] = signoff_type + instrument
else:
raise ValueError("action should be 'create', 'update', "
"'get', or 'delete'")
uri = template.format(**uri_kwargs)
# Get http method
method = getattr(self, http_method.lower())
if body:
response = method(uri, body=body)
else:
response = method(uri)
return response
@event_or_superevent
def signoffs(self, object_id, signoff_type=None, instrument='', *args,
**kwargs):
"""
Get a list of signoffs for an event or superevent, or a particular
signoff.
Signature:
signoffs(object_id, signoff_type=None, instrument='')
Arguments:
object_id: event graceid or superevent id
signoff_type: 'OP' or 'operator' for operator signoff,
'ADV' or 'advocate' for advocate signoff
instrument: instrument abbreviation ('H1', 'L1', etc.). Blank
string for advocate signoffs
NOTE: not currently implemented for events.
"""
# Get URI template
is_superevent = kwargs.pop('is_superevent', False)
if is_superevent:
if signoff_type:
template = self.templates['superevent-signoff-detail-template']
else:
template = self.templates['superevent-signoff-list-template']
uri_kwargs = {'superevent_id': object_id}
else:
raise NotImplementedError('Not yet implemented for events')
return self._signoff_helper(object_id, 'get', template, uri_kwargs,
signoff_type, instrument)
@event_or_superevent
def create_signoff(self, object_id, signoff_type, status, comment,
instrument='', *args, **kwargs):
# Get URI template
is_superevent = kwargs.pop('is_superevent', False)
if is_superevent:
template = self.templates['superevent-signoff-list-template']
uri_kwargs = {'superevent_id': object_id}
else:
raise NotImplementedError('Not yet implemented for events')
return self._signoff_helper(object_id, 'create', template, uri_kwargs,
signoff_type=signoff_type, instrument=instrument, status=status,
comment=comment)
@event_or_superevent
def update_signoff(self, object_id, signoff_type, status=None,
comment=None, instrument='', *args, **kwargs):
# This will make a PATCH request
# Either status or comment must be included - otherwise the user
# is not updating anything
if not (status or comment):
raise ValueError("Provide at least one of 'status' or 'comment'")
# Get URI template
is_superevent = kwargs.pop('is_superevent', False)
if is_superevent:
template = self.templates['superevent-signoff-detail-template']
uri_kwargs = {'superevent_id': object_id}
else:
raise NotImplementedError('Not yet implemented for events')
return self._signoff_helper(object_id, 'update', template, uri_kwargs,
signoff_type=signoff_type, instrument=instrument, status=status,
comment=comment)
@event_or_superevent
def delete_signoff(self, object_id, signoff_type, instrument='', *args,
**kwargs):
# Get URI template
is_superevent = kwargs.pop('is_superevent', False)
if is_superevent:
template = self.templates['superevent-signoff-detail-template']
uri_kwargs = {'superevent_id': object_id}
else:
raise NotImplementedError('Not yet implemented for events')
return self._signoff_helper(object_id, 'delete', template, uri_kwargs,
signoff_type, instrument)
#-----------------------------------------------------------------
# TBD
# Media Types
......
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