From 5bc8cd523a1cf42e92f13ae760fa130a1012dbb8 Mon Sep 17 00:00:00 2001 From: Alexander-D-Karpov Date: Fri, 8 Dec 2023 17:17:18 +0300 Subject: [PATCH] updated music and files api --- akarpov/files/views.py | 36 ++++++++++++++++++++------------ akarpov/music/api/serializers.py | 18 ++++++++++++---- akarpov/music/tasks.py | 3 ++- 3 files changed, 39 insertions(+), 18 deletions(-) diff --git a/akarpov/files/views.py b/akarpov/files/views.py index 97331db..585a2b0 100644 --- a/akarpov/files/views.py +++ b/akarpov/files/views.py @@ -1,5 +1,6 @@ import os +import elastic_transport import structlog from django.contrib.auth.mixins import LoginRequiredMixin from django.core.exceptions import PermissionDenied @@ -91,7 +92,9 @@ def get_queryset(self): ): return self.filter(BaseFileItem.objects.none()) return self.filter( - BaseFileItem.objects.filter(user=self.request.user, parent__isnull=True) + BaseFileItem.objects.cache().filter( + user=self.request.user, parent__isnull=True + ) ) @@ -313,18 +316,25 @@ def on_completion(self, uploaded_file, request): } prepared = False if prepared and uploaded_file.size <= request.user.left_file_upload: - f = File.objects.create( - user=request.user, - file_obj=uploaded_file, - name=uploaded_file.name, - parent=folder, - ) - request.user.left_file_upload -= uploaded_file.size - request.user.save() - self.message = { - "message": f"File {f.file.name.split('/')[-1]} successfully uploaded", - "status": True, - } + try: + f = File.objects.create( + user=request.user, + file_obj=uploaded_file, + name=uploaded_file.name, + parent=folder, + ) + request.user.left_file_upload -= uploaded_file.size + request.user.save() + self.message = { + "message": f"File {f.file.name.split('/')[-1]} successfully uploaded", + "status": True, + } + except elastic_transport.ConnectionError: + self.message = { + "message": "Service is down, please try again later or contact support", + "status": False, + } + logger.error("Elasticsearch is down") elif prepared: self.message = { "message": "File is too large, please increase disk space", diff --git a/akarpov/music/api/serializers.py b/akarpov/music/api/serializers.py index bb160ea..a78c63d 100644 --- a/akarpov/music/api/serializers.py +++ b/akarpov/music/api/serializers.py @@ -16,13 +16,13 @@ class AuthorSerializer(serializers.ModelSerializer): class Meta: model = Author - fields = ["name", "slug", "link", "image_cropped"] + fields = ["name", "slug", "image_cropped"] class AlbumSerializer(serializers.ModelSerializer): class Meta: model = Album - fields = ["name", "slug", "link", "image_cropped"] + fields = ["name", "slug", "image_cropped"] class SongSerializer(serializers.ModelSerializer): @@ -61,7 +61,8 @@ class Meta: class ListSongSerializer(SetUserModelSerializer): - album = serializers.CharField(source="album.name", read_only=True) + album = AlbumSerializer(read_only=True) + authors = AuthorSerializer(many=True, read_only=True) liked = serializers.SerializerMethodField(method_name="get_liked") @extend_schema_field(serializers.BooleanField) @@ -74,7 +75,16 @@ def get_liked(self, obj): class Meta: model = Song - fields = ["name", "slug", "file", "image_cropped", "length", "album", "liked"] + fields = [ + "name", + "slug", + "file", + "image_cropped", + "length", + "album", + "authors", + "liked", + ] extra_kwargs = { "slug": {"read_only": True}, "image_cropped": {"read_only": True}, diff --git a/akarpov/music/tasks.py b/akarpov/music/tasks.py index 12d9a6d..69b81cd 100644 --- a/akarpov/music/tasks.py +++ b/akarpov/music/tasks.py @@ -48,7 +48,8 @@ def process_file(path, user_id): @shared_task def load_ym_file_meta(track, user_id): - return yandex.load_file_meta(track, user_id) + yb = yandex.load_file_meta(track, user_id) + return yb @shared_task()