Skip to content

Instantly share code, notes, and snippets.

@hub-cap
Last active December 16, 2015 01:59
Show Gist options
  • Save hub-cap/5359056 to your computer and use it in GitHub Desktop.
Save hub-cap/5359056 to your computer and use it in GitHub Desktop.
class ActionsDecorator(object):
def __init__(self, action=None, event=None):
self.action = action
self.event = event
self.context = local.store.context
def __call__(self, func):
def wrapped(*args, **kwargs):
event = None
try:
event = self.event_log(self.action, self.event, *args, **kwargs)
return func(*args, **kwargs)
except Exception, e:
tb = traceback.format_exc()
self.event_exception(event, e, tb)
return wrapped
def event_log(self, action, event, *args, **kwargs):
uuid = self.find_uuid(*args[1:])
try:
existing_action = models.Action.find_by_uuid_and_action(
self.context, uuid, action)
# just add a event to the existing action
return models.ActionEvent.create(event=event,
action_id=existing_action.id)
except exception.ModelNotFoundError:
# create a new action
new_action = models.Action.create(
action=action,
action_uuid=uuid,
request_id=self.context.request_id,
user_id=self.context.user,
tenant_id=self.context.tenant)
return models.ActionEvent.create(
event=event,
action_id=new_action.id)
def event_exception(self, event, ex, tb):
event.update(traceback=tb)
action = models.Action.find_by(id=event.id)
action.message = ex.message
def find_uuid(self, *args, **kwargs):
# We need to find a uuid
# If its not in the the local.store, then
# assume its in the first param in the set of args.
# This assumption is generally a-ok since most of the
# calls assume the XXX_id is the first of the params
if hasattr(local.store, 'action_uuid'):
return local.store.action_uuid
else:
return args[0]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment