filters.py 2.33 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
    status = django_filters.CharFilter(method='filter_status')
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
42
43
    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')
44
45
        return queryset

46
47
48
49
50
51
52
53
54
55
56
    class Meta:
        model = models.LibraryTrack
        fields = {
            'library': ['exact'],
            'artist_name': ['exact', 'icontains'],
            'title': ['exact', 'icontains'],
            'album_title': ['exact', 'icontains'],
            'audio_mimetype': ['exact', 'icontains'],
        }


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

    class Meta:
        model = models.Follow
73
74
75
76
77
78
        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