Verified Commit 4f8db661 authored by Eliot Berriot's avatar Eliot Berriot
Browse files

See #228: now expose library track status in API

parent f12fe004
......@@ -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:
......
......@@ -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)
......
......@@ -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 = (
......
......@@ -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'
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment