Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
funkwhale
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Container registry
Model registry
Operate
Environments
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
techknowlogick
funkwhale
Commits
4268fe3a
Verified
Commit
4268fe3a
authored
5 years ago
by
Eliot Berriot
Browse files
Options
Downloads
Patches
Plain Diff
See #170: can now filter tracks and albums by channel
parent
b48a4cd0
Branches
Branches containing commit
No related tags found
No related merge requests found
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
api/funkwhale_api/music/filters.py
+30
-2
30 additions, 2 deletions
api/funkwhale_api/music/filters.py
api/tests/music/test_filters.py
+32
-0
32 additions, 0 deletions
api/tests/music/test_filters.py
with
62 additions
and
2 deletions
api/funkwhale_api/music/filters.py
+
30
−
2
View file @
4268fe3a
from
django_filters
import
rest_framework
as
filters
from
funkwhale_api.audio
import
filters
as
audio_filters
from
funkwhale_api.audio
import
models
as
audio_models
from
funkwhale_api.common
import
fields
from
funkwhale_api.common
import
filters
as
common_filters
from
funkwhale_api.common
import
search
...
...
@@ -20,9 +21,30 @@ def filter_tags(queryset, name, value):
TAG_FILTER
=
common_filters
.
MultipleQueryFilter
(
method
=
filter_tags
)
class
ChannelFilterSet
(
filters
.
FilterSet
):
channel
=
filters
.
CharFilter
(
field_name
=
"
_
"
,
method
=
"
filter_channel
"
)
def
filter_channel
(
self
,
queryset
,
name
,
value
):
if
not
value
:
return
queryset
channel
=
audio_models
.
Channel
.
objects
.
filter
(
uuid
=
value
).
first
()
if
not
channel
:
return
queryset
.
none
()
uploads
=
models
.
Upload
.
objects
.
filter
(
library
=
channel
.
library
)
actor
=
utils
.
get_actor_from_request
(
self
.
request
)
uploads
=
uploads
.
playable_by
(
actor
)
ids
=
uploads
.
values_list
(
self
.
Meta
.
channel_filter_field
,
flat
=
True
)
return
queryset
.
filter
(
pk__in
=
ids
)
class
ArtistFilter
(
audio_filters
.
IncludeChannelsFilterSet
,
moderation_filters
.
HiddenContentFilterSet
):
q
=
fields
.
SearchFilter
(
search_fields
=
[
"
name
"
],
fts_search_fields
=
[
"
body_text
"
])
playable
=
filters
.
BooleanFilter
(
field_name
=
"
_
"
,
method
=
"
filter_playable
"
)
tag
=
TAG_FILTER
...
...
@@ -47,7 +69,9 @@ class ArtistFilter(
class
TrackFilter
(
audio_filters
.
IncludeChannelsFilterSet
,
moderation_filters
.
HiddenContentFilterSet
ChannelFilterSet
,
audio_filters
.
IncludeChannelsFilterSet
,
moderation_filters
.
HiddenContentFilterSet
,
):
q
=
fields
.
SearchFilter
(
search_fields
=
[
"
title
"
,
"
album__title
"
,
"
artist__name
"
],
...
...
@@ -74,6 +98,7 @@ class TrackFilter(
}
hidden_content_fields_mapping
=
moderation_filters
.
USER_FILTER_CONFIG
[
"
TRACK
"
]
include_channels_field
=
"
artist__channel
"
channel_filter_field
=
"
track
"
def
filter_playable
(
self
,
queryset
,
name
,
value
):
actor
=
utils
.
get_actor_from_request
(
self
.
request
)
...
...
@@ -127,7 +152,9 @@ class UploadFilter(audio_filters.IncludeChannelsFilterSet):
class
AlbumFilter
(
audio_filters
.
IncludeChannelsFilterSet
,
moderation_filters
.
HiddenContentFilterSet
ChannelFilterSet
,
audio_filters
.
IncludeChannelsFilterSet
,
moderation_filters
.
HiddenContentFilterSet
,
):
playable
=
filters
.
BooleanFilter
(
field_name
=
"
_
"
,
method
=
"
filter_playable
"
)
q
=
fields
.
SearchFilter
(
...
...
@@ -144,6 +171,7 @@ class AlbumFilter(
fields
=
[
"
playable
"
,
"
q
"
,
"
artist
"
,
"
scope
"
,
"
mbid
"
]
hidden_content_fields_mapping
=
moderation_filters
.
USER_FILTER_CONFIG
[
"
ALBUM
"
]
include_channels_field
=
"
artist__channel
"
channel_filter_field
=
"
track__album
"
def
filter_playable
(
self
,
queryset
,
name
,
value
):
actor
=
utils
.
get_actor_from_request
(
self
.
request
)
...
...
This diff is collapsed.
Click to expand it.
api/tests/music/test_filters.py
+
32
−
0
View file @
4268fe3a
...
...
@@ -110,3 +110,35 @@ def test_track_filter_tag_multiple(
)
assert
filterset
.
qs
==
[
tagged
]
def
test_channel_filter_track
(
factories
,
queryset_equal_list
,
mocker
,
anonymous_user
):
channel
=
factories
[
"
audio.Channel
"
]()
upload
=
factories
[
"
music.Upload
"
](
library
=
channel
.
library
,
playable
=
True
,
track__artist
=
channel
.
artist
)
factories
[
"
music.Track
"
]()
qs
=
upload
.
track
.
__class__
.
objects
.
all
()
filterset
=
filters
.
TrackFilter
(
{
"
channel
"
:
channel
.
uuid
,
"
include_channels
"
:
"
true
"
},
request
=
mocker
.
Mock
(
user
=
anonymous_user
,
actor
=
None
),
queryset
=
qs
,
)
assert
filterset
.
qs
==
[
upload
.
track
]
def
test_channel_filter_album
(
factories
,
queryset_equal_list
,
mocker
,
anonymous_user
):
channel
=
factories
[
"
audio.Channel
"
]()
upload
=
factories
[
"
music.Upload
"
](
library
=
channel
.
library
,
playable
=
True
,
track__artist
=
channel
.
artist
)
factories
[
"
music.Album
"
]()
qs
=
upload
.
track
.
album
.
__class__
.
objects
.
all
()
filterset
=
filters
.
AlbumFilter
(
{
"
channel
"
:
channel
.
uuid
,
"
include_channels
"
:
"
true
"
},
request
=
mocker
.
Mock
(
user
=
anonymous_user
,
actor
=
None
),
queryset
=
qs
,
)
assert
filterset
.
qs
==
[
upload
.
track
.
album
]
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment