See #228: now expose library track status in API

merge-requests/237/head
Eliot Berriot 2018-05-24 18:33:26 +02:00
rodzic f12fe0047f
commit 4f8db661fa
Nie znaleziono w bazie danych klucza dla tego podpisu
ID klucza GPG: DD6965E2476E5C27
4 zmienionych plików z 47 dodań i 7 usunięć

Wyświetl plik

@ -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:

Wyświetl plik

@ -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)

Wyświetl plik

@ -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 = (

Wyświetl plik

@ -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'