Skip to content

Instantly share code, notes, and snippets.

@MrLotU
Last active April 24, 2020 09:40
Show Gist options
  • Save MrLotU/fb2b84abd9584556ca22e5280fac884d to your computer and use it in GitHub Desktop.
Save MrLotU/fb2b84abd9584556ca22e5280fac884d to your computer and use it in GitHub Desktop.
@staticmethod
def log(f=None):
def deco(f):
@functools.wraps(f)
def func(*args, **kwargs):
audit = AuditModel()
user = request.headers.get("X-Auth-User")
if user is None:
return 'UNAUTHORIZED. Please provide X-Auth-User header.', 403
audit.user = user
audit.action = f'{f.__module__}.{f.__name__}'
audit.timestamp = datetime.utcnow()
if 'audit' in f.__code__.co_varnames:
kwargs['audit'] = audit
result = f(*args, **kwargs)
if not audit.abort:
audit.save(force_insert=True)
return result
return func
if f and callable(f):
return deco(f)
return deco
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment