Hi All,
Could anyone tell me how do I use the no_logging decorator?
I am not able to retrieve the correct function name from the following piece of code:
def _should_log_route(self, request):
# request.urlconf may be set by middleware or application level code.
# Use this urlconf if present or default to None.
# https://docs.djangoproject.com/en/2.1/topics/http/urls/#how-django-processes-a-request
# https://docs.djangoproject.com/en/2.1/ref/request-response/#attributes-set-by-middleware
urlconf = getattr(request, "urlconf", None)
try:
route_match = resolve(request.path, urlconf=urlconf)
except Resolver404:
return False, None
method = request.method.lower()
view = route_match.func
func = view
# This is for "django rest framework"
if hasattr(view, "cls"):
if hasattr(view, "actions"):
actions = view.actions
method_name = actions.get(method)
if method_name:
func = getattr(view.cls, view.actions[method], None)
else:
func = getattr(view.cls, method, None)
elif hasattr(view, "view_class"):
# This is for django class-based views
func = getattr(view.view_class, method, None)
no_logging = getattr(func, NO_LOGGING_ATTR, False)
return no_logging
And my no_logging function looks like this:
def no_logging():
def wrapper(func):
setattr(func, NO_LOGGING_ATTR, True)
return func
return wrapper
Note: I removed the NO_LOGGING_MSG_ATTR as I did not need that, correspondingly I made changes and removed the because variable in the remaining functions too.
I am using DRF view classes which have a few arguments.
The func evaluates to be one of the argument and not the class itself for which reason, the getattr is not able to retrieve NO_LOGGING_ATTR.
My use case is this:
@no_logging()
class MapImageViewSet(arg1, RetrieveModelMixin, UpdateModelMixin):
The func in above should_log_route class evaluates to be <function RetrieveModelMixin.retrieve at 0x7f8986b91940>.
Can someone tell if I am using the no_logging decorator correctly or there is some error in evaluating the func in the should_log_route function?
Thanks
EDIT:
So, I figured out the class we are decorating the @no_logging func with should be a view class.
And view.cls gets us the complete class name so we can retrieve the attribute set by our no_logging can be retrieved by using:
no_logging = getattr(view.cls, NO_LOGGING_ATTR, False)
The above is true for DRF use cases.
Didn't understand why are we trying to retrieve the method and stuff.
Any explanation is greatly appreciated.
Hi All,
Could anyone tell me how do I use the no_logging decorator?
I am not able to retrieve the correct function name from the following piece of code:
And my no_logging function looks like this:
Note: I removed the NO_LOGGING_MSG_ATTR as I did not need that, correspondingly I made changes and removed the because variable in the remaining functions too.
I am using DRF view classes which have a few arguments.
The func evaluates to be one of the argument and not the class itself for which reason, the getattr is not able to retrieve NO_LOGGING_ATTR.
My use case is this:
The func in above should_log_route class evaluates to be <function RetrieveModelMixin.retrieve at 0x7f8986b91940>.
Can someone tell if I am using the no_logging decorator correctly or there is some error in evaluating the func in the should_log_route function?
Thanks
EDIT:
So, I figured out the class we are decorating the @no_logging func with should be a view class.
And view.cls gets us the complete class name so we can retrieve the attribute set by our no_logging can be retrieved by using:
no_logging = getattr(view.cls, NO_LOGGING_ATTR, False)The above is true for DRF use cases.
Didn't understand why are we trying to retrieve the method and stuff.
Any explanation is greatly appreciated.