Skip to content

Commit 372e620

Browse files
authored
Add support for collection "filtering based on user" advanced setting (#894)
* Add collectionFilterBasedOnUser attribute * Add filterUserUpdate method * Check for smart collection for updateSort * Update test collection filterUserUpdate
1 parent 0cb8712 commit 372e620

2 files changed

Lines changed: 60 additions & 3 deletions

File tree

plexapi/collection.py

Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,10 @@ class Collection(
3232
art (str): URL to artwork image (/library/metadata/<ratingKey>/art/<artid>).
3333
artBlurHash (str): BlurHash string for artwork image.
3434
childCount (int): Number of items in the collection.
35-
collectionMode (str): How the items in the collection are displayed.
35+
collectionFilterBasedOnUser (int): Which user's activity is used for the collection filtering.
36+
collectionMode (int): How the items in the collection are displayed.
3637
collectionPublished (bool): True if the collection is published to the Plex homepage.
37-
collectionSort (str): How to sort the items in the collection.
38+
collectionSort (int): How to sort the items in the collection.
3839
content (str): The filter URI string for smart collections.
3940
contentRating (str) Content rating (PG-13; NR; TV-G).
4041
fields (List<:class:`~plexapi.media.Field`>): List of field objects.
@@ -71,6 +72,7 @@ def _loadData(self, data):
7172
self.art = data.attrib.get('art')
7273
self.artBlurHash = data.attrib.get('artBlurHash')
7374
self.childCount = utils.cast(int, data.attrib.get('childCount'))
75+
self.collectionFilterBasedOnUser = utils.cast(int, data.attrib.get('collectionFilterBasedOnUser', '0'))
7476
self.collectionMode = utils.cast(int, data.attrib.get('collectionMode', '-1'))
7577
self.collectionPublished = utils.cast(bool, data.attrib.get('collectionPublished', '0'))
7678
self.collectionSort = utils.cast(int, data.attrib.get('collectionSort', '0'))
@@ -196,6 +198,32 @@ def get(self, title):
196198
""" Alias to :func:`~plexapi.library.Collection.item`. """
197199
return self.item(title)
198200

201+
def filterUserUpdate(self, user=None):
202+
""" Update the collection filtering user advanced setting.
203+
204+
Parameters:
205+
user (str): One of the following values:
206+
"admin" (Always the server admin user),
207+
"user" (User currently viewing the content)
208+
209+
Example:
210+
211+
.. code-block:: python
212+
213+
collection.updateMode(user="user")
214+
"""
215+
if not self.smart:
216+
raise BadRequest('Cannot change collection filtering user for a non-smart collection.')
217+
218+
user_dict = {
219+
'admin': 0,
220+
'user': 1
221+
}
222+
key = user_dict.get(user)
223+
if key is None:
224+
raise BadRequest('Unknown collection filtering user: %s. Options %s' % (user, list(user_dict)))
225+
self.editAdvanced(collectionFilterBasedOnUser=key)
226+
199227
def modeUpdate(self, mode=None):
200228
""" Update the collection mode advanced setting.
201229
@@ -220,7 +248,7 @@ def modeUpdate(self, mode=None):
220248
}
221249
key = mode_dict.get(mode)
222250
if key is None:
223-
raise BadRequest('Unknown collection mode : %s. Options %s' % (mode, list(mode_dict)))
251+
raise BadRequest('Unknown collection mode: %s. Options %s' % (mode, list(mode_dict)))
224252
self.editAdvanced(collectionMode=key)
225253

226254
def sortUpdate(self, sort=None):
@@ -238,6 +266,9 @@ def sortUpdate(self, sort=None):
238266
239267
collection.updateSort(mode="alpha")
240268
"""
269+
if self.smart:
270+
raise BadRequest('Cannot change collection order for a smart collection.')
271+
241272
sort_dict = {
242273
'release': 0,
243274
'alpha': 1,

tests/test_collection.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ def test_Collection_attrs(collection):
1313
assert collection.art is None
1414
assert collection.artBlurHash is None
1515
assert collection.childCount == 1
16+
assert collection.collectionFilterBasedOnUser == 0
1617
assert collection.collectionMode == -1
1718
assert collection.collectionPublished is False
1819
assert collection.collectionSort == 0
@@ -66,6 +67,27 @@ def test_Collection_items(collection):
6667
assert len(items) == 1
6768

6869

70+
def test_Collection_filterUserUpdate(plex, movies):
71+
title = "test_Collection_filterUserUpdate"
72+
try:
73+
collection = plex.createCollection(
74+
title=title,
75+
section=movies,
76+
smart=True
77+
)
78+
79+
mode_dict = {"admin": 0, "user": 1}
80+
for key, value in mode_dict.items():
81+
collection.filterUserUpdate(user=key)
82+
collection.reload()
83+
assert collection.collectionFilterBasedOnUser == value
84+
with pytest.raises(BadRequest):
85+
collection.filterUserUpdate(user="bad-user")
86+
collection.filterUserUpdate("admin")
87+
finally:
88+
collection.delete()
89+
90+
6991
def test_Collection_modeUpdate(collection):
7092
mode_dict = {"default": -1, "hide": 0, "hideItems": 1, "showItems": 2}
7193
for key, value in mode_dict.items():
@@ -244,6 +266,8 @@ def test_Collection_exceptions(plex, movies, movie, artist):
244266
collection.updateFilters()
245267
with pytest.raises(BadRequest):
246268
collection.addItems(artist)
269+
with pytest.raises(BadRequest):
270+
collection.filterUserUpdate("user")
247271
finally:
248272
collection.delete()
249273

@@ -260,6 +284,8 @@ def test_Collection_exceptions(plex, movies, movie, artist):
260284
collection.removeItems(movie)
261285
with pytest.raises(BadRequest):
262286
collection.moveItem(movie)
287+
with pytest.raises(BadRequest):
288+
collection.sortUpdate("custom")
263289
finally:
264290
collection.delete()
265291

0 commit comments

Comments
 (0)