def get_security_requirement(self, auto_schema): # TODO generalize (will also be used in versioning) from collections import namedtuple from oauth2_provider.contrib.rest_framework import ( IsAuthenticatedOrTokenHasScope, TokenHasScope, TokenMatchesOASRequirements, ) Request = namedtuple('Request', ['method']) view = auto_schema.view request = Request(auto_schema.method) for permission in auto_schema.view.get_permissions(): if isinstance(permission, TokenMatchesOASRequirements): return { self.name: permission.get_required_alternate_scopes(request, view) } if isinstance(permission, IsAuthenticatedOrTokenHasScope): return {self.name: TokenHasScope().get_scopes(request, view)} if isinstance(permission, TokenHasScope): # catch-all for subclasses of TokenHasScope like TokenHasReadWriteScope return {self.name: permission.get_scopes(request, view)}
def get_permissions(self): if self.action in ['create']: return [AllowAny()] return [TokenHasReadWriteScope(), TokenHasScope()]