From 8132e38d46a009d69b5f3364b9d1ce72ba41143f Mon Sep 17 00:00:00 2001 From: spl0k Date: Thu, 1 Nov 2018 14:02:12 +0100 Subject: [PATCH] Delete track dependents before tracks on folder deletion Fixes #129 --- supysonic/managers/folder.py | 7 ++++- tests/__init__.py | 2 ++ tests/issue129.py | 52 ++++++++++++++++++++++++++++++++++++ 3 files changed, 60 insertions(+), 1 deletion(-) create mode 100644 tests/issue129.py diff --git a/supysonic/managers/folder.py b/supysonic/managers/folder.py index c92985e..9ec4643 100644 --- a/supysonic/managers/folder.py +++ b/supysonic/managers/folder.py @@ -13,7 +13,7 @@ import uuid from pony.orm import select from pony.orm import ObjectNotFound -from ..db import Folder, Track, Artist, Album +from ..db import Folder, Track, Artist, Album, User, RatingTrack, StarredTrack from ..py23 import strtype class FolderManager: @@ -51,6 +51,11 @@ class FolderManager: if not folder.root: raise ObjectNotFound(Folder) + for user in User.select(lambda u: u.last_play.root_folder == folder): + user.last_play = None + RatingTrack.select(lambda r: r.rated.root_folder == folder).delete(bulk = True) + StarredTrack.select(lambda s: s.starred.root_folder == folder).delete(bulk = True) + Track.select(lambda t: t.root_folder == folder).delete(bulk = True) Album.prune() Artist.prune() diff --git a/tests/__init__.py b/tests/__init__.py index 80bc299..a7c5e76 100644 --- a/tests/__init__.py +++ b/tests/__init__.py @@ -16,6 +16,7 @@ from . import api from . import frontend from .issue101 import Issue101TestCase +from .issue129 import Issue129TestCase def suite(): suite = unittest.TestSuite() @@ -25,6 +26,7 @@ def suite(): suite.addTest(api.suite()) suite.addTest(frontend.suite()) suite.addTest(unittest.makeSuite(Issue101TestCase)) + suite.addTest(unittest.makeSuite(Issue129TestCase)) return suite diff --git a/tests/issue129.py b/tests/issue129.py new file mode 100644 index 0000000..b57c57b --- /dev/null +++ b/tests/issue129.py @@ -0,0 +1,52 @@ +# coding: utf-8 +# +# This file is part of Supysonic. +# Supysonic is a Python implementation of the Subsonic server API. +# +# Copyright (C) 2018 Alban 'spl0k' FĂ©ron +# +# Distributed under terms of the GNU AGPLv3 license. + +import os.path +import unittest + +from pony.orm import db_session + +from supysonic.db import User, Track, StarredTrack, RatingTrack +from supysonic.managers.folder import FolderManager +from supysonic.scanner import Scanner + +from .testbase import TestBase + +class Issue129TestCase(TestBase): + def setUp(self): + super(Issue129TestCase, self).setUp() + + with db_session: + folder = FolderManager.add('folder', os.path.abspath('tests/assets/folder')) + scanner = Scanner() + scanner.scan(folder) + scanner.finish() + + self.trackid = Track.select().first().id + self.userid = User.get(name = 'alice').id + + def test_last_play(self): + with db_session: + User[self.userid].last_play = Track[self.trackid] + with db_session: + FolderManager.delete_by_name('folder') + + def test_starred(self): + with db_session: + StarredTrack(user = self.userid, starred = self.trackid) + FolderManager.delete_by_name('folder') + + def test_rating(self): + with db_session: + RatingTrack(user = self.userid, rated = self.trackid, rating = 5) + FolderManager.delete_by_name('folder') + +if __name__ == '__main__': + unittest.main() +