Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
from flask.ext.restful import marshal_with
from flask.ext.jwt import current_user
from functools import wraps
class marshal_rm_role(object):
def __init__(self, fields, role, remove_fields):
self.fields = fields
self.role = role
self.remove_fields = remove_fields
def __call__(self, f):
def wrapper(*args, **kwargs):
fields = self.fields
if current_user.role not in self.role:
fields = self.fields.copy()
for field in self.remove_fields:
del fields[field]
m = marshal_with(fields)
return m(f)(*args, **kwargs)
return wrapper
# Hot to use
class Profile(ProfileViewMixin):
@marshal_rm_role(profile_fields, ["admin"], ["secret_field1", "secret_field2"])
def get(self):
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment