1
0
mirror of https://github.com/spl0k/supysonic.git synced 2024-09-19 19:01:03 +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:
e = cls[eid]
except ObjectNotFound:
except cls.DoesNotExist:
raise NotFound("{} {}".format(cls.__name__, eid))
try:
starcls[request.user, eid]
raise GenericError("{} {} already starred".format(cls.__name__, eid))
except ObjectNotFound:
except starcls.DoesNotExist:
pass
starcls(user=request.user, starred=e)
starcls.create(user=request.user, starred=e)
def unstar_single(cls, starcls, eid):
@ -48,7 +48,9 @@ def unstar_single(cls, starcls, eid):
: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):
@ -139,17 +141,13 @@ def rate():
if rating == 0:
if tid is not None:
delete(
r
for r in RatingTrack
if r.user.id == request.user.id and r.rated.id == tid
)
RatingTrack.delete().where(
RatingTrack.user == request.user, RatingTrack.rated == tid
).execute()
else:
delete(
r
for r in RatingFolder
if r.user.id == request.user.id and r.rated.id == fid
)
RatingFolder.delete().where(
RatingFolder.user == request.user, RatingFolder.rated == fid
).execute()
else:
if tid is not None:
rated = Track[tid]
@ -163,8 +161,9 @@ def rate():
try:
rating_info = rating_cls[request.user, uid]
rating_info.rating = rating
except ObjectNotFound:
rating_cls(user=request.user, rated=rated, rating=rating)
rating_info.save()
except rating_cls.DoesNotExist:
rating_cls.create(user=request.user, rated=rated, rating=rating)
return request.formatter.empty

View File

@ -1,15 +1,13 @@
# This file is part of Supysonic.
# 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.
import unittest
import uuid
from pony.orm import db_session
from supysonic.db import Folder, Artist, Album, Track, User, ClientPrefs
from .apitestbase import ApiTestBase
@ -19,35 +17,37 @@ class AnnotationTestCase(ApiTestBase):
def setUp(self):
super().setUp()
with db_session:
root = Folder(name="Root", root=True, path="tests")
folder = Folder(name="Folder", path="tests/assets", parent=root)
artist = Artist(name="Artist")
album = Album(name="Album", artist=artist)
root = Folder.create(name="Root", root=True, path="tests")
folder = Folder.create(
name="Folder", root=False, path="tests/assets", parent=root
)
artist = Artist.create(name="Artist")
album = Album.create(name="Album", artist=artist)
# Populate folder ids
root = Folder.get(name="Root")
folder = Folder.get(name="Folder")
# Populate folder ids
root = Folder.get(name="Root")
folder = Folder.get(name="Folder")
track = Track(
title="Track",
album=album,
artist=artist,
disc=1,
number=1,
path="tests/assets/empty",
folder=folder,
root_folder=root,
duration=2,
bitrate=320,
last_modification=0,
)
track = Track.create(
title="Track",
album=album,
artist=artist,
disc=1,
number=1,
path="tests/assets/empty",
folder=folder,
root_folder=root,
duration=2,
bitrate=320,
last_modification=0,
)
self.folderid = folder.id
self.artistid = artist.id
self.albumid = album.id
self.trackid = track.id
self.user = User.get(name="alice")
self.folderid = folder.id
self.artistid = artist.id
self.albumid = album.id
self.trackid = track.id
self.user = User.get(name="alice")
self.prefs = ClientPrefs.create(user=self.user, client_name="tests")
def test_star(self):
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.albumid)}, error=70)
self._make_request("star", {"id": str(self.trackid)}, skip_post=True)
with db_session:
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.assertIn(
"starred", Track[self.trackid].as_subsonic_child(self.user, self.prefs)
)
self._make_request("star", {"id": str(self.trackid)}, error=0)
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", {"albumId": str(self.folderid)}, error=0)
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.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", {"artistId": str(self.folderid)}, error=0)
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.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)
def test_unstar(self):
@ -110,31 +101,18 @@ class AnnotationTestCase(ApiTestBase):
self._make_request("unstar", {"artistId": "unknown"}, error=0)
self._make_request("unstar", {"id": str(self.trackid)}, skip_post=True)
with db_session:
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.assertNotIn(
"starred", Track[self.trackid].as_subsonic_child(self.user, self.prefs)
)
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)
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)
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):
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)
with db_session:
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", Track[self.trackid].as_subsonic_child(self.user, self.prefs)
)
for i in range(1, 6):
self._make_request(
"setRating", {"id": str(self.trackid), "rating": i}, skip_post=True
)
with db_session:
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.assertEqual(
Track[self.trackid].as_subsonic_child(self.user, self.prefs)[
"userRating"
],
i,
)
self._make_request(
"setRating", {"id": str(self.trackid), "rating": 0}, skip_post=True
)
with db_session:
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", Track[self.trackid].as_subsonic_child(self.user, self.prefs)
)
self.assertNotIn(
"userRating", Folder[self.folderid].as_subsonic_child(self.user)
)
self.assertNotIn(
"userRating", Folder[self.folderid].as_subsonic_child(self.user)
)
for i in range(1, 6):
self._make_request(
"setRating", {"id": str(self.folderid), "rating": i}, skip_post=True
)
with db_session:
self.assertEqual(
Folder[self.folderid].as_subsonic_child(self.user)["userRating"], i
)
self.assertEqual(
Folder[self.folderid].as_subsonic_child(self.user)["userRating"], i
)
self._make_request(
"setRating", {"id": str(self.folderid), "rating": 0}, skip_post=True
)
with db_session:
self.assertNotIn(
"userRating", Folder[self.folderid].as_subsonic_child(self.user)
)
self.assertNotIn(
"userRating", Folder[self.folderid].as_subsonic_child(self.user)
)
def test_scrobble(self):
self._make_request("scrobble", error=10)