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
This commit is contained in:
JonnyWong16 2022-02-26 22:50:04 -08:00 committed by GitHub
parent 0cb8712896
commit 372e620d60
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 60 additions and 3 deletions

View file

@ -32,9 +32,10 @@ class Collection(
art (str): URL to artwork image (/library/metadata/<ratingKey>/art/<artid>).
artBlurHash (str): BlurHash string for artwork image.
childCount (int): Number of items in the collection.
collectionMode (str): How the items in the collection are displayed.
collectionFilterBasedOnUser (int): Which user's activity is used for the collection filtering.
collectionMode (int): How the items in the collection are displayed.
collectionPublished (bool): True if the collection is published to the Plex homepage.
collectionSort (str): How to sort the items in the collection.
collectionSort (int): How to sort the items in the collection.
content (str): The filter URI string for smart collections.
contentRating (str) Content rating (PG-13; NR; TV-G).
fields (List<:class:`~plexapi.media.Field`>): List of field objects.
@ -71,6 +72,7 @@ class Collection(
self.art = data.attrib.get('art')
self.artBlurHash = data.attrib.get('artBlurHash')
self.childCount = utils.cast(int, data.attrib.get('childCount'))
self.collectionFilterBasedOnUser = utils.cast(int, data.attrib.get('collectionFilterBasedOnUser', '0'))
self.collectionMode = utils.cast(int, data.attrib.get('collectionMode', '-1'))
self.collectionPublished = utils.cast(bool, data.attrib.get('collectionPublished', '0'))
self.collectionSort = utils.cast(int, data.attrib.get('collectionSort', '0'))
@ -196,6 +198,32 @@ class Collection(
""" Alias to :func:`~plexapi.library.Collection.item`. """
return self.item(title)
def filterUserUpdate(self, user=None):
""" Update the collection filtering user advanced setting.
Parameters:
user (str): One of the following values:
"admin" (Always the server admin user),
"user" (User currently viewing the content)
Example:
.. code-block:: python
collection.updateMode(user="user")
"""
if not self.smart:
raise BadRequest('Cannot change collection filtering user for a non-smart collection.')
user_dict = {
'admin': 0,
'user': 1
}
key = user_dict.get(user)
if key is None:
raise BadRequest('Unknown collection filtering user: %s. Options %s' % (user, list(user_dict)))
self.editAdvanced(collectionFilterBasedOnUser=key)
def modeUpdate(self, mode=None):
""" Update the collection mode advanced setting.
@ -220,7 +248,7 @@ class Collection(
}
key = mode_dict.get(mode)
if key is None:
raise BadRequest('Unknown collection mode : %s. Options %s' % (mode, list(mode_dict)))
raise BadRequest('Unknown collection mode: %s. Options %s' % (mode, list(mode_dict)))
self.editAdvanced(collectionMode=key)
def sortUpdate(self, sort=None):
@ -238,6 +266,9 @@ class Collection(
collection.updateSort(mode="alpha")
"""
if self.smart:
raise BadRequest('Cannot change collection order for a smart collection.')
sort_dict = {
'release': 0,
'alpha': 1,

View file

@ -13,6 +13,7 @@ def test_Collection_attrs(collection):
assert collection.art is None
assert collection.artBlurHash is None
assert collection.childCount == 1
assert collection.collectionFilterBasedOnUser == 0
assert collection.collectionMode == -1
assert collection.collectionPublished is False
assert collection.collectionSort == 0
@ -66,6 +67,27 @@ def test_Collection_items(collection):
assert len(items) == 1
def test_Collection_filterUserUpdate(plex, movies):
title = "test_Collection_filterUserUpdate"
try:
collection = plex.createCollection(
title=title,
section=movies,
smart=True
)
mode_dict = {"admin": 0, "user": 1}
for key, value in mode_dict.items():
collection.filterUserUpdate(user=key)
collection.reload()
assert collection.collectionFilterBasedOnUser == value
with pytest.raises(BadRequest):
collection.filterUserUpdate(user="bad-user")
collection.filterUserUpdate("admin")
finally:
collection.delete()
def test_Collection_modeUpdate(collection):
mode_dict = {"default": -1, "hide": 0, "hideItems": 1, "showItems": 2}
for key, value in mode_dict.items():
@ -244,6 +266,8 @@ def test_Collection_exceptions(plex, movies, movie, artist):
collection.updateFilters()
with pytest.raises(BadRequest):
collection.addItems(artist)
with pytest.raises(BadRequest):
collection.filterUserUpdate("user")
finally:
collection.delete()
@ -260,6 +284,8 @@ def test_Collection_exceptions(plex, movies, movie, artist):
collection.removeItems(movie)
with pytest.raises(BadRequest):
collection.moveItem(movie)
with pytest.raises(BadRequest):
collection.sortUpdate("custom")
finally:
collection.delete()