1
0
mirror of https://github.com/spl0k/supysonic.git synced 2024-11-10 04:02:17 +00:00

Porting supysonic.api.annotation

This commit is contained in:
Alban Féron 2022-12-18 17:24:05 +01:00
parent 2b472b4d97
commit 7401b4dec9
No known key found for this signature in database
GPG Key ID: 8CE0313646D16165
2 changed files with 77 additions and 114 deletions

View File

@ -28,16 +28,16 @@ def star_single(cls, starcls, eid):
try: try:
e = cls[eid] e = cls[eid]
except ObjectNotFound: except cls.DoesNotExist:
raise NotFound("{} {}".format(cls.__name__, eid)) raise NotFound("{} {}".format(cls.__name__, eid))
try: try:
starcls[request.user, eid] starcls[request.user, eid]
raise GenericError("{} {} already starred".format(cls.__name__, eid)) raise GenericError("{} {} already starred".format(cls.__name__, eid))
except ObjectNotFound: except starcls.DoesNotExist:
pass pass
starcls(user=request.user, starred=e) starcls.create(user=request.user, starred=e)
def unstar_single(cls, starcls, eid): def unstar_single(cls, starcls, eid):
@ -48,7 +48,9 @@ def unstar_single(cls, starcls, eid):
:param eid: id of the entity to unstar :param eid: id of the entity to unstar
""" """
delete(s for s in starcls if s.user.id == request.user.id and s.starred.id == eid) starcls.delete().where(
starcls.user == request.user, starcls.starred == eid
).execute()
def handle_star_request(func): def handle_star_request(func):
@ -139,17 +141,13 @@ def rate():
if rating == 0: if rating == 0:
if tid is not None: if tid is not None:
delete( RatingTrack.delete().where(
r RatingTrack.user == request.user, RatingTrack.rated == tid
for r in RatingTrack ).execute()
if r.user.id == request.user.id and r.rated.id == tid
)
else: else:
delete( RatingFolder.delete().where(
r RatingFolder.user == request.user, RatingFolder.rated == fid
for r in RatingFolder ).execute()
if r.user.id == request.user.id and r.rated.id == fid
)
else: else:
if tid is not None: if tid is not None:
rated = Track[tid] rated = Track[tid]
@ -163,8 +161,9 @@ def rate():
try: try:
rating_info = rating_cls[request.user, uid] rating_info = rating_cls[request.user, uid]
rating_info.rating = rating rating_info.rating = rating
except ObjectNotFound: rating_info.save()
rating_cls(user=request.user, rated=rated, rating=rating) except rating_cls.DoesNotExist:
rating_cls.create(user=request.user, rated=rated, rating=rating)
return request.formatter.empty return request.formatter.empty

View File

@ -1,15 +1,13 @@
# This file is part of Supysonic. # This file is part of Supysonic.
# Supysonic is a Python implementation of the Subsonic server API. # Supysonic is a Python implementation of the Subsonic server API.
# #
# Copyright (C) 2017 Alban 'spl0k' Féron # Copyright (C) 2017-2022 Alban 'spl0k' Féron
# #
# Distributed under terms of the GNU AGPLv3 license. # Distributed under terms of the GNU AGPLv3 license.
import unittest import unittest
import uuid import uuid
from pony.orm import db_session
from supysonic.db import Folder, Artist, Album, Track, User, ClientPrefs from supysonic.db import Folder, Artist, Album, Track, User, ClientPrefs
from .apitestbase import ApiTestBase from .apitestbase import ApiTestBase
@ -19,35 +17,37 @@ class AnnotationTestCase(ApiTestBase):
def setUp(self): def setUp(self):
super().setUp() super().setUp()
with db_session: root = Folder.create(name="Root", root=True, path="tests")
root = Folder(name="Root", root=True, path="tests") folder = Folder.create(
folder = Folder(name="Folder", path="tests/assets", parent=root) name="Folder", root=False, path="tests/assets", parent=root
artist = Artist(name="Artist") )
album = Album(name="Album", artist=artist) artist = Artist.create(name="Artist")
album = Album.create(name="Album", artist=artist)
# Populate folder ids # Populate folder ids
root = Folder.get(name="Root") root = Folder.get(name="Root")
folder = Folder.get(name="Folder") folder = Folder.get(name="Folder")
track = Track( track = Track.create(
title="Track", title="Track",
album=album, album=album,
artist=artist, artist=artist,
disc=1, disc=1,
number=1, number=1,
path="tests/assets/empty", path="tests/assets/empty",
folder=folder, folder=folder,
root_folder=root, root_folder=root,
duration=2, duration=2,
bitrate=320, bitrate=320,
last_modification=0, last_modification=0,
) )
self.folderid = folder.id self.folderid = folder.id
self.artistid = artist.id self.artistid = artist.id
self.albumid = album.id self.albumid = album.id
self.trackid = track.id self.trackid = track.id
self.user = User.get(name="alice") self.user = User.get(name="alice")
self.prefs = ClientPrefs.create(user=self.user, client_name="tests")
def test_star(self): def test_star(self):
self._make_request("star", error=10) self._make_request("star", error=10)
@ -61,36 +61,27 @@ class AnnotationTestCase(ApiTestBase):
self._make_request("star", {"id": str(self.artistid)}, error=70) self._make_request("star", {"id": str(self.artistid)}, error=70)
self._make_request("star", {"id": str(self.albumid)}, error=70) self._make_request("star", {"id": str(self.albumid)}, error=70)
self._make_request("star", {"id": str(self.trackid)}, skip_post=True) self._make_request("star", {"id": str(self.trackid)}, skip_post=True)
with db_session: self.assertIn(
prefs = ClientPrefs.get( "starred", Track[self.trackid].as_subsonic_child(self.user, self.prefs)
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) self._make_request("star", {"id": str(self.trackid)}, error=0)
self._make_request("star", {"id": str(self.folderid)}, skip_post=True) self._make_request("star", {"id": str(self.folderid)}, skip_post=True)
with db_session: self.assertIn("starred", Folder[self.folderid].as_subsonic_child(self.user))
self.assertIn("starred", Folder[self.folderid].as_subsonic_child(self.user))
self._make_request("star", {"id": str(self.folderid)}, error=0) self._make_request("star", {"id": str(self.folderid)}, error=0)
self._make_request("star", {"albumId": str(self.folderid)}, error=0) self._make_request("star", {"albumId": str(self.folderid)}, error=0)
self._make_request("star", {"albumId": str(self.artistid)}, error=70) self._make_request("star", {"albumId": str(self.artistid)}, error=70)
self._make_request("star", {"albumId": str(self.trackid)}, error=70) self._make_request("star", {"albumId": str(self.trackid)}, error=70)
self._make_request("star", {"albumId": str(self.albumid)}, skip_post=True) self._make_request("star", {"albumId": str(self.albumid)}, skip_post=True)
with db_session: self.assertIn("starred", Album[self.albumid].as_subsonic_album(self.user))
self.assertIn("starred", Album[self.albumid].as_subsonic_album(self.user))
self._make_request("star", {"albumId": str(self.albumid)}, error=0) self._make_request("star", {"albumId": str(self.albumid)}, error=0)
self._make_request("star", {"artistId": str(self.folderid)}, error=0) self._make_request("star", {"artistId": str(self.folderid)}, error=0)
self._make_request("star", {"artistId": str(self.albumid)}, error=70) self._make_request("star", {"artistId": str(self.albumid)}, error=70)
self._make_request("star", {"artistId": str(self.trackid)}, error=70) self._make_request("star", {"artistId": str(self.trackid)}, error=70)
self._make_request("star", {"artistId": str(self.artistid)}, skip_post=True) self._make_request("star", {"artistId": str(self.artistid)}, skip_post=True)
with db_session: self.assertIn("starred", Artist[self.artistid].as_subsonic_artist(self.user))
self.assertIn(
"starred", Artist[self.artistid].as_subsonic_artist(self.user)
)
self._make_request("star", {"artistId": str(self.artistid)}, error=0) self._make_request("star", {"artistId": str(self.artistid)}, error=0)
def test_unstar(self): def test_unstar(self):
@ -110,31 +101,18 @@ class AnnotationTestCase(ApiTestBase):
self._make_request("unstar", {"artistId": "unknown"}, error=0) self._make_request("unstar", {"artistId": "unknown"}, error=0)
self._make_request("unstar", {"id": str(self.trackid)}, skip_post=True) self._make_request("unstar", {"id": str(self.trackid)}, skip_post=True)
with db_session: self.assertNotIn(
prefs = ClientPrefs.get( "starred", Track[self.trackid].as_subsonic_child(self.user, self.prefs)
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) self._make_request("unstar", {"id": str(self.folderid)}, skip_post=True)
with db_session: self.assertNotIn("starred", Folder[self.folderid].as_subsonic_child(self.user))
self.assertNotIn(
"starred", Folder[self.folderid].as_subsonic_child(self.user)
)
self._make_request("unstar", {"albumId": str(self.albumid)}, skip_post=True) self._make_request("unstar", {"albumId": str(self.albumid)}, skip_post=True)
with db_session: self.assertNotIn("starred", Album[self.albumid].as_subsonic_album(self.user))
self.assertNotIn(
"starred", Album[self.albumid].as_subsonic_album(self.user)
)
self._make_request("unstar", {"artistId": str(self.artistid)}, skip_post=True) self._make_request("unstar", {"artistId": str(self.artistid)}, skip_post=True)
with db_session: self.assertNotIn("starred", Artist[self.artistid].as_subsonic_artist(self.user))
self.assertNotIn(
"starred", Artist[self.artistid].as_subsonic_artist(self.user)
)
def test_set_rating(self): def test_set_rating(self):
self._make_request("setRating", error=10) self._make_request("setRating", error=10)
@ -158,58 +136,44 @@ class AnnotationTestCase(ApiTestBase):
) )
self._make_request("setRating", {"id": str(self.trackid), "rating": 6}, error=0) self._make_request("setRating", {"id": str(self.trackid), "rating": 6}, error=0)
with db_session: self.assertNotIn(
prefs = ClientPrefs.get( "userRating", Track[self.trackid].as_subsonic_child(self.user, self.prefs)
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): for i in range(1, 6):
self._make_request( self._make_request(
"setRating", {"id": str(self.trackid), "rating": i}, skip_post=True "setRating", {"id": str(self.trackid), "rating": i}, skip_post=True
) )
with db_session: self.assertEqual(
prefs = ClientPrefs.get( Track[self.trackid].as_subsonic_child(self.user, self.prefs)[
lambda p: p.user.name == "alice" and p.client_name == "tests" "userRating"
) ],
self.assertEqual( i,
Track[self.trackid].as_subsonic_child(self.user, prefs)[ )
"userRating"
],
i,
)
self._make_request( self._make_request(
"setRating", {"id": str(self.trackid), "rating": 0}, skip_post=True "setRating", {"id": str(self.trackid), "rating": 0}, skip_post=True
) )
with db_session: self.assertNotIn(
prefs = ClientPrefs.get( "userRating", Track[self.trackid].as_subsonic_child(self.user, self.prefs)
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( self.assertNotIn(
"userRating", Folder[self.folderid].as_subsonic_child(self.user) "userRating", Folder[self.folderid].as_subsonic_child(self.user)
) )
for i in range(1, 6): for i in range(1, 6):
self._make_request( self._make_request(
"setRating", {"id": str(self.folderid), "rating": i}, skip_post=True "setRating", {"id": str(self.folderid), "rating": i}, skip_post=True
) )
with db_session: self.assertEqual(
self.assertEqual( Folder[self.folderid].as_subsonic_child(self.user)["userRating"], i
Folder[self.folderid].as_subsonic_child(self.user)["userRating"], i )
)
self._make_request( self._make_request(
"setRating", {"id": str(self.folderid), "rating": 0}, skip_post=True "setRating", {"id": str(self.folderid), "rating": 0}, skip_post=True
) )
with db_session: self.assertNotIn(
self.assertNotIn( "userRating", Folder[self.folderid].as_subsonic_child(self.user)
"userRating", Folder[self.folderid].as_subsonic_child(self.user) )
)
def test_scrobble(self): def test_scrobble(self):
self._make_request("scrobble", error=10) self._make_request("scrobble", error=10)