I am new to Django and working on a project. In my project I need to call a same API from multiple places with different "permission_classes"

Viewed 28

I am new to Django and I am working on project for a Blood bank. in my project I have created an API and provided a custom permission class. However I need to call the same API in my project but at different location (in different app). So do I need to create another API for that or I can call the same with a different permission class.

So the API I have created in an app named "Form_Hospital > views.py" and this is the code:-

class RequisitionFormAV(APIView):
    '''
    to post a "Requisition Form" and get list.
    '''
    permission_classes = [IsAuthenticatedActiveHospitalStaff]
    # permission_classes = [IsAdminUser]
    
    def get(self, request):

        search = request.query_params.get('search')
        page = request.query_params.get('page')
        qty = request.query_params.get('qty')
        
        snippet= RequisitionForm.objects.all()
                
        if search:
            snippet = snippet.filter(
                Q(requisition_no__icontains=search) |
                Q(requested_name__icontains=search) |
                Q(organization_name__icontains=search) |
                Q(contact_no__exact=search)
            )
        output = RequisitionFormSerializer(snippet, many=True).data
        if page and qty:
            output = paginations.page(output, qty, page)
        return Response({'data': output}, status=status.HTTP_200_OK)
    
    def post(self, request):
        
        data = request.data
        
        # getting the organization name of logged in user
        data['organization_name'] = request.user.organization.value
        
        # getting the last DonorRegistrationForm object.
        try:
            snippet = RequisitionForm.objects.all().last().requisition_no
            
            _str = snippet[5:]
            _num = int(_str) + 1
            _len = len(str(_num))
            
            if _len == 1:
                _str = "000" + str(_num)
            if _len == 2:
                _str = "00" + str(_num)
            if _len == 3:
                _str = "0" + str(_num)
            if _len > 3:
                _str = str(_num)
                
            data['requisition_no'] = "RE" + data['organization_name'] + _str
            
        except:
            data['requisition_no'] = "RE" + data['organization_name'] + "0001"
            
        data['created_by'] = request.user.id
        serializer = RequisitionFormSerializer(data=data)
        if not serializer.is_valid():
            return Response({'err_msg': serializer.errors}, status=status.HTTP_400_BAD_REQUEST)
        serializer.save()
        return Response({'data': serializer.data}, status=status.HTTP_201_CREATED)

Now, I need to call this same API in different app named "Form" in my project with different permission class. how can I achieve that?

1 Answers

This is how we can call a view inside another view.

class BloodBankRequisitionFormAV(APIView):
    '''
    to get the lsit of requisition forms.
    '''
    permission_classes = [IsAdminUser]
    
    def get(self, request):
        view = RequisitionFormAV.get(self, request).__dict__
        return Response(view)

here, I am to accessing "RequisitionFormAV" in "BloodBankRequisitionFormAV".

First I have created "BloodBankRequisitionFormAV" and then provided a different permission and in get method directly calling the get function of "RequisitionFormAV" and converting the object into dictionary and returning the response into json format.

Related