-
Notifications
You must be signed in to change notification settings - Fork 340
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
Create Resource patched in flask_restplus_patched that inherits restplus Resource and define the `options` method there. Patch some of decorators to be possible to be impemented with classes. Patch some decorators to get access to permission decorators to perform permission checks in option.
- Loading branch information
Showing
4 changed files
with
73 additions
and
5 deletions.
There are no files selected for viewing
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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
# -*- coding: utf-8 -*- | ||
# pylint: disable=protected-access | ||
import flask | ||
from flask_restplus import Resource as OriginalResource | ||
from werkzeug.exceptions import HTTPException | ||
|
||
|
||
class Resource(OriginalResource): | ||
""" | ||
Extended Flast-RESTPlus Resource to add options method | ||
""" | ||
|
||
def __init__(self, *args, **kwargs): | ||
super(Resource, self).__init__(*args, **kwargs) | ||
|
||
def options(self, *args, **kwargs): | ||
""" | ||
Implementation of universal OPTIONS method for resources | ||
This method checks every permissions provided as decorators | ||
for other methods to provide information about what methods | ||
current_user can use | ||
""" | ||
funcs = [getattr(self, m.lower()) for m in self.methods] | ||
methods = self.methods[:] | ||
for method in funcs: | ||
if hasattr(method, '_access_restriction_decorators'): | ||
if not hasattr(method, '_cached_ok_func'): | ||
ok_func = lambda *args, **kwargs: True | ||
for decorator in method._access_restriction_decorators: | ||
ok_func = decorator(ok_func) | ||
method.__dict__['_cached_ok_func'] = ok_func | ||
else: | ||
ok_func = method._cached_ok_func | ||
try: | ||
ok_func(*args, **kwargs) | ||
except HTTPException: | ||
del methods[methods.index(method.__name__.upper())] | ||
else: | ||
pass # !!! all checks are passed, so we should be fine here! | ||
return flask.Response(status=204, headers={'Allow': ", ".join(methods)}) |