Skip to content

Commit

Permalink
Use edit mixins for Playlists (#1284)
Browse files Browse the repository at this point in the history
* Use mixins to edit playlists

* Add titleSort attribute to Playlists

* Update Playlists edit tests

* Fix deprecation warning stacklevel

* Fix edit title for m3u playlist
  • Loading branch information
JonnyWong16 authored Nov 13, 2023
1 parent 47f11f0 commit 556b4b3
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 5 deletions.
7 changes: 7 additions & 0 deletions plexapi/mixins.py
Original file line number Diff line number Diff line change
Expand Up @@ -1223,3 +1223,10 @@ class CollectionEditMixins(
LabelMixin
):
pass


class PlaylistEditMixins(
ArtLockMixin, PosterLockMixin,
SortTitleMixin, SummaryMixin, TitleMixin
):
pass
16 changes: 12 additions & 4 deletions plexapi/playlist.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,16 @@
from plexapi.base import Playable, PlexPartialObject
from plexapi.exceptions import BadRequest, NotFound, Unsupported
from plexapi.library import LibrarySection, MusicSection
from plexapi.mixins import SmartFilterMixin, ArtMixin, PosterMixin
from plexapi.mixins import SmartFilterMixin, ArtMixin, PosterMixin, PlaylistEditMixins
from plexapi.utils import deprecated


@utils.registerPlexObject
class Playlist(
PlexPartialObject, Playable,
SmartFilterMixin,
ArtMixin, PosterMixin
ArtMixin, PosterMixin,
PlaylistEditMixins
):
""" Represents a single Playlist.
Expand All @@ -42,6 +43,7 @@ class Playlist(
smart (bool): True if the playlist is a smart playlist.
summary (str): Summary of the playlist.
title (str): Name of the playlist.
titleSort (str): Title to use when sorting (defaults to title).
type (str): 'playlist'
updatedAt (datetime): Datetime the playlist was updated.
"""
Expand Down Expand Up @@ -71,6 +73,7 @@ def _loadData(self, data):
self.smart = utils.cast(bool, data.attrib.get('smart'))
self.summary = data.attrib.get('summary')
self.title = data.attrib.get('title')
self.titleSort = data.attrib.get('titleSort', self.title)
self.type = data.attrib.get('type')
self.updatedAt = utils.toDatetime(data.attrib.get('updatedAt'))
self._items = None # cache for self.items
Expand Down Expand Up @@ -224,7 +227,7 @@ def addItems(self, items):
self._server.query(key, method=self._server._session.put)
return self

@deprecated('use "removeItems" instead', stacklevel=3)
@deprecated('use "removeItems" instead')
def removeItem(self, item):
self.removeItems(item)

Expand Down Expand Up @@ -308,10 +311,15 @@ def updateFilters(self, limit=None, sort=None, filters=None, **kwargs):

def _edit(self, **kwargs):
""" Actually edit the playlist. """
if isinstance(self._edits, dict):
self._edits.update(kwargs)
return self

key = f'{self.key}{utils.joinArgs(kwargs)}'
self._server.query(key, method=self._server._session.put)
return self

@deprecated('use "editTitle" and "editSummary" instead')
def edit(self, title=None, summary=None):
""" Edit the playlist.
Expand Down Expand Up @@ -384,7 +392,7 @@ def _createFromM3U(cls, server, title, section, m3ufilepath):
key = f"/playlists/upload{utils.joinArgs(args)}"
server.query(key, method=server._session.post)
try:
return server.playlists(sectionId=section.key, guid__endswith=m3ufilepath)[0].edit(title=title).reload()
return server.playlists(sectionId=section.key, guid__endswith=m3ufilepath)[0].editTitle(title).reload()
except IndexError:
raise BadRequest('Failed to create playlist from m3u file.') from None

Expand Down
8 changes: 7 additions & 1 deletion tests/test_playlist.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ def test_Playlist_edit(plex, movie):
playlist = plex.createPlaylist(title, items=movie)
assert playlist.title == title
assert playlist.summary == ''
playlist.edit(title=new_title, summary=new_summary)
playlist.editTitle(new_title).editSummary(new_summary)
playlist.reload()
assert playlist.title == new_title
assert playlist.summary == new_summary
Expand Down Expand Up @@ -330,3 +330,9 @@ def test_Playlist_mixins_images(playlist):
test_mixins.lock_poster(playlist)
test_mixins.edit_art(playlist)
test_mixins.edit_poster(playlist)


def test_Playlist_mixins_fields(playlist):
test_mixins.edit_sort_title(playlist)
test_mixins.edit_summary(playlist)
test_mixins.edit_title(playlist)

0 comments on commit 556b4b3

Please sign in to comment.