Skip to content

Instantly share code, notes, and snippets.

@bolerap
Last active November 12, 2016 05:42
Show Gist options
  • Save bolerap/d10d9df3b4b08327c9cae4584710d3df to your computer and use it in GitHub Desktop.
Save bolerap/d10d9df3b4b08327c9cae4584710d3df to your computer and use it in GitHub Desktop.
import functools
import inspect
# decorator
def check_role(f):
# Use this decorator to keep update some attributes of f such as doc string, name ...
@functools.wraps(f)
def wrapper(*args, **kwargs):
# make arguments as a dict, this avoid to check argument which is position or keyword argument
func_args = inspect.getcallargs(f, *args, **kwargs)
if func_args.get('user') != 'admin':
raise Exception('You have not allowed to operate with products')
return f(*args, **kwargs)
return wrapper
# class
class Store(object):
@check_role
def get_product(self, user, product):
return self.storage.get(product)
@check_role
def put_product(self, user, product):
return self.storage.put(product)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment