Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closes #7: Implement OPTIONS method #29

Merged
merged 1 commit into from
Sep 26, 2016
Merged

Conversation

khorolets
Copy link
Collaborator

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.

@coveralls
Copy link

coveralls commented Sep 26, 2016

Coverage Status

Coverage increased (+0.07%) to 91.21% when pulling 539ae85 on khorolets:implement_options into 7dc29bc on frol:master.

1 similar comment
@coveralls
Copy link

Coverage Status

Coverage increased (+0.07%) to 91.21% when pulling 539ae85 on khorolets:implement_options into 7dc29bc on frol:master.

def wrapper(*args, **kwargs):
kwargs[object_arg_name] = resolver(kwargs)
return func(*args, **kwargs)
return wrapper
if isinstance(func, type):
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please, swap the if block with the wrapper function as if it is a class, there is no need in the function at all.

@@ -135,6 +139,18 @@ def dump_wrapper(*args, **kwargs):
return model.dump(response).data
return dump_wrapper

def dump_response_with_model_decorator(func):
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess, it is a duplicate chunk of code.

@@ -153,6 +157,7 @@ def decorator(func):
# Avoid circilar dependency
from app.modules.users import permissions


Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks like a redundant extra new line (1 blank line is enough).

@@ -170,6 +175,9 @@ def wrapper(*args, **kwargs):

protected_func = _permission_decorator(func)

if not hasattr(protected_func, '_decorators'):
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please, create a separate function to avoid such XXX code duplication. I'm still in doubt if I advised a right way of doing this (I mean using _decorators attribute on the function itself).

@@ -46,6 +46,12 @@ def resolve_object(self, object_arg_name, resolver):
... # user is a User instance here
"""
def decorator(func):
if isinstance(func, type):
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, I have just realized that we need to rename func to func_or_class here since we support both use-cases.

@coveralls
Copy link

coveralls commented Sep 26, 2016

Coverage Status

Coverage increased (+0.07%) to 91.21% when pulling 47415a7 on khorolets:implement_options into 7dc29bc on frol:master.

@@ -83,6 +83,8 @@ def decorator(func):
from app.extensions import oauth2
from app.modules.users import permissions

self.register_decorator_in_function(func, decorator)
Copy link
Owner

@frol frol Sep 26, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please, use oauth2.require_oauth(*oauth_scopes) as a decorator here (and move it closer to the oauth_protected_func = ... line) since otherwise this will call self.doc and other unnecessary stuff.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

self.register_decorator_in_function(func, oauth2.require_oauth(*oauth_scopes))

@@ -170,6 +172,7 @@ def wrapper(*args, **kwargs):

protected_func = _permission_decorator(func)

self.register_decorator_in_function(protected_func, decorator)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In fact, we only need to register _permission_decorator.

@coveralls
Copy link

coveralls commented Sep 26, 2016

Coverage Status

Coverage increased (+0.07%) to 91.21% when pulling 011d393 on khorolets:implement_options into 7dc29bc on frol:master.

1 similar comment
@coveralls
Copy link

coveralls commented Sep 26, 2016

Coverage Status

Coverage increased (+0.07%) to 91.21% when pulling 011d393 on khorolets:implement_options into 7dc29bc on frol:master.

@@ -199,6 +202,15 @@ def wrapper(*args, **kwargs):

return decorator

def register_decorator_in_function(self, func, decorator_to_register):
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. Let's start the name with underscore since it is for internal use only.
  2. I think since we only register access-restriction-related decorators, we may indicate this in the function name, e.g. _register_access_restriction_decorator.
  3. Also, rename _decorators to _access_restriction_decorators.

@coveralls
Copy link

coveralls commented Sep 26, 2016

Coverage Status

Coverage increased (+0.08%) to 91.221% when pulling bbb397a on khorolets:implement_options into 7dc29bc on frol:master.

def wrapper(cls):
if 'OPTIONS' in cls.methods:
cls.options = self.response(code=204)(cls.options)
doc = kwargs.pop('doc', None)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You don't need to copy this, just nest the decorators:

def route(...):
    base_wrapper = super().route(self, *args, **kwargs)
    def wrapper(cls):
        if 'OPTIONS'...
        ...
        return base_wrapper(cls)
    return wrapper

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, thanks!

@@ -83,6 +83,7 @@ def decorator(func):
from app.extensions import oauth2
from app.modules.users import permissions


Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No need for this empty line either.

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.
@coveralls
Copy link

coveralls commented Sep 26, 2016

Coverage Status

Coverage increased (+0.08%) to 91.221% when pulling fc2722b on khorolets:implement_options into 7dc29bc on frol:master.

@frol frol merged commit e17bde5 into frol:master Sep 26, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants