RknRobin
RknRobin

Reputation: 401

How can I return an HttpResponse with Django Rest Framework?

I'm building an API function that allows the client to sent a GET request with URL parameters, the server receives and process a file based on the given info, and returns a custom file. The good news is I all of the steps working independently!

I was able to get everything working inside the def get_query function except returning the HttpResponse. The function requires a Queryset response (makes sense I guess..). I figured I needed another function so I could return the HttpResponse, so I created def send_file. I'm not sure how to call this function and right now it just skips it.

views.py.

class SubFileViewSet(viewsets.ModelViewSet):

    queryset = subfiles.objects.all()
    serializer_class = SubFilesSerializer
    permission_classes = (permissions.IsAuthenticatedOrReadOnly,
                          IsOwnerOrReadOnly,)

    def send_file(self, request):

        req = self.request
        make = req.query_params.get('make')
        model = req.query_params.get('model')
        plastic = req.query_params.get('plastic')
        quality = req.query_params.get('qual')
        filenum = req.query_params.get('fileid')
        hotend = req.query_params.get('hotendtemp')
        bed = req.query_params.get('bedtemp')

        if make and model and plastic and quality and filenum:            

            filepath = subfiles.objects.values('STL', 'FileTitle').get(fileid = filenum)

            path = filepath['STL']
            title =  filepath['FileTitle']    

            '''
            #Live processing (very slow)
            gcode = TheMagic(
                make=make, 
                model=model, 
                plastic=plastic, 
                qual=quality, 
                path=path, 
                title=title, 
                hotendtemp=hotend,
                bedtemp=bed)

            '''
            #Local data for faster testing
            gcode = "/home/bradman/Documents/Programming/DjangoWebProjects/3dprinceprod/fullprince/media/uploads"

            test_file = open(gcode, 'r')
            response = HttpResponse(test_file, content_type='text/plain')
            response['Content-Disposition'] = "attachment; filename=%s" % title


            print (response)
            return response

    def perform_create(self, serializer):
        serializer.save(owner=self.request.user)        


    def get_queryset(self):
        req = self.request
        make = req.query_params.get('make')
        model = req.query_params.get('model')
        plastic = req.query_params.get('plastic')
        quality = req.query_params.get('qual')
        filenum = req.query_params.get('fileid')
        hotend = req.query_params.get('hotendtemp')
        bed = req.query_params.get('bedtemp')

        return self.queryset
        #function proved in here then removed and placed above
        #get_query required a queryset response

I have little experience with Django Rest Framework and I'm not sure if there is a better way to implement this, or how I could call the function def send_file?

Upvotes: 0

Views: 2431

Answers (1)

Sardorbek Imomaliev
Sardorbek Imomaliev

Reputation: 15390

You are looking for custom routes. Set your send_file as list_route http://www.django-rest-framework.org/api-guide/viewsets/#marking-extra-actions-for-routing

from rest_framework.decorators import list_route

class SubFileViewSet(viewsets.ModelViewSet):
    ...

    @list_route(methods=['get'])
    def send_file(self, request):
        ...

And then you can access this method as

/subfile/send_file/?params

Upvotes: 2

Related Questions