filters.py 2.78 KB
Newer Older
1
2
import django_filters

3
from funkwhale_api.common import fields
4
from funkwhale_api.common import search
5

6
7
8
from . import models


Eliot Berriot's avatar
Eliot Berriot committed
9
class LibraryFilter(django_filters.FilterSet):
Eliot Berriot's avatar
Eliot Berriot committed
10
11
    approved = django_filters.BooleanFilter("following__approved")
    q = fields.SearchFilter(search_fields=["actor__domain"])
Eliot Berriot's avatar
Eliot Berriot committed
12
13
14
15

    class Meta:
        model = models.Library
        fields = {
Eliot Berriot's avatar
Eliot Berriot committed
16
17
18
19
20
            "approved": ["exact"],
            "federation_enabled": ["exact"],
            "download_files": ["exact"],
            "autoimport": ["exact"],
            "tracks_count": ["exact"],
Eliot Berriot's avatar
Eliot Berriot committed
21
22
23
        }


24
class LibraryTrackFilter(django_filters.FilterSet):
Eliot Berriot's avatar
Eliot Berriot committed
25
26
    library = django_filters.CharFilter("library__uuid")
    status = django_filters.CharFilter(method="filter_status")
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
    q = fields.SmartSearchFilter(
        config=search.SearchConfig(
            search_fields={
                "domain": {"to": "library__actor__domain"},
                "artist": {"to": "artist_name"},
                "album": {"to": "album_title"},
                "title": {"to": "title"},
            },
            filter_fields={
                "domain": {"to": "library__actor__domain"},
                "artist": {"to": "artist_name__iexact"},
                "album": {"to": "album_title__iexact"},
                "title": {"to": "title__iexact"},
            },
        )
Eliot Berriot's avatar
Eliot Berriot committed
42
    )
43

44
    def filter_status(self, queryset, field_name, value):
Eliot Berriot's avatar
Eliot Berriot committed
45
        if value == "imported":
46
            return queryset.filter(local_track_file__isnull=False)
Eliot Berriot's avatar
Eliot Berriot committed
47
48
49
50
51
52
        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")
53
54
        return queryset

55
56
57
    class Meta:
        model = models.LibraryTrack
        fields = {
Eliot Berriot's avatar
Eliot Berriot committed
58
59
60
61
62
            "library": ["exact"],
            "artist_name": ["exact", "icontains"],
            "title": ["exact", "icontains"],
            "album_title": ["exact", "icontains"],
            "audio_mimetype": ["exact", "icontains"],
63
64
65
        }


66
class FollowFilter(django_filters.FilterSet):
Eliot Berriot's avatar
Eliot Berriot committed
67
    pending = django_filters.CharFilter(method="filter_pending")
68
69
70
    ordering = django_filters.OrderingFilter(
        # tuple-mapping retains order
        fields=(
Eliot Berriot's avatar
Eliot Berriot committed
71
72
73
74
75
76
            ("creation_date", "creation_date"),
            ("modification_date", "modification_date"),
        )
    )
    q = fields.SearchFilter(
        search_fields=["actor__domain", "actor__preferred_username"]
77
78
79
80
    )

    class Meta:
        model = models.Follow
Eliot Berriot's avatar
Eliot Berriot committed
81
        fields = ["approved", "pending", "q"]
82
83

    def filter_pending(self, queryset, field_name, value):
Eliot Berriot's avatar
Eliot Berriot committed
84
        if value.lower() in ["true", "1", "yes"]:
85
86
            queryset = queryset.filter(approved__isnull=True)
        return queryset