filters.py 1.82 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
28
29
30
31
32
    q = fields.SearchFilter(search_fields=[
        'artist_name',
        'title',
        'album_title',
        'library__actor__domain',
    ])
33
34
35
36
37
38
39
40
41
42
43
44

    class Meta:
        model = models.LibraryTrack
        fields = {
            'library': ['exact'],
            'artist_name': ['exact', 'icontains'],
            'title': ['exact', 'icontains'],
            'album_title': ['exact', 'icontains'],
            'audio_mimetype': ['exact', 'icontains'],
        }


45
class FollowFilter(django_filters.FilterSet):
46
    pending = django_filters.CharFilter(method='filter_pending')
47
48
49
50
51
52
53
    ordering = django_filters.OrderingFilter(
        # tuple-mapping retains order
        fields=(
            ('creation_date', 'creation_date'),
            ('modification_date', 'modification_date'),
        ),
    )
54
55
56
57
    q = fields.SearchFilter(search_fields=[
        'actor__domain',
        'actor__preferred_username',
    ])
58
59
60

    class Meta:
        model = models.Follow
61
62
63
64
65
66
        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