filters.py 2.21 KB
Newer Older
1
2
import django_filters

3
4
from funkwhale_api.common import fields

5
6
7
from . import models


Eliot Berriot's avatar
Eliot Berriot committed
8
9
class LibraryFilter(django_filters.FilterSet):
    approved = django_filters.BooleanFilter('following__approved')
10
11
12
    q = fields.SearchFilter(search_fields=[
        'actor__domain',
    ])
Eliot Berriot's avatar
Eliot Berriot committed
13
14
15
16
17
18
19
20
21
22
23
24

    class Meta:
        model = models.Library
        fields = {
            'approved': ['exact'],
            'federation_enabled': ['exact'],
            'download_files': ['exact'],
            'autoimport': ['exact'],
            'tracks_count': ['exact'],
        }


25
26
class LibraryTrackFilter(django_filters.FilterSet):
    library = django_filters.CharFilter('library__uuid')
27
    imported = django_filters.CharFilter(method='filter_imported')
28
29
30
31
32
33
    q = fields.SearchFilter(search_fields=[
        'artist_name',
        'title',
        'album_title',
        'library__actor__domain',
    ])
34

35
36
37
38
39
40
41
    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)
        return queryset

42
43
44
45
46
47
48
49
50
51
52
    class Meta:
        model = models.LibraryTrack
        fields = {
            'library': ['exact'],
            'artist_name': ['exact', 'icontains'],
            'title': ['exact', 'icontains'],
            'album_title': ['exact', 'icontains'],
            'audio_mimetype': ['exact', 'icontains'],
        }


53
class FollowFilter(django_filters.FilterSet):
54
    pending = django_filters.CharFilter(method='filter_pending')
55
56
57
58
59
60
61
    ordering = django_filters.OrderingFilter(
        # tuple-mapping retains order
        fields=(
            ('creation_date', 'creation_date'),
            ('modification_date', 'modification_date'),
        ),
    )
62
63
64
65
    q = fields.SearchFilter(search_fields=[
        'actor__domain',
        'actor__preferred_username',
    ])
66
67
68

    class Meta:
        model = models.Follow
69
70
71
72
73
74
        fields = ['approved', 'pending', 'q']

    def filter_pending(self, queryset, field_name, value):
        if value.lower() in ['true', '1', 'yes']:
            queryset = queryset.filter(approved__isnull=True)
        return queryset