Skip to content

Instantly share code, notes, and snippets.

@VladimirPal
Last active August 29, 2015 14:22
Show Gist options
  • Save VladimirPal/6998849de6feb19975ff to your computer and use it in GitHub Desktop.
Save VladimirPal/6998849de6feb19975ff to your computer and use it in GitHub Desktop.
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):
@wraps(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):
pass
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment