2018-07-17 11:09:13 +00:00
|
|
|
from django.db.models import Count
|
2018-03-19 18:07:52 +00:00
|
|
|
from django_filters import rest_framework as filters
|
|
|
|
|
2018-03-21 10:58:11 +00:00
|
|
|
from funkwhale_api.music import utils
|
|
|
|
|
2018-03-19 18:07:52 +00:00
|
|
|
from . import models
|
|
|
|
|
|
|
|
|
|
|
|
class PlaylistFilter(filters.FilterSet):
|
2019-01-11 11:03:06 +00:00
|
|
|
q = filters.CharFilter(field_name="_", method="filter_q")
|
|
|
|
playable = filters.BooleanFilter(field_name="_", method="filter_playable")
|
2018-03-19 18:07:52 +00:00
|
|
|
|
|
|
|
class Meta:
|
|
|
|
model = models.Playlist
|
2018-07-17 11:09:13 +00:00
|
|
|
fields = {
|
|
|
|
"user": ["exact"],
|
|
|
|
"name": ["exact", "icontains"],
|
|
|
|
"q": "exact",
|
2018-09-06 18:35:02 +00:00
|
|
|
"playable": "exact",
|
2018-07-17 11:09:13 +00:00
|
|
|
}
|
|
|
|
|
2018-09-06 18:35:02 +00:00
|
|
|
def filter_playable(self, queryset, name, value):
|
2018-07-17 11:09:13 +00:00
|
|
|
queryset = queryset.annotate(plts_count=Count("playlist_tracks"))
|
|
|
|
if value:
|
|
|
|
return queryset.filter(plts_count__gt=0)
|
|
|
|
else:
|
|
|
|
return queryset.filter(plts_count=0)
|
2018-03-21 10:58:11 +00:00
|
|
|
|
|
|
|
def filter_q(self, queryset, name, value):
|
2018-06-09 13:36:16 +00:00
|
|
|
query = utils.get_query(value, ["name", "user__username"])
|
2018-03-21 10:58:11 +00:00
|
|
|
return queryset.filter(query)
|