refactor(api): fix pylint errors
This commit is contained in:
parent
bf8db3e6f0
commit
0bbd46c33f
14 changed files with 159 additions and 169 deletions
|
@ -1,5 +1,3 @@
|
|||
import os
|
||||
|
||||
from django.conf import settings
|
||||
from model_bakery import baker
|
||||
from rest_framework.test import APITestCase
|
||||
|
@ -30,13 +28,13 @@ class TestFileViewSet(APITestCase):
|
|||
"storage.MusicDir",
|
||||
directory=str(fixture_path),
|
||||
)
|
||||
f = baker.make(
|
||||
file = baker.make(
|
||||
"storage.File",
|
||||
directory=music_dir,
|
||||
mime="audio/mp3",
|
||||
filepath=AUDIO_FILENAME,
|
||||
)
|
||||
path = self.path.format(id=str(f.pk))
|
||||
path = self.path.format(id=str(file.pk))
|
||||
self.client.credentials(HTTP_AUTHORIZATION=f"Api-Key {self.token}")
|
||||
response = self.client.get(path)
|
||||
self.assertEqual(response.status_code, 200)
|
||||
|
|
|
@ -2,9 +2,9 @@ import os
|
|||
|
||||
from django.http import FileResponse
|
||||
from django.shortcuts import get_object_or_404
|
||||
from rest_framework import status, viewsets
|
||||
from rest_framework import viewsets
|
||||
from rest_framework.decorators import action
|
||||
from rest_framework.response import Response
|
||||
from rest_framework.serializers import IntegerField
|
||||
|
||||
from ..models import File
|
||||
from ..serializers import FileSerializer
|
||||
|
@ -17,17 +17,10 @@ class FileViewSet(viewsets.ModelViewSet):
|
|||
|
||||
@action(detail=True, methods=["GET"])
|
||||
def download(self, request, pk=None):
|
||||
if pk is None:
|
||||
return Response("No file requested", status=status.HTTP_400_BAD_REQUEST)
|
||||
try:
|
||||
pk = int(pk)
|
||||
except ValueError:
|
||||
return Response(
|
||||
"File ID should be an integer", status=status.HTTP_400_BAD_REQUEST
|
||||
)
|
||||
pk = IntegerField().to_internal_value(data=pk)
|
||||
|
||||
filename = get_object_or_404(File, pk=pk)
|
||||
directory = filename.directory
|
||||
path = os.path.join(directory.directory, filename.filepath)
|
||||
response = FileResponse(open(path, "rb"), content_type=filename.mime)
|
||||
return response
|
||||
file = get_object_or_404(File, pk=pk)
|
||||
storage = file.directory
|
||||
path = os.path.join(storage.directory, file.filepath)
|
||||
|
||||
return FileResponse(open(path, "rb"), content_type=file.mime)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue