Last active
August 29, 2015 14:22
-
-
Save VladimirPal/6998849de6feb19975ff to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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