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

how to use it function base view #72

Open
pawanvirsingh opened this issue Sep 11, 2017 · 6 comments
Open

how to use it function base view #72

pawanvirsingh opened this issue Sep 11, 2017 · 6 comments

Comments

@pawanvirsingh
Copy link

how to use it function base view

@triat
Copy link
Contributor

triat commented Sep 12, 2017

Hello @pawanvirsingh, can you be more precise in your question ? maybe give some code exemple ? Thanks

@pawanvirsingh
Copy link
Author

pawanvirsingh commented Sep 12, 2017

@triat yes

views.py

from rest_framework import generics
from rest_framework.response import Response
from rest_framework_tracking.mixins import LoggingMixin

class LoggingView(LoggingMixin, generics.GenericAPIView):
def get(self, request):
return Response('with logging')

this is for the class base view

but if wanted it to use in function base view
how could I use this.
like this module

@api_view(['GET'])
@permission_classes((IsAuthenticated, ))
def bankAccountDataAll(request):
if request.method=='GET':
try:
userid = request.user.id
driver_id = userid_to_driverid(userid)
driver_bank_data = driver_bank_details.objects.filter(driver = driver_id)
driver_bank_serializer = bankAccountSerializer(driver_bank_data, many = True)
return Response({"success": True, "data": driver_bank_serializer.data})
except Exception as e:
print e
return Response({"success": False, "data": "no data found"})

@triat
Copy link
Contributor

triat commented Sep 22, 2017

I'm not sure about what's you're trying to do there. What's your looking for is a middleware that log stuff, it's not the purpose of DRF-tracking I think.

@pawanvirsingh
Copy link
Author

hey @triat I am saying that we are using drf tracking in class base view (Django ) can we also use it in function base view -- like any other decorator.

@triat
Copy link
Contributor

triat commented Sep 23, 2017

I'm not sure, this is really specific to DRF. You can try but again, that was not the main purpose there

@witold-gren
Copy link

This is solutions for your task, but I prefer work with class views 😉 it is possible update to work with rewrite should_log and handle_log custom methods 😀

from django.utils import six
from rest_framework_tracking.mixins import LoggingMixin

def tracking_api(logging_methods=[], sensitive_fields={}):
    """
    This decorator must by install before api_view decorator. Eg:

    @tracking_api(['GET', 'POST'])
    @api_view(['GET'])
    def view_packs(request):
        ...
    """
    http_logging_methods = ['GET'] if (logging_methods is None) else logging_methods

    def decorator(func):

        TrackingWrappedAPIView = type(
            six.PY3 and 'TrackingWrappedAPIView' or b'TrackingWrappedAPIView',
            (LoggingMixin, func.view_class),
            {'__doc__': func.__doc__})

        TrackingWrappedAPIView.logging_methods = http_logging_methods

        if sensitive_fields:
            TrackingWrappedAPIView.sensitive_fields = sensitive_fields

        return TrackingWrappedAPIView.as_view()
    return decorator

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

No branches or pull requests

3 participants