Verified Commit fe7ca088 authored by Agate's avatar Agate 💬

Library list endpoint

parent e82a53da
......@@ -3,6 +3,20 @@ import django_filters
from . import models
class LibraryFilter(django_filters.FilterSet):
approved = django_filters.BooleanFilter('following__approved')
class Meta:
model = models.Library
fields = {
'approved': ['exact'],
'federation_enabled': ['exact'],
'download_files': ['exact'],
'autoimport': ['exact'],
'tracks_count': ['exact'],
}
class FollowFilter(django_filters.FilterSet):
ordering = django_filters.OrderingFilter(
# tuple-mapping retains order
......
......@@ -137,6 +137,26 @@ class LibraryActorSerializer(ActorSerializer):
return validated_data
class APILibrarySerializer(serializers.ModelSerializer):
actor = APIActorSerializer
class Meta:
model = models.Library
fields = [
'actor',
'autoimport',
'federation_enabled',
'download_files',
'tracks_count',
'url',
'uuid',
'creation_date',
'follow',
'fetched_date',
'modification_date',
]
class APILibraryCreateSerializer(serializers.ModelSerializer):
actor = serializers.URLField()
......
......@@ -5,6 +5,7 @@ from django.db import transaction
from django.http import HttpResponse
from django.urls import reverse
from rest_framework import mixins
from rest_framework import permissions as rest_permissions
from rest_framework import response
from rest_framework import views
......@@ -164,9 +165,22 @@ class MusicFilesViewSet(FederationMixin, viewsets.GenericViewSet):
return response.Response(data)
class LibraryViewSet(viewsets.GenericViewSet):
class LibraryViewSet(
mixins.ListModelMixin,
viewsets.GenericViewSet):
permission_classes = [rest_permissions.DjangoModelPermissions]
queryset = models.Library.objects.all()
queryset = models.Library.objects.all().select_related(
'actor',
'follow',
)
filter_class = filters.LibraryFilter
serializer_class = serializers.APILibrarySerializer
ordering_fields = (
'id',
'creation_date',
'fetched_date',
'actor__domain',
)
@list_route(methods=['get'])
def scan(self, request, *args, **kwargs):
......
......@@ -261,3 +261,17 @@ def test_can_list_system_actor_followers(factories, superuser_api_client):
assert response.data['results'] == [
serializers.APIFollowSerializer(follow2).data
]
def test_can_list_libraries(factories, superuser_api_client):
library1 = factories['federation.Library']()
library2 = factories['federation.Library']()
url = reverse('api:v1:federation:libraries-list')
response = superuser_api_client.get(url)
assert response.status_code == 200
assert response.data['results'] == [
serializers.APILibrarySerializer(library1).data,
serializers.APILibrarySerializer(library2).data,
]
Markdown is supported
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