From adb4e7e89b69b3525ae8817fd3a3fbc7834e3df1 Mon Sep 17 00:00:00 2001 From: spl0k Date: Sat, 10 Mar 2018 18:35:00 +0100 Subject: [PATCH] API: improved client preference passing --- supysonic/api/__init__.py | 6 ++---- supysonic/api/media.py | 2 +- supysonic/db.py | 3 +-- tests/api/test_annotation.py | 15 ++++++++++----- 4 files changed, 14 insertions(+), 12 deletions(-) diff --git a/supysonic/api/__init__.py b/supysonic/api/__init__.py index 38ef75f..aeab867 100644 --- a/supysonic/api/__init__.py +++ b/supysonic/api/__init__.py @@ -67,11 +67,9 @@ def authorize(): def get_client_prefs(): client = request.values['c'] try: - ClientPrefs[request.user, client] + request.client = ClientPrefs[request.user, client] except ObjectNotFound: - ClientPrefs(user = request.user, client_name = client) - - request.client = client + request.client = ClientPrefs(user = request.user, client_name = client) def get_entity(cls, param = 'id'): eid = request.values[param] diff --git a/supysonic/api/media.py b/supysonic/api/media.py index d14ccf4..d01a228 100644 --- a/supysonic/api/media.py +++ b/supysonic/api/media.py @@ -48,7 +48,7 @@ def stream_media(): dst_bitrate = res.bitrate dst_mimetype = res.content_type - prefs = ClientPrefs.get(lambda p: p.user.id == request.user.id and p.client_name == request.client) + prefs = request.client if prefs.format: dst_suffix = prefs.format if prefs.bitrate and prefs.bitrate < dst_bitrate: diff --git a/supysonic/db.py b/supysonic/db.py index 836e9f7..408756e 100644 --- a/supysonic/db.py +++ b/supysonic/db.py @@ -172,7 +172,7 @@ class Track(db.Entity): stars = Set(lambda: StarredTrack) ratings = Set(lambda: RatingTrack) - def as_subsonic_child(self, user, client): + def as_subsonic_child(self, user, prefs): info = dict( id = str(self.id), parent = str(self.folder.id), @@ -216,7 +216,6 @@ class Track(db.Entity): if avgRating: info['averageRating'] = avgRating - prefs = ClientPrefs.get(lambda p: p.user.id == user.id and p.client_name == client) if prefs is not None and prefs.format is not None and prefs.format != self.suffix(): info['transcodedSuffix'] = prefs.format info['transcodedContentType'] = mimetypes.guess_type('dummyname.' + prefs.format, False)[0] or 'application/octet-stream' diff --git a/tests/api/test_annotation.py b/tests/api/test_annotation.py index 8cdfc53..52f81f7 100644 --- a/tests/api/test_annotation.py +++ b/tests/api/test_annotation.py @@ -60,7 +60,8 @@ class AnnotationTestCase(ApiTestBase): self._make_request('star', { 'id': str(self.albumid) }, error = 70, skip_xsd = True) self._make_request('star', { 'id': str(self.trackid) }, skip_post = True) with db_session: - self.assertIn('starred', Track[self.trackid].as_subsonic_child(self.user, 'tests')) + prefs = ClientPrefs.get(lambda p: p.user.name == 'alice' and p.client_name == 'tests') + self.assertIn('starred', Track[self.trackid].as_subsonic_child(self.user, prefs)) self._make_request('star', { 'id': str(self.trackid) }, error = 0, skip_xsd = True) self._make_request('star', { 'id': str(self.folderid) }, skip_post = True) @@ -94,7 +95,8 @@ class AnnotationTestCase(ApiTestBase): self._make_request('unstar', { 'id': str(self.trackid) }, skip_post = True) with db_session: - self.assertNotIn('starred', Track[self.trackid].as_subsonic_child(self.user, 'tests')) + prefs = ClientPrefs.get(lambda p: p.user.name == 'alice' and p.client_name == 'tests') + self.assertNotIn('starred', Track[self.trackid].as_subsonic_child(self.user, prefs)) self._make_request('unstar', { 'id': str(self.folderid) }, skip_post = True) with db_session: @@ -121,16 +123,19 @@ class AnnotationTestCase(ApiTestBase): self._make_request('setRating', { 'id': str(self.trackid), 'rating': 6 }, error = 0) with db_session: - self.assertNotIn('userRating', Track[self.trackid].as_subsonic_child(self.user, 'tests')) + prefs = ClientPrefs.get(lambda p: p.user.name == 'alice' and p.client_name == 'tests') + self.assertNotIn('userRating', Track[self.trackid].as_subsonic_child(self.user, prefs)) for i in range(1, 6): self._make_request('setRating', { 'id': str(self.trackid), 'rating': i }, skip_post = True) with db_session: - self.assertEqual(Track[self.trackid].as_subsonic_child(self.user, 'tests')['userRating'], i) + prefs = ClientPrefs.get(lambda p: p.user.name == 'alice' and p.client_name == 'tests') + self.assertEqual(Track[self.trackid].as_subsonic_child(self.user, prefs)['userRating'], i) self._make_request('setRating', { 'id': str(self.trackid), 'rating': 0 }, skip_post = True) with db_session: - self.assertNotIn('userRating', Track[self.trackid].as_subsonic_child(self.user, 'tests')) + prefs = ClientPrefs.get(lambda p: p.user.name == 'alice' and p.client_name == 'tests') + self.assertNotIn('userRating', Track[self.trackid].as_subsonic_child(self.user, prefs)) self.assertNotIn('userRating', Folder[self.folderid].as_subsonic_child(self.user)) for i in range(1, 6):