kopia lustrzana https://dev.funkwhale.audio/funkwhale/funkwhale
				
				
				
			Added artist filtering on API
							rodzic
							
								
									c4ed7513c5
								
							
						
					
					
						commit
						c6d408924b
					
				|  | @ -309,6 +309,7 @@ REST_FRAMEWORK = { | |||
|     ), | ||||
|     'DEFAULT_FILTER_BACKENDS': ( | ||||
|         'rest_framework.filters.OrderingFilter', | ||||
|         'django_filters.rest_framework.DjangoFilterBackend', | ||||
|     ) | ||||
| } | ||||
| 
 | ||||
|  |  | |||
|  | @ -0,0 +1,12 @@ | |||
| import django_filters | ||||
| 
 | ||||
| from . import models | ||||
| 
 | ||||
| 
 | ||||
| class ArtistFilter(django_filters.FilterSet): | ||||
| 
 | ||||
|     class Meta: | ||||
|         model = models.Artist | ||||
|         fields = { | ||||
|             'name': ['exact', 'iexact', 'startswith'] | ||||
|         } | ||||
|  | @ -182,6 +182,21 @@ class TestAPI(TMPDirTestCaseMixin, TestCase): | |||
| 
 | ||||
|         self.assertJSONEqual(expected, json.loads(response.content.decode('utf-8'))) | ||||
| 
 | ||||
|     def test_can_search_artist_by_name_start(self): | ||||
|         artist1 = factories.ArtistFactory(name='alpha') | ||||
|         artist2 = factories.ArtistFactory(name='beta') | ||||
|         results = { | ||||
|             'next': None, | ||||
|             'previous': None, | ||||
|             'count': 1, | ||||
|             'results': [serializers.ArtistSerializerNested(artist1).data] | ||||
|         } | ||||
|         expected = json.dumps(results) | ||||
|         url = self.reverse('api:v1:artists-list') | ||||
|         response = self.client.get(url, {'name__startswith': 'a'}) | ||||
| 
 | ||||
|         self.assertJSONEqual(expected, json.loads(response.content.decode('utf-8'))) | ||||
| 
 | ||||
|     def test_can_search_tracks(self): | ||||
|         artist1 = models.Artist.objects.create(name='Test1') | ||||
|         artist2 = models.Artist.objects.create(name='Test2') | ||||
|  |  | |||
|  | @ -21,8 +21,10 @@ from taggit.models import Tag | |||
| from . import models | ||||
| from . import serializers | ||||
| from . import importers | ||||
| from . import filters | ||||
| from . import utils | ||||
| 
 | ||||
| 
 | ||||
| class SearchMixin(object): | ||||
|     search_fields = [] | ||||
| 
 | ||||
|  | @ -52,7 +54,8 @@ class ArtistViewSet(SearchMixin, viewsets.ReadOnlyModelViewSet): | |||
|     serializer_class = serializers.ArtistSerializerNested | ||||
|     permission_classes = [ConditionalAuthentication] | ||||
|     search_fields = ['name'] | ||||
|     ordering_fields = ('creation_date',) | ||||
|     ordering_fields = ('creation_date', 'name') | ||||
|     filter_class = filters.ArtistFilter | ||||
| 
 | ||||
| 
 | ||||
| class AlbumViewSet(SearchMixin, viewsets.ReadOnlyModelViewSet): | ||||
|  |  | |||
|  | @ -45,6 +45,7 @@ django-taggit==0.22.1 | |||
| persisting-theory==0.2.1 | ||||
| django-versatileimagefield==1.7.1 | ||||
| django-cachalot==1.5.0 | ||||
| django-filter==1.1 | ||||
| django-rest-auth==0.9.1 | ||||
| beautifulsoup4==4.6.0 | ||||
| Markdown==2.6.8 | ||||
|  |  | |||
		Ładowanie…
	
		Reference in New Issue
	
	 Eliot Berriot
						Eliot Berriot