From 4f8db661fa80c3b1c38fdbbcbc84f64b0fe644c6 Mon Sep 17 00:00:00 2001 From: Eliot Berriot Date: Thu, 24 May 2018 18:33:26 +0200 Subject: [PATCH] See #228: now expose library track status in API --- api/funkwhale_api/federation/filters.py | 16 ++++++++------ api/funkwhale_api/federation/serializers.py | 13 ++++++++++++ api/funkwhale_api/federation/views.py | 2 +- api/tests/federation/test_serializers.py | 23 +++++++++++++++++++++ 4 files changed, 47 insertions(+), 7 deletions(-) diff --git a/api/funkwhale_api/federation/filters.py b/api/funkwhale_api/federation/filters.py index 7a388ff12..1d93f68b9 100644 --- a/api/funkwhale_api/federation/filters.py +++ b/api/funkwhale_api/federation/filters.py @@ -24,7 +24,7 @@ class LibraryFilter(django_filters.FilterSet): class LibraryTrackFilter(django_filters.FilterSet): library = django_filters.CharFilter('library__uuid') - imported = django_filters.CharFilter(method='filter_imported') + status = django_filters.CharFilter(method='filter_status') q = fields.SearchFilter(search_fields=[ 'artist_name', 'title', @@ -32,11 +32,15 @@ class LibraryTrackFilter(django_filters.FilterSet): 'library__actor__domain', ]) - def filter_imported(self, queryset, field_name, value): - if value.lower() in ['true', '1', 'yes']: - queryset = queryset.filter(local_track_file__isnull=False) - elif value.lower() in ['false', '0', 'no']: - queryset = queryset.filter(local_track_file__isnull=True) + def filter_status(self, queryset, field_name, value): + if value == 'imported': + return queryset.filter(local_track_file__isnull=False) + elif value == 'not_imported': + return queryset.filter( + local_track_file__isnull=True + ).exclude(import_jobs__status='pending') + elif value == 'import_pending': + return queryset.filter(import_jobs__status='pending') return queryset class Meta: diff --git a/api/funkwhale_api/federation/serializers.py b/api/funkwhale_api/federation/serializers.py index 77cb25096..59e93a9b1 100644 --- a/api/funkwhale_api/federation/serializers.py +++ b/api/funkwhale_api/federation/serializers.py @@ -296,6 +296,7 @@ class APILibraryCreateSerializer(serializers.ModelSerializer): class APILibraryTrackSerializer(serializers.ModelSerializer): library = APILibrarySerializer() + status = serializers.SerializerMethodField() class Meta: model = models.LibraryTrack @@ -314,8 +315,20 @@ class APILibraryTrackSerializer(serializers.ModelSerializer): 'title', 'library', 'local_track_file', + 'status', ] + def get_status(self, o): + try: + if o.local_track_file is not None: + return 'imported' + except music_models.TrackFile.DoesNotExist: + pass + for job in o.import_jobs.all(): + if job.status == 'pending': + return 'import_pending' + return 'not_imported' + class FollowSerializer(serializers.Serializer): id = serializers.URLField(max_length=500) diff --git a/api/funkwhale_api/federation/views.py b/api/funkwhale_api/federation/views.py index 01bc02fdf..1350ec731 100644 --- a/api/funkwhale_api/federation/views.py +++ b/api/funkwhale_api/federation/views.py @@ -296,7 +296,7 @@ class LibraryTrackViewSet( 'library__actor', 'library__follow', 'local_track_file', - ) + ).prefetch_related('import_jobs') filter_class = filters.LibraryTrackFilter serializer_class = serializers.APILibraryTrackSerializer ordering_fields = ( diff --git a/api/tests/federation/test_serializers.py b/api/tests/federation/test_serializers.py index f298c61f5..fcf2ba1b6 100644 --- a/api/tests/federation/test_serializers.py +++ b/api/tests/federation/test_serializers.py @@ -699,3 +699,26 @@ def test_api_library_create_serializer_save(factories, r_mock): assert library.tracks_count == 10 assert library.actor == actor assert library.follow == follow + + +def test_tapi_library_track_serializer_not_imported(factories): + lt = factories['federation.LibraryTrack']() + serializer = serializers.APILibraryTrackSerializer(lt) + + assert serializer.get_status(lt) == 'not_imported' + + +def test_tapi_library_track_serializer_imported(factories): + tf = factories['music.TrackFile'](federation=True) + lt = tf.library_track + serializer = serializers.APILibraryTrackSerializer(lt) + + assert serializer.get_status(lt) == 'imported' + + +def test_tapi_library_track_serializer_import_pending(factories): + job = factories['music.ImportJob'](federation=True, status='pending') + lt = job.library_track + serializer = serializers.APILibraryTrackSerializer(lt) + + assert serializer.get_status(lt) == 'import_pending'