Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use edit mixins for Playlists #1284

Merged
merged 5 commits into from
Nov 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)