filters.py 2.28 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
class LibraryFilter(django_filters.FilterSet):
Eliot Berriot's avatar
Eliot Berriot committed
9
10
    approved = django_filters.BooleanFilter("following__approved")
    q = fields.SearchFilter(search_fields=["actor__domain"])
Eliot Berriot's avatar
Eliot Berriot committed
11
12
13
14

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


23
class LibraryTrackFilter(django_filters.FilterSet):
Eliot Berriot's avatar
Eliot Berriot committed
24
25
26
27
28
    library = django_filters.CharFilter("library__uuid")
    status = django_filters.CharFilter(method="filter_status")
    q = fields.SearchFilter(
        search_fields=["artist_name", "title", "album_title", "library__actor__domain"]
    )
29

30
    def filter_status(self, queryset, field_name, value):
Eliot Berriot's avatar
Eliot Berriot committed
31
        if value == "imported":
32
            return queryset.filter(local_track_file__isnull=False)
Eliot Berriot's avatar
Eliot Berriot committed
33
34
35
36
37
38
        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")
39
40
        return queryset

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


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

    class Meta:
        model = models.Follow
Eliot Berriot's avatar
Eliot Berriot committed
67
        fields = ["approved", "pending", "q"]
68
69

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