Skip to content

Commit c0f2c05

Browse files
authored
Merge pull request #433 from pkkid/allowMediaDeletion
allowMediaDeletion
2 parents 4dfdf78 + ec8491c commit c0f2c05

File tree

2 files changed

+57
-0
lines changed

2 files changed

+57
-0
lines changed

plexapi/server.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -493,6 +493,25 @@ def refreshSync(self):
493493
self.refreshSynclist()
494494
self.refreshContent()
495495

496+
def _allowMediaDeletion(self, toggle=False):
497+
""" Toggle allowMediaDeletion.
498+
Parameters:
499+
toggle (bool): True enables Media Deletion
500+
False or None disable Media Deletion (Default)
501+
"""
502+
if self.allowMediaDeletion and toggle is False:
503+
log.debug('Plex is currently allowed to delete media. Toggling off.')
504+
elif self.allowMediaDeletion and toggle is True:
505+
log.debug('Plex is currently allowed to delete media. Toggle set to allow, exiting.')
506+
raise BadRequest('Plex is currently allowed to delete media. Toggle set to allow, exiting.')
507+
elif self.allowMediaDeletion is None and toggle is True:
508+
log.debug('Plex is currently not allowed to delete media. Toggle set to allow.')
509+
else:
510+
log.debug('Plex is currently not allowed to delete media. Toggle set to not allow, exiting.')
511+
raise BadRequest('Plex is currently not allowed to delete media. Toggle set to not allow, exiting.')
512+
value = 1 if toggle is True else 0
513+
return self.query('/:/prefs?allowMediaDeletion=%s' % value, self._session.put)
514+
496515

497516
class Account(PlexObject):
498517
""" Contains the locally cached MyPlex account information. The properties provided don't

tests/test_server.py

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -269,3 +269,41 @@ def test_server_downloadLogs(tmpdir, plex):
269269
def test_server_downloadDatabases(tmpdir, plex):
270270
plex.downloadDatabases(savepath=str(tmpdir), unpack=True)
271271
assert len(tmpdir.listdir()) > 1
272+
273+
def test_server_allowMediaDeletion(account):
274+
plex = PlexServer(utils.SERVER_BASEURL, account.authenticationToken)
275+
# Check server current allowMediaDeletion setting
276+
if plex.allowMediaDeletion:
277+
# If allowed then test disallowed
278+
plex._allowMediaDeletion(False)
279+
time.sleep(1)
280+
plex = PlexServer(utils.SERVER_BASEURL, account.authenticationToken)
281+
assert plex.allowMediaDeletion is None
282+
# Test redundant toggle
283+
with pytest.raises(BadRequest):
284+
plex._allowMediaDeletion(False)
285+
286+
plex._allowMediaDeletion(True)
287+
time.sleep(1)
288+
plex = PlexServer(utils.SERVER_BASEURL, account.authenticationToken)
289+
assert plex.allowMediaDeletion is True
290+
# Test redundant toggle
291+
with pytest.raises(BadRequest):
292+
plex._allowMediaDeletion(True)
293+
else:
294+
# If disallowed then test allowed
295+
plex._allowMediaDeletion(True)
296+
time.sleep(1)
297+
plex = PlexServer(utils.SERVER_BASEURL, account.authenticationToken)
298+
assert plex.allowMediaDeletion is True
299+
# Test redundant toggle
300+
with pytest.raises(BadRequest):
301+
plex._allowMediaDeletion(True)
302+
303+
plex._allowMediaDeletion(False)
304+
time.sleep(1)
305+
plex = PlexServer(utils.SERVER_BASEURL, account.authenticationToken)
306+
assert plex.allowMediaDeletion is None
307+
# Test redundant toggle
308+
with pytest.raises(BadRequest):
309+
plex._allowMediaDeletion(False)

0 commit comments

Comments
 (0)